/proc/self/cwd/external/flatbuffers/src/util.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2016 Google Inc. All rights reserved. |
3 | | * |
4 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | * you may not use this file except in compliance with the License. |
6 | | * You may obtain a copy of the License at |
7 | | * |
8 | | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | * |
10 | | * Unless required by applicable law or agreed to in writing, software |
11 | | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | * See the License for the specific language governing permissions and |
14 | | * limitations under the License. |
15 | | */ |
16 | | |
17 | | // clang-format off |
18 | | // Dont't remove `format off`, it prevent reordering of win-includes. |
19 | | |
20 | | #include <cstring> |
21 | | #if defined(__MINGW32__) || defined(__MINGW64__) || defined(__CYGWIN__) || \ |
22 | | defined(__QNXNTO__) |
23 | | # define _POSIX_C_SOURCE 200809L |
24 | | # define _XOPEN_SOURCE 700L |
25 | | #endif |
26 | | |
27 | | #if defined(_WIN32) || defined(__MINGW32__) || defined(__MINGW64__) || defined(__CYGWIN__) |
28 | | # ifndef WIN32_LEAN_AND_MEAN |
29 | | # define WIN32_LEAN_AND_MEAN |
30 | | # endif |
31 | | # ifndef NOMINMAX |
32 | | # define NOMINMAX |
33 | | # endif |
34 | | # ifdef _MSC_VER |
35 | | # include <crtdbg.h> |
36 | | # endif |
37 | | # include <windows.h> // Must be included before <direct.h> |
38 | | # ifndef __CYGWIN__ |
39 | | # include <direct.h> |
40 | | # endif |
41 | | # include <winbase.h> |
42 | | # undef interface // This is also important because of reasons |
43 | | #endif |
44 | | // clang-format on |
45 | | |
46 | | #include "flatbuffers/util.h" |
47 | | |
48 | | #include <sys/stat.h> |
49 | | |
50 | | #include <clocale> |
51 | | #include <cstdlib> |
52 | | #include <fstream> |
53 | | #include <functional> |
54 | | |
55 | | #include "flatbuffers/base.h" |
56 | | |
57 | | namespace flatbuffers { |
58 | | |
59 | | namespace { |
60 | | |
61 | 0 | static bool FileExistsRaw(const char *name) { |
62 | 0 | std::ifstream ifs(name); |
63 | 0 | return ifs.good(); |
64 | 0 | } |
65 | | |
66 | 0 | static bool LoadFileRaw(const char *name, bool binary, std::string *buf) { |
67 | 0 | if (DirExists(name)) return false; |
68 | 0 | std::ifstream ifs(name, binary ? std::ifstream::binary : std::ifstream::in); |
69 | 0 | if (!ifs.is_open()) return false; |
70 | 0 | if (binary) { |
71 | | // The fastest way to read a file into a string. |
72 | 0 | ifs.seekg(0, std::ios::end); |
73 | 0 | auto size = ifs.tellg(); |
74 | 0 | (*buf).resize(static_cast<size_t>(size)); |
75 | 0 | ifs.seekg(0, std::ios::beg); |
76 | 0 | ifs.read(&(*buf)[0], (*buf).size()); |
77 | 0 | } else { |
78 | | // This is slower, but works correctly on all platforms for text files. |
79 | 0 | std::ostringstream oss; |
80 | 0 | oss << ifs.rdbuf(); |
81 | 0 | *buf = oss.str(); |
82 | 0 | } |
83 | 0 | return !ifs.bad(); |
84 | 0 | } |
85 | | |
86 | | LoadFileFunction g_load_file_function = LoadFileRaw; |
87 | | FileExistsFunction g_file_exists_function = FileExistsRaw; |
88 | | |
89 | 0 | static std::string ToCamelCase(const std::string &input, bool is_upper) { |
90 | 0 | std::string s; |
91 | 0 | for (size_t i = 0; i < input.length(); i++) { |
92 | 0 | if (!i && input[i] == '_') { |
93 | 0 | s += input[i]; |
94 | | // we ignore leading underscore but make following |
95 | | // alphabet char upper. |
96 | 0 | if (i + 1 < input.length() && is_alpha(input[i + 1])) |
97 | 0 | s += CharToUpper(input[++i]); |
98 | 0 | } else if (!i) |
99 | 0 | s += is_upper ? CharToUpper(input[i]) : CharToLower(input[i]); |
100 | 0 | else if (input[i] == '_' && i + 1 < input.length()) |
101 | 0 | s += CharToUpper(input[++i]); |
102 | 0 | else |
103 | 0 | s += input[i]; |
104 | 0 | } |
105 | 0 | return s; |
106 | 0 | } |
107 | | |
108 | 0 | static std::string ToSnakeCase(const std::string &input, bool screaming) { |
109 | 0 | std::string s; |
110 | 0 | for (size_t i = 0; i < input.length(); i++) { |
111 | 0 | if (i == 0) { |
112 | 0 | s += screaming ? CharToUpper(input[i]) : CharToLower(input[i]); |
113 | 0 | } else if (input[i] == '_') { |
114 | 0 | s += '_'; |
115 | 0 | } else if (!islower(input[i])) { |
116 | | // Prevent duplicate underscores for Upper_Snake_Case strings |
117 | | // and UPPERCASE strings. |
118 | 0 | if (islower(input[i - 1]) || |
119 | 0 | (isdigit(input[i - 1]) && !isdigit(input[i]))) { |
120 | 0 | s += '_'; |
121 | 0 | } |
122 | 0 | s += screaming ? CharToUpper(input[i]) : CharToLower(input[i]); |
123 | 0 | } else { |
124 | 0 | s += screaming ? CharToUpper(input[i]) : input[i]; |
125 | 0 | } |
126 | 0 | } |
127 | 0 | return s; |
128 | 0 | } |
129 | | |
130 | | std::string ToAll(const std::string &input, |
131 | 0 | std::function<char(const char)> transform) { |
132 | 0 | std::string s; |
133 | 0 | for (size_t i = 0; i < input.length(); i++) { s += transform(input[i]); } |
134 | 0 | return s; |
135 | 0 | } |
136 | | |
137 | 0 | std::string CamelToSnake(const std::string &input) { |
138 | 0 | std::string s; |
139 | 0 | for (size_t i = 0; i < input.length(); i++) { |
140 | 0 | if (i == 0) { |
141 | 0 | s += CharToLower(input[i]); |
142 | 0 | } else if (input[i] == '_') { |
143 | 0 | s += '_'; |
144 | 0 | } else if (!islower(input[i])) { |
145 | | // Prevent duplicate underscores for Upper_Snake_Case strings |
146 | | // and UPPERCASE strings. |
147 | 0 | if (islower(input[i - 1]) || |
148 | 0 | (isdigit(input[i - 1]) && !isdigit(input[i]))) { |
149 | 0 | s += '_'; |
150 | 0 | } |
151 | 0 | s += CharToLower(input[i]); |
152 | 0 | } else { |
153 | 0 | s += input[i]; |
154 | 0 | } |
155 | 0 | } |
156 | 0 | return s; |
157 | 0 | } |
158 | | |
159 | 0 | std::string DasherToSnake(const std::string &input) { |
160 | 0 | std::string s; |
161 | 0 | for (size_t i = 0; i < input.length(); i++) { |
162 | 0 | if (input[i] == '-') { |
163 | 0 | s += "_"; |
164 | 0 | } else { |
165 | 0 | s += input[i]; |
166 | 0 | } |
167 | 0 | } |
168 | 0 | return s; |
169 | 0 | } |
170 | | |
171 | 0 | std::string ToDasher(const std::string &input) { |
172 | 0 | std::string s; |
173 | 0 | char p = 0; |
174 | 0 | for (size_t i = 0; i < input.length(); i++) { |
175 | 0 | char const &c = input[i]; |
176 | 0 | if (c == '_') { |
177 | 0 | if (i > 0 && p != kPathSeparator && |
178 | | // The following is a special case to ignore digits after a _. This is |
179 | | // because ThisExample3 would be converted to this_example_3 in the |
180 | | // CamelToSnake conversion, and then dasher would do this-example-3, |
181 | | // but it expects this-example3. |
182 | 0 | !(i + 1 < input.length() && isdigit(input[i + 1]))) |
183 | 0 | s += "-"; |
184 | 0 | } else { |
185 | 0 | s += c; |
186 | 0 | } |
187 | 0 | p = c; |
188 | 0 | } |
189 | 0 | return s; |
190 | 0 | } |
191 | | |
192 | | // Converts foo_bar_123baz_456 to foo_bar123_baz456 |
193 | 0 | std::string SnakeToSnake2(const std::string &s) { |
194 | 0 | if (s.length() <= 1) return s; |
195 | 0 | std::string result; |
196 | 0 | result.reserve(s.size()); |
197 | 0 | for (size_t i = 0; i < s.length() - 1; i++) { |
198 | 0 | if (s[i] == '_' && isdigit(s[i + 1])) { |
199 | 0 | continue; // Move the `_` until after the digits. |
200 | 0 | } |
201 | | |
202 | 0 | result.push_back(s[i]); |
203 | |
|
204 | 0 | if (isdigit(s[i]) && isalpha(s[i + 1]) && islower(s[i + 1])) { |
205 | 0 | result.push_back('_'); |
206 | 0 | } |
207 | 0 | } |
208 | 0 | result.push_back(s.back()); |
209 | |
|
210 | 0 | return result; |
211 | 0 | } |
212 | | |
213 | | } // namespace |
214 | | |
215 | 0 | bool LoadFile(const char *name, bool binary, std::string *buf) { |
216 | 0 | FLATBUFFERS_ASSERT(g_load_file_function); |
217 | 0 | return g_load_file_function(name, binary, buf); |
218 | 0 | } |
219 | | |
220 | 0 | bool FileExists(const char *name) { |
221 | 0 | FLATBUFFERS_ASSERT(g_file_exists_function); |
222 | 0 | return g_file_exists_function(name); |
223 | 0 | } |
224 | | |
225 | 0 | bool DirExists(const char *name) { |
226 | | // clang-format off |
227 | |
|
228 | | #ifdef _WIN32 |
229 | | #define flatbuffers_stat _stat |
230 | | #define FLATBUFFERS_S_IFDIR _S_IFDIR |
231 | | #else |
232 | 0 | #define flatbuffers_stat stat |
233 | 0 | #define FLATBUFFERS_S_IFDIR S_IFDIR |
234 | 0 | #endif |
235 | | // clang-format on |
236 | 0 | struct flatbuffers_stat file_info; |
237 | 0 | if (flatbuffers_stat(name, &file_info) != 0) return false; |
238 | 0 | return (file_info.st_mode & FLATBUFFERS_S_IFDIR) != 0; |
239 | 0 | } |
240 | | |
241 | 0 | LoadFileFunction SetLoadFileFunction(LoadFileFunction load_file_function) { |
242 | 0 | LoadFileFunction previous_function = g_load_file_function; |
243 | 0 | g_load_file_function = load_file_function ? load_file_function : LoadFileRaw; |
244 | 0 | return previous_function; |
245 | 0 | } |
246 | | |
247 | | FileExistsFunction SetFileExistsFunction( |
248 | 0 | FileExistsFunction file_exists_function) { |
249 | 0 | FileExistsFunction previous_function = g_file_exists_function; |
250 | 0 | g_file_exists_function = |
251 | 0 | file_exists_function ? file_exists_function : FileExistsRaw; |
252 | 0 | return previous_function; |
253 | 0 | } |
254 | | |
255 | 0 | bool SaveFile(const char *name, const char *buf, size_t len, bool binary) { |
256 | 0 | std::ofstream ofs(name, binary ? std::ofstream::binary : std::ofstream::out); |
257 | 0 | if (!ofs.is_open()) return false; |
258 | 0 | ofs.write(buf, len); |
259 | 0 | return !ofs.bad(); |
260 | 0 | } |
261 | | |
262 | | // We internally store paths in posix format ('/'). Paths supplied |
263 | | // by the user should go through PosixPath to ensure correct behavior |
264 | | // on Windows when paths are string-compared. |
265 | | |
266 | | static const char kPathSeparatorWindows = '\\'; |
267 | | static const char *PathSeparatorSet = "\\/"; // Intentionally no ':' |
268 | | |
269 | 0 | std::string StripExtension(const std::string &filepath) { |
270 | 0 | size_t i = filepath.find_last_of('.'); |
271 | 0 | return i != std::string::npos ? filepath.substr(0, i) : filepath; |
272 | 0 | } |
273 | | |
274 | 0 | std::string GetExtension(const std::string &filepath) { |
275 | 0 | size_t i = filepath.find_last_of('.'); |
276 | 0 | return i != std::string::npos ? filepath.substr(i + 1) : ""; |
277 | 0 | } |
278 | | |
279 | 0 | std::string StripPath(const std::string &filepath) { |
280 | 0 | size_t i = filepath.find_last_of(PathSeparatorSet); |
281 | 0 | return i != std::string::npos ? filepath.substr(i + 1) : filepath; |
282 | 0 | } |
283 | | |
284 | 0 | std::string StripFileName(const std::string &filepath) { |
285 | 0 | size_t i = filepath.find_last_of(PathSeparatorSet); |
286 | 0 | return i != std::string::npos ? filepath.substr(0, i) : ""; |
287 | 0 | } |
288 | | |
289 | | std::string StripPrefix(const std::string &filepath, |
290 | 0 | const std::string &prefix_to_remove) { |
291 | 0 | if (!strncmp(filepath.c_str(), prefix_to_remove.c_str(), |
292 | 0 | prefix_to_remove.size())) { |
293 | 0 | return filepath.substr(prefix_to_remove.size()); |
294 | 0 | } |
295 | 0 | return filepath; |
296 | 0 | } |
297 | | |
298 | | std::string ConCatPathFileName(const std::string &path, |
299 | 0 | const std::string &filename) { |
300 | 0 | std::string filepath = path; |
301 | 0 | if (filepath.length()) { |
302 | 0 | char &filepath_last_character = filepath.back(); |
303 | 0 | if (filepath_last_character == kPathSeparatorWindows) { |
304 | 0 | filepath_last_character = kPathSeparator; |
305 | 0 | } else if (filepath_last_character != kPathSeparator) { |
306 | 0 | filepath += kPathSeparator; |
307 | 0 | } |
308 | 0 | } |
309 | 0 | filepath += filename; |
310 | | // Ignore './' at the start of filepath. |
311 | 0 | if (filepath[0] == '.' && filepath[1] == kPathSeparator) { |
312 | 0 | filepath.erase(0, 2); |
313 | 0 | } |
314 | 0 | return filepath; |
315 | 0 | } |
316 | | |
317 | 0 | std::string PosixPath(const char *path) { |
318 | 0 | std::string p = path; |
319 | 0 | std::replace(p.begin(), p.end(), '\\', '/'); |
320 | 0 | return p; |
321 | 0 | } |
322 | 0 | std::string PosixPath(const std::string &path) { |
323 | 0 | return PosixPath(path.c_str()); |
324 | 0 | } |
325 | | |
326 | 0 | void EnsureDirExists(const std::string &filepath) { |
327 | 0 | auto parent = StripFileName(filepath); |
328 | 0 | if (parent.length()) EnsureDirExists(parent); |
329 | | // clang-format off |
330 | |
|
331 | | #ifdef _WIN32 |
332 | | (void)_mkdir(filepath.c_str()); |
333 | | #else |
334 | 0 | mkdir(filepath.c_str(), S_IRWXU|S_IRGRP|S_IXGRP); |
335 | 0 | #endif |
336 | | // clang-format on |
337 | 0 | } |
338 | | |
339 | 0 | std::string FilePath(const std::string& project, const std::string& filePath, bool absolute) { |
340 | 0 | return (absolute) ? AbsolutePath(filePath) : RelativeToRootPath(project, filePath); |
341 | 0 | } |
342 | | |
343 | 0 | std::string AbsolutePath(const std::string &filepath) { |
344 | | // clang-format off |
345 | |
|
346 | | #ifdef FLATBUFFERS_NO_ABSOLUTE_PATH_RESOLUTION |
347 | | return filepath; |
348 | | #else |
349 | | #if defined(_WIN32) || defined(__MINGW32__) || defined(__MINGW64__) || defined(__CYGWIN__) |
350 | | char abs_path[MAX_PATH]; |
351 | | return GetFullPathNameA(filepath.c_str(), MAX_PATH, abs_path, nullptr) |
352 | | #else |
353 | 0 | char *abs_path_temp = realpath(filepath.c_str(), nullptr); |
354 | 0 | bool success = abs_path_temp != nullptr; |
355 | 0 | std::string abs_path; |
356 | 0 | if(success) { |
357 | 0 | abs_path = abs_path_temp; |
358 | 0 | free(abs_path_temp); |
359 | 0 | } |
360 | 0 | return success |
361 | 0 | #endif |
362 | 0 | ? abs_path |
363 | 0 | : filepath; |
364 | 0 | #endif // FLATBUFFERS_NO_ABSOLUTE_PATH_RESOLUTION |
365 | | // clang-format on |
366 | 0 | } |
367 | | |
368 | | std::string RelativeToRootPath(const std::string &project, |
369 | 0 | const std::string &filepath) { |
370 | 0 | std::string absolute_project = PosixPath(AbsolutePath(project)); |
371 | 0 | if (absolute_project.back() != '/') absolute_project += "/"; |
372 | 0 | std::string absolute_filepath = PosixPath(AbsolutePath(filepath)); |
373 | | |
374 | | // Find the first character where they disagree. |
375 | | // The previous directory is the lowest common ancestor; |
376 | 0 | const char *a = absolute_project.c_str(); |
377 | 0 | const char *b = absolute_filepath.c_str(); |
378 | 0 | size_t common_prefix_len = 0; |
379 | 0 | while (*a != '\0' && *b != '\0' && *a == *b) { |
380 | 0 | if (*a == '/') common_prefix_len = a - absolute_project.c_str(); |
381 | 0 | a++; |
382 | 0 | b++; |
383 | 0 | } |
384 | | // the number of ../ to prepend to b depends on the number of remaining |
385 | | // directories in A. |
386 | 0 | const char *suffix = absolute_project.c_str() + common_prefix_len; |
387 | 0 | size_t num_up = 0; |
388 | 0 | while (*suffix != '\0') |
389 | 0 | if (*suffix++ == '/') num_up++; |
390 | 0 | num_up--; // last one is known to be '/'. |
391 | 0 | std::string result = "//"; |
392 | 0 | for (size_t i = 0; i < num_up; i++) result += "../"; |
393 | 0 | result += absolute_filepath.substr(common_prefix_len + 1); |
394 | |
|
395 | 0 | return result; |
396 | 0 | } |
397 | | |
398 | | // Locale-independent code. |
399 | | #if defined(FLATBUFFERS_LOCALE_INDEPENDENT) && \ |
400 | | (FLATBUFFERS_LOCALE_INDEPENDENT > 0) |
401 | | |
402 | | // clang-format off |
403 | | // Allocate locale instance at startup of application. |
404 | | ClassicLocale ClassicLocale::instance_; |
405 | | |
406 | | #ifdef _MSC_VER |
407 | | ClassicLocale::ClassicLocale() |
408 | | : locale_(_create_locale(LC_ALL, "C")) {} |
409 | | ClassicLocale::~ClassicLocale() { _free_locale(locale_); } |
410 | | #else |
411 | | ClassicLocale::ClassicLocale() |
412 | 4 | : locale_(newlocale(LC_ALL, "C", nullptr)) {} |
413 | 0 | ClassicLocale::~ClassicLocale() { freelocale(locale_); } |
414 | | #endif |
415 | | // clang-format on |
416 | | |
417 | | #endif // !FLATBUFFERS_LOCALE_INDEPENDENT |
418 | | |
419 | 0 | std::string RemoveStringQuotes(const std::string &s) { |
420 | 0 | auto ch = *s.c_str(); |
421 | 0 | return ((s.size() >= 2) && (ch == '\"' || ch == '\'') && (ch == s.back())) |
422 | 0 | ? s.substr(1, s.length() - 2) |
423 | 0 | : s; |
424 | 0 | } |
425 | | |
426 | 0 | bool SetGlobalTestLocale(const char *locale_name, std::string *_value) { |
427 | 0 | const auto the_locale = setlocale(LC_ALL, locale_name); |
428 | 0 | if (!the_locale) return false; |
429 | 0 | if (_value) *_value = std::string(the_locale); |
430 | 0 | return true; |
431 | 0 | } |
432 | | |
433 | 0 | bool ReadEnvironmentVariable(const char *var_name, std::string *_value) { |
434 | | #ifdef _MSC_VER |
435 | | __pragma(warning(disable : 4996)); // _CRT_SECURE_NO_WARNINGS |
436 | | #endif |
437 | 0 | auto env_str = std::getenv(var_name); |
438 | 0 | if (!env_str) return false; |
439 | 0 | if (_value) *_value = std::string(env_str); |
440 | 0 | return true; |
441 | 0 | } |
442 | | |
443 | | std::string ConvertCase(const std::string &input, Case output_case, |
444 | 0 | Case input_case) { |
445 | 0 | if (output_case == Case::kKeep) return input; |
446 | | // The output cases expect snake_case inputs, so if we don't have that input |
447 | | // format, try to convert to snake_case. |
448 | 0 | switch (input_case) { |
449 | 0 | case Case::kLowerCamel: |
450 | 0 | case Case::kUpperCamel: |
451 | 0 | return ConvertCase(CamelToSnake(input), output_case); |
452 | 0 | case Case::kDasher: return ConvertCase(DasherToSnake(input), output_case); |
453 | 0 | case Case::kKeep: printf("WARNING: Converting from kKeep case.\n"); break; |
454 | 0 | default: |
455 | 0 | case Case::kSnake: |
456 | 0 | case Case::kScreamingSnake: |
457 | 0 | case Case::kAllLower: |
458 | 0 | case Case::kAllUpper: break; |
459 | 0 | } |
460 | | |
461 | 0 | switch (output_case) { |
462 | 0 | case Case::kUpperCamel: return ToCamelCase(input, true); |
463 | 0 | case Case::kLowerCamel: return ToCamelCase(input, false); |
464 | 0 | case Case::kSnake: return input; |
465 | 0 | case Case::kScreamingSnake: return ToSnakeCase(input, true); |
466 | 0 | case Case::kAllUpper: return ToAll(input, CharToUpper); |
467 | 0 | case Case::kAllLower: return ToAll(input, CharToLower); |
468 | 0 | case Case::kDasher: return ToDasher(input); |
469 | 0 | case Case::kSnake2: return SnakeToSnake2(input); |
470 | 0 | default: |
471 | 0 | case Case::kUnknown: return input; |
472 | 0 | } |
473 | 0 | } |
474 | | |
475 | | } // namespace flatbuffers |