1#define CPP_UTILITIES_PATHHELPER_STRING_VIEW
19 return std::string(
fileName(std::string_view(path)));
27 return std::string(
directory(std::string_view(path)));
33std::string_view
fileName(std::string_view path)
35 std::size_t lastSlash = path.rfind(
'/');
36 std::size_t lastBackSlash = path.rfind(
'\\');
37 std::size_t lastSeparator;
38 if (lastSlash == std::string::npos && lastBackSlash == std::string::npos) {
40 }
else if (lastSlash == std::string::npos) {
41 lastSeparator = lastBackSlash;
42 }
else if (lastBackSlash == std::string::npos) {
43 lastSeparator = lastSlash;
45 lastSeparator = lastSlash > lastBackSlash ? lastSlash : lastBackSlash;
47 return path.substr(lastSeparator + 1);
55 std::size_t lastSlash = path.rfind(
'/');
56 std::size_t lastBackSlash = path.rfind(
'\\');
57 std::size_t lastSeparator;
58 if (lastSlash == std::string::npos && lastBackSlash == std::string::npos) {
59 return std::string_view();
60 }
else if (lastSlash == std::string::npos) {
61 lastSeparator = lastBackSlash;
62 }
else if (lastBackSlash == std::string::npos) {
63 lastSeparator = lastSlash;
65 lastSeparator = lastSlash > lastBackSlash ? lastSlash : lastBackSlash;
67 return path.substr(0, lastSeparator + 1);
78 static const char invalidPathChars[] = {
'\"',
'<',
'>',
'?',
'!',
'*',
'|',
'/',
':',
'\\',
'\n' };
79 for (
const char *
i = invalidPathChars, *end = invalidPathChars +
sizeof(invalidPathChars);
i != end; ++
i) {
81 while (startPos != string::npos) {
82 fileName.replace(startPos, 1,
string());
Contains all utilities provides by the c++utilities library.
CPP_UTILITIES_EXPORT void removeInvalidChars(std::string &fileName)
Removes invalid characters from the specified fileName.
CPP_UTILITIES_EXPORT std::string fileName(const std::string &path)
Returns the file name and extension of the specified path string.
CPP_UTILITIES_EXPORT std::string directory(const std::string &path)
Returns the directory of the specified path string (including trailing slash).