1 /**************************************************************************
3 * Copyright 2010 VMware, Inc.
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
24 **************************************************************************/
27 * Simple OS abstraction layer.
43 #define snprintf _snprintf
46 #define vsnprintf _vsnprintf
59 void acquireMutex(void);
61 void releaseMutex(void);
66 typedef std::vector<char> Buffer;
69 Buffer::iterator rfind(char c) {
70 Buffer::iterator it = buffer.end();
71 while (it != buffer.begin()) {
86 buffer(s, s + strlen(s) + 1)
89 template <class InputIterator>
90 Path(InputIterator first, InputIterator last) :
96 char *buf(size_t size) {
101 void trimDirectory(void) {
102 Buffer::iterator sep = rfind(PATH_SEP);
103 if (sep != buffer.end()) {
104 buffer.erase(buffer.begin(), sep + 1);
108 void trimExtension(void) {
109 Buffer::iterator dot = rfind('.');
110 if (dot != buffer.end()) {
111 buffer.erase(dot, buffer.end());
115 size_t length(void) const {
116 size_t size = buffer.size();
118 assert(buffer[size - 1] == 0);
122 void truncate(size_t size) {
124 assert(size <= buffer.size());
125 assert(buffer[size - 1] == 0);
129 void truncate(void) {
130 truncate(strlen(str()));
133 const char *str(void) const {
134 assert(buffer[buffer.size() - 1] == 0);
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);
143 buffer.insert(buffer.begin() + len, other.buffer.begin(), other.buffer.end() - 1);
148 Path getProcessName();
149 Path getCurrentDir();
151 void log(const char *format, ...)
153 __attribute__ ((format (printf, 1, 2)))
157 #if defined _WIN32 || defined __CYGWIN__
158 /* We always use .def files on windows for now */
160 #define PUBLIC __declspec(dllexport)
167 #define PUBLIC __attribute__ ((visibility("default")))
168 #define PRIVATE __attribute__ ((visibility("hidden")))
176 * Get the current time in microseconds from an unknown base.
178 long long getTime(void);
182 void setExceptionCallback(void (*callback)(void));
183 void resetExceptionCallback(void);
187 #endif /* _OS_HPP_ */