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 while (it != buffer.begin()) {
98 buffer(s, s + strlen(s) + 1)
101 template <class InputIterator>
102 Path(InputIterator first, InputIterator last) :
108 char *buf(size_t size) {
113 void trimDirectory(void) {
114 Buffer::iterator sep = rfind(OS_DIR_SEP);
115 if (sep != buffer.end()) {
116 buffer.erase(buffer.begin(), sep + 1);
120 void trimExtension(void) {
121 Buffer::iterator dot = rfind('.');
122 if (dot != buffer.end()) {
123 buffer.erase(dot, buffer.end());
127 size_t length(void) const {
128 size_t size = buffer.size();
130 assert(buffer[size - 1] == 0);
134 void truncate(size_t length) {
135 assert(length < buffer.size());
137 buffer.resize(length + 1);
140 void truncate(void) {
141 truncate(strlen(str()));
144 const char *str(void) const {
145 assert(buffer[buffer.size() - 1] == 0);
149 operator const char *(void) const {
153 void join(const Path & other) {
154 size_t len = length();
155 if (len > 0 && buffer[len - 1] != OS_DIR_SEP) {
156 buffer.insert(buffer.begin() + len++, OS_DIR_SEP);
158 buffer.insert(buffer.begin() + len, other.buffer.begin(), other.buffer.end() - 1);
162 * Create a path from a printf-like format string
165 format(const char *format, ...)
167 __attribute__ ((format (printf, 1, 2)))
173 va_start(args, format);
177 va_copy(args_copy, args);
179 /* We need to use _vcsprintf to calculate the length as vsnprintf returns -1
180 * if the number of characters to write is greater than count.
182 length = _vscprintf(format, args_copy);
185 length = vsnprintf(&dummy, sizeof dummy, format, args_copy);
190 size_t size = length + 1;
194 va_start(args, format);
195 vsnprintf(path.buf(), size, format, args);
203 Path getProcessName();
204 Path getCurrentDir();
209 #endif /* _OS_PATH_HPP_ */