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 **************************************************************************/
42 #define OS_DIR_SEP '\\'
44 #define OS_DIR_SEP '/'
53 typedef std::vector<char> Buffer;
56 Buffer::iterator rfind(char c) {
57 Buffer::iterator it = buffer.end();
58 while (it != buffer.begin()) {
81 buffer(s, s + strlen(s) + 1)
84 template <class InputIterator>
85 Path(InputIterator first, InputIterator last) :
91 char *buf(size_t size) {
96 void trimDirectory(void) {
97 Buffer::iterator sep = rfind(OS_DIR_SEP);
98 if (sep != buffer.end()) {
99 buffer.erase(buffer.begin(), sep + 1);
103 void trimExtension(void) {
104 Buffer::iterator dot = rfind('.');
105 if (dot != buffer.end()) {
106 buffer.erase(dot, buffer.end());
110 size_t length(void) const {
111 size_t size = buffer.size();
113 assert(buffer[size - 1] == 0);
117 void truncate(size_t length) {
118 assert(length < buffer.size());
120 buffer.resize(length + 1);
123 void truncate(void) {
124 truncate(strlen(str()));
127 const char *str(void) const {
128 assert(buffer[buffer.size() - 1] == 0);
132 operator const char *(void) const {
136 void join(const Path & other) {
137 size_t len = length();
138 if (len > 0 && buffer[len - 1] != OS_DIR_SEP) {
139 buffer.insert(buffer.begin() + len++, OS_DIR_SEP);
141 buffer.insert(buffer.begin() + len, other.buffer.begin(), other.buffer.end() - 1);
145 * Create a path from a printf-like format string
148 format(const char *format, ...)
150 __attribute__ ((format (printf, 1, 2)))
156 va_start(args, format);
160 va_copy(args_copy, args);
162 /* We need to use _vcsprintf to calculate the length as vsnprintf returns -1
163 * if the number of characters to write is greater than count.
165 length = _vscprintf(format, args_copy);
168 length = vsnprintf(&dummy, sizeof dummy, format, args_copy);
173 size_t size = length + 1;
177 va_start(args, format);
178 vsnprintf(path.buf(), size, format, args);
186 Path getProcessName();
187 Path getCurrentDir();
192 #endif /* _OS_PATH_HPP_ */