C++ Utilities 5.27.0
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
8#endif
9
10#include <list>
11#include <string>
12#include <string_view>
13#ifdef CPP_UTILITIES_USE_STANDARD_FILESYSTEM
14#include <filesystem>
15#endif
16
17#ifdef PLATFORM_WINDOWS
18#define PATH_SEP_CHAR '\\'
19#define SEARCH_PATH_SEP_CHAR ';'
20#define PATH_SEP_STR "\\"
21#define SEARCH_PATH_SEP_STR ";"
22#else
23#define PATH_SEP_CHAR '/'
24#define SEARCH_PATH_SEP_CHAR ':'
25#define PATH_SEP_STR "/"
26#define SEARCH_PATH_SEP_STR ":"
27#endif
28
29namespace CppUtilities {
30
31CPP_UTILITIES_EXPORT std::string fileName(const std::string &path);
32CPP_UTILITIES_EXPORT std::string directory(const std::string &path);
33#ifdef CPP_UTILITIES_PATHHELPER_STRING_VIEW
34CPP_UTILITIES_EXPORT std::string_view fileName(std::string_view path);
35CPP_UTILITIES_EXPORT std::string_view directory(std::string_view path);
36#endif
38
42#ifdef PLATFORM_WINDOWS
43 wchar_t
44#else
45 char
46#endif
47 ;
48#ifdef CPP_UTILITIES_USE_STANDARD_FILESYSTEM
49static_assert(std::is_same_v<typename std::filesystem::path::value_type, PathValueType>);
50#endif
51
54using NativePathString = std::basic_string<PathValueType>;
57using NativePathStringView = std::basic_string_view<PathValueType>;
60using PathString = std::string;
63using PathStringView = std::string_view;
64
76inline
77#ifdef PLATFORM_WINDOWS
79#else
81#endif
83{
84#ifdef PLATFORM_WINDOWS
85 auto ec = std::error_code();
86 return convertMultiByteToWide(ec, path);
87#else
88 return path;
89#endif
90}
91
92#if !defined(CPP_UTILITIES_NO_ICONV) || !defined(PLATFORM_WINDOWS)
97inline
98#ifdef PLATFORM_WINDOWS
100#else
102#endif
104{
105#ifdef PLATFORM_WINDOWS
106 auto res = convertUtf16LEToUtf8(reinterpret_cast<const char *>(path.data()), path.size() * 2);
107 return std::string(res.first.get(), res.second);
108#else
109 return path;
110#endif
111}
112#endif
113
114} // namespace CppUtilities
115
116#endif // IOUTILITIES_PATHHELPER_H
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.
Definition global.h:14
Contains all utilities provides by the c++utilities library.
PathStringView extractNativePath(NativePathStringView path)
Returns path as UTF-8 string or string view.
Definition path.h:103
CPP_UTILITIES_EXPORT StringData convertUtf16LEToUtf8(const char *inputBuffer, std::size_t inputBufferSize)
Converts the specified UTF-16 (little-endian) string to UTF-8.
std::basic_string< PathValueType > NativePathString
The string type used to store paths in the native encoding.
Definition path.h:54
char PathValueType
The native type used by std::filesystem:path.
Definition path.h:41
std::string_view PathStringView
The string view type used to pass paths UTF-8 encoded.
Definition path.h:63
std::basic_string_view< PathValueType > NativePathStringView
The string view type used to pass paths in the native encoding.
Definition path.h:57
std::string PathString
The string type used to store paths UTF-8 encoded.
Definition path.h:60
CPP_UTILITIES_EXPORT void removeInvalidChars(std::string &fileName)
Removes invalid characters from the specified fileName.
Definition path.cpp:75
CPP_UTILITIES_EXPORT std::string fileName(const std::string &path)
Returns the file name and extension of the specified path string.
Definition path.cpp:17
NativePathStringView makeNativePath(PathStringView path)
Returns path in the platform's native encoding.
Definition path.h:82
CPP_UTILITIES_EXPORT std::string directory(const std::string &path)
Returns the directory of the specified path string (including trailing slash).
Definition path.cpp:25