Make the Parser independent from the global C-locale (#5028)
[platform/upstream/flatbuffers.git] / src / util.cpp
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 #include <clocale>
18
19 #include "flatbuffers/util.h"
20
21 namespace flatbuffers {
22
23 bool FileExistsRaw(const char *name) {
24   std::ifstream ifs(name);
25   return ifs.good();
26 }
27
28 bool LoadFileRaw(const char *name, bool binary, std::string *buf) {
29   if (DirExists(name)) return false;
30   std::ifstream ifs(name, binary ? std::ifstream::binary : std::ifstream::in);
31   if (!ifs.is_open()) return false;
32   if (binary) {
33     // The fastest way to read a file into a string.
34     ifs.seekg(0, std::ios::end);
35     auto size = ifs.tellg();
36     (*buf).resize(static_cast<size_t>(size));
37     ifs.seekg(0, std::ios::beg);
38     ifs.read(&(*buf)[0], (*buf).size());
39   } else {
40     // This is slower, but works correctly on all platforms for text files.
41     std::ostringstream oss;
42     oss << ifs.rdbuf();
43     *buf = oss.str();
44   }
45   return !ifs.bad();
46 }
47
48 static LoadFileFunction g_load_file_function = LoadFileRaw;
49 static FileExistsFunction g_file_exists_function = FileExistsRaw;
50
51 bool LoadFile(const char *name, bool binary, std::string *buf) {
52   FLATBUFFERS_ASSERT(g_load_file_function);
53   return g_load_file_function(name, binary, buf);
54 }
55
56 bool FileExists(const char *name) {
57   FLATBUFFERS_ASSERT(g_file_exists_function);
58   return g_file_exists_function(name);
59 }
60
61 bool DirExists(const char *name) {
62   // clang-format off
63
64   #ifdef _WIN32
65     #define flatbuffers_stat _stat
66     #define FLATBUFFERS_S_IFDIR _S_IFDIR
67   #else
68     #define flatbuffers_stat stat
69     #define FLATBUFFERS_S_IFDIR S_IFDIR
70   #endif
71   // clang-format on
72   struct flatbuffers_stat file_info;
73   if (flatbuffers_stat(name, &file_info) != 0) return false;
74   return (file_info.st_mode & FLATBUFFERS_S_IFDIR) != 0;
75 }
76
77 LoadFileFunction SetLoadFileFunction(LoadFileFunction load_file_function) {
78   LoadFileFunction previous_function = g_load_file_function;
79   g_load_file_function = load_file_function ? load_file_function : LoadFileRaw;
80   return previous_function;
81 }
82
83 FileExistsFunction SetFileExistsFunction(
84     FileExistsFunction file_exists_function) {
85   FileExistsFunction previous_function = g_file_exists_function;
86   g_file_exists_function =
87       file_exists_function ? file_exists_function : FileExistsRaw;
88   return previous_function;
89 }
90
91 // Locale-independent code.
92 #if defined(FLATBUFFERS_LOCALE_INDEPENDENT) && \
93     (FLATBUFFERS_LOCALE_INDEPENDENT > 0)
94
95 // clang-format off
96 // Allocate locale instance at startup of application.
97 ClassicLocale ClassicLocale::instance_;
98
99 #ifdef _MSC_VER
100   ClassicLocale::ClassicLocale()
101     : locale_(_create_locale(LC_ALL, "C")) {}
102   ClassicLocale::~ClassicLocale() { _free_locale(locale_); }
103 #else
104   ClassicLocale::ClassicLocale()
105     : locale_(newlocale(LC_ALL, "C", nullptr)) {}
106   ClassicLocale::~ClassicLocale() { freelocale(locale_); }
107 #endif
108 // clang-format on
109
110 #endif  // !FLATBUFFERS_LOCALE_INDEPENDENT
111
112 std::string RemoveStringQuotes(const std::string &s) {
113   auto ch = *s.c_str();
114   return ((s.size() >= 2) && (ch == '\"' || ch == '\'') &&
115           (ch == string_back(s)))
116              ? s.substr(1, s.length() - 2)
117              : s;
118 }
119
120 bool SetGlobalTestLocale(const char *locale_name, std::string *_value) {
121   const auto the_locale = setlocale(LC_ALL, locale_name);
122   if (!the_locale) return false;
123   if (_value) *_value = std::string(the_locale);
124   return true;
125 }
126
127 #ifdef _MSC_VER
128 #  pragma warning(disable : 4996)  // _CRT_SECURE_NO_WARNINGS
129 #endif
130 bool ReadEnvironmentVariable(const char *var_name, std::string *_value) {
131   auto env_str = std::getenv(var_name);
132   if (!env_str) return false;
133   if (_value) *_value = std::string(env_str);
134   return true;
135 }
136
137 }  // namespace flatbuffers