]> git.cworth.org Git - apitrace/blob - common/os_string.hpp
Use skiplist-based FastCallSet within trace::CallSet
[apitrace] / common / os_string.hpp
1 /**************************************************************************
2  *
3  * Copyright 2011 Jose Fonseca
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  * String manipulation.
28  */
29
30 #ifndef _OS_STRING_HPP_
31 #define _OS_STRING_HPP_
32
33
34 #include <assert.h>
35 #include <stdarg.h>
36 #include <stdio.h>
37 #include <stddef.h>
38
39 #ifdef __MINGW32__
40 // Some versions of MinGW are missing _vscprintf's declaration, although they
41 // still provide the symbol in the import library.
42 extern "C" _CRTIMP int _vscprintf(const char *format, va_list argptr);
43 #endif
44
45 #ifndef va_copy
46 #ifdef __va_copy
47 #define va_copy(dest, src) __va_copy((dest), (src))
48 #else
49 #define va_copy(dest, src) (dest) = (src)
50 #endif
51 #endif
52
53 #include <vector>
54
55 #include "os.hpp"
56
57
58 #ifdef _WIN32
59 #define OS_DIR_SEP '\\'
60 #else /* !_WIN32 */
61 #define OS_DIR_SEP '/'
62 #endif /* !_WIN32 */
63
64
65 namespace os {
66
67
68 /**
69  * Vector based zero-terminate string, suitable for passing strings or paths
70  * to/from OS calls.
71  */
72 class String {
73 protected:
74     typedef std::vector<char> Buffer;
75
76     /**
77      * The buffer's last element is always the '\0' character, therefore the
78      * buffer must never be empty.
79      */
80     Buffer buffer;
81
82     Buffer::iterator find(char c) {
83         Buffer::iterator it = buffer.begin();
84         assert(it != buffer.end());
85         while (it != buffer.end()) {
86             if (*it == c) {
87                 return it;
88             }
89             ++it;
90         }
91         return buffer.end();
92     }
93
94     Buffer::iterator rfind(char c) {
95         Buffer::iterator it = buffer.end();
96
97         // Skip trailing '\0'
98         assert(it != buffer.begin());
99         --it;
100         assert(*it == '\0');
101
102         while (it != buffer.begin()) {
103             --it;
104             if (*it == c) {
105                 return it;
106             }
107         }
108
109         return buffer.end();
110     }
111
112     String(size_t size) :
113         buffer(size) {
114     }
115
116     char *buf(void) {
117         return &buffer[0];
118     }
119
120     inline bool
121     isSep(char c) {
122         if (c == '/') {
123             return true;
124         }
125 #ifdef _WIN32
126         if (c == '\\') {
127             return true;
128         }
129 #endif
130         return false;
131     }
132
133     Buffer::iterator rfindSep(void) {
134         Buffer::iterator it = buffer.end();
135
136         // Skip trailing '\0'
137         assert(it != buffer.begin());
138         --it;
139         assert(*it == '\0');
140
141         // Skip trailing separators
142         while (it != buffer.begin()) {
143             --it;
144             if (isSep(*it)) {
145                 // Halt if find the root
146                 if (it == buffer.begin()) {
147                     return it;
148                 }
149             } else {
150                 break;
151             }
152         }
153
154         // Advance to the last separator
155         while (it != buffer.begin()) {
156             --it;
157             if (isSep(*it)) {
158                 return it;
159             }
160         }
161
162         return buffer.end();
163     }
164
165
166 public:
167
168     /*
169      * Constructors
170      */
171
172     String() {
173         buffer.push_back(0);
174     }
175
176     String(const char *s) :
177         buffer(s, s + strlen(s) + 1)
178     {}
179
180     String(const String &other) :
181         buffer(other.buffer)
182     {}
183
184     template <class InputIterator>
185     String(InputIterator first, InputIterator last) :
186         buffer(first, last)
187     {
188         buffer.push_back(0);
189     }
190
191     /**
192      * From a printf-like format string
193      */
194     static String
195     format(const char *format, ...)
196 #ifdef __GNUC__
197     __attribute__ ((format (printf, 1, 2)))
198 #endif
199     {
200
201         va_list args;
202
203         va_start(args, format);
204
205         int length;
206         va_list args_copy;
207         va_copy(args_copy, args);
208 #ifdef _WIN32
209         /* We need to use _vscprintf to calculate the length as vsnprintf returns -1
210          * if the number of characters to write is greater than count.
211          */
212         length = _vscprintf(format, args_copy);
213 #else
214         char dummy;
215         length = vsnprintf(&dummy, sizeof dummy, format, args_copy);
216 #endif
217         va_end(args_copy);
218
219         assert(length >= 0);
220         size_t size = length + 1;
221
222         String path(size);
223
224         va_start(args, format);
225         vsnprintf(path.buf(), size, format, args);
226         va_end(args);
227
228         return path;
229     }
230
231     /*
232      * Conversion to ordinary C strings.
233      */
234
235     const char *str(void) const {
236         assert(buffer.back() == 0);
237         return &buffer[0];
238     }
239
240     operator const char *(void) const {
241         return str();
242     }
243
244     /*
245      * Iterators
246      */
247
248     typedef Buffer::const_iterator const_iterator;
249     typedef Buffer::iterator iterator;
250
251     const_iterator begin(void) const {
252         return buffer.begin();
253     }
254
255     iterator begin(void) {
256         return buffer.begin();
257     }
258
259     const_iterator end(void) const {
260         const_iterator it = buffer.end();
261         assert(it != buffer.begin());
262         --it; // skip null
263         return it;
264     }
265
266     iterator end(void) {
267         iterator it = buffer.end();
268         assert(it != buffer.begin());
269         --it; // skip null
270         return it;
271     }
272
273     /*
274      * Operations
275      */
276
277     void insert(iterator position, char c) {
278         buffer.insert(position, c);
279     }
280
281     template <class InputIterator>
282     void insert(iterator position, InputIterator first, InputIterator last) {
283         buffer.insert(position, first, last);
284     }
285
286     void insert(iterator position, const char *s) {
287         assert(s);
288         insert(position, s, s + strlen(s));
289     }
290
291     void insert(iterator position, const String & other) {
292         insert(position, other.begin(), other.end());
293     }
294
295     void append(char c) {
296         insert(end(), c);
297     }
298
299     template <class InputIterator>
300     void append(InputIterator first, InputIterator last) {
301         insert(end(), first, last);
302     }
303
304     void append(const char *s) {
305         insert(end(), s);
306     }
307
308     void append(const String & other) {
309         insert(end(), other);
310     }
311
312     char *buf(size_t size) {
313         buffer.resize(size);
314         return &buffer[0];
315     }
316
317     size_t length(void) const {
318         size_t size = buffer.size();
319         assert(size > 0);
320         assert(buffer[size - 1] == 0);
321         return size - 1;
322     }
323
324     void truncate(size_t length) {
325         assert(length < buffer.size());
326         buffer[length] = 0;
327         buffer.resize(length + 1);
328     }
329
330     void truncate(void) {
331         truncate(strlen(str()));
332     }
333
334
335     /*
336      * Path manipulation
337      */
338
339     bool
340     exists(void) const;
341
342     /* Trim directory (leaving base filename).
343      */
344     void trimDirectory(void) {
345         iterator sep = rfindSep();
346         if (sep != buffer.end()) {
347             buffer.erase(buffer.begin(), sep + 1);
348         }
349     }
350
351     /* Trim filename component (leaving containing directory).
352      *
353      * - trailing separators are ignored
354      * - a path with no separator at all yields "."
355      * - a path consisting of just the root directory is left unchanged
356      */
357     void trimFilename(void) {
358         iterator sep = rfindSep();
359
360         // No separator found, so return '.'
361         if (sep == buffer.end()) {
362             buffer.resize(2);
363             buffer[0] = '.';
364             buffer[1] = 0;
365             return;
366         }
367
368         // Root. Nothing to do.
369         if (sep == buffer.begin()) {
370             return;
371         }
372
373         // Trim filename
374         buffer.erase(sep, end());
375     }
376
377     void trimExtension(void) {
378         iterator dot = rfind('.');
379         if (dot != buffer.end()) {
380             buffer.erase(dot, end());
381         }
382     }
383
384     void join(const String & other) {
385         if (length() && end()[-1] != OS_DIR_SEP) {
386             append(OS_DIR_SEP);
387         }
388         append(other.begin(), other.end());
389     }
390 };
391
392
393 String getProcessName();
394 String getCurrentDir();
395
396 bool copyFile(const String &srcFileName, const String &dstFileName, bool override = true);
397
398 bool removeFile(const String &fileName);
399
400 } /* namespace os */
401
402 #endif /* _OS_STRING_HPP_ */