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