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