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

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

Loading...
Searching...
No Matches
path.h
Go to the documentation of this file.
1#ifndef IOUTILITIES_PATHHELPER_H
2#define IOUTILITIES_PATHHELPER_H
3
4#include "../global.h"
5
6#ifdef PLATFORM_WINDOWS
7#include "../conversion/stringconversion.h"
8#endif
9
10#include <string>
11#include <string_view>
12#ifdef CPP_UTILITIES_USE_STANDARD_FILESYSTEM
13#include <filesystem>
14#endif
15
16#ifdef PLATFORM_WINDOWS
17#define PATH_SEP_CHAR '\\'
18#define SEARCH_PATH_SEP_CHAR ';'
19#define PATH_SEP_STR "\\"
20#define SEARCH_PATH_SEP_STR ";"
21#else
22#define PATH_SEP_CHAR '/'
23#define SEARCH_PATH_SEP_CHAR ':'
24#define PATH_SEP_STR "/"
25#define SEARCH_PATH_SEP_STR ":"
26#endif
27
28namespace CppUtilities {
29
30 CPP_UTILITIES_EXPORT std::string fileName(const std::string &path);
31 CPP_UTILITIES_EXPORT std::string directory(const std::string &path);
32#ifdef CPP_UTILITIES_PATHHELPER_STRING_VIEW
33 CPP_UTILITIES_EXPORT std::string_view fileName(std::string_view path);
34 CPP_UTILITIES_EXPORT std::string_view directory(std::string_view path);
35#endif
36 CPP_UTILITIES_EXPORT void removeInvalidChars(std::string &fileName);
37
40 using PathValueType =
41#ifdef PLATFORM_WINDOWS
42 wchar_t
43#else
44 char
45#endif
46 ;
47#ifdef CPP_UTILITIES_USE_STANDARD_FILESYSTEM
48static_assert(std::is_same_v<typename std::filesystem::path::value_type, PathValueType>);
49#endif
50
53 using NativePathString = std::basic_string<PathValueType>;
56 using NativePathStringView = std::basic_string_view<PathValueType>;
59 using PathString = std::string;
62 using PathStringView = std::string_view;
63
75inline
76#ifdef PLATFORM_WINDOWS
77 NativePathString
78#else
79 NativePathStringView
80#endif
81 makeNativePath(PathStringView path)
82{
83#ifdef PLATFORM_WINDOWS
84 auto ec = std::error_code();
85 return convertMultiByteToWide(ec, path);
86#else
87 return path;
88#endif
89}
90
91#if !defined(CPP_UTILITIES_NO_ICONV) || !defined(PLATFORM_WINDOWS)
96inline
97#ifdef PLATFORM_WINDOWS
98 PathString
99#else
100 PathStringView
101#endif
102 extractNativePath(NativePathStringView path)
103{
104#ifdef PLATFORM_WINDOWS
105 auto res = convertUtf16LEToUtf8(reinterpret_cast<const char *>(path.data()), path.size() * 2);
106 return std::string(res.first.get(), res.second);
107#else
108 return path;
109#endif
110}
111#endif
112
113} // namespace CppUtilities
114
115#endif // IOUTILITIES_PATHHELPER_H
global.h
CPP_UTILITIES_EXPORT
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.
Definition global.h:14
CppUtilities
Contains all utilities provided by the c++utilities library.
Definition argumentparser.h:18
CppUtilities::extractNativePath
PathStringView extractNativePath(NativePathStringView path)
Returns path as UTF-8 string or string view.
Definition path.h:102
CppUtilities::convertUtf16LEToUtf8
CPP_UTILITIES_EXPORT StringData convertUtf16LEToUtf8(const char *inputBuffer, std::size_t inputBufferSize)
Converts the specified UTF-16 (little-endian) string to UTF-8.
Definition stringconversion.cpp:170
CppUtilities::NativePathString
std::basic_string< PathValueType > NativePathString
The string type used to store paths in the native encoding.
Definition path.h:53
CppUtilities::PathValueType
char PathValueType
The native type used by std::filesystem:path.
Definition path.h:40
CppUtilities::PathStringView
std::string_view PathStringView
The string view type used to pass paths UTF-8 encoded.
Definition path.h:62
CppUtilities::NativePathStringView
std::basic_string_view< PathValueType > NativePathStringView
The string view type used to pass paths in the native encoding.
Definition path.h:56
CppUtilities::PathString
std::string PathString
The string type used to store paths UTF-8 encoded.
Definition path.h:59
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::makeNativePath
NativePathStringView makeNativePath(PathStringView path)
Returns path in the platform's native encoding.
Definition path.h:81
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
stringconversion.h