• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 is_upper)89 static std::string ToCamelCase(const std::string &input, bool is_upper) {
90   std::string s;
91   for (size_t i = 0; i < input.length(); i++) {
92     if (!i && input[i] == '_') {
93       s += input[i];
94       // we ignore leading underscore but make following
95       // alphabet char upper.
96       if (i + 1 < input.length() && is_alpha(input[i + 1]))
97         s += CharToUpper(input[++i]);
98     } else if (!i)
99       s += is_upper ? CharToUpper(input[i]) : CharToLower(input[i]);
100     else if (input[i] == '_' && i + 1 < input.length())
101       s += CharToUpper(input[++i]);
102     else
103       s += input[i];
104   }
105   return s;
106 }
107 
ToSnakeCase(const std::string & input,bool screaming)108 static std::string ToSnakeCase(const std::string &input, bool screaming) {
109   std::string s;
110   for (size_t i = 0; i < input.length(); i++) {
111     if (i == 0) {
112       s += screaming ? CharToUpper(input[i]) : CharToLower(input[i]);
113     } else if (input[i] == '_') {
114       s += '_';
115     } else if (!islower(input[i])) {
116       // Prevent duplicate underscores for Upper_Snake_Case strings
117       // and UPPERCASE strings.
118       if (islower(input[i - 1]) ||
119           (isdigit(input[i - 1]) && !isdigit(input[i]))) {
120         s += '_';
121       }
122       s += screaming ? CharToUpper(input[i]) : CharToLower(input[i]);
123     } else {
124       s += screaming ? CharToUpper(input[i]) : input[i];
125     }
126   }
127   return s;
128 }
129 
ToAll(const std::string & input,std::function<char (const char)> transform)130 std::string ToAll(const std::string &input,
131                   std::function<char(const char)> transform) {
132   std::string s;
133   for (size_t i = 0; i < input.length(); i++) { s += transform(input[i]); }
134   return s;
135 }
136 
CamelToSnake(const std::string & input)137 std::string CamelToSnake(const std::string &input) {
138   std::string s;
139   for (size_t i = 0; i < input.length(); i++) {
140     if (i == 0) {
141       s += CharToLower(input[i]);
142     } else if (input[i] == '_') {
143       s += '_';
144     } else if (!islower(input[i])) {
145       // Prevent duplicate underscores for Upper_Snake_Case strings
146       // and UPPERCASE strings.
147       if (islower(input[i - 1]) ||
148           (isdigit(input[i - 1]) && !isdigit(input[i]))) {
149         s += '_';
150       }
151       s += CharToLower(input[i]);
152     } else {
153       s += input[i];
154     }
155   }
156   return s;
157 }
158 
DasherToSnake(const std::string & input)159 std::string DasherToSnake(const std::string &input) {
160   std::string s;
161   for (size_t i = 0; i < input.length(); i++) {
162     if (input[i] == '-') {
163       s += "_";
164     } else {
165       s += input[i];
166     }
167   }
168   return s;
169 }
170 
ToDasher(const std::string & input)171 std::string ToDasher(const std::string &input) {
172   std::string s;
173   char p = 0;
174   for (size_t i = 0; i < input.length(); i++) {
175     char const &c = input[i];
176     if (c == '_') {
177       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           !(i + 1 < input.length() && isdigit(input[i + 1])))
183         s += "-";
184     } else {
185       s += c;
186     }
187     p = c;
188   }
189   return s;
190 }
191 
192 // Converts foo_bar_123baz_456 to foo_bar123_baz456
SnakeToSnake2(const std::string & s)193 std::string SnakeToSnake2(const std::string &s) {
194   if (s.length() <= 1) return s;
195   std::string result;
196   result.reserve(s.size());
197   for (size_t i = 0; i < s.length() - 1; i++) {
198     if (s[i] == '_' && isdigit(s[i + 1])) {
199       continue;  // Move the `_` until after the digits.
200     }
201 
202     result.push_back(s[i]);
203 
204     if (isdigit(s[i]) && isalpha(s[i + 1]) && islower(s[i + 1])) {
205       result.push_back('_');
206     }
207   }
208   result.push_back(s.back());
209 
210   return result;
211 }
212 
213 }  // namespace
214 
LoadFile(const char * name,bool binary,std::string * buf)215 bool LoadFile(const char *name, bool binary, std::string *buf) {
216   FLATBUFFERS_ASSERT(g_load_file_function);
217   return g_load_file_function(name, binary, buf);
218 }
219 
FileExists(const char * name)220 bool FileExists(const char *name) {
221   FLATBUFFERS_ASSERT(g_file_exists_function);
222   return g_file_exists_function(name);
223 }
224 
DirExists(const char * name)225 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     #define flatbuffers_stat stat
233     #define FLATBUFFERS_S_IFDIR S_IFDIR
234   #endif
235   // clang-format on
236   struct flatbuffers_stat file_info;
237   if (flatbuffers_stat(name, &file_info) != 0) return false;
238   return (file_info.st_mode & FLATBUFFERS_S_IFDIR) != 0;
239 }
240 
SetLoadFileFunction(LoadFileFunction load_file_function)241 LoadFileFunction SetLoadFileFunction(LoadFileFunction load_file_function) {
242   LoadFileFunction previous_function = g_load_file_function;
243   g_load_file_function = load_file_function ? load_file_function : LoadFileRaw;
244   return previous_function;
245 }
246 
SetFileExistsFunction(FileExistsFunction file_exists_function)247 FileExistsFunction SetFileExistsFunction(
248     FileExistsFunction file_exists_function) {
249   FileExistsFunction previous_function = g_file_exists_function;
250   g_file_exists_function =
251       file_exists_function ? file_exists_function : FileExistsRaw;
252   return previous_function;
253 }
254 
SaveFile(const char * name,const char * buf,size_t len,bool binary)255 bool SaveFile(const char *name, const char *buf, size_t len, bool binary) {
256   std::ofstream ofs(name, binary ? std::ofstream::binary : std::ofstream::out);
257   if (!ofs.is_open()) return false;
258   ofs.write(buf, len);
259   return !ofs.bad();
260 }
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 
StripExtension(const std::string & filepath)269 std::string StripExtension(const std::string &filepath) {
270   size_t i = filepath.find_last_of('.');
271   return i != std::string::npos ? filepath.substr(0, i) : filepath;
272 }
273 
GetExtension(const std::string & filepath)274 std::string GetExtension(const std::string &filepath) {
275   size_t i = filepath.find_last_of('.');
276   return i != std::string::npos ? filepath.substr(i + 1) : "";
277 }
278 
StripPath(const std::string & filepath)279 std::string StripPath(const std::string &filepath) {
280   size_t i = filepath.find_last_of(PathSeparatorSet);
281   return i != std::string::npos ? filepath.substr(i + 1) : filepath;
282 }
283 
StripFileName(const std::string & filepath)284 std::string StripFileName(const std::string &filepath) {
285   size_t i = filepath.find_last_of(PathSeparatorSet);
286   return i != std::string::npos ? filepath.substr(0, i) : "";
287 }
288 
StripPrefix(const std::string & filepath,const std::string & prefix_to_remove)289 std::string StripPrefix(const std::string &filepath,
290                         const std::string &prefix_to_remove) {
291   if (!strncmp(filepath.c_str(), prefix_to_remove.c_str(),
292                prefix_to_remove.size())) {
293     return filepath.substr(prefix_to_remove.size());
294   }
295   return filepath;
296 }
297 
ConCatPathFileName(const std::string & path,const std::string & filename)298 std::string ConCatPathFileName(const std::string &path,
299                                const std::string &filename) {
300   std::string filepath = path;
301   if (filepath.length()) {
302     char &filepath_last_character = filepath.back();
303     if (filepath_last_character == kPathSeparatorWindows) {
304       filepath_last_character = kPathSeparator;
305     } else if (filepath_last_character != kPathSeparator) {
306       filepath += kPathSeparator;
307     }
308   }
309   filepath += filename;
310   // Ignore './' at the start of filepath.
311   if (filepath[0] == '.' && filepath[1] == kPathSeparator) {
312     filepath.erase(0, 2);
313   }
314   return filepath;
315 }
316 
PosixPath(const char * path)317 std::string PosixPath(const char *path) {
318   std::string p = path;
319   std::replace(p.begin(), p.end(), '\\', '/');
320   return p;
321 }
PosixPath(const std::string & path)322 std::string PosixPath(const std::string &path) {
323   return PosixPath(path.c_str());
324 }
325 
EnsureDirExists(const std::string & filepath)326 void EnsureDirExists(const std::string &filepath) {
327   auto parent = StripFileName(filepath);
328   if (parent.length()) EnsureDirExists(parent);
329     // clang-format off
330 
331   #ifdef _WIN32
332     (void)_mkdir(filepath.c_str());
333   #else
334     mkdir(filepath.c_str(), S_IRWXU|S_IRGRP|S_IXGRP);
335   #endif
336   // clang-format on
337 }
338 
FilePath(const std::string & project,const std::string & filePath,bool absolute)339 std::string FilePath(const std::string& project, const std::string& filePath, bool absolute) {
340     return (absolute) ? AbsolutePath(filePath) : RelativeToRootPath(project, filePath);
341 }
342 
AbsolutePath(const std::string & filepath)343 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       char *abs_path_temp = realpath(filepath.c_str(), nullptr);
354       bool success = abs_path_temp != nullptr;
355       std::string abs_path;
356       if(success) {
357         abs_path = abs_path_temp;
358         free(abs_path_temp);
359       }
360       return success
361     #endif
362       ? abs_path
363       : filepath;
364   #endif // FLATBUFFERS_NO_ABSOLUTE_PATH_RESOLUTION
365   // clang-format on
366 }
367 
RelativeToRootPath(const std::string & project,const std::string & filepath)368 std::string RelativeToRootPath(const std::string &project,
369                                const std::string &filepath) {
370   std::string absolute_project = PosixPath(AbsolutePath(project));
371   if (absolute_project.back() != '/') absolute_project += "/";
372   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   const char *a = absolute_project.c_str();
377   const char *b = absolute_filepath.c_str();
378   size_t common_prefix_len = 0;
379   while (*a != '\0' && *b != '\0' && *a == *b) {
380     if (*a == '/') common_prefix_len = a - absolute_project.c_str();
381     a++;
382     b++;
383   }
384   // the number of ../ to prepend to b depends on the number of remaining
385   // directories in A.
386   const char *suffix = absolute_project.c_str() + common_prefix_len;
387   size_t num_up = 0;
388   while (*suffix != '\0')
389     if (*suffix++ == '/') num_up++;
390   num_up--;  // last one is known to be '/'.
391   std::string result = "//";
392   for (size_t i = 0; i < num_up; i++) result += "../";
393   result += absolute_filepath.substr(common_prefix_len + 1);
394 
395   return result;
396 }
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
ClassicLocale()407   ClassicLocale::ClassicLocale()
408     : locale_(_create_locale(LC_ALL, "C")) {}
~ClassicLocale()409   ClassicLocale::~ClassicLocale() { _free_locale(locale_); }
410 #else
ClassicLocale()411   ClassicLocale::ClassicLocale()
412     : locale_(newlocale(LC_ALL, "C", nullptr)) {}
~ClassicLocale()413   ClassicLocale::~ClassicLocale() { freelocale(locale_); }
414 #endif
415 // clang-format on
416 
417 #endif  // !FLATBUFFERS_LOCALE_INDEPENDENT
418 
RemoveStringQuotes(const std::string & s)419 std::string RemoveStringQuotes(const std::string &s) {
420   auto ch = *s.c_str();
421   return ((s.size() >= 2) && (ch == '\"' || ch == '\'') && (ch == s.back()))
422              ? s.substr(1, s.length() - 2)
423              : s;
424 }
425 
SetGlobalTestLocale(const char * locale_name,std::string * _value)426 bool SetGlobalTestLocale(const char *locale_name, std::string *_value) {
427   const auto the_locale = setlocale(LC_ALL, locale_name);
428   if (!the_locale) return false;
429   if (_value) *_value = std::string(the_locale);
430   return true;
431 }
432 
ReadEnvironmentVariable(const char * var_name,std::string * _value)433 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   auto env_str = std::getenv(var_name);
438   if (!env_str) return false;
439   if (_value) *_value = std::string(env_str);
440   return true;
441 }
442 
ConvertCase(const std::string & input,Case output_case,Case input_case)443 std::string ConvertCase(const std::string &input, Case output_case,
444                         Case input_case) {
445   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   switch (input_case) {
449     case Case::kLowerCamel:
450     case Case::kUpperCamel:
451       return ConvertCase(CamelToSnake(input), output_case);
452     case Case::kDasher: return ConvertCase(DasherToSnake(input), output_case);
453     case Case::kKeep: printf("WARNING: Converting from kKeep case.\n"); break;
454     default:
455     case Case::kSnake:
456     case Case::kScreamingSnake:
457     case Case::kAllLower:
458     case Case::kAllUpper: break;
459   }
460 
461   switch (output_case) {
462     case Case::kUpperCamel: return ToCamelCase(input, true);
463     case Case::kLowerCamel: return ToCamelCase(input, false);
464     case Case::kSnake: return input;
465     case Case::kScreamingSnake: return ToSnakeCase(input, true);
466     case Case::kAllUpper: return ToAll(input, CharToUpper);
467     case Case::kAllLower: return ToAll(input, CharToLower);
468     case Case::kDasher: return ToDasher(input);
469     case Case::kSnake2: return SnakeToSnake2(input);
470     default:
471     case Case::kUnknown: return input;
472   }
473 }
474 
475 }  // namespace flatbuffers
476