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) :
123 String(const char *s) :
124 buffer(s, s + strlen(s) + 1)
127 String(const String &other) :
131 template <class InputIterator>
132 String(InputIterator first, InputIterator last) :
139 * From a printf-like format string
142 format(const char *format, ...)
144 __attribute__ ((format (printf, 1, 2)))
150 va_start(args, format);
154 va_copy(args_copy, args);
156 /* We need to use _vcsprintf to calculate the length as vsnprintf returns -1
157 * if the number of characters to write is greater than count.
159 length = _vscprintf(format, args_copy);
162 length = vsnprintf(&dummy, sizeof dummy, format, args_copy);
167 size_t size = length + 1;
171 va_start(args, format);
172 vsnprintf(path.buf(), size, format, args);
179 * Conversion to ordinary C strings.
182 const char *str(void) const {
183 assert(buffer.back() == 0);
187 operator const char *(void) const {
195 typedef Buffer::const_iterator const_iterator;
196 typedef Buffer::iterator iterator;
198 const_iterator begin(void) const {
199 return buffer.begin();
202 iterator begin(void) {
203 return buffer.begin();
206 const_iterator end(void) const {
207 const_iterator it = buffer.end();
208 assert(it != buffer.begin());
214 iterator it = buffer.end();
215 assert(it != buffer.begin());
224 void insert(iterator position, char c) {
225 buffer.insert(position, c);
228 template <class InputIterator>
229 void insert(iterator position, InputIterator first, InputIterator last) {
230 buffer.insert(position, first, last);
233 void insert(iterator position, const char *s) {
235 insert(position, s, s + strlen(s));
238 void insert(iterator position, const String & other) {
239 insert(position, other.begin(), other.end());
242 void append(char c) {
246 template <class InputIterator>
247 void append(InputIterator first, InputIterator last) {
248 insert(end(), first, last);
251 void append(const char *s) {
255 void append(const String & other) {
256 insert(end(), other);
259 char *buf(size_t size) {
264 size_t length(void) const {
265 size_t size = buffer.size();
267 assert(buffer[size - 1] == 0);
271 void truncate(size_t length) {
272 assert(length < buffer.size());
274 buffer.resize(length + 1);
277 void truncate(void) {
278 truncate(strlen(str()));
283 * String manipulation
289 void trimDirectory(void) {
290 iterator sep = rfind(OS_DIR_SEP);
291 if (sep != buffer.end()) {
292 buffer.erase(buffer.begin(), sep + 1);
296 /* Trim filename component (leaving containing directory).
298 * This function removes everything after the final path
299 * separator, as well as that separator itself if it is not the
300 * only remaining separator.
302 * Some specific consequences of the above:
304 * 1. A path with no separator at all is unchanged.
305 * 2. A path with a trailing separator has only that separator removed
306 * 3. A path of just the root directory is unchaged.
308 void trimFilename(void) {
309 iterator first = find(OS_DIR_SEP);
310 iterator last = rfind(OS_DIR_SEP);
311 if (last == buffer.end()) {
315 buffer.erase(first + 1, end());
317 buffer.erase(last, end());
321 void trimExtension(void) {
322 iterator dot = rfind('.');
323 if (dot != buffer.end()) {
324 buffer.erase(dot, end());
328 void join(const String & other) {
329 if (length() && end()[-1] != OS_DIR_SEP) {
332 append(other.begin(), other.end());
337 String getProcessName();
338 String getCurrentDir();
343 #endif /* _OS_STRING_HPP_ */