json_tool.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright 2007-2010 Baptiste Lepilleur
  2. // Distributed under MIT license, or public domain if desired and
  3. // recognized in your jurisdiction.
  4. // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
  5. #ifndef LIB_JSONCPP_JSON_TOOL_H_INCLUDED
  6. #define LIB_JSONCPP_JSON_TOOL_H_INCLUDED
  7. // Also support old flag NO_LOCALE_SUPPORT
  8. #ifdef NO_LOCALE_SUPPORT
  9. #define JSONCPP_NO_LOCALE_SUPPORT
  10. #endif
  11. #ifndef JSONCPP_NO_LOCALE_SUPPORT
  12. #include <clocale>
  13. #endif
  14. /* This header provides common string manipulation support, such as UTF-8,
  15. * portable conversion from/to string...
  16. *
  17. * It is an internal header that must not be exposed.
  18. */
  19. namespace Json {
  20. static char getDecimalPoint() {
  21. #ifdef JSONCPP_NO_LOCALE_SUPPORT
  22. return '\0';
  23. #else
  24. //struct lconv* lc = localeconv();
  25. // return lc ? *(lc->decimal_point) : '\0';
  26. return '\0';
  27. #endif
  28. }
  29. /// Converts a unicode code-point to UTF-8.
  30. static inline JSONCPP_STRING codePointToUTF8(unsigned int cp) {
  31. JSONCPP_STRING result;
  32. // based on description from http://en.wikipedia.org/wiki/UTF-8
  33. if (cp <= 0x7f) {
  34. result.resize(1);
  35. result[0] = static_cast<char>(cp);
  36. } else if (cp <= 0x7FF) {
  37. result.resize(2);
  38. result[1] = static_cast<char>(0x80 | (0x3f & cp));
  39. result[0] = static_cast<char>(0xC0 | (0x1f & (cp >> 6)));
  40. } else if (cp <= 0xFFFF) {
  41. result.resize(3);
  42. result[2] = static_cast<char>(0x80 | (0x3f & cp));
  43. result[1] = static_cast<char>(0x80 | (0x3f & (cp >> 6)));
  44. result[0] = static_cast<char>(0xE0 | (0xf & (cp >> 12)));
  45. } else if (cp <= 0x10FFFF) {
  46. result.resize(4);
  47. result[3] = static_cast<char>(0x80 | (0x3f & cp));
  48. result[2] = static_cast<char>(0x80 | (0x3f & (cp >> 6)));
  49. result[1] = static_cast<char>(0x80 | (0x3f & (cp >> 12)));
  50. result[0] = static_cast<char>(0xF0 | (0x7 & (cp >> 18)));
  51. }
  52. return result;
  53. }
  54. /// Returns true if ch is a control character (in range [1,31]).
  55. static inline bool isControlCharacter(char ch) { return ch > 0 && ch <= 0x1F; }
  56. enum {
  57. /// Constant that specify the size of the buffer that must be passed to
  58. /// uintToString.
  59. uintToStringBufferSize = 3 * sizeof(LargestUInt) + 1
  60. };
  61. // Defines a char buffer for use with uintToString().
  62. typedef char UIntToStringBuffer[uintToStringBufferSize];
  63. /** Converts an unsigned integer to string.
  64. * @param value Unsigned interger to convert to string
  65. * @param current Input/Output string buffer.
  66. * Must have at least uintToStringBufferSize chars free.
  67. */
  68. static inline void uintToString(LargestUInt value, char*& current) {
  69. *--current = 0;
  70. do {
  71. *--current = static_cast<char>(value % 10U + static_cast<unsigned>('0'));
  72. value /= 10;
  73. } while (value != 0);
  74. }
  75. /** Change ',' to '.' everywhere in buffer.
  76. *
  77. * We had a sophisticated way, but it did not work in WinCE.
  78. * @see https://github.com/open-source-parsers/jsoncpp/pull/9
  79. */
  80. static inline void fixNumericLocale(char* begin, char* end) {
  81. while (begin < end) {
  82. if (*begin == ',') {
  83. *begin = '.';
  84. }
  85. ++begin;
  86. }
  87. }
  88. static inline void fixNumericLocaleInput(char* begin, char* end) {
  89. char decimalPoint = getDecimalPoint();
  90. if (decimalPoint != '\0' && decimalPoint != '.') {
  91. while (begin < end) {
  92. if (*begin == '.') {
  93. *begin = decimalPoint;
  94. }
  95. ++begin;
  96. }
  97. }
  98. }
  99. } // namespace Json {
  100. #endif // LIB_JSONCPP_JSON_TOOL_H_INCLUDED