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