Repository navigation
Expand file tree
/
Copy pathStringConversions.cpp
More file actions
176 lines (143 loc) · 4.48 KB
/
Copy pathStringConversions.cpp
File metadata and controls
176 lines (143 loc) · 4.48 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright © 2013 CCP ehf.
#include "include/StringConversions.h"
std::wstring UTF8ToWide( const std::string& utf8String )
{
return UTF8ToWide( utf8String.c_str() );
}
std::string WideToUTF8( const std::wstring& wideString )
{
return WideToUTF8( wideString.c_str() );
}
#if _WIN32
std::wstring UTF8ToWide( const char* utf8String )
{
return std::wstring( CA2W( utf8String, CP_UTF8 ) );
}
std::string WideToUTF8( const wchar_t* wideString )
{
return std::string( CW2A( wideString, CP_UTF8 ) );
}
#else
#include <wchar.h>
#include <string.h>
#include "CcpMemory.h"
BlueConvertWideToAscii::BlueConvertWideToAscii( const wchar_t* src ) : m_converted( nullptr )
{
Init( src );
}
BlueConvertWideToAscii::~BlueConvertWideToAscii()
{
if( m_converted != m_buffer )
{
CCP_FREE( (void*)m_converted );
}
}
void BlueConvertWideToAscii::Init( const wchar_t* src )
{
size_t sizeNeeded = wcsrtombs( nullptr, &src, 0, nullptr );
if( sizeNeeded == (size_t)-1 )
{
m_converted = m_buffer;
strcpy( m_converted, "Invalid string" );
return;
}
if( sizeNeeded >= BUFFER_SIZE )
{
m_converted = (char*)CCP_MALLOC( "ConvertWideToAscii", sizeNeeded + 1 );
}
else
{
m_converted = m_buffer;
}
wcsrtombs( m_converted, &src, sizeNeeded, nullptr );
m_converted[sizeNeeded] = 0;
}
BlueConvertAsciiToWide::BlueConvertAsciiToWide( const char* src ) : m_converted( nullptr )
{
Init( src );
}
BlueConvertAsciiToWide::~BlueConvertAsciiToWide()
{
if( m_converted != m_buffer )
{
CCP_FREE( (void*)m_converted );
}
}
void BlueConvertAsciiToWide::Init( const char* src )
{
size_t srcLen = strlen( src );
size_t sizeNeeded = mbsrtowcs( nullptr, &src, srcLen, nullptr );
if( sizeNeeded == (size_t)-1 )
{
m_converted = m_buffer;
wcscpy( m_converted, L"Invalid string" );
return;
}
if( sizeNeeded >= BUFFER_SIZE )
{
m_converted = (wchar_t*)CCP_MALLOC( "ConvertAsciiToWide", (sizeNeeded + 1) * sizeof( wchar_t ) );
}
else
{
m_converted = m_buffer;
}
mbsrtowcs( m_converted, &src, srcLen, nullptr );
m_converted[sizeNeeded] = 0;
}
#endif
#if __linux__
#include <iconv.h>
std::wstring UTF8ToWide(const char* utf8String)
{
std::wstring result;
const auto descriptor = iconv_open("WCHAR_T", "UTF-8");
if (descriptor == reinterpret_cast<iconv_t>(-1))
{
CCP_LOGERR("Failed to create code conversion descriptor from UTF-8 to WCHAR_T: [errno: %d]", errno);
return result;
}
std::string_view input = utf8String;
auto inBytesLeft = input.size();
auto inputBuffer = const_cast<char*>(input.data()); // iconv will increment buffer
auto outputBufferSize = input.size() * sizeof(wchar_t); // Size is in bytes
result.resize(outputBufferSize);
auto outputBuffer = reinterpret_cast<char*>(result.data()); // Calculate buffer position in case backing buffer is reallocated
auto outBytesLeft = outputBufferSize;
if (iconv(descriptor, &inputBuffer , &inBytesLeft, &outputBuffer, &outBytesLeft) == static_cast<size_t>(-1))
{
CCP_LOGERR("Failed to convert from WCHAR_T to UTF-8: [errno: %d, inBytesLeft: %lu, outBytesLeft: %lu, outputBufferSize: %lu]", errno, inBytesLeft, outBytesLeft, outputBufferSize);
iconv_close(descriptor);
return result;
}
iconv_close(descriptor);
return result;
}
std::string WideToUTF8( const wchar_t* wideString )
{
std::string result;
const auto descriptor = iconv_open("UTF-8", "WCHAR_T");
if (descriptor == reinterpret_cast<iconv_t>(-1))
{
CCP_LOGERR("Failed to create code conversion descriptor from UTF-8 to WCHAR_T: [errno: %d]", errno);
return result;
}
const std::wstring_view input = wideString;
auto inBytesLeft = input.size() * sizeof(wchar_t);
auto inputBuffer = const_cast<char*>(reinterpret_cast<const char*>(input.data())); // iconv will increment buffer
// UTF8 code points require four bytes at most
auto outputBufferSize = input.size() * 4; // Size is in bytes
result.resize(outputBufferSize);
auto outputBuffer = result.data(); // Calculate buffer position in case backing buffer is reallocated
auto outBytesLeft = outputBufferSize;
if (iconv(descriptor, &inputBuffer , &inBytesLeft, &outputBuffer, &outBytesLeft) == static_cast<size_t>(-1))
{
CCP_LOGERR("Failed to convert from UTF-8 to WCHAR_T: [errno: %d, inBytesLeft: %lu, outBytesLeft: %lu, outputBufferSize: %lu]", errno, inBytesLeft, outBytesLeft, outputBufferSize);
iconv_close(descriptor);
return result;
}
iconv_close(descriptor);
// The result must be truncated to the number of character elements read into the output buffer
result.resize(outputBufferSize - outBytesLeft);
return result;
}
#endif