|
1 |
| -/** |
2 |
| - * |
3 |
| - * @file Utilities.h |
4 |
| - * @author An Tao |
5 |
| - * |
6 |
| - * Copyright 2018, An Tao. All rights reserved. |
7 |
| - * https://github.com/an-tao/drogon |
8 |
| - * Use of this source code is governed by a MIT license |
9 |
| - * that can be found in the License file. |
10 |
| - * |
11 |
| - * Drogon |
12 |
| - * |
13 |
| - */ |
14 |
| - |
15 |
| -#pragma once |
16 |
| - |
17 |
| -#include <trantor/exports.h> |
18 |
| -#include <string> |
19 |
| - |
20 |
| -namespace trantor |
21 |
| -{ |
22 |
| -/** |
23 |
| - * @brief Trantor helper functions. |
24 |
| - * |
25 |
| - */ |
26 |
| -namespace utils |
27 |
| -{ |
28 |
| -/** |
29 |
| - * @brief Convert a wide string to a UTF-8. |
30 |
| - * @details UCS2 on Windows, UTF-32 on Linux & Mac |
31 |
| - * |
32 |
| - * @param str String to convert |
33 |
| - * |
34 |
| - * @return converted string. |
35 |
| - */ |
36 |
| -TRANTOR_EXPORT std::string toUtf8(const std::wstring &wstr); |
37 |
| -/** |
38 |
| - * @brief Convert a UTF-8 string to a wide string. |
39 |
| - * @details UCS2 on Windows, UTF-32 on Linux & Mac |
40 |
| - * |
41 |
| - * @param str String to convert |
42 |
| - * |
43 |
| - * @return converted string. |
44 |
| - */ |
45 |
| -TRANTOR_EXPORT std::wstring fromUtf8(const std::string &str); |
46 |
| - |
47 |
| -/** |
48 |
| - * @details Convert a wide string path with arbitrary directory separators |
49 |
| - * to a UTF-8 portable path for use with trantor. |
50 |
| - * |
51 |
| - * This is a helper, mainly for Windows and multi-platform projects. |
52 |
| - * |
53 |
| - * @note On Windows, backslash directory separators are converted to slash to |
54 |
| - * keep portable paths. |
55 |
| - * |
56 |
| - * @remarks On other OSes, backslashes are not converted to slash, since they |
57 |
| - * are valid characters for directory/file names. |
58 |
| - * |
59 |
| - * @param strPath Wide string path. |
60 |
| - * |
61 |
| - * @return std::string UTF-8 path, with slash directory separator. |
62 |
| - */ |
63 |
| -TRANTOR_EXPORT std::string fromWidePath(const std::wstring &strPath); |
64 |
| -/** |
65 |
| - * @details Convert a UTF-8 path with arbitrary directory separator to a wide |
66 |
| - * string path. |
67 |
| - * |
68 |
| - * This is a helper, mainly for Windows and multi-platform projects. |
69 |
| - * |
70 |
| - * @note On Windows, slash directory separators are converted to backslash. |
71 |
| - * Although it accepts both slash and backslash as directory separator in its |
72 |
| - * API, it is better to stick to its standard. |
73 |
| -
|
74 |
| - * @remarks On other OSes, slashes are not converted to backslashes, since they |
75 |
| - * are not interpreted as directory separators and are valid characters for |
76 |
| - * directory/file names. |
77 |
| - * |
78 |
| - * @param strUtf8Path Ascii path considered as being UTF-8. |
79 |
| - * |
80 |
| - * @return std::wstring path with, on windows, standard backslash directory |
81 |
| - * separator to stick to its standard. |
82 |
| - */ |
83 |
| -TRANTOR_EXPORT std::wstring toWidePath(const std::string &strUtf8Path); |
84 |
| - |
85 |
| -// Helpers for multi-platform development |
86 |
| -// OS dependent |
87 |
| -#ifdef _WIN32 |
88 |
| -/** |
89 |
| - * @details Convert an UTF-8 path to a native path. |
90 |
| - * |
91 |
| - * This is a helper for Windows and multi-platform projects. |
92 |
| - * |
93 |
| - * Converts the path from UTF-8 to wide string and replaces slash directory |
94 |
| - * separators with backslash. |
95 |
| - * |
96 |
| - * @remarks Although it accepts both slash and backslash as directory |
97 |
| - * separators in its API, it is better to stick to its standard. |
98 |
| - * |
99 |
| - * @param strPath UTF-8 path. |
100 |
| - * |
101 |
| - * @return Native path suitable for the Windows unicode API. |
102 |
| - */ |
103 |
| -inline std::wstring toNativePath(const std::string &strPath) |
104 |
| -{ |
105 |
| - return toWidePath(strPath); |
106 |
| -} |
107 |
| -/** |
108 |
| - * @details Convert an UTF-8 path to a native path. |
109 |
| - * |
110 |
| - * This is a helper for non-Windows OSes for multi-platform projects. |
111 |
| - * |
112 |
| - * Does nothing. |
113 |
| - * |
114 |
| - * @param strPath UTF-8 path. |
115 |
| - * |
116 |
| - * @return \p strPath. |
117 |
| - */ |
118 |
| -inline const std::wstring &toNativePath(const std::wstring &strPath) |
119 |
| -{ |
120 |
| - return strPath; |
121 |
| -} |
122 |
| -#else // __WIN32 |
123 |
| -/** |
124 |
| - * @details Convert an UTF-8 path to a native path. |
125 |
| - * |
126 |
| - * This is a helper for non-Windows OSes for multi-platform projects. |
127 |
| - * |
128 |
| - * Does nothing. |
129 |
| - * |
130 |
| - * @param strPath UTF-8 path. |
131 |
| - * |
132 |
| - * @return \p strPath. |
133 |
| - */ |
134 |
| -inline const std::string &toNativePath(const std::string &strPath) |
135 |
| -{ |
136 |
| - return strPath; |
137 |
| -} |
138 |
| -/** |
139 |
| - * @details Convert an wide string path to a UTF-8 path. |
140 |
| - * |
141 |
| - * This is a helper, mainly for Windows and multi-platform projects. |
142 |
| - * |
143 |
| - * @note On Windows, backslash directory separators are converted to slash to |
144 |
| - * keep portable paths. |
145 |
| -
|
146 |
| - * @warning On other OSes, backslashes are not converted to slash, since they |
147 |
| - * are valid characters for directory/file names. |
148 |
| - * |
149 |
| - * @param strPath wide string path. |
150 |
| - * |
151 |
| - * @return Generic path, with slash directory separators |
152 |
| - */ |
153 |
| -inline std::string toNativePath(const std::wstring &strPath) |
154 |
| -{ |
155 |
| - return fromWidePath(strPath); |
156 |
| -} |
157 |
| -#endif // _WIN32 |
158 |
| -/** |
159 |
| - * @note NoOP on all OSes |
160 |
| - */ |
161 |
| -inline const std::string &fromNativePath(const std::string &strPath) |
162 |
| -{ |
163 |
| - return strPath; |
164 |
| -} |
165 |
| -/** |
166 |
| - * @note fromWidePath() on all OSes |
167 |
| - */ |
168 |
| -// Convert on all systems |
169 |
| -inline std::string fromNativePath(const std::wstring &strPath) |
170 |
| -{ |
171 |
| - return fromWidePath(strPath); |
172 |
| -} |
173 |
| - |
174 |
| -} // namespace utils |
175 |
| - |
176 |
| -} // namespace trantor |
| 1 | +/** |
| 2 | + * |
| 3 | + * @file Utilities.h |
| 4 | + * @author An Tao |
| 5 | + * |
| 6 | + * Copyright 2018, An Tao. All rights reserved. |
| 7 | + * https://github.com/an-tao/drogon |
| 8 | + * Use of this source code is governed by a MIT license |
| 9 | + * that can be found in the License file. |
| 10 | + * |
| 11 | + * Drogon |
| 12 | + * |
| 13 | + */ |
| 14 | + |
| 15 | +#pragma once |
| 16 | + |
| 17 | +#include <trantor/exports.h> |
| 18 | +#include <string> |
| 19 | + |
| 20 | +namespace trantor |
| 21 | +{ |
| 22 | +/** |
| 23 | + * @brief Trantor helper functions. |
| 24 | + * |
| 25 | + */ |
| 26 | +namespace utils |
| 27 | +{ |
| 28 | +/** |
| 29 | + * @brief Convert a wide string to a UTF-8. |
| 30 | + * @details UCS2 on Windows, UTF-32 on Linux & Mac |
| 31 | + * |
| 32 | + * @param str String to convert |
| 33 | + * |
| 34 | + * @return converted string. |
| 35 | + */ |
| 36 | +TRANTOR_EXPORT std::string toUtf8(const std::wstring &wstr); |
| 37 | +/** |
| 38 | + * @brief Convert a UTF-8 string to a wide string. |
| 39 | + * @details UCS2 on Windows, UTF-32 on Linux & Mac |
| 40 | + * |
| 41 | + * @param str String to convert |
| 42 | + * |
| 43 | + * @return converted string. |
| 44 | + */ |
| 45 | +TRANTOR_EXPORT std::wstring fromUtf8(const std::string &str); |
| 46 | + |
| 47 | +/** |
| 48 | + * @details Convert a wide string path with arbitrary directory separators |
| 49 | + * to a UTF-8 portable path for use with trantor. |
| 50 | + * |
| 51 | + * This is a helper, mainly for Windows and multi-platform projects. |
| 52 | + * |
| 53 | + * @note On Windows, backslash directory separators are converted to slash to |
| 54 | + * keep portable paths. |
| 55 | + * |
| 56 | + * @remarks On other OSes, backslashes are not converted to slash, since they |
| 57 | + * are valid characters for directory/file names. |
| 58 | + * |
| 59 | + * @param strPath Wide string path. |
| 60 | + * |
| 61 | + * @return std::string UTF-8 path, with slash directory separator. |
| 62 | + */ |
| 63 | +TRANTOR_EXPORT std::string fromWidePath(const std::wstring &strPath); |
| 64 | +/** |
| 65 | + * @details Convert a UTF-8 path with arbitrary directory separator to a wide |
| 66 | + * string path. |
| 67 | + * |
| 68 | + * This is a helper, mainly for Windows and multi-platform projects. |
| 69 | + * |
| 70 | + * @note On Windows, slash directory separators are converted to backslash. |
| 71 | + * Although it accepts both slash and backslash as directory separator in its |
| 72 | + * API, it is better to stick to its standard. |
| 73 | +
|
| 74 | + * @remarks On other OSes, slashes are not converted to backslashes, since they |
| 75 | + * are not interpreted as directory separators and are valid characters for |
| 76 | + * directory/file names. |
| 77 | + * |
| 78 | + * @param strUtf8Path Ascii path considered as being UTF-8. |
| 79 | + * |
| 80 | + * @return std::wstring path with, on windows, standard backslash directory |
| 81 | + * separator to stick to its standard. |
| 82 | + */ |
| 83 | +TRANTOR_EXPORT std::wstring toWidePath(const std::string &strUtf8Path); |
| 84 | + |
| 85 | +// Helpers for multi-platform development |
| 86 | +// OS dependent |
| 87 | +#if defined(_WIN32) && !defined(__MINGW32__) |
| 88 | +/** |
| 89 | + * @details Convert an UTF-8 path to a native path. |
| 90 | + * |
| 91 | + * This is a helper for Windows and multi-platform projects. |
| 92 | + * |
| 93 | + * Converts the path from UTF-8 to wide string and replaces slash directory |
| 94 | + * separators with backslash. |
| 95 | + * |
| 96 | + * @remarks Although it accepts both slash and backslash as directory |
| 97 | + * separators in its API, it is better to stick to its standard. |
| 98 | + * |
| 99 | + * @param strPath UTF-8 path. |
| 100 | + * |
| 101 | + * @return Native path suitable for the Windows unicode API. |
| 102 | + */ |
| 103 | +inline std::wstring toNativePath(const std::string &strPath) |
| 104 | +{ |
| 105 | + return toWidePath(strPath); |
| 106 | +} |
| 107 | +/** |
| 108 | + * @details Convert an UTF-8 path to a native path. |
| 109 | + * |
| 110 | + * This is a helper for non-Windows OSes for multi-platform projects. |
| 111 | + * |
| 112 | + * Does nothing. |
| 113 | + * |
| 114 | + * @param strPath UTF-8 path. |
| 115 | + * |
| 116 | + * @return \p strPath. |
| 117 | + */ |
| 118 | +inline const std::wstring &toNativePath(const std::wstring &strPath) |
| 119 | +{ |
| 120 | + return strPath; |
| 121 | +} |
| 122 | +#else // __WIN32 |
| 123 | +/** |
| 124 | + * @details Convert an UTF-8 path to a native path. |
| 125 | + * |
| 126 | + * This is a helper for non-Windows OSes for multi-platform projects. |
| 127 | + * |
| 128 | + * Does nothing. |
| 129 | + * |
| 130 | + * @param strPath UTF-8 path. |
| 131 | + * |
| 132 | + * @return \p strPath. |
| 133 | + */ |
| 134 | +inline const std::string &toNativePath(const std::string &strPath) |
| 135 | +{ |
| 136 | + return strPath; |
| 137 | +} |
| 138 | +/** |
| 139 | + * @details Convert an wide string path to a UTF-8 path. |
| 140 | + * |
| 141 | + * This is a helper, mainly for Windows and multi-platform projects. |
| 142 | + * |
| 143 | + * @note On Windows, backslash directory separators are converted to slash to |
| 144 | + * keep portable paths. |
| 145 | +
|
| 146 | + * @warning On other OSes, backslashes are not converted to slash, since they |
| 147 | + * are valid characters for directory/file names. |
| 148 | + * |
| 149 | + * @param strPath wide string path. |
| 150 | + * |
| 151 | + * @return Generic path, with slash directory separators |
| 152 | + */ |
| 153 | +inline std::string toNativePath(const std::wstring &strPath) |
| 154 | +{ |
| 155 | + return fromWidePath(strPath); |
| 156 | +} |
| 157 | +#endif // _WIN32 |
| 158 | +/** |
| 159 | + * @note NoOP on all OSes |
| 160 | + */ |
| 161 | +inline const std::string &fromNativePath(const std::string &strPath) |
| 162 | +{ |
| 163 | + return strPath; |
| 164 | +} |
| 165 | +/** |
| 166 | + * @note fromWidePath() on all OSes |
| 167 | + */ |
| 168 | +// Convert on all systems |
| 169 | +inline std::string fromNativePath(const std::wstring &strPath) |
| 170 | +{ |
| 171 | + return fromWidePath(strPath); |
| 172 | +} |
| 173 | + |
| 174 | +} // namespace utils |
| 175 | + |
| 176 | +} // namespace trantor |
0 commit comments