]> git.cworth.org Git - apitrace/blob - common/os.hpp
b97f33070ed5387da0fcdce43a4e0f7a31afbedc
[apitrace] / common / os.hpp
1 /**************************************************************************
2  *
3  * Copyright 2010 VMware, Inc.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26 /*
27  * Simple OS abstraction layer.
28  */
29
30 #ifndef _OS_HPP_
31 #define _OS_HPP_
32
33 #include <assert.h>
34 #include <stdlib.h>
35 #include <stdarg.h>
36 #include <stdio.h>
37 #include <string.h>
38
39 #include <vector>
40
41 #ifdef _WIN32
42 #ifndef snprintf
43 #define snprintf _snprintf
44 #endif
45 #ifndef vsnprintf
46 #define vsnprintf _vsnprintf
47 #endif
48 #define PATH_SEP '\\'
49 #else /* !_WIN32 */
50 #define PATH_SEP '/'
51 #endif /* !_WIN32 */
52
53 #ifndef PATH_MAX
54 #define PATH_MAX 1024
55 #endif
56
57 namespace os {
58
59 void acquireMutex(void);
60
61 void releaseMutex(void);
62
63
64 class Path {
65 protected:
66     typedef std::vector<char> Buffer;
67     Buffer buffer;
68
69     Buffer::iterator rfind(char c) {
70         Buffer::iterator it = buffer.end();
71         while (it != buffer.begin()) {
72             --it;
73             if (*it == c) {
74                 return it;
75             }
76         }
77         return buffer.end();
78     }
79
80 public:
81     Path() {
82         buffer.push_back(0);
83     }
84
85     Path(const char *s) :
86         buffer(s, s + strlen(s) + 1)
87     {}
88
89     template <class InputIterator>
90     Path(InputIterator first, InputIterator last) :
91         buffer(first, last)
92     {
93         buffer.push_back(0);
94     }
95
96     char *buf(size_t size) {
97         buffer.resize(size);
98         return &buffer[0];
99     }
100
101     void trimDirectory(void) {
102         Buffer::iterator sep = rfind(PATH_SEP);
103         if (sep != buffer.end()) {
104             buffer.erase(buffer.begin(), sep + 1);
105         }
106     }
107
108     void trimExtension(void) {
109         Buffer::iterator dot = rfind('.');
110         if (dot != buffer.end()) {
111             buffer.erase(dot, buffer.end());
112         }
113     }
114
115     size_t length(void) const {
116         size_t size = buffer.size();
117         assert(size > 0);
118         assert(buffer[size - 1] == 0);
119         return size - 1;
120     }
121
122     void truncate(size_t length) {
123         assert(length < buffer.size());
124         buffer[length] = 0;
125         buffer.resize(length + 1);
126     }
127
128     void truncate(void) {
129         truncate(strlen(str()));
130     }
131
132     const char *str(void) const {
133         assert(buffer[buffer.size() - 1] == 0);
134         return &buffer[0];
135     }
136
137     void join(const Path & other) {
138         size_t len = length();
139         if (len > 0 && buffer[len - 1] != PATH_SEP) {
140             buffer.insert(buffer.begin() + len++, PATH_SEP);
141         }
142         buffer.insert(buffer.begin() + len, other.buffer.begin(), other.buffer.end() - 1);
143     }
144
145 };
146
147 Path getProcessName();
148 Path getCurrentDir();
149
150 void log(const char *format, ...)
151 #ifdef __GNUC__
152     __attribute__ ((format (printf, 1, 2)))
153 #endif
154 ;
155
156 #if defined _WIN32 || defined __CYGWIN__
157   /* We always use .def files on windows for now */
158   #if 0
159   #define PUBLIC __declspec(dllexport)
160   #else
161   #define PUBLIC
162   #endif
163   #define PRIVATE
164 #else
165   #if __GNUC__ >= 4
166     #define PUBLIC __attribute__ ((visibility("default")))
167     #define PRIVATE __attribute__ ((visibility("hidden")))
168   #else
169     #define PUBLIC
170     #define PRIVATE
171   #endif
172 #endif
173
174 /**
175  * Get the current time in microseconds from an unknown base.
176  */
177 long long getTime(void);
178
179 void abort(void);
180
181 void setExceptionCallback(void (*callback)(void));
182 void resetExceptionCallback(void);
183
184 } /* namespace os */
185
186 #endif /* _OS_HPP_ */