Repository navigation
Expand file tree
/
Copy pathUTF8.cpp
More file actions
248 lines (221 loc) · 7.36 KB
/
Copy pathUTF8.cpp
File metadata and controls
248 lines (221 loc) · 7.36 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#include <string>
#include <cstdio>
#include <fstream>
#ifndef _WIN32
#define _CRT_INSECURE_DEPRECATE(_Replacement)
#endif
namespace utf8
{
class converter
{
public:
static std::wstring utf8_to_wstring(const std::string& str)
{
std::wstring result;
result.reserve(str.size());
for (size_t i = 0; i < str.size();)
{
unsigned char c = str[i];
if (c < 0x80)
{
result.push_back(c);
i += 1;
}
else if ((c & 0xE0) == 0xC0)
{
wchar_t ch = ((str[i] & 0x1F) << 6) | (str[i + 1] & 0x3F);
result.push_back(ch);
i += 2;
}
else if ((c & 0xF0) == 0xE0)
{
wchar_t ch = ((str[i] & 0x0F) << 12) | ((str[i + 1] & 0x3F) << 6) | (str[i + 2] & 0x3F);
result.push_back(ch);
i += 3;
}
else if ((c & 0xF8) == 0xF0)
{
wchar_t ch = ((str[i] & 0x07) << 18) | ((str[i + 1] & 0x3F) << 12) | ((str[i + 2] & 0x3F) << 6) | (str[i + 3] & 0x3F);
result.push_back(ch);
i += 4;
}
}
return result;
}
static std::string wstring_to_utf8(const std::wstring& wstr)
{
std::string result;
result.reserve(wstr.size() * 2);
for (wchar_t wc : wstr)
{
if (wc < 0x80)
{
result.push_back(static_cast<char>(wc));
}
else if (wc < 0x800)
{
result.push_back(static_cast<char>((wc >> 6) | 0xC0));
result.push_back(static_cast<char>((wc & 0x3F) | 0x80));
}
else
{
result.push_back(static_cast<char>((wc >> 12) | 0xE0));
result.push_back(static_cast<char>(((wc >> 6) & 0x3F) | 0x80));
result.push_back(static_cast<char>((wc & 0x3F) | 0x80));
}
}
return result;
}
};
class fstream : public std::fstream, protected converter
{
public:
fstream() : std::fstream() {}
explicit fstream(const std::string& utf8_path, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out)
{
open(utf8_path, mode);
}
void open(const std::string& utf8_path, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out)
{
#ifdef _WIN32
auto wide_path = utf8_to_wstring(utf8_path);
std::fstream::open(wide_path.c_str(), mode);
#else
std::fstream::open(utf8_path, mode);
#endif
}
};
class ifstream : public std::ifstream, protected converter
{
public:
ifstream() : std::ifstream() {}
explicit ifstream(const std::string& utf8_path, std::ios_base::openmode mode = std::ios_base::in) { open(utf8_path, mode); }
void open(const std::string& utf8_path, std::ios_base::openmode mode = std::ios_base::in)
{
#ifdef _WIN32
auto wide_path = utf8_to_wstring(utf8_path);
std::ifstream::open(wide_path.c_str(), mode);
#else
std::ifstream::open(utf8_path, mode);
#endif
}
};
class ofstream : public std::ofstream, protected converter
{
public:
ofstream() : std::ofstream() {}
explicit ofstream(const std::string& utf8_path, std::ios_base::openmode mode = std::ios_base::out) { open(utf8_path, mode); }
void open(const std::string& utf8_path, std::ios_base::openmode mode = std::ios_base::out)
{
#ifdef _WIN32
auto wide_path = utf8_to_wstring(utf8_path);
std::ofstream::open(wide_path.c_str(), mode);
#else
std::ofstream::open(utf8_path, mode);
#endif
}
};
_CRT_INSECURE_DEPRECATE(utf8::fopen_s)
inline FILE* fopen(const char* path, const char* mode)
{
#ifdef _WIN32
auto wide_path = converter::utf8_to_wstring(path);
auto wide_mode = converter::utf8_to_wstring(mode);
#ifdef _CRT_INSECURE_DEPRECATE
FILE* f = nullptr;
_wfopen_s(&f, wide_path.c_str(), wide_mode.c_str());
return f;
#else
return _wfopen(wide_path.c_str(), wide_mode.c_str());
#endif
#else
return ::fopen(path, mode);
#endif
}
_CRT_INSECURE_DEPRECATE(utf8::freopen_s)
inline FILE* freopen(const char* path, const char* mode, FILE* stream)
{
#ifdef _WIN32
auto wide_path = converter::utf8_to_wstring(path);
auto wide_mode = converter::utf8_to_wstring(mode);
#ifdef _CRT_INSECURE_DEPRECATE
FILE* f = nullptr;
_wfreopen_s(&f, wide_path.c_str(), wide_mode.c_str(), stream);
return f;
#else
return _wfreopen(wide_path.c_str(), wide_mode.c_str(), stream);
#endif
#else
return ::freopen(path, mode, stream);
#endif
}
inline int rename(const char* old_filename, const char* new_filename)
{
#ifdef _WIN32
auto wide_oldpath = converter::utf8_to_wstring(old_filename);
auto wide_newpath = converter::utf8_to_wstring(new_filename);
return _wrename(wide_oldpath.c_str(), wide_newpath.c_str());
#else
return ::rename(old_filename, new_filename);
#endif
}
inline int remove(const char* pathname)
{
#ifdef _WIN32
auto wide_path = converter::utf8_to_wstring(pathname);
return _wremove(wide_path.c_str());
#else
return ::remove(pathname);
#endif
}
//***************************************************
//Windows only functions
#if defined(_WIN32)
FILE* _fsopen(const char* filename, const char* mode, int shflag)
{
auto wide_path = converter::utf8_to_wstring(filename);
auto wide_mode = converter::utf8_to_wstring(mode);
return _wfsopen(wide_path.c_str(), wide_mode.c_str(), shflag);
}
#if defined(_CRT_INSECURE_DEPRECATE)
inline errno_t fopen_s(FILE** stream, const char* path, const char* mode)
{
auto wide_path = converter::utf8_to_wstring(path);
auto wide_mode = converter::utf8_to_wstring(mode);
return _wfopen_s(stream, wide_path.c_str(), wide_mode.c_str());
}
inline errno_t freopen_s(FILE** stream, const char* path, const char* mode, FILE* oldstream)
{
auto wide_path = converter::utf8_to_wstring(path);
auto wide_mode = converter::utf8_to_wstring(mode);
return _wfreopen_s(stream, wide_path.c_str(), wide_mode.c_str(), oldstream);
}
#endif /*_CRT_INSECURE_DEPRECATE*/
#endif /*_WIN32*/
//***************************************************
} // namespace utf8
#include <iostream>
int main()
{
std::string fileName = "\xE3\x83\x86\xE3\x82\xB9\xE3\x83\x88.txt"; //テスト.txt
utf8::ofstream out(fileName);
out << "Hello World" << std::endl;
out.close();
std::string text;
utf8::ifstream in(fileName);
std::getline(in, text);
in >> text;
in.close();
std::cout << text << std::endl;
FILE* f = utf8::fopen(fileName.c_str(), "r");
char line[256];
if (f != nullptr)
{
while (fgets(line, sizeof(line), f))
{
std::cout << line << std::endl;
}
fclose(f);
}
return 0;
}