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();
96 while (it != buffer.begin()) {
105 String(size_t size) :
126 Buffer::iterator rfindSep(void) {
127 Buffer::iterator it = buffer.end();
129 // Skip trailing separators
130 while (it != buffer.begin() && isSep(*it)) {
134 // Advance to the last separator
135 while (it != buffer.begin()) {
156 String(const char *s) :
157 buffer(s, s + strlen(s) + 1)
160 String(const String &other) :
164 template <class InputIterator>
165 String(InputIterator first, InputIterator last) :
172 * From a printf-like format string
175 format(const char *format, ...)
177 __attribute__ ((format (printf, 1, 2)))
183 va_start(args, format);
187 va_copy(args_copy, args);
189 /* We need to use _vcsprintf to calculate the length as vsnprintf returns -1
190 * if the number of characters to write is greater than count.
192 length = _vscprintf(format, args_copy);
195 length = vsnprintf(&dummy, sizeof dummy, format, args_copy);
200 size_t size = length + 1;
204 va_start(args, format);
205 vsnprintf(path.buf(), size, format, args);
212 * Conversion to ordinary C strings.
215 const char *str(void) const {
216 assert(buffer.back() == 0);
220 operator const char *(void) const {
228 typedef Buffer::const_iterator const_iterator;
229 typedef Buffer::iterator iterator;
231 const_iterator begin(void) const {
232 return buffer.begin();
235 iterator begin(void) {
236 return buffer.begin();
239 const_iterator end(void) const {
240 const_iterator it = buffer.end();
241 assert(it != buffer.begin());
247 iterator it = buffer.end();
248 assert(it != buffer.begin());
257 void insert(iterator position, char c) {
258 buffer.insert(position, c);
261 template <class InputIterator>
262 void insert(iterator position, InputIterator first, InputIterator last) {
263 buffer.insert(position, first, last);
266 void insert(iterator position, const char *s) {
268 insert(position, s, s + strlen(s));
271 void insert(iterator position, const String & other) {
272 insert(position, other.begin(), other.end());
275 void append(char c) {
279 template <class InputIterator>
280 void append(InputIterator first, InputIterator last) {
281 insert(end(), first, last);
284 void append(const char *s) {
288 void append(const String & other) {
289 insert(end(), other);
292 char *buf(size_t size) {
297 size_t length(void) const {
298 size_t size = buffer.size();
300 assert(buffer[size - 1] == 0);
304 void truncate(size_t length) {
305 assert(length < buffer.size());
307 buffer.resize(length + 1);
310 void truncate(void) {
311 truncate(strlen(str()));
322 /* Trim directory (leaving base filename).
324 void trimDirectory(void) {
325 iterator sep = rfindSep();
326 if (sep != buffer.end()) {
327 buffer.erase(buffer.begin(), sep + 1);
331 /* Trim filename component (leaving containing directory).
333 * - trailing separators are ignored
334 * - a path with no separator at all yields "."
335 * - a path consisting of just the root directory is left unchanged
337 void trimFilename(void) {
338 iterator sep = rfindSep();
340 // No separator found, so return '.'
341 if (sep == buffer.end()) {
348 // Root. Nothing to do.
349 if (sep == buffer.begin()) {
354 buffer.erase(sep, end());
357 void trimExtension(void) {
358 iterator dot = rfind('.');
359 if (dot != buffer.end()) {
360 buffer.erase(dot, end());
364 void join(const String & other) {
365 if (length() && end()[-1] != OS_DIR_SEP) {
368 append(other.begin(), other.end());
373 String getProcessName();
374 String getCurrentDir();
376 bool copyFile(const String &srcFileName, const String &dstFileName, bool override = true);
378 bool removeFile(const String &fileName);
382 #endif /* _OS_STRING_HPP_ */