2 * Copyright 2016 Google Inc. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "flatbuffers/util.h"
21 namespace flatbuffers {
23 bool FileExistsRaw(const char *name) {
24 std::ifstream ifs(name);
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;
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());
40 // This is slower, but works correctly on all platforms for text files.
41 std::ostringstream oss;
48 static LoadFileFunction g_load_file_function = LoadFileRaw;
49 static FileExistsFunction g_file_exists_function = FileExistsRaw;
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);
56 bool FileExists(const char *name) {
57 FLATBUFFERS_ASSERT(g_file_exists_function);
58 return g_file_exists_function(name);
61 bool DirExists(const char *name) {
65 #define flatbuffers_stat _stat
66 #define FLATBUFFERS_S_IFDIR _S_IFDIR
68 #define flatbuffers_stat stat
69 #define FLATBUFFERS_S_IFDIR S_IFDIR
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;
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;
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;
91 // Locale-independent code.
92 #if defined(FLATBUFFERS_LOCALE_INDEPENDENT) && \
93 (FLATBUFFERS_LOCALE_INDEPENDENT > 0)
96 // Allocate locale instance at startup of application.
97 ClassicLocale ClassicLocale::instance_;
100 ClassicLocale::ClassicLocale()
101 : locale_(_create_locale(LC_ALL, "C")) {}
102 ClassicLocale::~ClassicLocale() { _free_locale(locale_); }
104 ClassicLocale::ClassicLocale()
105 : locale_(newlocale(LC_ALL, "C", nullptr)) {}
106 ClassicLocale::~ClassicLocale() { freelocale(locale_); }
110 #endif // !FLATBUFFERS_LOCALE_INDEPENDENT
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)
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);
128 # pragma warning(disable : 4996) // _CRT_SECURE_NO_WARNINGS
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);
137 } // namespace flatbuffers