forked from supercollider/supercollider
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSC_Codecvt.hpp
133 lines (119 loc) · 4.33 KB
/
SC_Codecvt.hpp
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
* \file SC_Codecvt.hpp
*
* \brief Codepage conversion utilities.
*
* This header provides convenience functions to convert strings and paths between UTF-8 and the OS's
* filesystem API encoding. On POSIX systems, this does nothing. On Windows, this converts between
* UTF-8 and UTF-16. On Windows, additional functions are provided to convert between UTF-8 std::string
* and UTF-16 std::wstring.
*
* $Author: Brian Heim $
*
* \version 1.1
*
* $Date: 2017-05-14 $
*
* $Contact: [email protected] $
*
* Created on: 2017-05-14
*/
/*
* Copyright (C) Brian Heim, 2017. All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#pragma once
#include <string> // string
#include <filesystem>
#ifdef _WIN32
# include <codecvt> // std::codecvt_utf8_utf16, utf16
# include <locale> // std::wstring_convert
#endif
/** \namespace SC_Codecvt
* \brief Namespace for codepage conversion functions. */
namespace SC_Codecvt {
// Windows helper functions. Only defined on Windows to avoid having
// to unnecessarily include <codecvt> and <locale>
#ifdef _WIN32
/** \brief Converts a UTF-8 char str to UTF-16 std::wstring
*
* This function is only defined on Windows, to avoid unnecessary header includes. */
inline std::wstring utf8_cstr_to_utf16_wstring(const char* s) {
return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>>().from_bytes(s);
}
/** \brief Converts a UTF-16 wchar_t str to UTF-8 std::string
*
* This function is only defined on Windows, to avoid unnecessary header includes. */
inline std::string utf16_wcstr_to_utf8_string(const wchar_t* s) {
return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>>().to_bytes(s);
}
#endif // _WIN32
/** \brief Converts a path to a UTF-8 encoded string.
*
* On POSIX platforms, this just converts using .string(). On Windows, uses
* conversion between UTF-16 and UTF-8. */
inline std::string path_to_utf8_str(const std::filesystem::path& p) {
#ifdef _WIN32
return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>>().to_bytes(p.native());
#else
return p.string();
#endif // _WIN32
}
/** \brief Converts a UTF-8 encoded string to a path.
*
* On POSIX platforms, this converts using the default constructor. On Windows,
* uses conversion between UTF-16 and UTF-8. */
inline std::filesystem::path utf8_str_to_path(const std::string& s) {
#ifdef _WIN32
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
std::wstring wideString = converter.from_bytes(s);
return std::filesystem::path(std::move(wideString));
#else
return std::filesystem::path(s);
#endif // _WIN32
}
/** \brief Converts a native filesystem-encoded string to a UTF-8 string.
*
* On Windows, converts between UTF-16 and UTF-8. On POSIX systems, no-op. */
inline std::string utf8_to_native_str(const std::string& s) {
#ifdef _WIN32
// first to wide string (native format)
std::wstring ws = utf8_cstr_to_utf16_wstring(s.c_str());
// then to string (still native)
std::wstring_convert<std::codecvt_utf16<wchar_t>> conv_16to16;
std::string ret = conv_16to16.to_bytes(ws);
return ret;
#else
return s;
#endif // _WIN32
}
/** \brief Converts a UTF-8 string to a native filesystem-encoded string.
*
* On Windows, converts between UTF-16 and UTF-8. On POSIX systems, no-op. */
inline std::string native_to_utf8_str(const std::string& s) {
#ifdef _WIN32
// first to wide string (still native format)
std::wstring_convert<std::codecvt_utf16<wchar_t>> conv_16to16;
std::wstring ws = conv_16to16.from_bytes(s);
// then to string (utf8)
std::string ret = utf16_wcstr_to_utf8_string(ws.c_str());
return ret;
#else
return s;
#endif // _WIN32
}
} // SC_Codecvt