涂 桂旺 / Mbed OS mbed-os-example-cj
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers json_tool.h Source File

json_tool.h

00001 // Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors
00002 // Distributed under MIT license, or public domain if desired and
00003 // recognized in your jurisdiction.
00004 // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
00005 
00006 #ifndef LIB_JSONCPP_JSON_TOOL_H_INCLUDED
00007 #define LIB_JSONCPP_JSON_TOOL_H_INCLUDED
00008 
00009 #define NO_LOCALE_SUPPORT //---------------tgw----------
00010 
00011 // Also support old flag NO_LOCALE_SUPPORT
00012 #ifdef NO_LOCALE_SUPPORT
00013 #define JSONCPP_NO_LOCALE_SUPPORT
00014 #endif
00015 
00016 #ifndef JSONCPP_NO_LOCALE_SUPPORT
00017 #include <clocale>
00018 #endif
00019 
00020 /* This header provides common string manipulation support, such as UTF-8,
00021  * portable conversion from/to string...
00022  *
00023  * It is an internal header that must not be exposed.
00024  */
00025 
00026 namespace Json {
00027 static char getDecimalPoint() {
00028 #ifdef JSONCPP_NO_LOCALE_SUPPORT
00029   return '\0';
00030 #else
00031   struct lconv* lc = localeconv();
00032   return lc ? *(lc->decimal_point) : '\0';
00033 #endif
00034 }
00035 
00036 /// Converts a unicode code-point to UTF-8.
00037 static inline JSONCPP_STRING codePointToUTF8(unsigned int cp) {
00038   JSONCPP_STRING result;
00039 
00040   // based on description from http://en.wikipedia.org/wiki/UTF-8
00041 
00042   if (cp <= 0x7f) {
00043     result.resize(1);
00044     result[0] = static_cast<char>(cp);
00045   } else if (cp <= 0x7FF) {
00046     result.resize(2);
00047     result[1] = static_cast<char>(0x80 | (0x3f & cp));
00048     result[0] = static_cast<char>(0xC0 | (0x1f & (cp >> 6)));
00049   } else if (cp <= 0xFFFF) {
00050     result.resize(3);
00051     result[2] = static_cast<char>(0x80 | (0x3f & cp));
00052     result[1] = static_cast<char>(0x80 | (0x3f & (cp >> 6)));
00053     result[0] = static_cast<char>(0xE0 | (0xf & (cp >> 12)));
00054   } else if (cp <= 0x10FFFF) {
00055     result.resize(4);
00056     result[3] = static_cast<char>(0x80 | (0x3f & cp));
00057     result[2] = static_cast<char>(0x80 | (0x3f & (cp >> 6)));
00058     result[1] = static_cast<char>(0x80 | (0x3f & (cp >> 12)));
00059     result[0] = static_cast<char>(0xF0 | (0x7 & (cp >> 18)));
00060   }
00061 
00062   return result;
00063 }
00064 
00065 enum {
00066   /// Constant that specify the size of the buffer that must be passed to
00067   /// uintToString.
00068   uintToStringBufferSize = 3 * sizeof(LargestUInt) + 1
00069 };
00070 
00071 // Defines a char buffer for use with uintToString().
00072 typedef char UIntToStringBuffer[uintToStringBufferSize];
00073 
00074 /** Converts an unsigned integer to string.
00075  * @param value Unsigned integer to convert to string
00076  * @param current Input/Output string buffer.
00077  *        Must have at least uintToStringBufferSize chars free.
00078  */
00079 static inline void uintToString(LargestUInt value, char*& current) {
00080   *--current = 0;
00081   do {
00082     *--current = static_cast<char>(value % 10U + static_cast<unsigned>('0'));
00083     value /= 10;
00084   } while (value != 0);
00085 }
00086 
00087 /** Change ',' to '.' everywhere in buffer.
00088  *
00089  * We had a sophisticated way, but it did not work in WinCE.
00090  * @see https://github.com/open-source-parsers/jsoncpp/pull/9
00091  */
00092 static inline void fixNumericLocale(char* begin, char* end) {
00093   while (begin < end) {
00094     if (*begin == ',') {
00095       *begin = '.';
00096     }
00097     ++begin;
00098   }
00099 }
00100 
00101 static inline void fixNumericLocaleInput(char* begin, char* end) {
00102   char decimalPoint = getDecimalPoint();
00103   if (decimalPoint != '\0' && decimalPoint != '.') {
00104     while (begin < end) {
00105       if (*begin == '.') {
00106         *begin = decimalPoint;
00107       }
00108       ++begin;
00109     }
00110   }
00111 }
00112 
00113 } // namespace Json {
00114 
00115 #endif // LIB_JSONCPP_JSON_TOOL_H_INCLUDED