C++ Utilities: io/path.cpp Source File

C++ Utilities 5.34.2 Useful C++ classes and routines such as argument parser, IO and conversion utilities

Loading...
Searching...
No Matches
path.cpp
Go to the documentation of this file.
1#define CPP_UTILITIES_PATHHELPER_STRING_VIEW
2
3#include "./path.h"
4
5#include <cstdlib>
6#include <string>
7
8using namespace std;
9
10namespace CppUtilities {
11
15std::string fileName(const std::string &path)
16{
17 return std::string(fileName(std::string_view(path)));
18}
19
23std::string directory(const std::string &path)
24{
25 return std::string(directory(std::string_view(path)));
26}
27
31std::string_view fileName(std::string_view path)
32{
33 std::size_t lastSlash = path.rfind('/');
34 std::size_t lastBackSlash = path.rfind('\\');
35 std::size_t lastSeparator;
36 if (lastSlash == std::string::npos && lastBackSlash == std::string::npos) {
37 return path;
38 } else if (lastSlash == std::string::npos) {
39 lastSeparator = lastBackSlash;
40 } else if (lastBackSlash == std::string::npos) {
41 lastSeparator = lastSlash;
42 } else {
43 lastSeparator = lastSlash > lastBackSlash ? lastSlash : lastBackSlash;
44 }
45 return path.substr(lastSeparator + 1);
46}
47
51std::string_view directory(std::string_view path)
52{
53 std::size_t lastSlash = path.rfind('/');
54 std::size_t lastBackSlash = path.rfind('\\');
55 std::size_t lastSeparator;
56 if (lastSlash == std::string::npos && lastBackSlash == std::string::npos) {
57 return std::string_view();
58 } else if (lastSlash == std::string::npos) {
59 lastSeparator = lastBackSlash;
60 } else if (lastBackSlash == std::string::npos) {
61 lastSeparator = lastSlash;
62 } else {
63 lastSeparator = lastSlash > lastBackSlash ? lastSlash : lastBackSlash;
64 }
65 return path.substr(0, lastSeparator + 1);
66}
67
73 void removeInvalidChars(std::string &fileName)
74{
75 size_t startPos = 0;
76 static const char invalidPathChars[] = { '\"', '<', '>', '?', '!', '*', '|', '/', ':', '\\', '\n' };
77 for (const char *i = invalidPathChars, *end = invalidPathChars + sizeof(invalidPathChars); i != end; ++i) {
78 startPos = fileName.find(*i);
79 while (startPos != string::npos) {
80 fileName.replace(startPos, 1, string());
81 startPos = fileName.find(*i, startPos);
82 }
83 }
84}
85
86} // namespace CppUtilities
CppUtilities
Contains all utilities provided by the c++utilities library.
Definition argumentparser.h:18
CppUtilities::removeInvalidChars
CPP_UTILITIES_EXPORT void removeInvalidChars(std::string &fileName)
Removes invalid characters from the specified fileName.
Definition path.cpp:73
CppUtilities::fileName
CPP_UTILITIES_EXPORT std::string fileName(const std::string &path)
Returns the file name and extension of the specified path string.
Definition path.cpp:15
CppUtilities::directory
CPP_UTILITIES_EXPORT std::string directory(const std::string &path)
Returns the directory of the specified path string (including trailing slash).
Definition path.cpp:23
std
STL namespace.
path.h
i
constexpr int i
Definition traitstests.cpp:106