]> git.cworth.org Git - apitrace/blob - common/os.hpp
Add a path manipulating class.
[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 size) {
123         assert(size > 0);
124         assert(size <= buffer.size());
125         assert(buffer[size - 1] == 0);
126         buffer.resize(size);
127     }
128
129     void truncate(void) {
130         truncate(strlen(str()));
131     }
132
133     const char *str(void) const {
134         assert(buffer[buffer.size() - 1] == 0);
135         return &buffer[0];
136     }
137
138     void join(const Path & other) {
139         size_t len = length();
140         if (len > 0 && buffer[len - 1] != PATH_SEP) {
141             buffer.insert(buffer.begin() + len, PATH_SEP);
142         }
143         buffer.insert(buffer.begin() + len, other.buffer.begin(), other.buffer.end() - 1);
144     }
145
146 };
147
148 Path getProcessName();
149 Path getCurrentDir();
150
151 void log(const char *format, ...)
152 #ifdef __GNUC__
153     __attribute__ ((format (printf, 1, 2)))
154 #endif
155 ;
156
157 #if defined _WIN32 || defined __CYGWIN__
158   /* We always use .def files on windows for now */
159   #if 0
160   #define PUBLIC __declspec(dllexport)
161   #else
162   #define PUBLIC
163   #endif
164   #define PRIVATE
165 #else
166   #if __GNUC__ >= 4
167     #define PUBLIC __attribute__ ((visibility("default")))
168     #define PRIVATE __attribute__ ((visibility("hidden")))
169   #else
170     #define PUBLIC
171     #define PRIVATE
172   #endif
173 #endif
174
175 /**
176  * Get the current time in microseconds from an unknown base.
177  */
178 long long getTime(void);
179
180 void abort(void);
181
182 void setExceptionCallback(void (*callback)(void));
183 void resetExceptionCallback(void);
184
185 } /* namespace os */
186
187 #endif /* _OS_HPP_ */