Test

Committer:
HelGast95
Date:
Mon Feb 04 12:35:18 2019 +0000
Revision:
81:dded8c042cca
Parent:
73:a91805f9e9f0
Se mete thread para el calculo del tipo de evento y comunicacion con colas - WIP

Who changed what in which revision?

UserRevisionLine numberNew contents of line
HelGast95 73:a91805f9e9f0 1 /* Copyright 2009 Cybozu Labs, Inc.
HelGast95 73:a91805f9e9f0 2 *
HelGast95 73:a91805f9e9f0 3 * Redistribution and use in source and binary forms, with or without
HelGast95 73:a91805f9e9f0 4 * modification, are permitted provided that the following conditions are met:
HelGast95 73:a91805f9e9f0 5 *
HelGast95 73:a91805f9e9f0 6 * 1. Redistributions of source code must retain the above copyright notice,
HelGast95 73:a91805f9e9f0 7 * this list of conditions and the following disclaimer.
HelGast95 73:a91805f9e9f0 8 * 2. Redistributions in binary form must reproduce the above copyright notice,
HelGast95 73:a91805f9e9f0 9 * this list of conditions and the following disclaimer in the documentation
HelGast95 73:a91805f9e9f0 10 * and/or other materials provided with the distribution.
HelGast95 73:a91805f9e9f0 11 *
HelGast95 73:a91805f9e9f0 12 * THIS SOFTWARE IS PROVIDED BY CYBOZU LABS, INC. ``AS IS'' AND ANY EXPRESS OR
HelGast95 73:a91805f9e9f0 13 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
HelGast95 73:a91805f9e9f0 14 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
HelGast95 73:a91805f9e9f0 15 * EVENT SHALL CYBOZU LABS, INC. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
HelGast95 73:a91805f9e9f0 16 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
HelGast95 73:a91805f9e9f0 17 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
HelGast95 73:a91805f9e9f0 18 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
HelGast95 73:a91805f9e9f0 19 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
HelGast95 73:a91805f9e9f0 20 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
HelGast95 73:a91805f9e9f0 21 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
HelGast95 73:a91805f9e9f0 22 *
HelGast95 73:a91805f9e9f0 23 * The views and conclusions contained in the software and documentation are
HelGast95 73:a91805f9e9f0 24 * those of the authors and should not be interpreted as representing official
HelGast95 73:a91805f9e9f0 25 * policies, either expressed or implied, of Cybozu Labs, Inc.
HelGast95 73:a91805f9e9f0 26 *
HelGast95 73:a91805f9e9f0 27 */
HelGast95 73:a91805f9e9f0 28 #ifndef picojson_h
HelGast95 73:a91805f9e9f0 29 #define picojson_h
HelGast95 73:a91805f9e9f0 30
HelGast95 73:a91805f9e9f0 31 #include <cassert>
HelGast95 73:a91805f9e9f0 32 #include <cmath>
HelGast95 73:a91805f9e9f0 33 #include <cstdio>
HelGast95 73:a91805f9e9f0 34 #include <cstdlib>
HelGast95 73:a91805f9e9f0 35 #include <cstring>
HelGast95 73:a91805f9e9f0 36 #include <iostream>
HelGast95 73:a91805f9e9f0 37 #include <iterator>
HelGast95 73:a91805f9e9f0 38 #include <map>
HelGast95 73:a91805f9e9f0 39 #include <string>
HelGast95 73:a91805f9e9f0 40 #include <vector>
HelGast95 73:a91805f9e9f0 41
HelGast95 73:a91805f9e9f0 42 #ifdef _MSC_VER
HelGast95 73:a91805f9e9f0 43 #define SNPRINTF _snprintf_s
HelGast95 73:a91805f9e9f0 44 #pragma warning(push)
HelGast95 73:a91805f9e9f0 45 #pragma warning(disable : 4244) // conversion from int to char
HelGast95 73:a91805f9e9f0 46 #else
HelGast95 73:a91805f9e9f0 47 #define SNPRINTF snprintf
HelGast95 73:a91805f9e9f0 48 #endif
HelGast95 73:a91805f9e9f0 49
HelGast95 73:a91805f9e9f0 50 namespace picojson {
HelGast95 73:a91805f9e9f0 51
HelGast95 73:a91805f9e9f0 52 enum {
HelGast95 73:a91805f9e9f0 53 null_type,
HelGast95 73:a91805f9e9f0 54 boolean_type,
HelGast95 73:a91805f9e9f0 55 number_type,
HelGast95 73:a91805f9e9f0 56 string_type,
HelGast95 73:a91805f9e9f0 57 array_type,
HelGast95 73:a91805f9e9f0 58 object_type
HelGast95 73:a91805f9e9f0 59 };
HelGast95 73:a91805f9e9f0 60
HelGast95 73:a91805f9e9f0 61 struct null {};
HelGast95 73:a91805f9e9f0 62
HelGast95 73:a91805f9e9f0 63 class value {
HelGast95 73:a91805f9e9f0 64 public:
HelGast95 73:a91805f9e9f0 65 typedef std::vector<value> array;
HelGast95 73:a91805f9e9f0 66 typedef std::map<std::string, value> object;
HelGast95 73:a91805f9e9f0 67 protected:
HelGast95 73:a91805f9e9f0 68 int type_;
HelGast95 73:a91805f9e9f0 69 union {
HelGast95 73:a91805f9e9f0 70 bool boolean_;
HelGast95 73:a91805f9e9f0 71 double number_;
HelGast95 73:a91805f9e9f0 72 std::string* string_;
HelGast95 73:a91805f9e9f0 73 array* array_;
HelGast95 73:a91805f9e9f0 74 object* object_;
HelGast95 73:a91805f9e9f0 75 };
HelGast95 73:a91805f9e9f0 76 public:
HelGast95 73:a91805f9e9f0 77 value();
HelGast95 73:a91805f9e9f0 78 value(int type, bool);
HelGast95 73:a91805f9e9f0 79 explicit value(bool b);
HelGast95 73:a91805f9e9f0 80 explicit value(double n);
HelGast95 73:a91805f9e9f0 81 explicit value(const std::string& s);
HelGast95 73:a91805f9e9f0 82 explicit value(const array& a);
HelGast95 73:a91805f9e9f0 83 explicit value(const object& o);
HelGast95 73:a91805f9e9f0 84 ~value();
HelGast95 73:a91805f9e9f0 85 value(const value& x);
HelGast95 73:a91805f9e9f0 86 value& operator=(const value& x);
HelGast95 73:a91805f9e9f0 87 template <typename T> bool is() const;
HelGast95 73:a91805f9e9f0 88 template <typename T> const T& get() const;
HelGast95 73:a91805f9e9f0 89 template <typename T> T& get();
HelGast95 73:a91805f9e9f0 90 operator bool() const;
HelGast95 73:a91805f9e9f0 91 const value& get(size_t idx) const;
HelGast95 73:a91805f9e9f0 92 const value& get(const std::string& key) const;
HelGast95 73:a91805f9e9f0 93 std::string to_str() const;
HelGast95 73:a91805f9e9f0 94 template <typename Iter> void serialize(Iter os) const;
HelGast95 73:a91805f9e9f0 95 std::string serialize() const;
HelGast95 73:a91805f9e9f0 96 };
HelGast95 73:a91805f9e9f0 97
HelGast95 73:a91805f9e9f0 98 typedef value::array array;
HelGast95 73:a91805f9e9f0 99 typedef value::object object;
HelGast95 73:a91805f9e9f0 100
HelGast95 73:a91805f9e9f0 101 inline value::value() : type_(null_type) {}
HelGast95 73:a91805f9e9f0 102
HelGast95 73:a91805f9e9f0 103 inline value::value(int type, bool) : type_(type) {
HelGast95 73:a91805f9e9f0 104 switch (type) {
HelGast95 73:a91805f9e9f0 105 #define INIT(p, v) case p##type: p = v; break
HelGast95 73:a91805f9e9f0 106 INIT(boolean_, false);
HelGast95 73:a91805f9e9f0 107 INIT(number_, 0.0);
HelGast95 73:a91805f9e9f0 108 INIT(string_, new std::string());
HelGast95 73:a91805f9e9f0 109 INIT(array_, new array());
HelGast95 73:a91805f9e9f0 110 INIT(object_, new object());
HelGast95 73:a91805f9e9f0 111 #undef INIT
HelGast95 73:a91805f9e9f0 112 default: break;
HelGast95 73:a91805f9e9f0 113 }
HelGast95 73:a91805f9e9f0 114 }
HelGast95 73:a91805f9e9f0 115
HelGast95 73:a91805f9e9f0 116 inline value::value(bool b) : type_(boolean_type) {
HelGast95 73:a91805f9e9f0 117 boolean_ = b;
HelGast95 73:a91805f9e9f0 118 }
HelGast95 73:a91805f9e9f0 119
HelGast95 73:a91805f9e9f0 120 inline value::value(double n) : type_(number_type) {
HelGast95 73:a91805f9e9f0 121 number_ = n;
HelGast95 73:a91805f9e9f0 122 }
HelGast95 73:a91805f9e9f0 123
HelGast95 73:a91805f9e9f0 124 inline value::value(const std::string& s) : type_(string_type) {
HelGast95 73:a91805f9e9f0 125 string_ = new std::string(s);
HelGast95 73:a91805f9e9f0 126 }
HelGast95 73:a91805f9e9f0 127
HelGast95 73:a91805f9e9f0 128 inline value::value(const array& a) : type_(array_type) {
HelGast95 73:a91805f9e9f0 129 array_ = new array(a);
HelGast95 73:a91805f9e9f0 130 }
HelGast95 73:a91805f9e9f0 131
HelGast95 73:a91805f9e9f0 132 inline value::value(const object& o) : type_(object_type) {
HelGast95 73:a91805f9e9f0 133 object_ = new object(o);
HelGast95 73:a91805f9e9f0 134 }
HelGast95 73:a91805f9e9f0 135
HelGast95 73:a91805f9e9f0 136 inline value::~value() {
HelGast95 73:a91805f9e9f0 137 switch (type_) {
HelGast95 73:a91805f9e9f0 138 #define DEINIT(p) case p##type: delete p; break
HelGast95 73:a91805f9e9f0 139 DEINIT(string_);
HelGast95 73:a91805f9e9f0 140 DEINIT(array_);
HelGast95 73:a91805f9e9f0 141 DEINIT(object_);
HelGast95 73:a91805f9e9f0 142 #undef DEINIT
HelGast95 73:a91805f9e9f0 143 default: break;
HelGast95 73:a91805f9e9f0 144 }
HelGast95 73:a91805f9e9f0 145 }
HelGast95 73:a91805f9e9f0 146
HelGast95 73:a91805f9e9f0 147 inline value::value(const value& x) : type_(x.type_) {
HelGast95 73:a91805f9e9f0 148 switch (type_) {
HelGast95 73:a91805f9e9f0 149 #define INIT(p, v) case p##type: p = v; break
HelGast95 73:a91805f9e9f0 150 INIT(boolean_, x.boolean_);
HelGast95 73:a91805f9e9f0 151 INIT(number_, x.number_);
HelGast95 73:a91805f9e9f0 152 INIT(string_, new std::string(*x.string_));
HelGast95 73:a91805f9e9f0 153 INIT(array_, new array(*x.array_));
HelGast95 73:a91805f9e9f0 154 INIT(object_, new object(*x.object_));
HelGast95 73:a91805f9e9f0 155 #undef INIT
HelGast95 73:a91805f9e9f0 156 default: break;
HelGast95 73:a91805f9e9f0 157 }
HelGast95 73:a91805f9e9f0 158 }
HelGast95 73:a91805f9e9f0 159
HelGast95 73:a91805f9e9f0 160 inline value& value::operator=(const value& x) {
HelGast95 73:a91805f9e9f0 161 if (this != &x) {
HelGast95 73:a91805f9e9f0 162 this->~value();
HelGast95 73:a91805f9e9f0 163 new (this) value(x);
HelGast95 73:a91805f9e9f0 164 }
HelGast95 73:a91805f9e9f0 165 return *this;
HelGast95 73:a91805f9e9f0 166 }
HelGast95 73:a91805f9e9f0 167
HelGast95 73:a91805f9e9f0 168 #define IS(ctype, jtype) \
HelGast95 73:a91805f9e9f0 169 template <> inline bool value::is<ctype>() const { \
HelGast95 73:a91805f9e9f0 170 return type_ == jtype##_type; \
HelGast95 73:a91805f9e9f0 171 }
HelGast95 73:a91805f9e9f0 172 IS(null, null)
HelGast95 73:a91805f9e9f0 173 IS(bool, boolean)
HelGast95 73:a91805f9e9f0 174 IS(int, number)
HelGast95 73:a91805f9e9f0 175 IS(double, number)
HelGast95 73:a91805f9e9f0 176 IS(std::string, string)
HelGast95 73:a91805f9e9f0 177 IS(array, array)
HelGast95 73:a91805f9e9f0 178 IS(object, object)
HelGast95 73:a91805f9e9f0 179 #undef IS
HelGast95 73:a91805f9e9f0 180
HelGast95 73:a91805f9e9f0 181 #define GET(ctype, var) \
HelGast95 73:a91805f9e9f0 182 template <> inline const ctype& value::get<ctype>() const { \
HelGast95 73:a91805f9e9f0 183 return var; \
HelGast95 73:a91805f9e9f0 184 } \
HelGast95 73:a91805f9e9f0 185 template <> inline ctype& value::get<ctype>() { \
HelGast95 73:a91805f9e9f0 186 return var; \
HelGast95 73:a91805f9e9f0 187 }
HelGast95 73:a91805f9e9f0 188 GET(bool, boolean_)
HelGast95 73:a91805f9e9f0 189 GET(double, number_)
HelGast95 73:a91805f9e9f0 190 GET(std::string, *string_)
HelGast95 73:a91805f9e9f0 191 GET(array, *array_)
HelGast95 73:a91805f9e9f0 192 GET(object, *object_)
HelGast95 73:a91805f9e9f0 193 #undef GET
HelGast95 73:a91805f9e9f0 194
HelGast95 73:a91805f9e9f0 195 inline value::operator bool() const {
HelGast95 73:a91805f9e9f0 196 switch (type_) {
HelGast95 73:a91805f9e9f0 197 case null_type:
HelGast95 73:a91805f9e9f0 198 return false;
HelGast95 73:a91805f9e9f0 199 case boolean_type:
HelGast95 73:a91805f9e9f0 200 return boolean_;
HelGast95 73:a91805f9e9f0 201 case number_type:
HelGast95 73:a91805f9e9f0 202 return number_ != 0;
HelGast95 73:a91805f9e9f0 203 case string_type:
HelGast95 73:a91805f9e9f0 204 return ! string_->empty();
HelGast95 73:a91805f9e9f0 205 default:
HelGast95 73:a91805f9e9f0 206 return true;
HelGast95 73:a91805f9e9f0 207 }
HelGast95 73:a91805f9e9f0 208 }
HelGast95 73:a91805f9e9f0 209
HelGast95 73:a91805f9e9f0 210 inline const value& value::get(size_t idx) const {
HelGast95 73:a91805f9e9f0 211 static value s_null;
HelGast95 73:a91805f9e9f0 212 assert(is<array>());
HelGast95 73:a91805f9e9f0 213 return idx < array_->size() ? (*array_)[idx] : s_null;
HelGast95 73:a91805f9e9f0 214 }
HelGast95 73:a91805f9e9f0 215
HelGast95 73:a91805f9e9f0 216 inline const value& value::get(const std::string& key) const {
HelGast95 73:a91805f9e9f0 217 static value s_null;
HelGast95 73:a91805f9e9f0 218 assert(is<object>());
HelGast95 73:a91805f9e9f0 219 object::const_iterator i = object_->find(key);
HelGast95 73:a91805f9e9f0 220 return i != object_->end() ? i->second : s_null;
HelGast95 73:a91805f9e9f0 221 }
HelGast95 73:a91805f9e9f0 222
HelGast95 73:a91805f9e9f0 223 inline std::string value::to_str() const {
HelGast95 73:a91805f9e9f0 224 switch (type_) {
HelGast95 73:a91805f9e9f0 225 case null_type: return "null";
HelGast95 73:a91805f9e9f0 226 case boolean_type: return boolean_ ? "true" : "false";
HelGast95 73:a91805f9e9f0 227 case number_type: {
HelGast95 73:a91805f9e9f0 228 char buf[256];
HelGast95 73:a91805f9e9f0 229 double tmp;
HelGast95 73:a91805f9e9f0 230 SNPRINTF(buf, sizeof(buf), modf(number_, &tmp) == 0 ? "%.f" : "%f", number_);
HelGast95 73:a91805f9e9f0 231 return buf;
HelGast95 73:a91805f9e9f0 232 }
HelGast95 73:a91805f9e9f0 233 case string_type: return *string_;
HelGast95 73:a91805f9e9f0 234 case array_type: return "array";
HelGast95 73:a91805f9e9f0 235 case object_type: return "object";
HelGast95 73:a91805f9e9f0 236 default: assert(0);
HelGast95 73:a91805f9e9f0 237 #ifdef _MSC_VER
HelGast95 73:a91805f9e9f0 238 __assume(0);
HelGast95 73:a91805f9e9f0 239 #endif
HelGast95 73:a91805f9e9f0 240 }
HelGast95 73:a91805f9e9f0 241 }
HelGast95 73:a91805f9e9f0 242
HelGast95 73:a91805f9e9f0 243 template <typename Iter> void copy(const std::string& s, Iter oi) {
HelGast95 73:a91805f9e9f0 244 std::copy(s.begin(), s.end(), oi);
HelGast95 73:a91805f9e9f0 245 }
HelGast95 73:a91805f9e9f0 246
HelGast95 73:a91805f9e9f0 247 template <typename Iter> void serialize_str(const std::string& s, Iter oi) {
HelGast95 73:a91805f9e9f0 248 *oi++ = '"';
HelGast95 73:a91805f9e9f0 249 for (std::string::const_iterator i = s.begin(); i != s.end(); ++i) {
HelGast95 73:a91805f9e9f0 250 switch (*i) {
HelGast95 73:a91805f9e9f0 251 #define MAP(val, sym) case val: copy(sym, oi); break
HelGast95 73:a91805f9e9f0 252 MAP('"', "\\\"");
HelGast95 73:a91805f9e9f0 253 MAP('\\', "\\\\");
HelGast95 73:a91805f9e9f0 254 MAP('/', "\\/");
HelGast95 73:a91805f9e9f0 255 MAP('\b', "\\b");
HelGast95 73:a91805f9e9f0 256 MAP('\f', "\\f");
HelGast95 73:a91805f9e9f0 257 MAP('\n', "\\n");
HelGast95 73:a91805f9e9f0 258 MAP('\r', "\\r");
HelGast95 73:a91805f9e9f0 259 MAP('\t', "\\t");
HelGast95 73:a91805f9e9f0 260 #undef MAP
HelGast95 73:a91805f9e9f0 261 default:
HelGast95 73:a91805f9e9f0 262 if ((unsigned char)*i < 0x20 || *i == 0x7f) {
HelGast95 73:a91805f9e9f0 263 char buf[7];
HelGast95 73:a91805f9e9f0 264 SNPRINTF(buf, sizeof(buf), "\\u%04x", *i & 0xff);
HelGast95 73:a91805f9e9f0 265 copy(buf, buf + 6, oi);
HelGast95 73:a91805f9e9f0 266 } else {
HelGast95 73:a91805f9e9f0 267 *oi++ = *i;
HelGast95 73:a91805f9e9f0 268 }
HelGast95 73:a91805f9e9f0 269 break;
HelGast95 73:a91805f9e9f0 270 }
HelGast95 73:a91805f9e9f0 271 }
HelGast95 73:a91805f9e9f0 272 *oi++ = '"';
HelGast95 73:a91805f9e9f0 273 }
HelGast95 73:a91805f9e9f0 274
HelGast95 73:a91805f9e9f0 275 template <typename Iter> void value::serialize(Iter oi) const {
HelGast95 73:a91805f9e9f0 276 switch (type_) {
HelGast95 73:a91805f9e9f0 277 case string_type:
HelGast95 73:a91805f9e9f0 278 serialize_str(*string_, oi);
HelGast95 73:a91805f9e9f0 279 break;
HelGast95 73:a91805f9e9f0 280 case array_type: {
HelGast95 73:a91805f9e9f0 281 *oi++ = '[';
HelGast95 73:a91805f9e9f0 282 for (array::const_iterator i = array_->begin(); i != array_->end(); ++i) {
HelGast95 73:a91805f9e9f0 283 if (i != array_->begin()) {
HelGast95 73:a91805f9e9f0 284 *oi++ = ',';
HelGast95 73:a91805f9e9f0 285 }
HelGast95 73:a91805f9e9f0 286 i->serialize(oi);
HelGast95 73:a91805f9e9f0 287 }
HelGast95 73:a91805f9e9f0 288 *oi++ = ']';
HelGast95 73:a91805f9e9f0 289 break;
HelGast95 73:a91805f9e9f0 290 }
HelGast95 73:a91805f9e9f0 291 case object_type: {
HelGast95 73:a91805f9e9f0 292 *oi++ = '{';
HelGast95 73:a91805f9e9f0 293 for (object::const_iterator i = object_->begin();
HelGast95 73:a91805f9e9f0 294 i != object_->end();
HelGast95 73:a91805f9e9f0 295 ++i) {
HelGast95 73:a91805f9e9f0 296 if (i != object_->begin()) {
HelGast95 73:a91805f9e9f0 297 *oi++ = ',';
HelGast95 73:a91805f9e9f0 298 }
HelGast95 73:a91805f9e9f0 299 serialize_str(i->first, oi);
HelGast95 73:a91805f9e9f0 300 *oi++ = ':';
HelGast95 73:a91805f9e9f0 301 i->second.serialize(oi);
HelGast95 73:a91805f9e9f0 302 }
HelGast95 73:a91805f9e9f0 303 *oi++ = '}';
HelGast95 73:a91805f9e9f0 304 break;
HelGast95 73:a91805f9e9f0 305 }
HelGast95 73:a91805f9e9f0 306 default:
HelGast95 73:a91805f9e9f0 307 copy(to_str(), oi);
HelGast95 73:a91805f9e9f0 308 break;
HelGast95 73:a91805f9e9f0 309 }
HelGast95 73:a91805f9e9f0 310 }
HelGast95 73:a91805f9e9f0 311
HelGast95 73:a91805f9e9f0 312 inline std::string value::serialize() const {
HelGast95 73:a91805f9e9f0 313 std::string s;
HelGast95 73:a91805f9e9f0 314 serialize(std::back_inserter(s));
HelGast95 73:a91805f9e9f0 315 return s;
HelGast95 73:a91805f9e9f0 316 }
HelGast95 73:a91805f9e9f0 317
HelGast95 73:a91805f9e9f0 318 template <typename Iter> class input {
HelGast95 73:a91805f9e9f0 319 protected:
HelGast95 73:a91805f9e9f0 320 Iter cur_, end_;
HelGast95 73:a91805f9e9f0 321 int last_ch_;
HelGast95 73:a91805f9e9f0 322 bool ungot_;
HelGast95 73:a91805f9e9f0 323 int line_;
HelGast95 73:a91805f9e9f0 324 public:
HelGast95 73:a91805f9e9f0 325 input(const Iter& first, const Iter& last) : cur_(first), end_(last), last_ch_(-1), ungot_(false), line_(1) {}
HelGast95 73:a91805f9e9f0 326 int getc() {
HelGast95 73:a91805f9e9f0 327 if (ungot_) {
HelGast95 73:a91805f9e9f0 328 ungot_ = false;
HelGast95 73:a91805f9e9f0 329 return last_ch_;
HelGast95 73:a91805f9e9f0 330 }
HelGast95 73:a91805f9e9f0 331 if (cur_ == end_) {
HelGast95 73:a91805f9e9f0 332 last_ch_ = -1;
HelGast95 73:a91805f9e9f0 333 return -1;
HelGast95 73:a91805f9e9f0 334 }
HelGast95 73:a91805f9e9f0 335 if (last_ch_ == '\n') {
HelGast95 73:a91805f9e9f0 336 line_++;
HelGast95 73:a91805f9e9f0 337 }
HelGast95 73:a91805f9e9f0 338 last_ch_ = *cur_++ & 0xff;
HelGast95 73:a91805f9e9f0 339 return last_ch_;
HelGast95 73:a91805f9e9f0 340 }
HelGast95 73:a91805f9e9f0 341 void ungetc() {
HelGast95 73:a91805f9e9f0 342 if (last_ch_ != -1) {
HelGast95 73:a91805f9e9f0 343 assert(! ungot_);
HelGast95 73:a91805f9e9f0 344 ungot_ = true;
HelGast95 73:a91805f9e9f0 345 }
HelGast95 73:a91805f9e9f0 346 }
HelGast95 73:a91805f9e9f0 347 Iter cur() const { return cur_; }
HelGast95 73:a91805f9e9f0 348 int line() const { return line_; }
HelGast95 73:a91805f9e9f0 349 void skip_ws() {
HelGast95 73:a91805f9e9f0 350 while (1) {
HelGast95 73:a91805f9e9f0 351 int ch = getc();
HelGast95 73:a91805f9e9f0 352 if (! (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r')) {
HelGast95 73:a91805f9e9f0 353 ungetc();
HelGast95 73:a91805f9e9f0 354 break;
HelGast95 73:a91805f9e9f0 355 }
HelGast95 73:a91805f9e9f0 356 }
HelGast95 73:a91805f9e9f0 357 }
HelGast95 73:a91805f9e9f0 358 int expect(int expect) {
HelGast95 73:a91805f9e9f0 359 skip_ws();
HelGast95 73:a91805f9e9f0 360 if (getc() != expect) {
HelGast95 73:a91805f9e9f0 361 ungetc();
HelGast95 73:a91805f9e9f0 362 return false;
HelGast95 73:a91805f9e9f0 363 }
HelGast95 73:a91805f9e9f0 364 return true;
HelGast95 73:a91805f9e9f0 365 }
HelGast95 73:a91805f9e9f0 366 bool match(const std::string& pattern) {
HelGast95 73:a91805f9e9f0 367 for (std::string::const_iterator pi(pattern.begin());
HelGast95 73:a91805f9e9f0 368 pi != pattern.end();
HelGast95 73:a91805f9e9f0 369 ++pi) {
HelGast95 73:a91805f9e9f0 370 if (getc() != *pi) {
HelGast95 73:a91805f9e9f0 371 ungetc();
HelGast95 73:a91805f9e9f0 372 return false;
HelGast95 73:a91805f9e9f0 373 }
HelGast95 73:a91805f9e9f0 374 }
HelGast95 73:a91805f9e9f0 375 return true;
HelGast95 73:a91805f9e9f0 376 }
HelGast95 73:a91805f9e9f0 377 };
HelGast95 73:a91805f9e9f0 378
HelGast95 73:a91805f9e9f0 379 template<typename Iter> inline int _parse_quadhex(input<Iter> &in) {
HelGast95 73:a91805f9e9f0 380 int uni_ch = 0, hex;
HelGast95 73:a91805f9e9f0 381 for (int i = 0; i < 4; i++) {
HelGast95 73:a91805f9e9f0 382 if ((hex = in.getc()) == -1) {
HelGast95 73:a91805f9e9f0 383 return -1;
HelGast95 73:a91805f9e9f0 384 }
HelGast95 73:a91805f9e9f0 385 if ('0' <= hex && hex <= '9') {
HelGast95 73:a91805f9e9f0 386 hex -= '0';
HelGast95 73:a91805f9e9f0 387 } else if ('A' <= hex && hex <= 'F') {
HelGast95 73:a91805f9e9f0 388 hex -= 'A' - 0xa;
HelGast95 73:a91805f9e9f0 389 } else if ('a' <= hex && hex <= 'f') {
HelGast95 73:a91805f9e9f0 390 hex -= 'a' - 0xa;
HelGast95 73:a91805f9e9f0 391 } else {
HelGast95 73:a91805f9e9f0 392 in.ungetc();
HelGast95 73:a91805f9e9f0 393 return -1;
HelGast95 73:a91805f9e9f0 394 }
HelGast95 73:a91805f9e9f0 395 uni_ch = uni_ch * 16 + hex;
HelGast95 73:a91805f9e9f0 396 }
HelGast95 73:a91805f9e9f0 397 return uni_ch;
HelGast95 73:a91805f9e9f0 398 }
HelGast95 73:a91805f9e9f0 399
HelGast95 73:a91805f9e9f0 400 template<typename Iter> inline bool _parse_codepoint(std::string& out, input<Iter>& in) {
HelGast95 73:a91805f9e9f0 401 int uni_ch;
HelGast95 73:a91805f9e9f0 402 if ((uni_ch = _parse_quadhex(in)) == -1) {
HelGast95 73:a91805f9e9f0 403 return false;
HelGast95 73:a91805f9e9f0 404 }
HelGast95 73:a91805f9e9f0 405 if (0xd800 <= uni_ch && uni_ch <= 0xdfff) {
HelGast95 73:a91805f9e9f0 406 if (0xdc00 <= uni_ch) {
HelGast95 73:a91805f9e9f0 407 // a second 16-bit of a surrogate pair appeared
HelGast95 73:a91805f9e9f0 408 return false;
HelGast95 73:a91805f9e9f0 409 }
HelGast95 73:a91805f9e9f0 410 // first 16-bit of surrogate pair, get the next one
HelGast95 73:a91805f9e9f0 411 if (in.getc() != '\\' || in.getc() != 'u') {
HelGast95 73:a91805f9e9f0 412 in.ungetc();
HelGast95 73:a91805f9e9f0 413 return false;
HelGast95 73:a91805f9e9f0 414 }
HelGast95 73:a91805f9e9f0 415 int second = _parse_quadhex(in);
HelGast95 73:a91805f9e9f0 416 if (! (0xdc00 <= second && second <= 0xdfff)) {
HelGast95 73:a91805f9e9f0 417 return false;
HelGast95 73:a91805f9e9f0 418 }
HelGast95 73:a91805f9e9f0 419 uni_ch = ((uni_ch - 0xd800) << 10) | ((second - 0xdc00) & 0x3ff);
HelGast95 73:a91805f9e9f0 420 uni_ch += 0x10000;
HelGast95 73:a91805f9e9f0 421 }
HelGast95 73:a91805f9e9f0 422 if (uni_ch < 0x80) {
HelGast95 73:a91805f9e9f0 423 out.push_back(uni_ch);
HelGast95 73:a91805f9e9f0 424 } else {
HelGast95 73:a91805f9e9f0 425 if (uni_ch < 0x800) {
HelGast95 73:a91805f9e9f0 426 out.push_back(0xc0 | (uni_ch >> 6));
HelGast95 73:a91805f9e9f0 427 } else {
HelGast95 73:a91805f9e9f0 428 if (uni_ch < 0x10000) {
HelGast95 73:a91805f9e9f0 429 out.push_back(0xe0 | (uni_ch >> 12));
HelGast95 73:a91805f9e9f0 430 } else {
HelGast95 73:a91805f9e9f0 431 out.push_back(0xf0 | (uni_ch >> 18));
HelGast95 73:a91805f9e9f0 432 out.push_back(0x80 | ((uni_ch >> 12) & 0x3f));
HelGast95 73:a91805f9e9f0 433 }
HelGast95 73:a91805f9e9f0 434 out.push_back(0x80 | ((uni_ch >> 6) & 0x3f));
HelGast95 73:a91805f9e9f0 435 }
HelGast95 73:a91805f9e9f0 436 out.push_back(0x80 | (uni_ch & 0x3f));
HelGast95 73:a91805f9e9f0 437 }
HelGast95 73:a91805f9e9f0 438 return true;
HelGast95 73:a91805f9e9f0 439 }
HelGast95 73:a91805f9e9f0 440
HelGast95 73:a91805f9e9f0 441 template<typename Iter> inline bool _parse_string(value& out, input<Iter>& in) {
HelGast95 73:a91805f9e9f0 442 // gcc 4.1 cannot compile if the below two lines are merged into one :-(
HelGast95 73:a91805f9e9f0 443 out = value(string_type, false);
HelGast95 73:a91805f9e9f0 444 std::string& s = out.get<std::string>();
HelGast95 73:a91805f9e9f0 445 while (1) {
HelGast95 73:a91805f9e9f0 446 int ch = in.getc();
HelGast95 73:a91805f9e9f0 447 if (ch < ' ') {
HelGast95 73:a91805f9e9f0 448 in.ungetc();
HelGast95 73:a91805f9e9f0 449 return false;
HelGast95 73:a91805f9e9f0 450 } else if (ch == '"') {
HelGast95 73:a91805f9e9f0 451 return true;
HelGast95 73:a91805f9e9f0 452 } else if (ch == '\\') {
HelGast95 73:a91805f9e9f0 453 if ((ch = in.getc()) == -1) {
HelGast95 73:a91805f9e9f0 454 return false;
HelGast95 73:a91805f9e9f0 455 }
HelGast95 73:a91805f9e9f0 456 switch (ch) {
HelGast95 73:a91805f9e9f0 457 #define MAP(sym, val) case sym: s.push_back(val); break
HelGast95 73:a91805f9e9f0 458 MAP('"', '\"');
HelGast95 73:a91805f9e9f0 459 MAP('\\', '\\');
HelGast95 73:a91805f9e9f0 460 MAP('/', '/');
HelGast95 73:a91805f9e9f0 461 MAP('b', '\b');
HelGast95 73:a91805f9e9f0 462 MAP('f', '\f');
HelGast95 73:a91805f9e9f0 463 MAP('n', '\n');
HelGast95 73:a91805f9e9f0 464 MAP('r', '\r');
HelGast95 73:a91805f9e9f0 465 MAP('t', '\t');
HelGast95 73:a91805f9e9f0 466 #undef MAP
HelGast95 73:a91805f9e9f0 467 case 'u':
HelGast95 73:a91805f9e9f0 468 if (! _parse_codepoint(s, in)) {
HelGast95 73:a91805f9e9f0 469 return false;
HelGast95 73:a91805f9e9f0 470 }
HelGast95 73:a91805f9e9f0 471 break;
HelGast95 73:a91805f9e9f0 472 default:
HelGast95 73:a91805f9e9f0 473 return false;
HelGast95 73:a91805f9e9f0 474 }
HelGast95 73:a91805f9e9f0 475 } else {
HelGast95 73:a91805f9e9f0 476 s.push_back(ch);
HelGast95 73:a91805f9e9f0 477 }
HelGast95 73:a91805f9e9f0 478 }
HelGast95 73:a91805f9e9f0 479 return false;
HelGast95 73:a91805f9e9f0 480 }
HelGast95 73:a91805f9e9f0 481
HelGast95 73:a91805f9e9f0 482 template <typename Iter> inline bool _parse_array(value& out, input<Iter>& in) {
HelGast95 73:a91805f9e9f0 483 out = value(array_type, false);
HelGast95 73:a91805f9e9f0 484 array& a = out.get<array>();
HelGast95 73:a91805f9e9f0 485 if (in.expect(']')) {
HelGast95 73:a91805f9e9f0 486 return true;
HelGast95 73:a91805f9e9f0 487 }
HelGast95 73:a91805f9e9f0 488 do {
HelGast95 73:a91805f9e9f0 489 a.push_back(value());
HelGast95 73:a91805f9e9f0 490 if (! _parse(a.back(), in)) {
HelGast95 73:a91805f9e9f0 491 return false;
HelGast95 73:a91805f9e9f0 492 }
HelGast95 73:a91805f9e9f0 493 } while (in.expect(','));
HelGast95 73:a91805f9e9f0 494 return in.expect(']');
HelGast95 73:a91805f9e9f0 495 }
HelGast95 73:a91805f9e9f0 496
HelGast95 73:a91805f9e9f0 497 template <typename Iter> inline bool _parse_object(value& out, input<Iter>& in) {
HelGast95 73:a91805f9e9f0 498 out = value(object_type, false);
HelGast95 73:a91805f9e9f0 499 object& o = out.get<object>();
HelGast95 73:a91805f9e9f0 500 if (in.expect('}')) {
HelGast95 73:a91805f9e9f0 501 return true;
HelGast95 73:a91805f9e9f0 502 }
HelGast95 73:a91805f9e9f0 503 do {
HelGast95 73:a91805f9e9f0 504 value key, val;
HelGast95 73:a91805f9e9f0 505 if (in.expect('"')
HelGast95 73:a91805f9e9f0 506 && _parse_string(key, in)
HelGast95 73:a91805f9e9f0 507 && in.expect(':')
HelGast95 73:a91805f9e9f0 508 && _parse(val, in)) {
HelGast95 73:a91805f9e9f0 509 o[key.to_str()] = val;
HelGast95 73:a91805f9e9f0 510 } else {
HelGast95 73:a91805f9e9f0 511 return false;
HelGast95 73:a91805f9e9f0 512 }
HelGast95 73:a91805f9e9f0 513 } while (in.expect(','));
HelGast95 73:a91805f9e9f0 514 return in.expect('}');
HelGast95 73:a91805f9e9f0 515 }
HelGast95 73:a91805f9e9f0 516
HelGast95 73:a91805f9e9f0 517 template <typename Iter> inline bool _parse_number(value& out, input<Iter>& in) {
HelGast95 73:a91805f9e9f0 518 std::string num_str;
HelGast95 73:a91805f9e9f0 519 while (1) {
HelGast95 73:a91805f9e9f0 520 int ch = in.getc();
HelGast95 73:a91805f9e9f0 521 if (('0' <= ch && ch <= '9') || ch == '+' || ch == '-' || ch == '.'
HelGast95 73:a91805f9e9f0 522 || ch == 'e' || ch == 'E') {
HelGast95 73:a91805f9e9f0 523 num_str.push_back(ch);
HelGast95 73:a91805f9e9f0 524 } else {
HelGast95 73:a91805f9e9f0 525 in.ungetc();
HelGast95 73:a91805f9e9f0 526 break;
HelGast95 73:a91805f9e9f0 527 }
HelGast95 73:a91805f9e9f0 528 }
HelGast95 73:a91805f9e9f0 529 char* endp;
HelGast95 73:a91805f9e9f0 530 out = value(strtod(num_str.c_str(), &endp));
HelGast95 73:a91805f9e9f0 531 return endp == num_str.c_str() + num_str.size();
HelGast95 73:a91805f9e9f0 532 }
HelGast95 73:a91805f9e9f0 533
HelGast95 73:a91805f9e9f0 534 template <typename Iter> inline bool _parse(value& out, input<Iter>& in) {
HelGast95 73:a91805f9e9f0 535 in.skip_ws();
HelGast95 73:a91805f9e9f0 536 int ch = in.getc();
HelGast95 73:a91805f9e9f0 537 switch (ch) {
HelGast95 73:a91805f9e9f0 538 #define IS(ch, text, val) case ch: \
HelGast95 73:a91805f9e9f0 539 if (in.match(text)) { \
HelGast95 73:a91805f9e9f0 540 out = val; \
HelGast95 73:a91805f9e9f0 541 return true; \
HelGast95 73:a91805f9e9f0 542 } else { \
HelGast95 73:a91805f9e9f0 543 return false; \
HelGast95 73:a91805f9e9f0 544 }
HelGast95 73:a91805f9e9f0 545 IS('n', "ull", value());
HelGast95 73:a91805f9e9f0 546 IS('f', "alse", value(false));
HelGast95 73:a91805f9e9f0 547 IS('t', "rue", value(true));
HelGast95 73:a91805f9e9f0 548 #undef IS
HelGast95 73:a91805f9e9f0 549 case '"':
HelGast95 73:a91805f9e9f0 550 return _parse_string(out, in);
HelGast95 73:a91805f9e9f0 551 case '[':
HelGast95 73:a91805f9e9f0 552 return _parse_array(out, in);
HelGast95 73:a91805f9e9f0 553 case '{':
HelGast95 73:a91805f9e9f0 554 return _parse_object(out, in);
HelGast95 73:a91805f9e9f0 555 default:
HelGast95 73:a91805f9e9f0 556 if (('0' <= ch && ch <= '9') || ch == '-') {
HelGast95 73:a91805f9e9f0 557 in.ungetc();
HelGast95 73:a91805f9e9f0 558 return _parse_number(out, in);
HelGast95 73:a91805f9e9f0 559 }
HelGast95 73:a91805f9e9f0 560 break;
HelGast95 73:a91805f9e9f0 561 }
HelGast95 73:a91805f9e9f0 562 in.ungetc();
HelGast95 73:a91805f9e9f0 563 return false;
HelGast95 73:a91805f9e9f0 564 }
HelGast95 73:a91805f9e9f0 565
HelGast95 73:a91805f9e9f0 566 // obsolete, use the version below
HelGast95 73:a91805f9e9f0 567 template <typename Iter> inline std::string parse(value& out, Iter& pos, const Iter& last) {
HelGast95 73:a91805f9e9f0 568 std::string err;
HelGast95 73:a91805f9e9f0 569 pos = parse(out, pos, last, &err);
HelGast95 73:a91805f9e9f0 570 return err;
HelGast95 73:a91805f9e9f0 571 }
HelGast95 73:a91805f9e9f0 572
HelGast95 73:a91805f9e9f0 573 template <typename Iter> inline Iter parse(value& out, const Iter& first, const Iter& last, std::string* err) {
HelGast95 73:a91805f9e9f0 574 input<Iter> in(first, last);
HelGast95 73:a91805f9e9f0 575 if (! _parse(out, in) && err != NULL) {
HelGast95 73:a91805f9e9f0 576 char buf[64];
HelGast95 73:a91805f9e9f0 577 SNPRINTF(buf, sizeof(buf), "syntax error at line %d near: ", in.line());
HelGast95 73:a91805f9e9f0 578 *err = buf;
HelGast95 73:a91805f9e9f0 579 while (1) {
HelGast95 73:a91805f9e9f0 580 int ch = in.getc();
HelGast95 73:a91805f9e9f0 581 if (ch == -1 || ch == '\n') {
HelGast95 73:a91805f9e9f0 582 break;
HelGast95 73:a91805f9e9f0 583 } else if (ch >= ' ') {
HelGast95 73:a91805f9e9f0 584 err->push_back(ch);
HelGast95 73:a91805f9e9f0 585 }
HelGast95 73:a91805f9e9f0 586 }
HelGast95 73:a91805f9e9f0 587 }
HelGast95 73:a91805f9e9f0 588 return in.cur();
HelGast95 73:a91805f9e9f0 589 }
HelGast95 73:a91805f9e9f0 590
HelGast95 73:a91805f9e9f0 591 inline std::string parse(value& out, std::istream& is) {
HelGast95 73:a91805f9e9f0 592 std::string err;
HelGast95 73:a91805f9e9f0 593 parse(out, std::istreambuf_iterator<char>(is.rdbuf()),
HelGast95 73:a91805f9e9f0 594 std::istreambuf_iterator<char>(), &err);
HelGast95 73:a91805f9e9f0 595 return err;
HelGast95 73:a91805f9e9f0 596 }
HelGast95 73:a91805f9e9f0 597
HelGast95 73:a91805f9e9f0 598 template <typename T> struct last_error_t {
HelGast95 73:a91805f9e9f0 599 static std::string s;
HelGast95 73:a91805f9e9f0 600 };
HelGast95 73:a91805f9e9f0 601 template <typename T> std::string last_error_t<T>::s;
HelGast95 73:a91805f9e9f0 602
HelGast95 73:a91805f9e9f0 603 inline void set_last_error(const std::string& s) {
HelGast95 73:a91805f9e9f0 604 last_error_t<bool>::s = s;
HelGast95 73:a91805f9e9f0 605 }
HelGast95 73:a91805f9e9f0 606
HelGast95 73:a91805f9e9f0 607 inline const std::string& get_last_error() {
HelGast95 73:a91805f9e9f0 608 return last_error_t<bool>::s;
HelGast95 73:a91805f9e9f0 609 }
HelGast95 73:a91805f9e9f0 610
HelGast95 73:a91805f9e9f0 611 inline bool operator==(const value& x, const value& y) {
HelGast95 73:a91805f9e9f0 612 if (x.is<null>())
HelGast95 73:a91805f9e9f0 613 return y.is<null>();
HelGast95 73:a91805f9e9f0 614 #define PICOJSON_CMP(type) \
HelGast95 73:a91805f9e9f0 615 if (x.is<type>()) \
HelGast95 73:a91805f9e9f0 616 return y.is<type>() && x.get<type>() == y.get<type>()
HelGast95 73:a91805f9e9f0 617 PICOJSON_CMP(bool);
HelGast95 73:a91805f9e9f0 618 PICOJSON_CMP(double);
HelGast95 73:a91805f9e9f0 619 PICOJSON_CMP(std::string);
HelGast95 73:a91805f9e9f0 620 PICOJSON_CMP(array);
HelGast95 73:a91805f9e9f0 621 PICOJSON_CMP(object);
HelGast95 73:a91805f9e9f0 622 #undef PICOJSON_CMP
HelGast95 73:a91805f9e9f0 623 assert(0);
HelGast95 73:a91805f9e9f0 624 #ifdef _MSC_VER
HelGast95 73:a91805f9e9f0 625 __assume(0);
HelGast95 73:a91805f9e9f0 626 #endif
HelGast95 73:a91805f9e9f0 627 return false;
HelGast95 73:a91805f9e9f0 628 }
HelGast95 73:a91805f9e9f0 629
HelGast95 73:a91805f9e9f0 630 inline bool operator!=(const value& x, const value& y) {
HelGast95 73:a91805f9e9f0 631 return ! (x == y);
HelGast95 73:a91805f9e9f0 632 }
HelGast95 73:a91805f9e9f0 633 }
HelGast95 73:a91805f9e9f0 634
HelGast95 73:a91805f9e9f0 635 inline std::istream& operator>>(std::istream& is, picojson::value& x)
HelGast95 73:a91805f9e9f0 636 {
HelGast95 73:a91805f9e9f0 637 picojson::set_last_error(std::string());
HelGast95 73:a91805f9e9f0 638 std::string err = picojson::parse(x, is);
HelGast95 73:a91805f9e9f0 639 if (! err.empty()) {
HelGast95 73:a91805f9e9f0 640 picojson::set_last_error(err);
HelGast95 73:a91805f9e9f0 641 is.setstate(std::ios::failbit);
HelGast95 73:a91805f9e9f0 642 }
HelGast95 73:a91805f9e9f0 643 return is;
HelGast95 73:a91805f9e9f0 644 }
HelGast95 73:a91805f9e9f0 645
HelGast95 73:a91805f9e9f0 646 inline std::ostream& operator<<(std::ostream& os, const picojson::value& x)
HelGast95 73:a91805f9e9f0 647 {
HelGast95 73:a91805f9e9f0 648 x.serialize(std::ostream_iterator<char>(os));
HelGast95 73:a91805f9e9f0 649 return os;
HelGast95 73:a91805f9e9f0 650 }
HelGast95 73:a91805f9e9f0 651 #ifdef _MSC_VER
HelGast95 73:a91805f9e9f0 652 #pragma warning(pop)
HelGast95 73:a91805f9e9f0 653 #endif
HelGast95 73:a91805f9e9f0 654
HelGast95 73:a91805f9e9f0 655 #endif
HelGast95 73:a91805f9e9f0 656 #ifdef TEST_PICOJSON
HelGast95 73:a91805f9e9f0 657 #ifdef _MSC_VER
HelGast95 73:a91805f9e9f0 658 #pragma warning(disable : 4127) // conditional expression is constant
HelGast95 73:a91805f9e9f0 659 #endif
HelGast95 73:a91805f9e9f0 660
HelGast95 73:a91805f9e9f0 661 using namespace std;
HelGast95 73:a91805f9e9f0 662
HelGast95 73:a91805f9e9f0 663 static void plan(int num)
HelGast95 73:a91805f9e9f0 664 {
HelGast95 73:a91805f9e9f0 665 printf("1..%d\n", num);
HelGast95 73:a91805f9e9f0 666 }
HelGast95 73:a91805f9e9f0 667
HelGast95 73:a91805f9e9f0 668 static void ok(bool b, const char* name = "")
HelGast95 73:a91805f9e9f0 669 {
HelGast95 73:a91805f9e9f0 670 static int n = 1;
HelGast95 73:a91805f9e9f0 671 printf("%s %d - %s\n", b ? "ok" : "ng", n++, name);
HelGast95 73:a91805f9e9f0 672 }
HelGast95 73:a91805f9e9f0 673
HelGast95 73:a91805f9e9f0 674 template <typename T> void is(const T& x, const T& y, const char* name = "")
HelGast95 73:a91805f9e9f0 675 {
HelGast95 73:a91805f9e9f0 676 if (x == y) {
HelGast95 73:a91805f9e9f0 677 ok(true, name);
HelGast95 73:a91805f9e9f0 678 } else {
HelGast95 73:a91805f9e9f0 679 ok(false, name);
HelGast95 73:a91805f9e9f0 680 }
HelGast95 73:a91805f9e9f0 681 }
HelGast95 73:a91805f9e9f0 682
HelGast95 73:a91805f9e9f0 683 #include <algorithm>
HelGast95 73:a91805f9e9f0 684
HelGast95 73:a91805f9e9f0 685 int main(void)
HelGast95 73:a91805f9e9f0 686 {
HelGast95 73:a91805f9e9f0 687 plan(62);
HelGast95 73:a91805f9e9f0 688
HelGast95 73:a91805f9e9f0 689 #define TEST(in, type, cmp, serialize_test) { \
HelGast95 73:a91805f9e9f0 690 picojson::value v; \
HelGast95 73:a91805f9e9f0 691 const char* s = in; \
HelGast95 73:a91805f9e9f0 692 string err = picojson::parse(v, s, s + strlen(s)); \
HelGast95 73:a91805f9e9f0 693 ok(err.empty(), in " no error"); \
HelGast95 73:a91805f9e9f0 694 ok(v.is<type>(), in " check type"); \
HelGast95 73:a91805f9e9f0 695 is(v.get<type>(), cmp, in " correct output"); \
HelGast95 73:a91805f9e9f0 696 is(*s, '\0', in " read to eof"); \
HelGast95 73:a91805f9e9f0 697 if (serialize_test) { \
HelGast95 73:a91805f9e9f0 698 is(v.serialize(), string(in), in " serialize"); \
HelGast95 73:a91805f9e9f0 699 } \
HelGast95 73:a91805f9e9f0 700 }
HelGast95 73:a91805f9e9f0 701 TEST("false", bool, false, true);
HelGast95 73:a91805f9e9f0 702 TEST("true", bool, true, true);
HelGast95 73:a91805f9e9f0 703 TEST("90.5", double, 90.5, false);
HelGast95 73:a91805f9e9f0 704 TEST("\"hello\"", string, string("hello"), true);
HelGast95 73:a91805f9e9f0 705 TEST("\"\\\"\\\\\\/\\b\\f\\n\\r\\t\"", string, string("\"\\/\b\f\n\r\t"),
HelGast95 73:a91805f9e9f0 706 true);
HelGast95 73:a91805f9e9f0 707 TEST("\"\\u0061\\u30af\\u30ea\\u30b9\"", string,
HelGast95 73:a91805f9e9f0 708 string("a\xe3\x82\xaf\xe3\x83\xaa\xe3\x82\xb9"), false);
HelGast95 73:a91805f9e9f0 709 TEST("\"\\ud840\\udc0b\"", string, string("\xf0\xa0\x80\x8b"), false);
HelGast95 73:a91805f9e9f0 710 #undef TEST
HelGast95 73:a91805f9e9f0 711
HelGast95 73:a91805f9e9f0 712 #define TEST(type, expr) { \
HelGast95 73:a91805f9e9f0 713 picojson::value v; \
HelGast95 73:a91805f9e9f0 714 const char *s = expr; \
HelGast95 73:a91805f9e9f0 715 string err = picojson::parse(v, s, s + strlen(s)); \
HelGast95 73:a91805f9e9f0 716 ok(err.empty(), "empty " #type " no error"); \
HelGast95 73:a91805f9e9f0 717 ok(v.is<picojson::type>(), "empty " #type " check type"); \
HelGast95 73:a91805f9e9f0 718 ok(v.get<picojson::type>().empty(), "check " #type " array size"); \
HelGast95 73:a91805f9e9f0 719 }
HelGast95 73:a91805f9e9f0 720 TEST(array, "[]");
HelGast95 73:a91805f9e9f0 721 TEST(object, "{}");
HelGast95 73:a91805f9e9f0 722 #undef TEST
HelGast95 73:a91805f9e9f0 723
HelGast95 73:a91805f9e9f0 724 {
HelGast95 73:a91805f9e9f0 725 picojson::value v;
HelGast95 73:a91805f9e9f0 726 const char *s = "[1,true,\"hello\"]";
HelGast95 73:a91805f9e9f0 727 string err = picojson::parse(v, s, s + strlen(s));
HelGast95 73:a91805f9e9f0 728 ok(err.empty(), "array no error");
HelGast95 73:a91805f9e9f0 729 ok(v.is<picojson::array>(), "array check type");
HelGast95 73:a91805f9e9f0 730 is(v.get<picojson::array>().size(), size_t(3), "check array size");
HelGast95 73:a91805f9e9f0 731 ok(v.get(0).is<double>(), "check array[0] type");
HelGast95 73:a91805f9e9f0 732 is(v.get(0).get<double>(), 1.0, "check array[0] value");
HelGast95 73:a91805f9e9f0 733 ok(v.get(1).is<bool>(), "check array[1] type");
HelGast95 73:a91805f9e9f0 734 ok(v.get(1).get<bool>(), "check array[1] value");
HelGast95 73:a91805f9e9f0 735 ok(v.get(2).is<string>(), "check array[2] type");
HelGast95 73:a91805f9e9f0 736 is(v.get(2).get<string>(), string("hello"), "check array[2] value");
HelGast95 73:a91805f9e9f0 737 }
HelGast95 73:a91805f9e9f0 738
HelGast95 73:a91805f9e9f0 739 {
HelGast95 73:a91805f9e9f0 740 picojson::value v;
HelGast95 73:a91805f9e9f0 741 const char *s = "{ \"a\": true }";
HelGast95 73:a91805f9e9f0 742 string err = picojson::parse(v, s, s + strlen(s));
HelGast95 73:a91805f9e9f0 743 ok(err.empty(), "object no error");
HelGast95 73:a91805f9e9f0 744 ok(v.is<picojson::object>(), "object check type");
HelGast95 73:a91805f9e9f0 745 is(v.get<picojson::object>().size(), size_t(1), "check object size");
HelGast95 73:a91805f9e9f0 746 ok(v.get("a").is<bool>(), "check property exists");
HelGast95 73:a91805f9e9f0 747 is(v.get("a").get<bool>(), true,
HelGast95 73:a91805f9e9f0 748 "check property value");
HelGast95 73:a91805f9e9f0 749 is(v.serialize(), string("{\"a\":true}"), "serialize object");
HelGast95 73:a91805f9e9f0 750 }
HelGast95 73:a91805f9e9f0 751
HelGast95 73:a91805f9e9f0 752 #define TEST(json, msg) do { \
HelGast95 73:a91805f9e9f0 753 picojson::value v; \
HelGast95 73:a91805f9e9f0 754 const char *s = json; \
HelGast95 73:a91805f9e9f0 755 string err = picojson::parse(v, s, s + strlen(s)); \
HelGast95 73:a91805f9e9f0 756 is(err, string("syntax error at line " msg), msg); \
HelGast95 73:a91805f9e9f0 757 } while (0)
HelGast95 73:a91805f9e9f0 758 TEST("falsoa", "1 near: oa");
HelGast95 73:a91805f9e9f0 759 TEST("{]", "1 near: ]");
HelGast95 73:a91805f9e9f0 760 TEST("\n\bbell", "2 near: bell");
HelGast95 73:a91805f9e9f0 761 TEST("\"abc\nd\"", "1 near: ");
HelGast95 73:a91805f9e9f0 762 #undef TEST
HelGast95 73:a91805f9e9f0 763
HelGast95 73:a91805f9e9f0 764 {
HelGast95 73:a91805f9e9f0 765 picojson::value v1, v2;
HelGast95 73:a91805f9e9f0 766 const char *s;
HelGast95 73:a91805f9e9f0 767 string err;
HelGast95 73:a91805f9e9f0 768 s = "{ \"b\": true, \"a\": [1,2,\"three\"], \"d\": 2 }";
HelGast95 73:a91805f9e9f0 769 err = picojson::parse(v1, s, s + strlen(s));
HelGast95 73:a91805f9e9f0 770 s = "{ \"d\": 2.0, \"b\": true, \"a\": [1,2,\"three\"] }";
HelGast95 73:a91805f9e9f0 771 err = picojson::parse(v2, s, s + strlen(s));
HelGast95 73:a91805f9e9f0 772 ok((v1 == v2), "check == operator in deep comparison");
HelGast95 73:a91805f9e9f0 773 }
HelGast95 73:a91805f9e9f0 774
HelGast95 73:a91805f9e9f0 775 {
HelGast95 73:a91805f9e9f0 776 picojson::value v1, v2;
HelGast95 73:a91805f9e9f0 777 const char *s;
HelGast95 73:a91805f9e9f0 778 string err;
HelGast95 73:a91805f9e9f0 779 s = "{ \"b\": true, \"a\": [1,2,\"three\"], \"d\": 2 }";
HelGast95 73:a91805f9e9f0 780 err = picojson::parse(v1, s, s + strlen(s));
HelGast95 73:a91805f9e9f0 781 s = "{ \"d\": 2.0, \"a\": [1,\"three\"], \"b\": true }";
HelGast95 73:a91805f9e9f0 782 err = picojson::parse(v2, s, s + strlen(s));
HelGast95 73:a91805f9e9f0 783 ok((v1 != v2), "check != operator for array in deep comparison");
HelGast95 73:a91805f9e9f0 784 }
HelGast95 73:a91805f9e9f0 785
HelGast95 73:a91805f9e9f0 786 {
HelGast95 73:a91805f9e9f0 787 picojson::value v1, v2;
HelGast95 73:a91805f9e9f0 788 const char *s;
HelGast95 73:a91805f9e9f0 789 string err;
HelGast95 73:a91805f9e9f0 790 s = "{ \"b\": true, \"a\": [1,2,\"three\"], \"d\": 2 }";
HelGast95 73:a91805f9e9f0 791 err = picojson::parse(v1, s, s + strlen(s));
HelGast95 73:a91805f9e9f0 792 s = "{ \"d\": 2.0, \"a\": [1,2,\"three\"], \"b\": false }";
HelGast95 73:a91805f9e9f0 793 err = picojson::parse(v2, s, s + strlen(s));
HelGast95 73:a91805f9e9f0 794 ok((v1 != v2), "check != operator for object in deep comparison");
HelGast95 73:a91805f9e9f0 795 }
HelGast95 73:a91805f9e9f0 796
HelGast95 73:a91805f9e9f0 797 {
HelGast95 73:a91805f9e9f0 798 picojson::value v1, v2;
HelGast95 73:a91805f9e9f0 799 const char *s;
HelGast95 73:a91805f9e9f0 800 string err;
HelGast95 73:a91805f9e9f0 801 s = "{ \"b\": true, \"a\": [1,2,\"three\"], \"d\": 2 }";
HelGast95 73:a91805f9e9f0 802 err = picojson::parse(v1, s, s + strlen(s));
HelGast95 73:a91805f9e9f0 803 picojson::object& o = v1.get<picojson::object>();
HelGast95 73:a91805f9e9f0 804 o.erase("b");
HelGast95 73:a91805f9e9f0 805 picojson::array& a = o["a"].get<picojson::array>();
HelGast95 73:a91805f9e9f0 806 picojson::array::iterator i;
HelGast95 73:a91805f9e9f0 807 i = std::remove(a.begin(), a.end(), picojson::value(std::string("three")));
HelGast95 73:a91805f9e9f0 808 a.erase(i, a.end());
HelGast95 73:a91805f9e9f0 809 s = "{ \"a\": [1,2], \"d\": 2 }";
HelGast95 73:a91805f9e9f0 810 err = picojson::parse(v2, s, s + strlen(s));
HelGast95 73:a91805f9e9f0 811 ok((v1 == v2), "check erase()");
HelGast95 73:a91805f9e9f0 812 }
HelGast95 73:a91805f9e9f0 813
HelGast95 73:a91805f9e9f0 814 ok(picojson::value(3.0).serialize() == "3",
HelGast95 73:a91805f9e9f0 815 "integral number should be serialized as a integer");
HelGast95 73:a91805f9e9f0 816
HelGast95 73:a91805f9e9f0 817 return 0;
HelGast95 73:a91805f9e9f0 818 }
HelGast95 73:a91805f9e9f0 819
HelGast95 73:a91805f9e9f0 820 #endif