00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef FLATBUFFERS_UTIL_H_
00018 #define FLATBUFFERS_UTIL_H_
00019
00020 #include <assert.h>
00021 #include <stdint.h>
00022 #include <stdlib.h>
00023 #include <fstream>
00024 #include <iomanip>
00025 #ifndef FLATBUFFERS_PREFER_PRINTF
00026 # include <sstream>
00027 #else // FLATBUFFERS_PREFER_PRINTF
00028 # include <float.h>
00029 # include <stdio.h>
00030 #endif // FLATBUFFERS_PREFER_PRINTF
00031 #include <string>
00032 #ifdef _WIN32
00033 # ifndef WIN32_LEAN_AND_MEAN
00034 # define WIN32_LEAN_AND_MEAN
00035 # endif
00036 # ifndef NOMINMAX
00037 # define NOMINMAX
00038 # endif
00039 # include <windows.h>
00040 # include <direct.h>
00041 # include <winbase.h>
00042 # undef interface // This is also important because of reasons
00043 #else
00044 # include <limits.h>
00045 #endif
00046 #include <sys/stat.h>
00047 #include <sys/types.h>
00048
00049 #include "flatbuffers/base.h"
00050
00051 namespace flatbuffers {
00052
00053 #ifdef FLATBUFFERS_PREFER_PRINTF
00054 template<typename T> size_t IntToDigitCount(T t) {
00055 size_t digit_count = 0;
00056
00057 if (t < 0) digit_count++;
00058
00059 if (-1 < t && t < 1) digit_count++;
00060
00061 T eps = std::numeric_limits<float>::epsilon();
00062 while (t <= (-1 + eps) || (1 - eps) <= t) {
00063 t /= 10;
00064 digit_count++;
00065 }
00066 return digit_count;
00067 }
00068
00069 template<typename T> size_t NumToStringWidth(T t, int precision = 0) {
00070 size_t string_width = IntToDigitCount(t);
00071
00072 if (precision) string_width += (precision + 1);
00073 return string_width;
00074 }
00075
00076 template<typename T> std::string NumToStringImplWrapper(T t, const char* fmt,
00077 int precision = 0) {
00078 size_t string_width = NumToStringWidth(t, precision);
00079 std::string s(string_width, 0x00);
00080
00081 snprintf(const_cast<char*>(s.data()), (s.size()+1), fmt, precision, t);
00082 return s;
00083 }
00084 #endif // FLATBUFFERS_PREFER_PRINTF
00085
00086
00087
00088
00089 template<typename T> std::string NumToString(T t) {
00090
00091 #ifndef FLATBUFFERS_PREFER_PRINTF
00092 std::stringstream ss;
00093 ss << t;
00094 return ss.str();
00095 #else // FLATBUFFERS_PREFER_PRINTF
00096 auto v = static_cast<long long>(t);
00097 return NumToStringImplWrapper(v, "%.*lld");
00098 #endif // FLATBUFFERS_PREFER_PRINTF
00099
00100 }
00101
00102 template<> inline std::string NumToString<signed char>(signed char t) {
00103 return NumToString(static_cast<int>(t));
00104 }
00105 template<> inline std::string NumToString<unsigned char>(unsigned char t) {
00106 return NumToString(static_cast<int>(t));
00107 }
00108 #if defined(FLATBUFFERS_CPP98_STL)
00109 template<> inline std::string NumToString<long long>(long long t) {
00110 char buf[21];
00111 snprintf(buf, sizeof(buf), "%lld", t);
00112 return std::string(buf);
00113 }
00114
00115 template<>
00116 inline std::string NumToString<unsigned long long>(unsigned long long t) {
00117 char buf[22];
00118 snprintf(buf, sizeof(buf), "%llu", t);
00119 return std::string(buf);
00120 }
00121 #endif // defined(FLATBUFFERS_CPP98_STL)
00122
00123
00124 template<typename T> std::string FloatToString(T t, int precision) {
00125
00126 #ifndef FLATBUFFERS_PREFER_PRINTF
00127
00128
00129 std::stringstream ss;
00130
00131 ss << std::fixed;
00132
00133 ss << std::setprecision(precision);
00134 ss << t;
00135 auto s = ss.str();
00136 #else // FLATBUFFERS_PREFER_PRINTF
00137 auto v = static_cast<double>(t);
00138 auto s = NumToStringImplWrapper(v, "%0.*f", precision);
00139 #endif // FLATBUFFERS_PREFER_PRINTF
00140
00141
00142 auto p = s.find_last_not_of('0');
00143 if (p != std::string::npos) {
00144
00145 s.resize(p + (s[p] == '.' ? 2 : 1));
00146 }
00147 return s;
00148 }
00149
00150 template<> inline std::string NumToString<double>(double t) {
00151 return FloatToString(t, 12);
00152 }
00153 template<> inline std::string NumToString<float>(float t) {
00154 return FloatToString(t, 6);
00155 }
00156
00157
00158
00159
00160 inline std::string IntToStringHex(int i, int xdigits) {
00161
00162 #ifndef FLATBUFFERS_PREFER_PRINTF
00163 std::stringstream ss;
00164 ss << std::setw(xdigits) << std::setfill('0') << std::hex << std::uppercase
00165 << i;
00166 return ss.str();
00167 #else // FLATBUFFERS_PREFER_PRINTF
00168 return NumToStringImplWrapper(i, "%.*X", xdigits);
00169 #endif // FLATBUFFERS_PREFER_PRINTF
00170
00171 }
00172
00173
00174 inline int64_t StringToInt(const char *str, char **endptr = nullptr,
00175 int base = 10) {
00176
00177 #ifdef _MSC_VER
00178 return _strtoi64(str, endptr, base);
00179 #else
00180 return strtoll(str, endptr, base);
00181 #endif
00182
00183 }
00184
00185
00186 inline uint64_t StringToUInt(const char *str, char **endptr = nullptr,
00187 int base = 10) {
00188
00189 #ifdef _MSC_VER
00190 return _strtoui64(str, endptr, base);
00191 #else
00192 return strtoull(str, endptr, base);
00193 #endif
00194
00195 }
00196
00197 typedef bool (*LoadFileFunction)(const char *filename, bool binary,
00198 std::string *dest);
00199 typedef bool (*FileExistsFunction)(const char *filename);
00200
00201 LoadFileFunction SetLoadFileFunction(LoadFileFunction load_file_function);
00202
00203 FileExistsFunction SetFileExistsFunction(
00204 FileExistsFunction file_exists_function);
00205
00206
00207 bool FileExists(const char *name);
00208
00209
00210 bool DirExists(const char *name);
00211
00212
00213
00214
00215
00216 bool LoadFile(const char *name, bool binary, std::string *buf);
00217
00218
00219
00220
00221
00222
00223 inline bool SaveFile(const char *name, const char *buf, size_t len,
00224 bool binary) {
00225 std::ofstream ofs(name, binary ? std::ofstream::binary : std::ofstream::out);
00226 if (!ofs.is_open()) return false;
00227 ofs.write(buf, len);
00228 return !ofs.bad();
00229 }
00230
00231
00232
00233
00234
00235 inline bool SaveFile(const char *name, const std::string &buf, bool binary) {
00236 return SaveFile(name, buf.c_str(), buf.size(), binary);
00237 }
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250 static const char kPathSeparator = '/';
00251 static const char kPathSeparatorWindows = '\\';
00252 static const char *PathSeparatorSet = "\\/";
00253
00254
00255 inline std::string StripExtension(const std::string &filepath) {
00256 size_t i = filepath.find_last_of(".");
00257 return i != std::string::npos ? filepath.substr(0, i) : filepath;
00258 }
00259
00260
00261 inline std::string GetExtension(const std::string &filepath) {
00262 size_t i = filepath.find_last_of(".");
00263 return i != std::string::npos ? filepath.substr(i + 1) : "";
00264 }
00265
00266
00267 inline std::string StripPath(const std::string &filepath) {
00268 size_t i = filepath.find_last_of(PathSeparatorSet);
00269 return i != std::string::npos ? filepath.substr(i + 1) : filepath;
00270 }
00271
00272
00273 inline std::string StripFileName(const std::string &filepath) {
00274 size_t i = filepath.find_last_of(PathSeparatorSet);
00275 return i != std::string::npos ? filepath.substr(0, i) : "";
00276 }
00277
00278
00279
00280 inline std::string ConCatPathFileName(const std::string &path,
00281 const std::string &filename) {
00282 std::string filepath = path;
00283 if (filepath.length()) {
00284 char &filepath_last_character = string_back(filepath);
00285 if (filepath_last_character == kPathSeparatorWindows) {
00286 filepath_last_character = kPathSeparator;
00287 } else if (filepath_last_character != kPathSeparator) {
00288 filepath += kPathSeparator;
00289 }
00290 }
00291 filepath += filename;
00292
00293 if (filepath[0] == '.' && filepath[1] == kPathSeparator) {
00294 filepath.erase(0, 2);
00295 }
00296 return filepath;
00297 }
00298
00299
00300 inline std::string PosixPath(const char *path) {
00301 std::string p = path;
00302 std::replace(p.begin(), p.end(), '\\', '/');
00303 return p;
00304 }
00305
00306
00307
00308 inline void EnsureDirExists(const std::string &filepath) {
00309 auto parent = StripFileName(filepath);
00310 if (parent.length()) EnsureDirExists(parent);
00311
00312 #ifdef _WIN32
00313 (void)_mkdir(filepath.c_str());
00314 #else
00315 mkdir(filepath.c_str(), S_IRWXU|S_IRGRP|S_IXGRP);
00316 #endif
00317
00318 }
00319
00320
00321
00322 inline std::string AbsolutePath(const std::string &filepath) {
00323
00324 #ifdef FLATBUFFERS_NO_ABSOLUTE_PATH_RESOLUTION
00325 return filepath;
00326 #else
00327 #ifdef _WIN32
00328 char abs_path[MAX_PATH];
00329 return GetFullPathNameA(filepath.c_str(), MAX_PATH, abs_path, nullptr)
00330 #else
00331 char abs_path[PATH_MAX];
00332 return realpath(filepath.c_str(), abs_path)
00333 #endif
00334 ? abs_path
00335 : filepath;
00336 #endif // FLATBUFFERS_NO_ABSOLUTE_PATH_RESOLUTION
00337
00338 }
00339
00340
00341
00342
00343
00344 inline int ToUTF8(uint32_t ucc, std::string *out) {
00345 FLATBUFFERS_ASSERT(!(ucc & 0x80000000));
00346
00347 for (int i = 0; i < 6; i++) {
00348
00349 uint32_t max_bits = 6 + i * 5 + static_cast<int>(!i);
00350 if (ucc < (1u << max_bits)) {
00351
00352 uint32_t remain_bits = i * 6;
00353
00354 (*out) += static_cast<char>((0xFE << (max_bits - remain_bits)) |
00355 (ucc >> remain_bits));
00356
00357 for (int j = i - 1; j >= 0; j--) {
00358 (*out) += static_cast<char>(((ucc >> (j * 6)) & 0x3F) | 0x80);
00359 }
00360 return i + 1;
00361 }
00362 }
00363 FLATBUFFERS_ASSERT(0);
00364 return -1;
00365 }
00366
00367
00368
00369
00370
00371
00372 inline int FromUTF8(const char **in) {
00373 int len = 0;
00374
00375 for (int mask = 0x80; mask >= 0x04; mask >>= 1) {
00376 if (**in & mask) {
00377 len++;
00378 } else {
00379 break;
00380 }
00381 }
00382 if ((static_cast<unsigned char>(**in) << len) & 0x80) return -1;
00383 if (!len) return *(*in)++;
00384
00385 if (len < 2 || len > 4) { return -1; }
00386
00387 int ucc = *(*in)++ & ((1 << (7 - len)) - 1);
00388 for (int i = 0; i < len - 1; i++) {
00389 if ((**in & 0xC0) != 0x80) return -1;
00390 ucc <<= 6;
00391 ucc |= *(*in)++ & 0x3F;
00392 }
00393
00394
00395 if (ucc >= 0xD800 && ucc <= 0xDFFF) { return -1; }
00396
00397 switch (len) {
00398 case 2:
00399
00400 if (ucc < 0x0080 || ucc > 0x07FF) { return -1; }
00401 break;
00402 case 3:
00403
00404 if (ucc < 0x0800 || ucc > 0xFFFF) { return -1; }
00405 break;
00406 case 4:
00407
00408 if (ucc < 0x10000 || ucc > 0x10FFFF) { return -1; }
00409 break;
00410 }
00411 return ucc;
00412 }
00413
00414 #ifndef FLATBUFFERS_PREFER_PRINTF
00415
00416
00417
00418
00419 inline std::string WordWrap(const std::string in, size_t max_length,
00420 const std::string wrapped_line_prefix,
00421 const std::string wrapped_line_suffix) {
00422 std::istringstream in_stream(in);
00423 std::string wrapped, line, word;
00424
00425 in_stream >> word;
00426 line = word;
00427
00428 while (in_stream >> word) {
00429 if ((line.length() + 1 + word.length() + wrapped_line_suffix.length()) <
00430 max_length) {
00431 line += " " + word;
00432 } else {
00433 wrapped += line + wrapped_line_suffix + "\n";
00434 line = wrapped_line_prefix + word;
00435 }
00436 }
00437 wrapped += line;
00438
00439 return wrapped;
00440 }
00441 #endif // !FLATBUFFERS_PREFER_PRINTF
00442
00443 inline bool EscapeString(const char *s, size_t length, std::string *_text,
00444 bool allow_non_utf8, bool natural_utf8) {
00445 std::string &text = *_text;
00446 text += "\"";
00447 for (uoffset_t i = 0; i < length; i++) {
00448 char c = s[i];
00449 switch (c) {
00450 case '\n': text += "\\n"; break;
00451 case '\t': text += "\\t"; break;
00452 case '\r': text += "\\r"; break;
00453 case '\b': text += "\\b"; break;
00454 case '\f': text += "\\f"; break;
00455 case '\"': text += "\\\""; break;
00456 case '\\': text += "\\\\"; break;
00457 default:
00458 if (c >= ' ' && c <= '~') {
00459 text += c;
00460 } else {
00461
00462 const char *utf8 = s + i;
00463 int ucc = FromUTF8(&utf8);
00464 if (ucc < 0) {
00465 if (allow_non_utf8) {
00466 text += "\\x";
00467 text += IntToStringHex(static_cast<uint8_t>(c), 2);
00468 } else {
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481 return false;
00482 }
00483 } else {
00484 if (natural_utf8) {
00485
00486 text.append(s + i, static_cast<size_t>(utf8 - s - i));
00487 } else if (ucc <= 0xFFFF) {
00488
00489 text += "\\u";
00490 text += IntToStringHex(ucc, 4);
00491 } else if (ucc <= 0x10FFFF) {
00492
00493
00494 uint32_t base = ucc - 0x10000;
00495 auto high_surrogate = (base >> 10) + 0xD800;
00496 auto low_surrogate = (base & 0x03FF) + 0xDC00;
00497 text += "\\u";
00498 text += IntToStringHex(high_surrogate, 4);
00499 text += "\\u";
00500 text += IntToStringHex(low_surrogate, 4);
00501 }
00502
00503 i = static_cast<uoffset_t>(utf8 - s - 1);
00504 }
00505 }
00506 break;
00507 }
00508 }
00509 text += "\"";
00510 return true;
00511 }
00512
00513 }
00514
00515 #endif // FLATBUFFERS_UTIL_H_