util.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2014 Google Inc. All rights reserved.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
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>  // Must be included before <direct.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   // Count the sign for negative numbers
00057   if (t < 0) digit_count++;
00058   // Count a single 0 left of the dot for fractional numbers
00059   if (-1 < t && t < 1) digit_count++;
00060   // Count digits until fractional part
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   // Count the dot for floating point numbers
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   // Allow snprintf to use std::string trailing null to detect buffer overflow
00081   snprintf(const_cast<char*>(s.data()), (s.size()+1), fmt, precision, t);
00082   return s;
00083 }
00084 #endif // FLATBUFFERS_PREFER_PRINTF
00085 
00086 // Convert an integer or floating point value to a string.
00087 // In contrast to std::stringstream, "char" values are
00088 // converted to a string of digits, and we don't use scientific notation.
00089 template<typename T> std::string NumToString(T t) {
00090   // clang-format off
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   // clang-format on
00100 }
00101 // Avoid char types used as character data.
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];  // (log((1 << 63) - 1) / log(10)) + 2
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];  // (log((1 << 63) - 1) / log(10)) + 1
00118   snprintf(buf, sizeof(buf), "%llu", t);
00119   return std::string(buf);
00120 }
00121 #endif  // defined(FLATBUFFERS_CPP98_STL)
00122 
00123 // Special versions for floats/doubles.
00124 template<typename T> std::string FloatToString(T t, int precision) {
00125   // clang-format off
00126   #ifndef FLATBUFFERS_PREFER_PRINTF
00127     // to_string() prints different numbers of digits for floats depending on
00128     // platform and isn't available on Android, so we use stringstream
00129     std::stringstream ss;
00130     // Use std::fixed to suppress scientific notation.
00131     ss << std::fixed;
00132     // Default precision is 6, we want that to be higher for doubles.
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   // clang-format on
00141   // Sadly, std::fixed turns "1" into "1.00000", so here we undo that.
00142   auto p = s.find_last_not_of('0');
00143   if (p != std::string::npos) {
00144     // Strip trailing zeroes. If it is a whole number, keep one zero.
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 // Convert an integer value to a hexadecimal string.
00158 // The returned string length is always xdigits long, prefixed by 0 digits.
00159 // For example, IntToStringHex(0x23, 8) returns the string "00000023".
00160 inline std::string IntToStringHex(int i, int xdigits) {
00161   // clang-format off
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   // clang-format on
00171 }
00172 
00173 // Portable implementation of strtoll().
00174 inline int64_t StringToInt(const char *str, char **endptr = nullptr,
00175                            int base = 10) {
00176   // clang-format off
00177   #ifdef _MSC_VER
00178     return _strtoi64(str, endptr, base);
00179   #else
00180     return strtoll(str, endptr, base);
00181   #endif
00182   // clang-format on
00183 }
00184 
00185 // Portable implementation of strtoull().
00186 inline uint64_t StringToUInt(const char *str, char **endptr = nullptr,
00187                              int base = 10) {
00188   // clang-format off
00189   #ifdef _MSC_VER
00190     return _strtoui64(str, endptr, base);
00191   #else
00192     return strtoull(str, endptr, base);
00193   #endif
00194   // clang-format on
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 // Check if file "name" exists.
00207 bool FileExists(const char *name);
00208 
00209 // Check if "name" exists and it is also a directory.
00210 bool DirExists(const char *name);
00211 
00212 // Load file "name" into "buf" returning true if successful
00213 // false otherwise.  If "binary" is false data is read
00214 // using ifstream's text mode, otherwise data is read with
00215 // no transcoding.
00216 bool LoadFile(const char *name, bool binary, std::string *buf);
00217 
00218 // Save data "buf" of length "len" bytes into a file
00219 // "name" returning true if successful, false otherwise.
00220 // If "binary" is false data is written using ifstream's
00221 // text mode, otherwise data is written with no
00222 // transcoding.
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 // Save data "buf" into file "name" returning true if
00232 // successful, false otherwise.  If "binary" is false
00233 // data is written using ifstream's text mode, otherwise
00234 // data is written with no transcoding.
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 // Functionality for minimalistic portable path handling.
00240 
00241 // The functions below behave correctly regardless of whether posix ('/') or
00242 // Windows ('/' or '\\') separators are used.
00243 
00244 // Any new separators inserted are always posix.
00245 
00246 // We internally store paths in posix format ('/'). Paths supplied
00247 // by the user should go through PosixPath to ensure correct behavior
00248 // on Windows when paths are string-compared.
00249 
00250 static const char kPathSeparator = '/';
00251 static const char kPathSeparatorWindows = '\\';
00252 static const char *PathSeparatorSet = "\\/";  // Intentionally no ':'
00253 
00254 // Returns the path with the extension, if any, removed.
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 // Returns the extension, if any.
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 // Return the last component of the path, after the last separator.
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 // Strip the last component of the path + separator.
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 // Concatenates a path with a filename, regardless of wether the path
00279 // ends in a separator or not.
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   // Ignore './' at the start of filepath.
00293   if (filepath[0] == '.' && filepath[1] == kPathSeparator) {
00294     filepath.erase(0, 2);
00295   }
00296   return filepath;
00297 }
00298 
00299 // Replaces any '\\' separators with '/'
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 // This function ensure a directory exists, by recursively
00307 // creating dirs for any parts of the path that don't exist yet.
00308 inline void EnsureDirExists(const std::string &filepath) {
00309   auto parent = StripFileName(filepath);
00310   if (parent.length()) EnsureDirExists(parent);
00311     // clang-format off
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   // clang-format on
00318 }
00319 
00320 // Obtains the absolute path from any other path.
00321 // Returns the input path if the absolute path couldn't be resolved.
00322 inline std::string AbsolutePath(const std::string &filepath) {
00323   // clang-format off
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   // clang-format on
00338 }
00339 
00340 // To and from UTF-8 unicode conversion functions
00341 
00342 // Convert a unicode code point into a UTF-8 representation by appending it
00343 // to a string. Returns the number of bytes generated.
00344 inline int ToUTF8(uint32_t ucc, std::string *out) {
00345   FLATBUFFERS_ASSERT(!(ucc & 0x80000000));  // Top bit can't be set.
00346   // 6 possible encodings: http://en.wikipedia.org/wiki/UTF-8
00347   for (int i = 0; i < 6; i++) {
00348     // Max bits this encoding can represent.
00349     uint32_t max_bits = 6 + i * 5 + static_cast<int>(!i);
00350     if (ucc < (1u << max_bits)) {  // does it fit?
00351       // Remaining bits not encoded in the first byte, store 6 bits each
00352       uint32_t remain_bits = i * 6;
00353       // Store first byte:
00354       (*out) += static_cast<char>((0xFE << (max_bits - remain_bits)) |
00355                                   (ucc >> remain_bits));
00356       // Store remaining bytes:
00357       for (int j = i - 1; j >= 0; j--) {
00358         (*out) += static_cast<char>(((ucc >> (j * 6)) & 0x3F) | 0x80);
00359       }
00360       return i + 1;  // Return the number of bytes added.
00361     }
00362   }
00363   FLATBUFFERS_ASSERT(0);  // Impossible to arrive here.
00364   return -1;
00365 }
00366 
00367 // Converts whatever prefix of the incoming string corresponds to a valid
00368 // UTF-8 sequence into a unicode code. The incoming pointer will have been
00369 // advanced past all bytes parsed.
00370 // returns -1 upon corrupt UTF-8 encoding (ignore the incoming pointer in
00371 // this case).
00372 inline int FromUTF8(const char **in) {
00373   int len = 0;
00374   // Count leading 1 bits.
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;  // Bit after leading 1's must be 0.
00383   if (!len) return *(*in)++;
00384   // UTF-8 encoded values with a length are between 2 and 4 bytes.
00385   if (len < 2 || len > 4) { return -1; }
00386   // Grab initial bits of the code.
00387   int ucc = *(*in)++ & ((1 << (7 - len)) - 1);
00388   for (int i = 0; i < len - 1; i++) {
00389     if ((**in & 0xC0) != 0x80) return -1;  // Upper bits must 1 0.
00390     ucc <<= 6;
00391     ucc |= *(*in)++ & 0x3F;  // Grab 6 more bits of the code.
00392   }
00393   // UTF-8 cannot encode values between 0xD800 and 0xDFFF (reserved for
00394   // UTF-16 surrogate pairs).
00395   if (ucc >= 0xD800 && ucc <= 0xDFFF) { return -1; }
00396   // UTF-8 must represent code points in their shortest possible encoding.
00397   switch (len) {
00398     case 2:
00399       // Two bytes of UTF-8 can represent code points from U+0080 to U+07FF.
00400       if (ucc < 0x0080 || ucc > 0x07FF) { return -1; }
00401       break;
00402     case 3:
00403       // Three bytes of UTF-8 can represent code points from U+0800 to U+FFFF.
00404       if (ucc < 0x0800 || ucc > 0xFFFF) { return -1; }
00405       break;
00406     case 4:
00407       // Four bytes of UTF-8 can represent code points from U+10000 to U+10FFFF.
00408       if (ucc < 0x10000 || ucc > 0x10FFFF) { return -1; }
00409       break;
00410   }
00411   return ucc;
00412 }
00413 
00414 #ifndef FLATBUFFERS_PREFER_PRINTF
00415 // Wraps a string to a maximum length, inserting new lines where necessary. Any
00416 // existing whitespace will be collapsed down to a single space. A prefix or
00417 // suffix can be provided, which will be inserted before or after a wrapped
00418 // line, respectively.
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           // Not printable ASCII data. Let's see if it's valid UTF-8 first:
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               // There are two cases here:
00470               //
00471               // 1) We reached here by parsing an IDL file. In that case,
00472               // we previously checked for non-UTF-8, so we shouldn't reach
00473               // here.
00474               //
00475               // 2) We reached here by someone calling GenerateText()
00476               // on a previously-serialized flatbuffer. The data might have
00477               // non-UTF-8 Strings, or might be corrupt.
00478               //
00479               // In both cases, we have to give up and inform the caller
00480               // they have no JSON.
00481               return false;
00482             }
00483           } else {
00484             if (natural_utf8) {
00485               // utf8 points to past all utf-8 bytes parsed
00486               text.append(s + i, static_cast<size_t>(utf8 - s - i));
00487             } else if (ucc <= 0xFFFF) {
00488               // Parses as Unicode within JSON's \uXXXX range, so use that.
00489               text += "\\u";
00490               text += IntToStringHex(ucc, 4);
00491             } else if (ucc <= 0x10FFFF) {
00492               // Encode Unicode SMP values to a surrogate pair using two \u
00493               // escapes.
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             // Skip past characters recognized.
00503             i = static_cast<uoffset_t>(utf8 - s - 1);
00504           }
00505         }
00506         break;
00507     }
00508   }
00509   text += "\"";
00510   return true;
00511 }
00512 
00513 }  // namespace flatbuffers
00514 
00515 #endif  // FLATBUFFERS_UTIL_H_


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sat Feb 2 2019 03:50:10