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
FileExistsRaw(const char * name)61 static bool FileExistsRaw(const char *name) {
62 std::ifstream ifs(name);
63 return ifs.good();
64 }
65
LoadFileRaw(const char * name,bool binary,std::string * buf)66 static bool LoadFileRaw(const char *name, bool binary, std::string *buf) {
67 if (DirExists(name)) return false;
68 std::ifstream ifs(name, binary ? std::ifstream::binary : std::ifstream::in);
69 if (!ifs.is_open()) return false;
70 if (binary) {
71 // The fastest way to read a file into a string.
72 ifs.seekg(0, std::ios::end);
73 auto size = ifs.tellg();
74 (*buf).resize(static_cast<size_t>(size));
75 ifs.seekg(0, std::ios::beg);
76 ifs.read(&(*buf)[0], (*buf).size());
77 } else {
78 // This is slower, but works correctly on all platforms for text files.
79 std::ostringstream oss;
80 oss << ifs.rdbuf();
81 *buf = oss.str();
82 }
83 return !ifs.bad();
84 }
85
86 LoadFileFunction g_load_file_function = LoadFileRaw;
87 FileExistsFunction g_file_exists_function = FileExistsRaw;
88
ToCamelCase(const std::string & input,bool first)89 static std::string ToCamelCase(const std::string &input, bool first) {
90 std::string s;
91 for (size_t i = 0; i < input.length(); i++) {
92 if (!i && first)
93 s += CharToUpper(input[i]);
94 else if (input[i] == '_' && i + 1 < input.length())
95 s += CharToUpper(input[++i]);
96 else
97 s += input[i];
98 }
99 return s;
100 }
101
ToSnakeCase(const std::string & input,bool screaming)102 static std::string ToSnakeCase(const std::string &input, bool screaming) {
103 std::string s;
104 for (size_t i = 0; i < input.length(); i++) {
105 if (i == 0) {
106 s += screaming ? CharToUpper(input[i]) : CharToLower(input[i]);
107 } else if (input[i] == '_') {
108 s += '_';
109 } else if (!islower(input[i])) {
110 // Prevent duplicate underscores for Upper_Snake_Case strings
111 // and UPPERCASE strings.
112 if (islower(input[i - 1])) { s += '_'; }
113 s += screaming ? CharToUpper(input[i]) : CharToLower(input[i]);
114 } else {
115 s += screaming ? CharToUpper(input[i]) : input[i];
116 }
117 }
118 return s;
119 }
120
ToAll(const std::string & input,std::function<char (const char)> transform)121 std::string ToAll(const std::string &input,
122 std::function<char(const char)> transform) {
123 std::string s;
124 for (size_t i = 0; i < input.length(); i++) { s += transform(input[i]); }
125 return s;
126 }
127
CamelToSnake(const std::string & input)128 std::string CamelToSnake(const std::string &input) {
129 std::string s;
130 for (size_t i = 0; i < input.length(); i++) {
131 if (i == 0) {
132 s += CharToLower(input[i]);
133 } else if (input[i] == '_') {
134 s += '_';
135 } else if (!islower(input[i])) {
136 // Prevent duplicate underscores for Upper_Snake_Case strings
137 // and UPPERCASE strings.
138 if (islower(input[i - 1])) { s += '_'; }
139 s += CharToLower(input[i]);
140 } else {
141 s += input[i];
142 }
143 }
144 return s;
145 }
146
DasherToSnake(const std::string & input)147 std::string DasherToSnake(const std::string &input) {
148 std::string s;
149 for (size_t i = 0; i < input.length(); i++) {
150 if (input[i] == '-') {
151 s += "_";
152 } else {
153 s += input[i];
154 }
155 }
156 return s;
157 }
158
ToDasher(const std::string & input)159 std::string ToDasher(const std::string &input) {
160 std::string s;
161 char p = 0;
162 for (size_t i = 0; i < input.length(); i++) {
163 char const &c = input[i];
164 if (c == '_') {
165 if (i > 0 && p != kPathSeparator &&
166 // The following is a special case to ignore digits after a _. This is
167 // because ThisExample3 would be converted to this_example_3 in the
168 // CamelToSnake conversion, and then dasher would do this-example-3,
169 // but it expects this-example3.
170 !(i + 1 < input.length() && isdigit(input[i + 1])))
171 s += "-";
172 } else {
173 s += c;
174 }
175 p = c;
176 }
177 return s;
178 }
179
180
181 // Converts foo_bar_123baz_456 to foo_bar123_baz456
SnakeToSnake2(const std::string & s)182 std::string SnakeToSnake2(const std::string &s) {
183 if (s.length() <= 1) return s;
184 std::string result;
185 result.reserve(s.size());
186 for (size_t i = 0; i < s.length() - 1; i++) {
187 if (s[i] == '_' && isdigit(s[i + 1])) {
188 continue; // Move the `_` until after the digits.
189 }
190
191 result.push_back(s[i]);
192
193 if (isdigit(s[i]) && isalpha(s[i + 1]) && islower(s[i + 1])) {
194 result.push_back('_');
195 }
196 }
197 result.push_back(s.back());
198
199 return result;
200 }
201
202 } // namespace
203
204
LoadFile(const char * name,bool binary,std::string * buf)205 bool LoadFile(const char *name, bool binary, std::string *buf) {
206 FLATBUFFERS_ASSERT(g_load_file_function);
207 return g_load_file_function(name, binary, buf);
208 }
209
FileExists(const char * name)210 bool FileExists(const char *name) {
211 FLATBUFFERS_ASSERT(g_file_exists_function);
212 return g_file_exists_function(name);
213 }
214
DirExists(const char * name)215 bool DirExists(const char *name) {
216 // clang-format off
217
218 #ifdef _WIN32
219 #define flatbuffers_stat _stat
220 #define FLATBUFFERS_S_IFDIR _S_IFDIR
221 #else
222 #define flatbuffers_stat stat
223 #define FLATBUFFERS_S_IFDIR S_IFDIR
224 #endif
225 // clang-format on
226 struct flatbuffers_stat file_info;
227 if (flatbuffers_stat(name, &file_info) != 0) return false;
228 return (file_info.st_mode & FLATBUFFERS_S_IFDIR) != 0;
229 }
230
SetLoadFileFunction(LoadFileFunction load_file_function)231 LoadFileFunction SetLoadFileFunction(LoadFileFunction load_file_function) {
232 LoadFileFunction previous_function = g_load_file_function;
233 g_load_file_function = load_file_function ? load_file_function : LoadFileRaw;
234 return previous_function;
235 }
236
SetFileExistsFunction(FileExistsFunction file_exists_function)237 FileExistsFunction SetFileExistsFunction(
238 FileExistsFunction file_exists_function) {
239 FileExistsFunction previous_function = g_file_exists_function;
240 g_file_exists_function =
241 file_exists_function ? file_exists_function : FileExistsRaw;
242 return previous_function;
243 }
244
SaveFile(const char * name,const char * buf,size_t len,bool binary)245 bool SaveFile(const char *name, const char *buf, size_t len, bool binary) {
246 std::ofstream ofs(name, binary ? std::ofstream::binary : std::ofstream::out);
247 if (!ofs.is_open()) return false;
248 ofs.write(buf, len);
249 return !ofs.bad();
250 }
251
252 // We internally store paths in posix format ('/'). Paths supplied
253 // by the user should go through PosixPath to ensure correct behavior
254 // on Windows when paths are string-compared.
255
256 static const char kPathSeparatorWindows = '\\';
257 static const char *PathSeparatorSet = "\\/"; // Intentionally no ':'
258
StripExtension(const std::string & filepath)259 std::string StripExtension(const std::string &filepath) {
260 size_t i = filepath.find_last_of('.');
261 return i != std::string::npos ? filepath.substr(0, i) : filepath;
262 }
263
GetExtension(const std::string & filepath)264 std::string GetExtension(const std::string &filepath) {
265 size_t i = filepath.find_last_of('.');
266 return i != std::string::npos ? filepath.substr(i + 1) : "";
267 }
268
StripPath(const std::string & filepath)269 std::string StripPath(const std::string &filepath) {
270 size_t i = filepath.find_last_of(PathSeparatorSet);
271 return i != std::string::npos ? filepath.substr(i + 1) : filepath;
272 }
273
StripFileName(const std::string & filepath)274 std::string StripFileName(const std::string &filepath) {
275 size_t i = filepath.find_last_of(PathSeparatorSet);
276 return i != std::string::npos ? filepath.substr(0, i) : "";
277 }
278
StripPrefix(const std::string & filepath,const std::string & prefix_to_remove)279 std::string StripPrefix(const std::string &filepath,
280 const std::string &prefix_to_remove) {
281 if (!strncmp(filepath.c_str(), prefix_to_remove.c_str(),
282 prefix_to_remove.size())) {
283 return filepath.substr(prefix_to_remove.size());
284 }
285 return filepath;
286 }
287
ConCatPathFileName(const std::string & path,const std::string & filename)288 std::string ConCatPathFileName(const std::string &path,
289 const std::string &filename) {
290 std::string filepath = path;
291 if (filepath.length()) {
292 char &filepath_last_character = filepath.back();
293 if (filepath_last_character == kPathSeparatorWindows) {
294 filepath_last_character = kPathSeparator;
295 } else if (filepath_last_character != kPathSeparator) {
296 filepath += kPathSeparator;
297 }
298 }
299 filepath += filename;
300 // Ignore './' at the start of filepath.
301 if (filepath[0] == '.' && filepath[1] == kPathSeparator) {
302 filepath.erase(0, 2);
303 }
304 return filepath;
305 }
306
PosixPath(const char * path)307 std::string PosixPath(const char *path) {
308 std::string p = path;
309 std::replace(p.begin(), p.end(), '\\', '/');
310 return p;
311 }
PosixPath(const std::string & path)312 std::string PosixPath(const std::string &path) {
313 return PosixPath(path.c_str());
314 }
315
EnsureDirExists(const std::string & filepath)316 void EnsureDirExists(const std::string &filepath) {
317 auto parent = StripFileName(filepath);
318 if (parent.length()) EnsureDirExists(parent);
319 // clang-format off
320
321 #ifdef _WIN32
322 (void)_mkdir(filepath.c_str());
323 #else
324 mkdir(filepath.c_str(), S_IRWXU|S_IRGRP|S_IXGRP);
325 #endif
326 // clang-format on
327 }
328
AbsolutePath(const std::string & filepath)329 std::string AbsolutePath(const std::string &filepath) {
330 // clang-format off
331
332 #ifdef FLATBUFFERS_NO_ABSOLUTE_PATH_RESOLUTION
333 return filepath;
334 #else
335 #if defined(_WIN32) || defined(__MINGW32__) || defined(__MINGW64__) || defined(__CYGWIN__)
336 char abs_path[MAX_PATH];
337 return GetFullPathNameA(filepath.c_str(), MAX_PATH, abs_path, nullptr)
338 #else
339 char *abs_path_temp = realpath(filepath.c_str(), nullptr);
340 bool success = abs_path_temp != nullptr;
341 std::string abs_path;
342 if(success) {
343 abs_path = abs_path_temp;
344 free(abs_path_temp);
345 }
346 return success
347 #endif
348 ? abs_path
349 : filepath;
350 #endif // FLATBUFFERS_NO_ABSOLUTE_PATH_RESOLUTION
351 // clang-format on
352 }
353
RelativeToRootPath(const std::string & project,const std::string & filepath)354 std::string RelativeToRootPath(const std::string &project,
355 const std::string &filepath) {
356 std::string absolute_project = PosixPath(AbsolutePath(project));
357 if (absolute_project.back() != '/') absolute_project += "/";
358 std::string absolute_filepath = PosixPath(AbsolutePath(filepath));
359
360 // Find the first character where they disagree.
361 // The previous directory is the lowest common ancestor;
362 const char *a = absolute_project.c_str();
363 const char *b = absolute_filepath.c_str();
364 size_t common_prefix_len = 0;
365 while (*a != '\0' && *b != '\0' && *a == *b) {
366 if (*a == '/') common_prefix_len = a - absolute_project.c_str();
367 a++;
368 b++;
369 }
370 // the number of ../ to prepend to b depends on the number of remaining
371 // directories in A.
372 const char *suffix = absolute_project.c_str() + common_prefix_len;
373 size_t num_up = 0;
374 while (*suffix != '\0')
375 if (*suffix++ == '/') num_up++;
376 num_up--; // last one is known to be '/'.
377 std::string result = "//";
378 for (size_t i = 0; i < num_up; i++) result += "../";
379 result += absolute_filepath.substr(common_prefix_len + 1);
380
381 return result;
382 }
383
384 // Locale-independent code.
385 #if defined(FLATBUFFERS_LOCALE_INDEPENDENT) && \
386 (FLATBUFFERS_LOCALE_INDEPENDENT > 0)
387
388 // clang-format off
389 // Allocate locale instance at startup of application.
390 ClassicLocale ClassicLocale::instance_;
391
392 #ifdef _MSC_VER
ClassicLocale()393 ClassicLocale::ClassicLocale()
394 : locale_(_create_locale(LC_ALL, "C")) {}
~ClassicLocale()395 ClassicLocale::~ClassicLocale() { _free_locale(locale_); }
396 #else
ClassicLocale()397 ClassicLocale::ClassicLocale()
398 : locale_(newlocale(LC_ALL, "C", nullptr)) {}
~ClassicLocale()399 ClassicLocale::~ClassicLocale() { freelocale(locale_); }
400 #endif
401 // clang-format on
402
403 #endif // !FLATBUFFERS_LOCALE_INDEPENDENT
404
RemoveStringQuotes(const std::string & s)405 std::string RemoveStringQuotes(const std::string &s) {
406 auto ch = *s.c_str();
407 return ((s.size() >= 2) && (ch == '\"' || ch == '\'') && (ch == s.back()))
408 ? s.substr(1, s.length() - 2)
409 : s;
410 }
411
SetGlobalTestLocale(const char * locale_name,std::string * _value)412 bool SetGlobalTestLocale(const char *locale_name, std::string *_value) {
413 const auto the_locale = setlocale(LC_ALL, locale_name);
414 if (!the_locale) return false;
415 if (_value) *_value = std::string(the_locale);
416 return true;
417 }
418
ReadEnvironmentVariable(const char * var_name,std::string * _value)419 bool ReadEnvironmentVariable(const char *var_name, std::string *_value) {
420 #ifdef _MSC_VER
421 __pragma(warning(disable : 4996)); // _CRT_SECURE_NO_WARNINGS
422 #endif
423 auto env_str = std::getenv(var_name);
424 if (!env_str) return false;
425 if (_value) *_value = std::string(env_str);
426 return true;
427 }
428
SetupDefaultCRTReportMode()429 void SetupDefaultCRTReportMode() {
430 // clang-format off
431
432 #ifdef _MSC_VER
433 // By default, send all reports to STDOUT to prevent CI hangs.
434 // Enable assert report box [Abort|Retry|Ignore] if a debugger is present.
435 const int dbg_mode = (_CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG) |
436 (IsDebuggerPresent() ? _CRTDBG_MODE_WNDW : 0);
437 (void)dbg_mode; // release mode fix
438 // CrtDebug reports to _CRT_WARN channel.
439 _CrtSetReportMode(_CRT_WARN, dbg_mode);
440 _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDOUT);
441 // The assert from <assert.h> reports to _CRT_ERROR channel
442 _CrtSetReportMode(_CRT_ERROR, dbg_mode);
443 _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDOUT);
444 // Internal CRT assert channel?
445 _CrtSetReportMode(_CRT_ASSERT, dbg_mode);
446 _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDOUT);
447 #endif
448
449 // clang-format on
450 }
451
ConvertCase(const std::string & input,Case output_case,Case input_case)452 std::string ConvertCase(const std::string &input, Case output_case,
453 Case input_case) {
454 if (output_case == Case::kKeep) return input;
455 // The output cases expect snake_case inputs, so if we don't have that input
456 // format, try to convert to snake_case.
457 switch (input_case) {
458 case Case::kLowerCamel:
459 case Case::kUpperCamel:
460 return ConvertCase(CamelToSnake(input), output_case);
461 case Case::kDasher: return ConvertCase(DasherToSnake(input), output_case);
462 case Case::kKeep: printf("WARNING: Converting from kKeep case.\n"); break;
463 default:
464 case Case::kSnake:
465 case Case::kScreamingSnake:
466 case Case::kAllLower:
467 case Case::kAllUpper: break;
468 }
469
470 switch (output_case) {
471 case Case::kUpperCamel: return ToCamelCase(input, true);
472 case Case::kLowerCamel: return ToCamelCase(input, false);
473 case Case::kSnake: return input;
474 case Case::kScreamingSnake: return ToSnakeCase(input, true);
475 case Case::kAllUpper: return ToAll(input, CharToUpper);
476 case Case::kAllLower: return ToAll(input, CharToLower);
477 case Case::kDasher: return ToDasher(input);
478 case Case::kSnake2: return SnakeToSnake2(input);
479 default:
480 case Case::kUnknown: return input;
481 }
482 }
483
484 } // namespace flatbuffers
485