$extrastylesheet
JsonCpp project page Classes Namespace JsonCpp home page

src/lib_json/json_tool.h
Go to the documentation of this file.
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 
00010 // Also support old flag NO_LOCALE_SUPPORT
00011 #ifdef NO_LOCALE_SUPPORT
00012 #define JSONCPP_NO_LOCALE_SUPPORT
00013 #endif
00014 
00015 #ifndef JSONCPP_NO_LOCALE_SUPPORT
00016 #include <clocale>
00017 #endif
00018 
00019 /* This header provides common string manipulation support, such as UTF-8,
00020  * portable conversion from/to string...
00021  *
00022  * It is an internal header that must not be exposed.
00023  */
00024 
00025 namespace Json {
00026 static char getDecimalPoint() {
00027 #ifdef JSONCPP_NO_LOCALE_SUPPORT
00028   return '\0';
00029 #else
00030   struct lconv* lc = localeconv();
00031   return lc ? *(lc->decimal_point) : '\0';
00032 #endif
00033 }
00034 
00036 static inline JSONCPP_STRING codePointToUTF8(unsigned int cp) {
00037   JSONCPP_STRING result;
00038 
00039   // based on description from http://en.wikipedia.org/wiki/UTF-8
00040 
00041   if (cp <= 0x7f) {
00042     result.resize(1);
00043     result[0] = static_cast<char>(cp);
00044   } else if (cp <= 0x7FF) {
00045     result.resize(2);
00046     result[1] = static_cast<char>(0x80 | (0x3f & cp));
00047     result[0] = static_cast<char>(0xC0 | (0x1f & (cp >> 6)));
00048   } else if (cp <= 0xFFFF) {
00049     result.resize(3);
00050     result[2] = static_cast<char>(0x80 | (0x3f & cp));
00051     result[1] = static_cast<char>(0x80 | (0x3f & (cp >> 6)));
00052     result[0] = static_cast<char>(0xE0 | (0xf & (cp >> 12)));
00053   } else if (cp <= 0x10FFFF) {
00054     result.resize(4);
00055     result[3] = static_cast<char>(0x80 | (0x3f & cp));
00056     result[2] = static_cast<char>(0x80 | (0x3f & (cp >> 6)));
00057     result[1] = static_cast<char>(0x80 | (0x3f & (cp >> 12)));
00058     result[0] = static_cast<char>(0xF0 | (0x7 & (cp >> 18)));
00059   }
00060 
00061   return result;
00062 }
00063 
00064 enum {
00067   uintToStringBufferSize = 3 * sizeof(LargestUInt) + 1
00068 };
00069 
00070 // Defines a char buffer for use with uintToString().
00071 typedef char UIntToStringBuffer[uintToStringBufferSize];
00072 
00078 static inline void uintToString(LargestUInt value, char*& current) {
00079   *--current = 0;
00080   do {
00081     *--current = static_cast<char>(value % 10U + static_cast<unsigned>('0'));
00082     value /= 10;
00083   } while (value != 0);
00084 }
00085 
00091 static inline void fixNumericLocale(char* begin, char* end) {
00092   while (begin < end) {
00093     if (*begin == ',') {
00094       *begin = '.';
00095     }
00096     ++begin;
00097   }
00098 }
00099 
00100 static inline void fixNumericLocaleInput(char* begin, char* end) {
00101   char decimalPoint = getDecimalPoint();
00102   if (decimalPoint != '\0' && decimalPoint != '.') {
00103     while (begin < end) {
00104       if (*begin == '.') {
00105         *begin = decimalPoint;
00106       }
00107       ++begin;
00108     }
00109   }
00110 }
00111 
00112 } // namespace Json {
00113 
00114 #endif // LIB_JSONCPP_JSON_TOOL_H_INCLUDED