util.h
Go to the documentation of this file.
1 /*
2  * Copyright 2014 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 #ifndef FLATBUFFERS_UTIL_H_
18 #define FLATBUFFERS_UTIL_H_
19 
20 #include <assert.h>
21 #include <stdint.h>
22 #include <stdlib.h>
23 #include <fstream>
24 #include <iomanip>
25 #ifndef FLATBUFFERS_PREFER_PRINTF
26 # include <sstream>
27 #else // FLATBUFFERS_PREFER_PRINTF
28 # include <float.h>
29 # include <stdio.h>
30 #endif // FLATBUFFERS_PREFER_PRINTF
31 #include <string>
32 #ifdef _WIN32
33 # ifndef WIN32_LEAN_AND_MEAN
34 # define WIN32_LEAN_AND_MEAN
35 # endif
36 # ifndef NOMINMAX
37 # define NOMINMAX
38 # endif
39 # include <windows.h> // Must be included before <direct.h>
40 # include <direct.h>
41 # include <winbase.h>
42 # undef interface // This is also important because of reasons
43 #else
44 # include <limits.h>
45 #endif
46 #include <sys/stat.h>
47 #include <sys/types.h>
48 
49 #include "flatbuffers/base.h"
50 
51 namespace flatbuffers {
52 
53 #ifdef FLATBUFFERS_PREFER_PRINTF
54 template<typename T> size_t IntToDigitCount(T t) {
55  size_t digit_count = 0;
56  // Count the sign for negative numbers
57  if (t < 0) digit_count++;
58  // Count a single 0 left of the dot for fractional numbers
59  if (-1 < t && t < 1) digit_count++;
60  // Count digits until fractional part
61  T eps = std::numeric_limits<float>::epsilon();
62  while (t <= (-1 + eps) || (1 - eps) <= t) {
63  t /= 10;
64  digit_count++;
65  }
66  return digit_count;
67 }
68 
69 template<typename T> size_t NumToStringWidth(T t, int precision = 0) {
70  size_t string_width = IntToDigitCount(t);
71  // Count the dot for floating point numbers
72  if (precision) string_width += (precision + 1);
73  return string_width;
74 }
75 
76 template<typename T> std::string NumToStringImplWrapper(T t, const char* fmt,
77  int precision = 0) {
78  size_t string_width = NumToStringWidth(t, precision);
79  std::string s(string_width, 0x00);
80  // Allow snprintf to use std::string trailing null to detect buffer overflow
81  snprintf(const_cast<char*>(s.data()), (s.size()+1), fmt, precision, t);
82  return s;
83 }
84 #endif // FLATBUFFERS_PREFER_PRINTF
85 
86 // Convert an integer or floating point value to a string.
87 // In contrast to std::stringstream, "char" values are
88 // converted to a string of digits, and we don't use scientific notation.
89 template<typename T> std::string NumToString(T t) {
90  // clang-format off
91  #ifndef FLATBUFFERS_PREFER_PRINTF
92  std::stringstream ss;
93  ss << t;
94  return ss.str();
95  #else // FLATBUFFERS_PREFER_PRINTF
96  auto v = static_cast<long long>(t);
97  return NumToStringImplWrapper(v, "%.*lld");
98  #endif // FLATBUFFERS_PREFER_PRINTF
99  // clang-format on
100 }
101 // Avoid char types used as character data.
102 template<> inline std::string NumToString<signed char>(signed char t) {
103  return NumToString(static_cast<int>(t));
104 }
105 template<> inline std::string NumToString<unsigned char>(unsigned char t) {
106  return NumToString(static_cast<int>(t));
107 }
108 #if defined(FLATBUFFERS_CPP98_STL)
109 template<> inline std::string NumToString<long long>(long long t) {
110  char buf[21]; // (log((1 << 63) - 1) / log(10)) + 2
111  snprintf(buf, sizeof(buf), "%lld", t);
112  return std::string(buf);
113 }
114 
115 template<>
116 inline std::string NumToString<unsigned long long>(unsigned long long t) {
117  char buf[22]; // (log((1 << 63) - 1) / log(10)) + 1
118  snprintf(buf, sizeof(buf), "%llu", t);
119  return std::string(buf);
120 }
121 #endif // defined(FLATBUFFERS_CPP98_STL)
122 
123 // Special versions for floats/doubles.
124 template<typename T> std::string FloatToString(T t, int precision) {
125  // clang-format off
126  #ifndef FLATBUFFERS_PREFER_PRINTF
127  // to_string() prints different numbers of digits for floats depending on
128  // platform and isn't available on Android, so we use stringstream
129  std::stringstream ss;
130  // Use std::fixed to suppress scientific notation.
131  ss << std::fixed;
132  // Default precision is 6, we want that to be higher for doubles.
133  ss << std::setprecision(precision);
134  ss << t;
135  auto s = ss.str();
136  #else // FLATBUFFERS_PREFER_PRINTF
137  auto v = static_cast<double>(t);
138  auto s = NumToStringImplWrapper(v, "%0.*f", precision);
139  #endif // FLATBUFFERS_PREFER_PRINTF
140  // clang-format on
141  // Sadly, std::fixed turns "1" into "1.00000", so here we undo that.
142  auto p = s.find_last_not_of('0');
143  if (p != std::string::npos) {
144  // Strip trailing zeroes. If it is a whole number, keep one zero.
145  s.resize(p + (s[p] == '.' ? 2 : 1));
146  }
147  return s;
148 }
149 
150 template<> inline std::string NumToString<double>(double t) {
151  return FloatToString(t, 12);
152 }
153 template<> inline std::string NumToString<float>(float t) {
154  return FloatToString(t, 6);
155 }
156 
157 // Convert an integer value to a hexadecimal string.
158 // The returned string length is always xdigits long, prefixed by 0 digits.
159 // For example, IntToStringHex(0x23, 8) returns the string "00000023".
160 inline std::string IntToStringHex(int i, int xdigits) {
161  // clang-format off
162  #ifndef FLATBUFFERS_PREFER_PRINTF
163  std::stringstream ss;
164  ss << std::setw(xdigits) << std::setfill('0') << std::hex << std::uppercase
165  << i;
166  return ss.str();
167  #else // FLATBUFFERS_PREFER_PRINTF
168  return NumToStringImplWrapper(i, "%.*X", xdigits);
169  #endif // FLATBUFFERS_PREFER_PRINTF
170  // clang-format on
171 }
172 
173 // Portable implementation of strtoll().
174 inline int64_t StringToInt(const char *str, char **endptr = nullptr,
175  int base = 10) {
176  // clang-format off
177  #ifdef _MSC_VER
178  return _strtoi64(str, endptr, base);
179  #else
180  return strtoll(str, endptr, base);
181  #endif
182  // clang-format on
183 }
184 
185 // Portable implementation of strtoull().
186 inline uint64_t StringToUInt(const char *str, char **endptr = nullptr,
187  int base = 10) {
188  // clang-format off
189  #ifdef _MSC_VER
190  return _strtoui64(str, endptr, base);
191  #else
192  return strtoull(str, endptr, base);
193  #endif
194  // clang-format on
195 }
196 
197 typedef bool (*LoadFileFunction)(const char *filename, bool binary,
198  std::string *dest);
199 typedef bool (*FileExistsFunction)(const char *filename);
200 
202 
204  FileExistsFunction file_exists_function);
205 
206 // Check if file "name" exists.
207 bool FileExists(const char *name);
208 
209 // Check if "name" exists and it is also a directory.
210 bool DirExists(const char *name);
211 
212 // Load file "name" into "buf" returning true if successful
213 // false otherwise. If "binary" is false data is read
214 // using ifstream's text mode, otherwise data is read with
215 // no transcoding.
216 bool LoadFile(const char *name, bool binary, std::string *buf);
217 
218 // Save data "buf" of length "len" bytes into a file
219 // "name" returning true if successful, false otherwise.
220 // If "binary" is false data is written using ifstream's
221 // text mode, otherwise data is written with no
222 // transcoding.
223 inline bool SaveFile(const char *name, const char *buf, size_t len,
224  bool binary) {
225  std::ofstream ofs(name, binary ? std::ofstream::binary : std::ofstream::out);
226  if (!ofs.is_open()) return false;
227  ofs.write(buf, len);
228  return !ofs.bad();
229 }
230 
231 // Save data "buf" into file "name" returning true if
232 // successful, false otherwise. If "binary" is false
233 // data is written using ifstream's text mode, otherwise
234 // data is written with no transcoding.
235 inline bool SaveFile(const char *name, const std::string &buf, bool binary) {
236  return SaveFile(name, buf.c_str(), buf.size(), binary);
237 }
238 
239 // Functionality for minimalistic portable path handling.
240 
241 // The functions below behave correctly regardless of whether posix ('/') or
242 // Windows ('/' or '\\') separators are used.
243 
244 // Any new separators inserted are always posix.
245 
246 // We internally store paths in posix format ('/'). Paths supplied
247 // by the user should go through PosixPath to ensure correct behavior
248 // on Windows when paths are string-compared.
249 
250 static const char kPathSeparator = '/';
251 static const char kPathSeparatorWindows = '\\';
252 static const char *PathSeparatorSet = "\\/"; // Intentionally no ':'
253 
254 // Returns the path with the extension, if any, removed.
255 inline std::string StripExtension(const std::string &filepath) {
256  size_t i = filepath.find_last_of(".");
257  return i != std::string::npos ? filepath.substr(0, i) : filepath;
258 }
259 
260 // Returns the extension, if any.
261 inline std::string GetExtension(const std::string &filepath) {
262  size_t i = filepath.find_last_of(".");
263  return i != std::string::npos ? filepath.substr(i + 1) : "";
264 }
265 
266 // Return the last component of the path, after the last separator.
267 inline std::string StripPath(const std::string &filepath) {
268  size_t i = filepath.find_last_of(PathSeparatorSet);
269  return i != std::string::npos ? filepath.substr(i + 1) : filepath;
270 }
271 
272 // Strip the last component of the path + separator.
273 inline std::string StripFileName(const std::string &filepath) {
274  size_t i = filepath.find_last_of(PathSeparatorSet);
275  return i != std::string::npos ? filepath.substr(0, i) : "";
276 }
277 
278 // Concatenates a path with a filename, regardless of wether the path
279 // ends in a separator or not.
280 inline std::string ConCatPathFileName(const std::string &path,
281  const std::string &filename) {
282  std::string filepath = path;
283  if (filepath.length()) {
284  char &filepath_last_character = string_back(filepath);
285  if (filepath_last_character == kPathSeparatorWindows) {
286  filepath_last_character = kPathSeparator;
287  } else if (filepath_last_character != kPathSeparator) {
288  filepath += kPathSeparator;
289  }
290  }
291  filepath += filename;
292  // Ignore './' at the start of filepath.
293  if (filepath[0] == '.' && filepath[1] == kPathSeparator) {
294  filepath.erase(0, 2);
295  }
296  return filepath;
297 }
298 
299 // Replaces any '\\' separators with '/'
300 inline std::string PosixPath(const char *path) {
301  std::string p = path;
302  std::replace(p.begin(), p.end(), '\\', '/');
303  return p;
304 }
305 
306 // This function ensure a directory exists, by recursively
307 // creating dirs for any parts of the path that don't exist yet.
308 inline void EnsureDirExists(const std::string &filepath) {
309  auto parent = StripFileName(filepath);
310  if (parent.length()) EnsureDirExists(parent);
311  // clang-format off
312  #ifdef _WIN32
313  (void)_mkdir(filepath.c_str());
314  #else
315  mkdir(filepath.c_str(), S_IRWXU|S_IRGRP|S_IXGRP);
316  #endif
317  // clang-format on
318 }
319 
320 // Obtains the absolute path from any other path.
321 // Returns the input path if the absolute path couldn't be resolved.
322 inline std::string AbsolutePath(const std::string &filepath) {
323  // clang-format off
324  #ifdef FLATBUFFERS_NO_ABSOLUTE_PATH_RESOLUTION
325  return filepath;
326  #else
327  #ifdef _WIN32
328  char abs_path[MAX_PATH];
329  return GetFullPathNameA(filepath.c_str(), MAX_PATH, abs_path, nullptr)
330  #else
331  char abs_path[PATH_MAX];
332  return realpath(filepath.c_str(), abs_path)
333  #endif
334  ? abs_path
335  : filepath;
336  #endif // FLATBUFFERS_NO_ABSOLUTE_PATH_RESOLUTION
337  // clang-format on
338 }
339 
340 // To and from UTF-8 unicode conversion functions
341 
342 // Convert a unicode code point into a UTF-8 representation by appending it
343 // to a string. Returns the number of bytes generated.
344 inline int ToUTF8(uint32_t ucc, std::string *out) {
345  FLATBUFFERS_ASSERT(!(ucc & 0x80000000)); // Top bit can't be set.
346  // 6 possible encodings: http://en.wikipedia.org/wiki/UTF-8
347  for (int i = 0; i < 6; i++) {
348  // Max bits this encoding can represent.
349  uint32_t max_bits = 6 + i * 5 + static_cast<int>(!i);
350  if (ucc < (1u << max_bits)) { // does it fit?
351  // Remaining bits not encoded in the first byte, store 6 bits each
352  uint32_t remain_bits = i * 6;
353  // Store first byte:
354  (*out) += static_cast<char>((0xFE << (max_bits - remain_bits)) |
355  (ucc >> remain_bits));
356  // Store remaining bytes:
357  for (int j = i - 1; j >= 0; j--) {
358  (*out) += static_cast<char>(((ucc >> (j * 6)) & 0x3F) | 0x80);
359  }
360  return i + 1; // Return the number of bytes added.
361  }
362  }
363  FLATBUFFERS_ASSERT(0); // Impossible to arrive here.
364  return -1;
365 }
366 
367 // Converts whatever prefix of the incoming string corresponds to a valid
368 // UTF-8 sequence into a unicode code. The incoming pointer will have been
369 // advanced past all bytes parsed.
370 // returns -1 upon corrupt UTF-8 encoding (ignore the incoming pointer in
371 // this case).
372 inline int FromUTF8(const char **in) {
373  int len = 0;
374  // Count leading 1 bits.
375  for (int mask = 0x80; mask >= 0x04; mask >>= 1) {
376  if (**in & mask) {
377  len++;
378  } else {
379  break;
380  }
381  }
382  if ((static_cast<unsigned char>(**in) << len) & 0x80) return -1; // Bit after leading 1's must be 0.
383  if (!len) return *(*in)++;
384  // UTF-8 encoded values with a length are between 2 and 4 bytes.
385  if (len < 2 || len > 4) { return -1; }
386  // Grab initial bits of the code.
387  int ucc = *(*in)++ & ((1 << (7 - len)) - 1);
388  for (int i = 0; i < len - 1; i++) {
389  if ((**in & 0xC0) != 0x80) return -1; // Upper bits must 1 0.
390  ucc <<= 6;
391  ucc |= *(*in)++ & 0x3F; // Grab 6 more bits of the code.
392  }
393  // UTF-8 cannot encode values between 0xD800 and 0xDFFF (reserved for
394  // UTF-16 surrogate pairs).
395  if (ucc >= 0xD800 && ucc <= 0xDFFF) { return -1; }
396  // UTF-8 must represent code points in their shortest possible encoding.
397  switch (len) {
398  case 2:
399  // Two bytes of UTF-8 can represent code points from U+0080 to U+07FF.
400  if (ucc < 0x0080 || ucc > 0x07FF) { return -1; }
401  break;
402  case 3:
403  // Three bytes of UTF-8 can represent code points from U+0800 to U+FFFF.
404  if (ucc < 0x0800 || ucc > 0xFFFF) { return -1; }
405  break;
406  case 4:
407  // Four bytes of UTF-8 can represent code points from U+10000 to U+10FFFF.
408  if (ucc < 0x10000 || ucc > 0x10FFFF) { return -1; }
409  break;
410  }
411  return ucc;
412 }
413 
414 #ifndef FLATBUFFERS_PREFER_PRINTF
415 // Wraps a string to a maximum length, inserting new lines where necessary. Any
416 // existing whitespace will be collapsed down to a single space. A prefix or
417 // suffix can be provided, which will be inserted before or after a wrapped
418 // line, respectively.
419 inline std::string WordWrap(const std::string in, size_t max_length,
420  const std::string wrapped_line_prefix,
421  const std::string wrapped_line_suffix) {
422  std::istringstream in_stream(in);
423  std::string wrapped, line, word;
424 
425  in_stream >> word;
426  line = word;
427 
428  while (in_stream >> word) {
429  if ((line.length() + 1 + word.length() + wrapped_line_suffix.length()) <
430  max_length) {
431  line += " " + word;
432  } else {
433  wrapped += line + wrapped_line_suffix + "\n";
434  line = wrapped_line_prefix + word;
435  }
436  }
437  wrapped += line;
438 
439  return wrapped;
440 }
441 #endif // !FLATBUFFERS_PREFER_PRINTF
442 
443 inline bool EscapeString(const char *s, size_t length, std::string *_text,
444  bool allow_non_utf8, bool natural_utf8) {
445  std::string &text = *_text;
446  text += "\"";
447  for (uoffset_t i = 0; i < length; i++) {
448  char c = s[i];
449  switch (c) {
450  case '\n': text += "\\n"; break;
451  case '\t': text += "\\t"; break;
452  case '\r': text += "\\r"; break;
453  case '\b': text += "\\b"; break;
454  case '\f': text += "\\f"; break;
455  case '\"': text += "\\\""; break;
456  case '\\': text += "\\\\"; break;
457  default:
458  if (c >= ' ' && c <= '~') {
459  text += c;
460  } else {
461  // Not printable ASCII data. Let's see if it's valid UTF-8 first:
462  const char *utf8 = s + i;
463  int ucc = FromUTF8(&utf8);
464  if (ucc < 0) {
465  if (allow_non_utf8) {
466  text += "\\x";
467  text += IntToStringHex(static_cast<uint8_t>(c), 2);
468  } else {
469  // There are two cases here:
470  //
471  // 1) We reached here by parsing an IDL file. In that case,
472  // we previously checked for non-UTF-8, so we shouldn't reach
473  // here.
474  //
475  // 2) We reached here by someone calling GenerateText()
476  // on a previously-serialized flatbuffer. The data might have
477  // non-UTF-8 Strings, or might be corrupt.
478  //
479  // In both cases, we have to give up and inform the caller
480  // they have no JSON.
481  return false;
482  }
483  } else {
484  if (natural_utf8) {
485  // utf8 points to past all utf-8 bytes parsed
486  text.append(s + i, static_cast<size_t>(utf8 - s - i));
487  } else if (ucc <= 0xFFFF) {
488  // Parses as Unicode within JSON's \uXXXX range, so use that.
489  text += "\\u";
490  text += IntToStringHex(ucc, 4);
491  } else if (ucc <= 0x10FFFF) {
492  // Encode Unicode SMP values to a surrogate pair using two \u
493  // escapes.
494  uint32_t base = ucc - 0x10000;
495  auto high_surrogate = (base >> 10) + 0xD800;
496  auto low_surrogate = (base & 0x03FF) + 0xDC00;
497  text += "\\u";
498  text += IntToStringHex(high_surrogate, 4);
499  text += "\\u";
500  text += IntToStringHex(low_surrogate, 4);
501  }
502  // Skip past characters recognized.
503  i = static_cast<uoffset_t>(utf8 - s - 1);
504  }
505  }
506  break;
507  }
508  }
509  text += "\"";
510  return true;
511 }
512 
513 } // namespace flatbuffers
514 
515 #endif // FLATBUFFERS_UTIL_H_
std::string IntToStringHex(int i, int xdigits)
Definition: util.h:160
std::string AbsolutePath(const std::string &filepath)
Definition: util.h:322
bool(* FileExistsFunction)(const char *filename)
Definition: util.h:199
static const char kPathSeparatorWindows
Definition: util.h:251
bool FileExists(const char *name)
bool DirExists(const char *name)
std::string NumToString< signed char >(signed char t)
Definition: util.h:102
static const char kPathSeparator
Definition: util.h:250
uint64_t StringToUInt(const char *str, char **endptr=nullptr, int base=10)
Definition: util.h:186
bool LoadFile(const char *name, bool binary, std::string *buf)
bool(* LoadFileFunction)(const char *filename, bool binary, std::string *dest)
Definition: util.h:197
std::string GetExtension(const std::string &filepath)
Definition: util.h:261
bool EscapeString(const char *s, size_t length, std::string *_text, bool allow_non_utf8, bool natural_utf8)
Definition: util.h:443
std::string ConCatPathFileName(const std::string &path, const std::string &filename)
Definition: util.h:280
std::string PosixPath(const char *path)
Definition: util.h:300
static const char * PathSeparatorSet
Definition: util.h:252
LoadFileFunction SetLoadFileFunction(LoadFileFunction load_file_function)
std::string WordWrap(const std::string in, size_t max_length, const std::string wrapped_line_prefix, const std::string wrapped_line_suffix)
Definition: util.h:419
void EnsureDirExists(const std::string &filepath)
Definition: util.h:308
#define FLATBUFFERS_ASSERT
Definition: base.h:13
FileExistsFunction SetFileExistsFunction(FileExistsFunction file_exists_function)
Simple class for manipulating paths on Linux/Windows/Mac OS.
Definition: path.h:42
int FromUTF8(const char **in)
Definition: util.h:372
std::string NumToString(T t)
Definition: util.h:89
std::string NumToString< unsigned char >(unsigned char t)
Definition: util.h:105
std::string NumToString< float >(float t)
Definition: util.h:153
std::string StripPath(const std::string &filepath)
Definition: util.h:267
int64_t StringToInt(const char *str, char **endptr=nullptr, int base=10)
Definition: util.h:174
char & string_back(std::string &value)
Definition: stl_emulation.h:41
std::string FloatToString(T t, int precision)
Definition: util.h:124
std::string NumToString< double >(double t)
Definition: util.h:150
std::string StripExtension(const std::string &filepath)
Definition: util.h:255
std::string StripFileName(const std::string &filepath)
Definition: util.h:273
int ToUTF8(uint32_t ucc, std::string *out)
Definition: util.h:344
bool SaveFile(const char *name, const char *buf, size_t len, bool binary)
Definition: util.h:223


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sun Feb 3 2019 03:14:32