-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilePathTest.cpp
More file actions
282 lines (213 loc) · 8.27 KB
/
FilePathTest.cpp
File metadata and controls
282 lines (213 loc) · 8.27 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#include <algorithm>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include "Log.h"
void FilePathTest()
{
{
std::cout << "\nFILEPATH TESTING" << std::endl;
// input testfiles
#ifdef BUILD_WIN
std::filesystem::path testfileList("test-files\\files.dat");
#else
std::filesystem::path testfileList("test-files/files-linux.dat");
#endif
std::ifstream inputFilestream(testfileList, std::ios_base::binary | std::ios_base::ate);
// basic string paths
std::vector<std::filesystem::path> testfiles;
// u8string paths
std::vector<std::filesystem::path> u8Testfiles;
inputFilestream.seekg(0);
/*
reading to char buffer should always be ok, as there will be no conversion
filepaths can contain UTF-8 chars encoded with Unicode codepoints
*/
char readBuf[4096];
std::vector<char> buffer;
while (inputFilestream)
{
inputFilestream.read(readBuf, 4096);
buffer.insert(buffer.end(), readBuf, &readBuf[inputFilestream.gcount()]);
}
inputFilestream.close();
/*
file contains paths seperated by zero-chars
filepaths are also present as testfiles
*/
std::vector<char>::iterator first = buffer.begin();
std::vector<char>::iterator last = buffer.begin();
last = std::find(last, buffer.end(), '\0');
while (last != buffer.end())
{
// trying standard string from char array
std::string line(first, last);
// also creating u8String from char array
std::u8string u8Line(first, last);
// standard and u8string path
std::filesystem::path testfile(line);
std::filesystem::path u8Testfile(u8Line);
testfiles.push_back(testfile);
u8Testfiles.push_back(u8Testfile);
last++;
first = last;
last = std::find(last, buffer.end(), '\0');
}
/*
first:
create temp string for filename (as filesystem::path does)
so printing is save
then:
standard fprintf printing with c_str
*/
LOG_OUT("Standard string path testing: ");
for (auto& testfile : testfiles)
{
std::string filepath(testfile.string());
/*
LINUX:
works!
surprisingly on linux all files are found
WIN:
printing works (atleast for file print)
BUT files are not found!
-> method not advised to use (atleast for files containing Unicode chars)
*/
if (std::filesystem::exists(testfile) && std::filesystem::is_regular_file(testfile))
{
LOG_OUT("File: <%s> exists!", filepath.c_str());
} else
{
LOG_OUT("File: <%s> doesn't exist!", filepath.c_str());
}
}
/*
first:
create temp string for printing (as above)
u8string needs to be selected as method, but isn't syntactically needed
(BUT can crash the program!)
then:
printing with fprintf, BUT casted to char*
*/
std::cout << std::endl;
LOG_OUT("U8String path testing");
for (auto& u8Testfile : u8Testfiles)
{
std::u8string filepath(u8Testfile.u8string());
/*
LINUX:
works
WIN:
works (file locating and printing)
*/
if (std::filesystem::exists(u8Testfile) && std::filesystem::is_regular_file(u8Testfile))
{
LOG_OUT("File: <%s> exists!", reinterpret_cast<const char*>(filepath.c_str()));
} else
{
LOG_OUT("File: <%s> doen't exist!", reinterpret_cast<const char*>(filepath.c_str()));
}
}
}
{
std::cout << "\nLITERAL STRING FILEPATH TESTING" << std::endl;
/*
filesystem path testing with literals
construction is done with simple string literals
no conversion for utf-8 string!
*/
std::vector<std::filesystem::path> testfiles;
#ifdef BUILD_WIN
testfiles.emplace_back("test-files\\test.bin");
testfiles.emplace_back("test-files\\test-α-ω.bin");
testfiles.emplace_back("test-files\\test-ッ.bin");
#else
testfiles.emplace_back("test-files/test.bin");
testfiles.emplace_back("test-files/test-α-ω.bin");
testfiles.emplace_back("test-files/test-ッ.bin");
#endif
for (auto& file : testfiles)
{
std::string filename(file.string());
/*
here, searching for the file with a simple string literal is done
LINUX:
works
WIN:
doesn't work
filepaths seem to be constructed with a widestring L"" literal
-> explicit conversion to u8string
*/
if (std::filesystem::exists(file) && std::filesystem::is_regular_file(file))
{
LOG_OUT("String Filename - File: <%s> exists!", filename.c_str());
} else
{
LOG_OUT("String Filename - File: <%s> doesn't exist!", filename.c_str());
}
}
for (auto& file : testfiles)
{
std::u8string filename(file.u8string());
/*
here, searching for the file with a simple string literal is done
filename will be taken as u8string!
LINUX:
works
WIN:
doesn't work either!
string encoding is also (probably) done again (double-conversion)
-> as fileoutput with UTF-8 encoding, no chars will be displayed correctly
*/
if (std::filesystem::exists(file) && std::filesystem::is_regular_file(file))
{
LOG_OUT("U8String Filename - File: <%s> exists!", filename.c_str());
}
else
{
LOG_OUT("U8String Filename - File: <%s> doesn't exist!", filename.c_str());
}
}
}
{
std::cout << "\nLITERAL U8STRING FILEPATH TESTING" << std::endl;
/*
filesystem path testing with u8 literals
construction is done with char8_t* string literals
*/
std::vector<std::filesystem::path> testfiles;
#ifdef BUILD_WIN
testfiles.emplace_back(u8"test-files\\test.bin");
testfiles.emplace_back(u8"test-files\\test-α-ω.bin");
testfiles.emplace_back(u8"test-files\\test-ッ.bin");
#else
testfiles.emplace_back(u8"test-files/test.bin");
testfiles.emplace_back(u8"test-files/test-α-ω.bin");
testfiles.emplace_back(u8"test-files/test-ッ.bin");
#endif
for (auto& file : testfiles)
{
std::u8string filename(file.u8string());
/*
here, searching for the file with a u8 string literal is done
filename will be taken as u8string
LINUX:
works
WIN:
works!
seems that on windows filepaths need to be explicitly constructed as u8string
to also respect the Unicode encoding in filepaths
*/
if (std::filesystem::exists(file) && std::filesystem::is_regular_file(file))
{
LOG_OUT("U8String Filename - File: <%s> exists!", filename.c_str());
}
else
{
LOG_OUT("U8String Filename - File: <%s> doesn't exist!", filename.c_str());
}
}
}
}