File: path.cpp

package info (click to toggle)
martchus-cpp-utilities 5.33.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,396 kB
  • sloc: cpp: 12,679; awk: 18; ansic: 12; makefile: 10
file content (86 lines) | stat: -rw-r--r-- 2,650 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#define CPP_UTILITIES_PATHHELPER_STRING_VIEW

#include "./path.h"

#include <cstdlib>
#include <string>

using namespace std;

namespace CppUtilities {

/*!
 * \brief Returns the file name and extension of the specified \a path string.
 */
std::string fileName(const std::string &path)
{
    return std::string(fileName(std::string_view(path)));
}

/*!
 * \brief Returns the directory of the specified \a path string (including trailing slash).
 */
std::string directory(const std::string &path)
{
    return std::string(directory(std::string_view(path)));
}

/*!
 * \brief Returns the file name and extension of the specified \a path string.
 */
std::string_view fileName(std::string_view path)
{
    std::size_t lastSlash = path.rfind('/');
    std::size_t lastBackSlash = path.rfind('\\');
    std::size_t lastSeparator;
    if (lastSlash == std::string::npos && lastBackSlash == std::string::npos) {
        return path;
    } else if (lastSlash == std::string::npos) {
        lastSeparator = lastBackSlash;
    } else if (lastBackSlash == std::string::npos) {
        lastSeparator = lastSlash;
    } else {
        lastSeparator = lastSlash > lastBackSlash ? lastSlash : lastBackSlash;
    }
    return path.substr(lastSeparator + 1);
}

/*!
 * \brief Returns the directory of the specified \a path string (including trailing slash).
 */
std::string_view directory(std::string_view path)
{
    std::size_t lastSlash = path.rfind('/');
    std::size_t lastBackSlash = path.rfind('\\');
    std::size_t lastSeparator;
    if (lastSlash == std::string::npos && lastBackSlash == std::string::npos) {
        return std::string_view();
    } else if (lastSlash == std::string::npos) {
        lastSeparator = lastBackSlash;
    } else if (lastBackSlash == std::string::npos) {
        lastSeparator = lastSlash;
    } else {
        lastSeparator = lastSlash > lastBackSlash ? lastSlash : lastBackSlash;
    }
    return path.substr(0, lastSeparator + 1);
}

/*!
 * \brief Removes invalid characters from the specified \a fileName.
 *
 * The characters <, >, ?, !, *, |, /, :, \ and new lines are considered as invalid.
 */
void removeInvalidChars(std::string &fileName)
{
    size_t startPos = 0;
    static const char invalidPathChars[] = { '\"', '<', '>', '?', '!', '*', '|', '/', ':', '\\', '\n' };
    for (const char *i = invalidPathChars, *end = invalidPathChars + sizeof(invalidPathChars); i != end; ++i) {
        startPos = fileName.find(*i);
        while (startPos != string::npos) {
            fileName.replace(startPos, 1, string());
            startPos = fileName.find(*i, startPos);
        }
    }
}

} // namespace CppUtilities