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 **************************************************************************/
40 // Some versions of are missing _vscprintf's decleration, although still
41 // 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 '/'
70 typedef std::vector<char> Buffer;
73 Buffer::iterator rfind(char c) {
74 Buffer::iterator it = buffer.end();
75 assert(it != buffer.begin());
77 while (it != buffer.begin()) {
100 buffer(s, s + strlen(s) + 1)
103 template <class InputIterator>
104 Path(InputIterator first, InputIterator last) :
110 char *buf(size_t size) {
115 void trimDirectory(void) {
116 Buffer::iterator sep = rfind(OS_DIR_SEP);
117 if (sep != buffer.end()) {
118 buffer.erase(buffer.begin(), sep + 1);
122 void trimExtension(void) {
123 Buffer::iterator dot = rfind('.');
124 if (dot != buffer.end()) {
125 buffer.erase(dot, buffer.end() - 1);
129 size_t length(void) const {
130 size_t size = buffer.size();
132 assert(buffer[size - 1] == 0);
136 void truncate(size_t length) {
137 assert(length < buffer.size());
139 buffer.resize(length + 1);
142 void truncate(void) {
143 truncate(strlen(str()));
146 const char *str(void) const {
147 assert(buffer[buffer.size() - 1] == 0);
151 operator const char *(void) const {
155 void join(const Path & other) {
156 size_t len = length();
157 if (len > 0 && buffer[len - 1] != OS_DIR_SEP) {
158 buffer.insert(buffer.begin() + len++, OS_DIR_SEP);
160 buffer.insert(buffer.begin() + len, other.buffer.begin(), other.buffer.end() - 1);
164 * Create a path from a printf-like format string
167 format(const char *format, ...)
169 __attribute__ ((format (printf, 1, 2)))
175 va_start(args, format);
179 va_copy(args_copy, args);
181 /* We need to use _vcsprintf to calculate the length as vsnprintf returns -1
182 * if the number of characters to write is greater than count.
184 length = _vscprintf(format, args_copy);
187 length = vsnprintf(&dummy, sizeof dummy, format, args_copy);
192 size_t size = length + 1;
196 va_start(args, format);
197 vsnprintf(path.buf(), size, format, args);
205 Path getProcessName();
206 Path getCurrentDir();
211 #endif /* _OS_PATH_HPP_ */