JsonCpp project page | Classes | Namespace | JsonCpp home page

json_tool.h
Go to the documentation of this file.
1 // Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors
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 static char getDecimalPoint() {
27 #ifdef JSONCPP_NO_LOCALE_SUPPORT
28 return '\0';
29 #else
30 struct lconv* lc = localeconv();
31 return lc ? *(lc->decimal_point) : '\0';
32 #endif
33 }
34
36 static inline JSONCPP_STRING codePointToUTF8(unsigned int cp) {
37 JSONCPP_STRING result;
38
39 // based on description from http://en.wikipedia.org/wiki/UTF-8
40
41 if (cp <= 0x7f) {
42 result.resize(1);
43 result[0] = static_cast<char>(cp);
44 } else if (cp <= 0x7FF) {
45 result.resize(2);
46 result[1] = static_cast<char>(0x80 | (0x3f & cp));
47 result[0] = static_cast<char>(0xC0 | (0x1f & (cp >> 6)));
48 } else if (cp <= 0xFFFF) {
49 result.resize(3);
50 result[2] = static_cast<char>(0x80 | (0x3f & cp));
51 result[1] = static_cast<char>(0x80 | (0x3f & (cp >> 6)));
52 result[0] = static_cast<char>(0xE0 | (0xf & (cp >> 12)));
53 } else if (cp <= 0x10FFFF) {
54 result.resize(4);
55 result[3] = static_cast<char>(0x80 | (0x3f & cp));
56 result[2] = static_cast<char>(0x80 | (0x3f & (cp >> 6)));
57 result[1] = static_cast<char>(0x80 | (0x3f & (cp >> 12)));
58 result[0] = static_cast<char>(0xF0 | (0x7 & (cp >> 18)));
59 }
60
61 return result;
62 }
63
64 enum {
67 uintToStringBufferSize = 3 * sizeof(LargestUInt) + 1
68 };
69
70 // Defines a char buffer for use with uintToString().
71 typedef char UIntToStringBuffer[uintToStringBufferSize];
72
78 static inline void uintToString(LargestUInt value, char*& current) {
79 *--current = 0;
80 do {
81 *--current = static_cast<char>(value % 10U + static_cast<unsigned>('0'));
82 value /= 10;
83 } while (value != 0);
84 }
85
91 static inline void fixNumericLocale(char* begin, char* end) {
92 while (begin < end) {
93 if (*begin == ',') {
94 *begin = '.';
95 }
96 ++begin;
97 }
98 }
99
100 static inline void fixNumericLocaleInput(char* begin, char* end) {
101 char decimalPoint = getDecimalPoint();
102 if (decimalPoint != '\0' && decimalPoint != '.') {
103 while (begin < end) {
104 if (*begin == '.') {
105 *begin = decimalPoint;
106 }
107 ++begin;
108 }
109 }
110 }
111
112 } // namespace Json {
113
114 #endif // LIB_JSONCPP_JSON_TOOL_H_INCLUDED
Json::uintToString
static void uintToString(LargestUInt value, char *&current)
Converts an unsigned integer to string.
Definition: json_tool.h:78
JSONCPP_STRING
#define JSONCPP_STRING
Definition: config.h:179
Json::getDecimalPoint
static char getDecimalPoint()
Definition: json_tool.h:26
Json::codePointToUTF8
static JSONCPP_STRING codePointToUTF8(unsigned int cp)
Converts a unicode code-point to UTF-8.
Definition: json_tool.h:36
Json::UIntToStringBuffer
char UIntToStringBuffer[uintToStringBufferSize]
Definition: json_tool.h:71
Json::fixNumericLocale
static void fixNumericLocale(char *begin, char *end)
Change &#39;,&#39; to &#39;.
Definition: json_tool.h:91
Json::fixNumericLocaleInput
static void fixNumericLocaleInput(char *begin, char *end)
Definition: json_tool.h:100
Json::LargestUInt
UInt64 LargestUInt
Definition: config.h:169
Json
JSON (JavaScript Object Notation).
Definition: allocator.h:14
Json::uintToStringBufferSize
Constant that specify the size of the buffer that must be passed to uintToString. ...
Definition: json_tool.h:67