1 /**************************************************************************
3 * Copyright 2011 Jose Fonseca
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 * String manipulation.
30 #ifndef _OS_STRING_HPP_
31 #define _OS_STRING_HPP_
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);
47 #define va_copy(dest, src) __va_copy((dest), (src))
49 #define va_copy(dest, src) (dest) = (src)
59 #define OS_DIR_SEP '\\'
61 #define OS_DIR_SEP '/'
69 * Vector based zero-terminate string, suitable for passing strings or paths
74 typedef std::vector<char> Buffer;
77 * The buffer's last element is always the '\0' character, therefore the
78 * buffer must never be empty.
82 Buffer::iterator find(char c) {
83 Buffer::iterator it = buffer.begin();
84 assert(it != buffer.end());
85 while (it != buffer.end()) {
94 Buffer::iterator rfind(char c) {
95 Buffer::iterator it = buffer.end();
98 assert(it != buffer.begin());
102 while (it != buffer.begin()) {
112 String(size_t size) :
133 Buffer::iterator rfindSep(void) {
134 Buffer::iterator it = buffer.end();
136 // Skip trailing '\0'
137 assert(it != buffer.begin());
141 // Skip trailing separators
142 while (it != buffer.begin()) {
145 // Halt if find the root
146 if (it == buffer.begin()) {
154 // Advance to the last separator
155 while (it != buffer.begin()) {
176 String(const char *s) :
177 buffer(s, s + strlen(s) + 1)
180 String(const String &other) :
184 template <class InputIterator>
185 String(InputIterator first, InputIterator last) :
192 * From a printf-like format string
195 format(const char *format, ...)
197 __attribute__ ((format (printf, 1, 2)))
203 va_start(args, format);
207 va_copy(args_copy, args);
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.
212 length = _vscprintf(format, args_copy);
215 length = vsnprintf(&dummy, sizeof dummy, format, args_copy);
220 size_t size = length + 1;
224 va_start(args, format);
225 vsnprintf(path.buf(), size, format, args);
232 * Conversion to ordinary C strings.
235 const char *str(void) const {
236 assert(buffer.back() == 0);
240 operator const char *(void) const {
248 typedef Buffer::const_iterator const_iterator;
249 typedef Buffer::iterator iterator;
251 const_iterator begin(void) const {
252 return buffer.begin();
255 iterator begin(void) {
256 return buffer.begin();
259 const_iterator end(void) const {
260 const_iterator it = buffer.end();
261 assert(it != buffer.begin());
267 iterator it = buffer.end();
268 assert(it != buffer.begin());
277 void insert(iterator position, char c) {
278 buffer.insert(position, c);
281 template <class InputIterator>
282 void insert(iterator position, InputIterator first, InputIterator last) {
283 buffer.insert(position, first, last);
286 void insert(iterator position, const char *s) {
288 insert(position, s, s + strlen(s));
291 void insert(iterator position, const String & other) {
292 insert(position, other.begin(), other.end());
295 void append(char c) {
299 template <class InputIterator>
300 void append(InputIterator first, InputIterator last) {
301 insert(end(), first, last);
304 void append(const char *s) {
308 void append(const String & other) {
309 insert(end(), other);
312 char *buf(size_t size) {
317 size_t length(void) const {
318 size_t size = buffer.size();
320 assert(buffer[size - 1] == 0);
324 void truncate(size_t length) {
325 assert(length < buffer.size());
327 buffer.resize(length + 1);
330 void truncate(void) {
331 truncate(strlen(str()));
342 /* Trim directory (leaving base filename).
344 void trimDirectory(void) {
345 iterator sep = rfindSep();
346 if (sep != buffer.end()) {
347 buffer.erase(buffer.begin(), sep + 1);
351 /* Trim filename component (leaving containing directory).
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
357 void trimFilename(void) {
358 iterator sep = rfindSep();
360 // No separator found, so return '.'
361 if (sep == buffer.end()) {
368 // Root. Nothing to do.
369 if (sep == buffer.begin()) {
374 buffer.erase(sep, end());
377 void trimExtension(void) {
378 iterator dot = rfind('.');
379 if (dot != buffer.end()) {
380 buffer.erase(dot, end());
384 void join(const String & other) {
385 if (length() && end()[-1] != OS_DIR_SEP) {
388 append(other.begin(), other.end());
393 String getProcessName();
394 String getCurrentDir();
396 bool copyFile(const String &srcFileName, const String &dstFileName, bool override = true);
398 bool removeFile(const String &fileName);
402 #endif /* _OS_STRING_HPP_ */