1 |
/*************************************************************************** |
/*************************************************************************** |
2 |
* * |
* * |
3 |
* Copyright (C) 2007-2016 Christian Schoenebeck * |
* Copyright (C) 2007-2017 Christian Schoenebeck * |
4 |
* * |
* * |
5 |
* This library is free software; you can redistribute it and/or modify * |
* This library is free software; you can redistribute it and/or modify * |
6 |
* it under the terms of the GNU General Public License as published by * |
* it under the terms of the GNU General Public License as published by * |
48 |
absolute = true; |
absolute = true; |
49 |
} |
} |
50 |
|
|
51 |
|
std::string Path::toNativeFSPath() const { |
52 |
|
#if WIN32 |
53 |
|
return toWindows(); |
54 |
|
#else |
55 |
|
return toPosix(); |
56 |
|
#endif |
57 |
|
} |
58 |
|
|
59 |
std::string Path::toPosix() const { |
std::string Path::toPosix() const { |
60 |
// POSIX paths consist of forward slash separators and requires forward |
// POSIX paths consist of forward slash separators and requires forward |
61 |
// slashes in path and file names to be encoded as "%2f", the file names |
// slashes in path and file names to be encoded as "%2f", the file names |
162 |
return *this + *p; |
return *this + *p; |
163 |
} |
} |
164 |
|
|
165 |
|
Path Path::fromUnknownFS(std::string path) { |
166 |
|
bool hasDrive = false; |
167 |
|
int nSlash = 0, nBackSlash = 0; |
168 |
|
|
169 |
|
if (path.length() >= 2) |
170 |
|
hasDrive = (path[1] == ':'); |
171 |
|
|
172 |
|
for (size_t i = 0; i < path.size(); ++i) { |
173 |
|
if (path[i] == '/') nSlash++; |
174 |
|
if (path[i] == '\\') nBackSlash++; |
175 |
|
} |
176 |
|
|
177 |
|
if (!hasDrive && nSlash > nBackSlash) |
178 |
|
return Path::fromPosix(path); |
179 |
|
else if (hasDrive || nBackSlash > nSlash) |
180 |
|
return Path::fromWindows(path); |
181 |
|
else |
182 |
|
return Path(path); // expect local file system encoding |
183 |
|
} |
184 |
|
|
185 |
Path Path::fromPosix(std::string path) { |
Path Path::fromPosix(std::string path) { |
186 |
Path result; |
Path result; |
187 |
// first split the nodes |
// first split the nodes |
299 |
return p.getName(); |
return p.getName(); |
300 |
} |
} |
301 |
|
|
302 |
std::string Path::getName() { |
std::string Path::getName() const { |
303 |
if(elements.empty()) return ""; |
if(elements.empty()) return ""; |
304 |
return elements[elements.size() - 1]; |
return elements[elements.size() - 1]; |
305 |
} |
} |
315 |
return p.getBaseName(); |
return p.getBaseName(); |
316 |
} |
} |
317 |
|
|
318 |
std::string Path::getBaseName() { |
std::string Path::getBaseName() const { |
319 |
std::string name = getName(); |
std::string name = getName(); |
320 |
size_t lastdot = name.find_last_of('.'); |
size_t lastdot = name.find_last_of('.'); |
321 |
if(lastdot == std::string::npos) return name; |
if(lastdot == std::string::npos) return name; |
326 |
if (elements.size() > 0) elements.pop_back(); |
if (elements.size() > 0) elements.pop_back(); |
327 |
#if WIN32 |
#if WIN32 |
328 |
return toWindows(); |
return toWindows(); |
329 |
#endif |
#else |
|
|
|
330 |
return toPosix(); |
return toPosix(); |
331 |
|
#endif |
332 |
} |
} |
333 |
|
|
334 |
std::string Path::stripLastName(std::string path) { |
std::string Path::stripLastName(std::string path) { |