[286] | 1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
| 2 | // Copyright (C) 1999-2015 Maciej Komosinski and Szymon Ulatowski. |
---|
| 3 | // See LICENSE.txt for details. |
---|
[109] | 4 | |
---|
| 5 | #include "extvalue.h" |
---|
| 6 | #include <frams/param/param.h> |
---|
| 7 | #include "sstringutils.h" |
---|
| 8 | #include <ctype.h> |
---|
| 9 | #include <frams/vm/classes/collectionobj.h> |
---|
| 10 | #include <frams/vm/classes/3dobject.h> |
---|
[205] | 11 | #include <frams/vm/classes/genoobj.h> |
---|
[109] | 12 | #include <common/nonstd_math.h> |
---|
| 13 | #include <common/Convert.h> |
---|
[325] | 14 | #include <climits> |
---|
[333] | 15 | #include <errno.h> |
---|
[109] | 16 | |
---|
| 17 | #ifndef NO_BARRIER |
---|
| 18 | #include <frams/simul/barrier.h> |
---|
| 19 | #include <common/threads.h> |
---|
| 20 | #endif |
---|
| 21 | |
---|
| 22 | #ifdef MULTITHREADED |
---|
| 23 | #include <pthread.h> |
---|
| 24 | //this lock only protects against ref.counter corruption caused by concurrent reads. |
---|
| 25 | //read/write conficts and nonatomicity are handled by BarrierObject (at least in theory ;-)) |
---|
[337] | 26 | static pthread_mutex_t extobject_ref_lock = PTHREAD_MUTEX_INITIALIZER; |
---|
[109] | 27 | #define REF_LOCK pthread_mutex_lock(&extobject_ref_lock) |
---|
| 28 | #define REF_UNLOCK pthread_mutex_unlock(&extobject_ref_lock) |
---|
| 29 | #else |
---|
| 30 | #define REF_LOCK |
---|
| 31 | #define REF_UNLOCK |
---|
| 32 | #endif |
---|
| 33 | |
---|
| 34 | void ExtObject::incref() const |
---|
| 35 | { |
---|
[337] | 36 | if (subtype & 1) |
---|
[109] | 37 | { |
---|
[337] | 38 | REF_LOCK; |
---|
| 39 | dbobject->refcount++; |
---|
| 40 | REF_UNLOCK; |
---|
[109] | 41 | } |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | void ExtObject::decref() const |
---|
| 45 | { |
---|
[337] | 46 | if (subtype & 1) |
---|
[109] | 47 | { |
---|
[337] | 48 | REF_LOCK; |
---|
| 49 | bool destroy = !--dbobject->refcount; |
---|
| 50 | REF_UNLOCK; |
---|
| 51 | //another thread can now access the object while we are deleting it |
---|
| 52 | //but this is not a bug since we only guarantee read/read safety |
---|
| 53 | if (destroy) delete dbobject; |
---|
[109] | 54 | } |
---|
| 55 | } |
---|
| 56 | |
---|
[257] | 57 | bool ExtObject::operator==(const ExtObject& src) const |
---|
| 58 | { |
---|
[337] | 59 | if (object != src.object) return false; |
---|
| 60 | const char* n1 = interfaceName(); |
---|
| 61 | const char* n2 = src.interfaceName(); |
---|
| 62 | return (n1 == n2) || (strcmp(n1, n2) == 0); |
---|
[257] | 63 | } |
---|
| 64 | |
---|
[109] | 65 | bool ExtObject::makeUnique() |
---|
| 66 | { |
---|
[337] | 67 | if (!(subtype & 1)) return false; |
---|
| 68 | if (dbobject->refcount == 1) return false; |
---|
| 69 | VectorObject* v = VectorObject::fromObject(*this, false); |
---|
| 70 | if (v) |
---|
[109] | 71 | { |
---|
[337] | 72 | VectorObject* n = new VectorObject; |
---|
| 73 | n->data.setSize(n->data.size()); |
---|
| 74 | for (int i = 0; i < v->data.size(); i++) |
---|
[109] | 75 | { |
---|
[337] | 76 | ExtValue *x = (ExtValue*)v->data(i); |
---|
| 77 | n->data.set(i, x ? new ExtValue(*x) : NULL); |
---|
[109] | 78 | } |
---|
[337] | 79 | operator=(n->makeObject()); |
---|
| 80 | return true; |
---|
[109] | 81 | } |
---|
[337] | 82 | return false; |
---|
[109] | 83 | } |
---|
| 84 | |
---|
[171] | 85 | void* ExtObject::getTarget(const char* classname, bool through_barrier, bool warn) const |
---|
[109] | 86 | { |
---|
[337] | 87 | if (!strcmp(interfaceName(), classname)) |
---|
| 88 | return getTarget(); |
---|
[109] | 89 | #ifndef NO_BARRIER |
---|
[337] | 90 | if (through_barrier) |
---|
[109] | 91 | { |
---|
[337] | 92 | BarrierObject *bo = BarrierObject::fromObject(*this); |
---|
| 93 | if (bo) |
---|
| 94 | return bo->getSourceObject().getTarget(classname, true, warn); |
---|
[109] | 95 | } |
---|
| 96 | #endif |
---|
[171] | 97 | |
---|
[337] | 98 | if (warn) |
---|
[171] | 99 | { |
---|
[375] | 100 | logPrintf("ExtValue", "getObjectTarget", LOG_WARN, "%s object expected, %s found", classname, interfaceName()); |
---|
[171] | 101 | } |
---|
| 102 | |
---|
[337] | 103 | return NULL; |
---|
[109] | 104 | } |
---|
| 105 | |
---|
| 106 | |
---|
| 107 | SString ExtObject::toString() const |
---|
| 108 | { |
---|
[337] | 109 | if (isEmpty()) return SString("null"); |
---|
| 110 | Param tmp_param; |
---|
| 111 | ParamInterface *p = getParamInterface(tmp_param); |
---|
| 112 | int tostr = p->findId("toString"); |
---|
| 113 | if (tostr >= 0) |
---|
[109] | 114 | { |
---|
[337] | 115 | return SString(p->getString(tostr)); |
---|
[109] | 116 | } |
---|
[337] | 117 | else |
---|
[109] | 118 | { |
---|
[337] | 119 | SString tmp("<"); |
---|
| 120 | tmp += p->getName(); |
---|
| 121 | tmp += SString::sprintf(" object at %p>", object ? object : paraminterface); |
---|
| 122 | return tmp; |
---|
[109] | 123 | } |
---|
| 124 | } |
---|
| 125 | |
---|
[371] | 126 | THREAD_LOCAL_DEF(ExtObject::Serialization, ExtObject_serialization); |
---|
[109] | 127 | |
---|
| 128 | void ExtObject::Serialization::begin() |
---|
| 129 | { |
---|
[337] | 130 | if (level == 0) |
---|
| 131 | refs.clear(); |
---|
| 132 | level++; |
---|
[109] | 133 | } |
---|
| 134 | |
---|
| 135 | int ExtObject::Serialization::add(const ExtObject &o) |
---|
| 136 | { |
---|
[337] | 137 | if (o.isEmpty()) return -1; |
---|
| 138 | for (int i = 0; i < (int)refs.size(); i++) |
---|
[109] | 139 | { |
---|
[337] | 140 | ExtObject& r = refs[i]; |
---|
| 141 | if (r == o) return i; |
---|
[109] | 142 | } |
---|
[337] | 143 | refs.push_back(o); |
---|
| 144 | return -1; |
---|
[109] | 145 | } |
---|
| 146 | |
---|
[337] | 147 | void ExtObject::Serialization::replace(const ExtObject& o, const ExtObject& other) |
---|
[109] | 148 | { |
---|
[337] | 149 | if (o.isEmpty()) return; |
---|
| 150 | for (int i = 0; i < (int)refs.size(); i++) |
---|
[109] | 151 | { |
---|
[337] | 152 | ExtObject& r = refs[i]; |
---|
| 153 | if (r == o) |
---|
[109] | 154 | { |
---|
[337] | 155 | r = other; |
---|
| 156 | return; |
---|
[109] | 157 | } |
---|
| 158 | } |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | void ExtObject::Serialization::remove(const ExtObject& o) |
---|
| 162 | { |
---|
[337] | 163 | if (o.isEmpty()) return; |
---|
| 164 | for (int i = 0; i < (int)refs.size(); i++) |
---|
[109] | 165 | { |
---|
[337] | 166 | ExtObject& r = refs[i]; |
---|
| 167 | if (o == r) refs.erase(refs.begin() + i); |
---|
[109] | 168 | } |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | const ExtObject* ExtObject::Serialization::get(int ref) |
---|
| 172 | { |
---|
[337] | 173 | if (ref < 0) return NULL; |
---|
| 174 | if (ref >= (int)refs.size()) return NULL; |
---|
| 175 | return &refs[ref]; |
---|
[109] | 176 | } |
---|
| 177 | |
---|
| 178 | void ExtObject::Serialization::end() |
---|
| 179 | { |
---|
[337] | 180 | level--; |
---|
| 181 | if (level == 0) |
---|
| 182 | refs.clear(); |
---|
[109] | 183 | } |
---|
| 184 | |
---|
[464] | 185 | SString ExtObject::serialize_inner(SerializationFormat format) const |
---|
[109] | 186 | { |
---|
[371] | 187 | int ref = tlsGetRef(ExtObject_serialization).add(*this); |
---|
[464] | 188 | SString ret; |
---|
| 189 | |
---|
[337] | 190 | if (ref >= 0) |
---|
[464] | 191 | { |
---|
| 192 | switch(format) |
---|
| 193 | { |
---|
| 194 | case NativeSerialization: return SString::sprintf("^%d", ref); |
---|
| 195 | case JSONSerialization: return SString("null"); |
---|
| 196 | } |
---|
| 197 | } |
---|
[109] | 198 | |
---|
[337] | 199 | if (isEmpty()) return SString("null"); |
---|
[464] | 200 | { |
---|
[337] | 201 | VectorObject *vec = VectorObject::fromObject(*this, false); |
---|
| 202 | if (vec) |
---|
[464] | 203 | { ret=vec->serialize(format); goto finally; } |
---|
| 204 | } |
---|
| 205 | { |
---|
[337] | 206 | DictionaryObject *dic = DictionaryObject::fromObject(*this, false); |
---|
| 207 | if (dic) |
---|
[464] | 208 | { ret=dic->serialize(format); goto finally; } |
---|
| 209 | } |
---|
| 210 | { |
---|
[337] | 211 | Param tmp_param; |
---|
| 212 | ParamInterface *p = getParamInterface(tmp_param); |
---|
| 213 | int m = p->findId("toVector"); |
---|
| 214 | if (m < 0) |
---|
| 215 | m = p->findId("toDictionary"); |
---|
| 216 | if (m >= 0) |
---|
[109] | 217 | { |
---|
[337] | 218 | ExtObject o(p->getObject(m)); |
---|
[464] | 219 | switch(format) |
---|
| 220 | { |
---|
| 221 | case NativeSerialization: ret=SString(interfaceName())+o.serialize(format); break; |
---|
| 222 | case JSONSerialization: ret=SString::sprintf("{\"class\":\"%s\",\"data\":%s}",interfaceName(),o.serialize(format).c_str()); break; |
---|
| 223 | } |
---|
| 224 | goto finally; |
---|
[109] | 225 | } |
---|
[337] | 226 | m = p->findId("toString"); |
---|
| 227 | if (m >= 0) |
---|
[109] | 228 | { |
---|
[337] | 229 | SString str = p->getString(m); |
---|
| 230 | sstringQuote(str); |
---|
[464] | 231 | switch(format) |
---|
| 232 | { |
---|
| 233 | case NativeSerialization: ret=SString(interfaceName())+"\"" +str+"\""; break; |
---|
| 234 | case JSONSerialization: ret=SString::sprintf("{\"class\":\"%s\",\"data\":\"%s\"}",interfaceName(),str.c_str()); break; |
---|
| 235 | } |
---|
| 236 | goto finally; |
---|
[109] | 237 | } |
---|
[464] | 238 | } |
---|
[109] | 239 | |
---|
[371] | 240 | tlsGetRef(ExtObject_serialization).remove(*this);//undo nonserializable reference |
---|
[464] | 241 | switch(format) |
---|
| 242 | { |
---|
| 243 | case NativeSerialization: return SString(interfaceName())+SString::sprintf("<%p>",object ? object : paraminterface); break; |
---|
| 244 | case JSONSerialization: return SString::sprintf("{\"class\":\"%s\"}",interfaceName()); break; |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | finally: // not 100% "finally", the case of nonserializable reference (directly above) returns directly without going through finally |
---|
| 248 | |
---|
| 249 | switch(format) |
---|
| 250 | { |
---|
| 251 | case JSONSerialization: |
---|
| 252 | tlsGetRef(ExtObject_serialization).remove(*this);//JSON only tracks recursion, does not track reuse |
---|
| 253 | break; |
---|
[465] | 254 | case NativeSerialization:; //nop (avoid warning) |
---|
[464] | 255 | } |
---|
| 256 | |
---|
[337] | 257 | return ret; |
---|
[109] | 258 | } |
---|
| 259 | |
---|
[464] | 260 | SString ExtObject::serialize(SerializationFormat format) const |
---|
[109] | 261 | { |
---|
[371] | 262 | tlsGetRef(ExtObject_serialization).begin(); |
---|
[464] | 263 | SString ret = serialize_inner(format); |
---|
[371] | 264 | tlsGetRef(ExtObject_serialization).end(); |
---|
[337] | 265 | return ret; |
---|
[109] | 266 | } |
---|
| 267 | |
---|
| 268 | /////////////////////////////////////// |
---|
| 269 | |
---|
[228] | 270 | SString ExtValue::typeDescription() const |
---|
| 271 | { |
---|
[337] | 272 | switch (type) |
---|
[228] | 273 | { |
---|
[337] | 274 | case TInt: return SString("int"); |
---|
| 275 | case TDouble: return SString("float"); |
---|
| 276 | case TString: return SString("string"); |
---|
| 277 | case TUnknown: return SString("null"); |
---|
| 278 | case TInvalid: return SString("invalid"); |
---|
| 279 | case TObj: return getObject().isEmpty() ? SString("null") : SString(getObject().interfaceName()); |
---|
[228] | 280 | } |
---|
[337] | 281 | return SString::empty(); |
---|
[228] | 282 | } |
---|
| 283 | |
---|
[337] | 284 | SString ExtValue::typeAndValue() const |
---|
[171] | 285 | { |
---|
[337] | 286 | SString msg = typeDescription(); |
---|
| 287 | switch (type) |
---|
[171] | 288 | { |
---|
[337] | 289 | case TInt: case TDouble: case TString: case TObj: |
---|
| 290 | msg += " '"; |
---|
| 291 | msg += getString(); |
---|
| 292 | msg += "'"; |
---|
| 293 | default:; |
---|
| 294 | } |
---|
| 295 | return msg; |
---|
| 296 | } |
---|
| 297 | |
---|
| 298 | void *ExtValue::getObjectTarget(const char* classname, bool warn) const |
---|
| 299 | { |
---|
| 300 | if (type != TObj) |
---|
| 301 | { |
---|
| 302 | if (warn) |
---|
[171] | 303 | { |
---|
[337] | 304 | SString tmp = getString(); |
---|
| 305 | if (tmp.len() > 30) tmp = tmp.substr(0, 30) + "..."; |
---|
| 306 | if (type == TString) tmp = SString("\"") + tmp + SString("\""); |
---|
[375] | 307 | logPrintf("ExtValue", "getObjectTarget", LOG_WARN, "%s object expected, %s found", classname, tmp.c_str()); |
---|
[171] | 308 | } |
---|
[337] | 309 | return NULL; |
---|
[171] | 310 | } |
---|
| 311 | |
---|
[337] | 312 | return getObject().getTarget(classname, true, warn); |
---|
[171] | 313 | } |
---|
| 314 | |
---|
[109] | 315 | void ExtValue::set(const ExtValue& src) |
---|
| 316 | { |
---|
[337] | 317 | switch (src.type) |
---|
[109] | 318 | { |
---|
| 319 | case TString: sets(src.sdata()); break; |
---|
| 320 | case TInt: seti(src.idata()); break; |
---|
| 321 | case TDouble: setd(src.ddata()); break; |
---|
| 322 | case TObj: seto(src.odata()); break; |
---|
[337] | 323 | default:type = src.type; break; |
---|
[109] | 324 | } |
---|
| 325 | } |
---|
| 326 | |
---|
| 327 | void ExtValue::setEmpty() |
---|
| 328 | { |
---|
[337] | 329 | switch (type) |
---|
[109] | 330 | { |
---|
| 331 | #ifdef EXTVALUEUNION |
---|
| 332 | case TString: sdata().~SString(); break; |
---|
| 333 | case TObj: odata().~ExtObject(); break; |
---|
| 334 | #else |
---|
| 335 | case TString: delete s; break; |
---|
| 336 | case TObj: delete o; break; |
---|
| 337 | #endif |
---|
| 338 | default:; |
---|
| 339 | } |
---|
[337] | 340 | type = TUnknown; |
---|
[109] | 341 | } |
---|
| 342 | |
---|
[333] | 343 | static ExtValue::CompareResult longsign(paInt x) |
---|
[109] | 344 | { |
---|
[337] | 345 | if (x < 0) return ExtValue::ResultLower; |
---|
| 346 | if (x > 0) return ExtValue::ResultHigher; |
---|
| 347 | return ExtValue::ResultEqual; |
---|
[109] | 348 | } |
---|
| 349 | |
---|
[333] | 350 | static ExtValue::CompareResult compareNull(const ExtValue& v) |
---|
[144] | 351 | { |
---|
[337] | 352 | if (v.isNull()) return ExtValue::ResultEqualUnordered; |
---|
| 353 | if ((v.getType() == TInt) && (v.getInt() == 0)) return ExtValue::ResultUnequal_RelaxedEqual; |
---|
| 354 | return ExtValue::ResultUnequal_RelaxedUnequal; //comparing anything else with null is valid but null is neither higher nor lower than numbers or strings |
---|
[144] | 355 | } |
---|
| 356 | |
---|
[337] | 357 | static ExtValue::CompareResult compareFloat(double a, double b) |
---|
[109] | 358 | { |
---|
[337] | 359 | double t = a - b; |
---|
| 360 | if (t < 0) return ExtValue::ResultLower; |
---|
| 361 | else if (t > 0) return ExtValue::ResultHigher; |
---|
| 362 | return ExtValue::ResultEqual; |
---|
[333] | 363 | } |
---|
| 364 | |
---|
[337] | 365 | static ExtValue::CompareResult compareString(const SString &a, const SString &b) |
---|
[333] | 366 | { |
---|
[348] | 367 | const char* s1 = a.c_str(); |
---|
| 368 | const char* s2 = b.c_str(); |
---|
[337] | 369 | return longsign(strcmp(s1, s2)); |
---|
[333] | 370 | } |
---|
| 371 | |
---|
| 372 | ExtValue::CompareResult ExtValue::compare(const ExtValue& src) const |
---|
| 373 | { |
---|
[337] | 374 | if (isNull()) |
---|
| 375 | return compareNull(src); |
---|
| 376 | else if (src.isNull()) |
---|
| 377 | return compareNull(*this); |
---|
| 378 | switch (type) |
---|
[109] | 379 | { |
---|
[333] | 380 | |
---|
[109] | 381 | case TInt: |
---|
[333] | 382 | |
---|
[337] | 383 | if (src.getType() == TInt) |
---|
| 384 | { |
---|
| 385 | paInt t = src.getInt(); |
---|
| 386 | if (idata() > 0) |
---|
[333] | 387 | { |
---|
[337] | 388 | if (t > 0) return longsign(idata() - t); else return ResultHigher; |
---|
| 389 | } |
---|
[333] | 390 | else |
---|
[337] | 391 | { |
---|
| 392 | if (t <= 0) return longsign(idata() - t); else return ResultLower; |
---|
[333] | 393 | } |
---|
[337] | 394 | } |
---|
| 395 | else if (src.getType() == TDouble) |
---|
| 396 | return compareFloat((double)idata(), src.getDouble()); |
---|
[333] | 397 | else |
---|
| 398 | return ResultMismatch;//comparing numbers with other things is invalid |
---|
| 399 | break; |
---|
| 400 | |
---|
[109] | 401 | case TDouble: |
---|
[337] | 402 | if ((src.getType() == TDouble) || (src.getType() == TInt)) |
---|
| 403 | return compareFloat(getDouble(), src.getDouble()); |
---|
[333] | 404 | else |
---|
| 405 | return ResultMismatch; |
---|
| 406 | break; |
---|
| 407 | |
---|
[109] | 408 | case TString: |
---|
[337] | 409 | if (src.getType() == TString) |
---|
| 410 | return compareString(sdata(), src.getString()); |
---|
[333] | 411 | else |
---|
| 412 | return ResultMismatch; |
---|
| 413 | break; |
---|
| 414 | |
---|
[337] | 415 | case TObj: |
---|
[109] | 416 | { |
---|
[337] | 417 | if (src.type == TObj) |
---|
| 418 | return odata() == src.odata() ? ResultEqualUnordered : ResultUnequal_RelaxedUnequal; |
---|
| 419 | if ((src.type == TInt) && (src.getInt() == 0)) |
---|
| 420 | return ResultMismatch_RelaxedUnequal; |
---|
| 421 | return ResultMismatch; |
---|
[109] | 422 | } |
---|
| 423 | default:; |
---|
| 424 | } |
---|
[337] | 425 | return ResultMismatch; |
---|
[109] | 426 | } |
---|
| 427 | |
---|
[337] | 428 | const char* ExtValue::cmp_op_names[] = { "==", "!=", ">=", "<=", ">", "<", "~=", "!~", NULL }; |
---|
[333] | 429 | |
---|
[337] | 430 | int ExtValue::interpretCompare(CmpOperator op, CompareResult result, CmpContext *context) |
---|
[333] | 431 | { |
---|
[337] | 432 | CompareResult error_threshold = ResultUnequal_RelaxedEqual;//error when ResultUnequal_RelaxedEqual or higher (not comparable) |
---|
| 433 | int ret = 0; |
---|
| 434 | switch (op) |
---|
[333] | 435 | { |
---|
[337] | 436 | case CmpEQ: ret = (result == ResultEqual) || (result == ResultEqualUnordered); error_threshold = ResultMismatch_RelaxedUnequal; break; |
---|
| 437 | case CmpNE: ret = !((result == ResultEqual) || (result == ResultEqualUnordered)); error_threshold = ResultMismatch_RelaxedUnequal; break; |
---|
| 438 | case CmpGT: ret = (result == ResultHigher); error_threshold = ResultEqualUnordered; break; |
---|
| 439 | case CmpGE: ret = (result == ResultEqual) || (result == ResultHigher); error_threshold = ResultEqualUnordered; break; |
---|
| 440 | case CmpLT: ret = (result == ResultLower); error_threshold = ResultEqualUnordered; break; |
---|
| 441 | case CmpLE: ret = (result == ResultEqual) || (result == ResultLower); error_threshold = ResultEqualUnordered; break; |
---|
| 442 | case CmpREQ: ret = (result == ResultEqual) || (result == ResultEqualUnordered) || (result == ResultUnequal_RelaxedEqual); error_threshold = ResultMismatch; break; |
---|
| 443 | case CmpRNE: ret = !((result == ResultEqual) || (result == ResultEqualUnordered) || (result == ResultUnequal_RelaxedEqual)); error_threshold = ResultMismatch; break; |
---|
[333] | 444 | default:; |
---|
| 445 | } |
---|
[337] | 446 | if (result >= error_threshold) |
---|
[333] | 447 | { |
---|
[337] | 448 | SString msg = "Type mismatch while comparing"; |
---|
| 449 | if (context) |
---|
[333] | 450 | { |
---|
[337] | 451 | if (context->v1 && context->v2) |
---|
| 452 | msg += SString::sprintf(": %s %s %s", |
---|
[348] | 453 | context->v1->typeAndValue().c_str(), |
---|
[337] | 454 | cmp_op_names[op - CmpFIRST], |
---|
[348] | 455 | context->v2->typeAndValue().c_str()); |
---|
[333] | 456 | } |
---|
[375] | 457 | logPrintf("ExtValue", "interpretCompare", LOG_ERROR, "%s", msg.c_str()); |
---|
[337] | 458 | ret = -1; |
---|
[333] | 459 | } |
---|
[337] | 460 | return ret; |
---|
[333] | 461 | } |
---|
| 462 | |
---|
[109] | 463 | int ExtValue::operator==(const ExtValue& src) const |
---|
| 464 | { |
---|
[337] | 465 | if (type != src.type) return 0; |
---|
| 466 | switch (type) |
---|
[109] | 467 | { |
---|
[337] | 468 | case TInt: return idata() == src.idata(); |
---|
| 469 | case TDouble: return ddata() == src.ddata(); |
---|
| 470 | case TString: return sdata() == src.sdata(); |
---|
| 471 | case TObj: return odata() == src.odata(); |
---|
[109] | 472 | default:; |
---|
| 473 | } |
---|
[337] | 474 | return 1; |
---|
[109] | 475 | } |
---|
| 476 | |
---|
| 477 | void ExtValue::operator+=(const ExtValue& src) |
---|
| 478 | { |
---|
[337] | 479 | // return = ok, break = fail |
---|
| 480 | switch (type) |
---|
[109] | 481 | { |
---|
[337] | 482 | case TInt: |
---|
| 483 | switch (src.getType()) |
---|
| 484 | { |
---|
| 485 | case TDouble: |
---|
| 486 | setDouble(double(getInt()) + src.getDouble()); |
---|
| 487 | return; |
---|
| 488 | case TString: |
---|
| 489 | break; |
---|
| 490 | default: |
---|
| 491 | idata() += src.getInt(); |
---|
| 492 | return; |
---|
| 493 | } |
---|
| 494 | break; |
---|
| 495 | case TDouble: |
---|
| 496 | switch (src.getType()) |
---|
| 497 | { |
---|
| 498 | case TString: |
---|
| 499 | break; |
---|
| 500 | default: |
---|
| 501 | ddata() += src.getDouble(); |
---|
| 502 | return; |
---|
| 503 | } |
---|
| 504 | break; |
---|
| 505 | case TString: sdata() += src.getString(); return; |
---|
[109] | 506 | case TObj: |
---|
[337] | 507 | { |
---|
| 508 | VectorObject *vec = VectorObject::fromObject(getObject(), false); |
---|
| 509 | VectorObject *vec2 = VectorObject::fromObject(src.getObject(), false); |
---|
| 510 | if (vec && vec2) |
---|
[109] | 511 | { |
---|
[337] | 512 | for (int i = 0; i < vec2->data.size(); i++) |
---|
[109] | 513 | { |
---|
[337] | 514 | ExtValue *s = (ExtValue*)vec2->data(i); |
---|
| 515 | ExtValue *d = s ? new ExtValue(*s) : NULL; |
---|
| 516 | vec->data += d; |
---|
| 517 | } |
---|
[228] | 518 | return; |
---|
[109] | 519 | } |
---|
[337] | 520 | } |
---|
[228] | 521 | //NO break; |
---|
[109] | 522 | default:; |
---|
| 523 | } |
---|
[375] | 524 | logPrintf("ExtValue", "add", LOG_ERROR, "Can't add %s to %s", src.typeAndValue().c_str(), typeAndValue().c_str()); |
---|
[109] | 525 | } |
---|
| 526 | |
---|
| 527 | void ExtValue::operator-=(const ExtValue& src) |
---|
| 528 | { |
---|
[337] | 529 | // return = ok, break = fail |
---|
| 530 | switch (type) |
---|
[109] | 531 | { |
---|
[337] | 532 | case TInt: |
---|
| 533 | switch (src.getType()) |
---|
| 534 | { |
---|
| 535 | case TInt: |
---|
| 536 | idata() -= src.getInt(); |
---|
| 537 | return; |
---|
| 538 | case TDouble: |
---|
| 539 | setDouble(double(getInt()) - src.getDouble()); |
---|
| 540 | return; |
---|
| 541 | default:; |
---|
| 542 | } |
---|
[228] | 543 | break; |
---|
[337] | 544 | case TDouble: |
---|
| 545 | switch (src.getType()) |
---|
| 546 | { |
---|
| 547 | case TDouble: |
---|
| 548 | case TInt: |
---|
| 549 | ddata() -= src.getDouble(); |
---|
| 550 | return; |
---|
| 551 | default:; |
---|
| 552 | } |
---|
| 553 | break; |
---|
[109] | 554 | default:; |
---|
| 555 | } |
---|
[375] | 556 | logPrintf("ExtValue", "subtract", LOG_ERROR, "Can't subtract %s from %s", src.typeAndValue().c_str(), typeAndValue().c_str()); |
---|
[109] | 557 | } |
---|
| 558 | |
---|
| 559 | void ExtValue::operator*=(const ExtValue& src) |
---|
| 560 | { |
---|
[337] | 561 | // return = ok, break = fail |
---|
| 562 | switch (type) |
---|
[109] | 563 | { |
---|
[337] | 564 | case TInt: |
---|
| 565 | switch (src.getType()) |
---|
| 566 | { |
---|
| 567 | case TInt: |
---|
| 568 | idata() *= src.getInt(); |
---|
| 569 | return; |
---|
| 570 | case TDouble: |
---|
| 571 | setDouble(double(getInt())*src.getDouble()); |
---|
| 572 | return; |
---|
| 573 | default:; |
---|
| 574 | } |
---|
| 575 | break; |
---|
| 576 | case TDouble: |
---|
| 577 | switch (src.getType()) |
---|
| 578 | { |
---|
| 579 | case TInt: |
---|
| 580 | case TDouble: |
---|
| 581 | ddata() *= src.getDouble(); |
---|
| 582 | return; |
---|
| 583 | default:; |
---|
| 584 | } |
---|
| 585 | break; |
---|
[109] | 586 | case TString: |
---|
[337] | 587 | switch (src.getType()) |
---|
| 588 | { |
---|
| 589 | case TInt: case TDouble: |
---|
| 590 | { |
---|
| 591 | SString t; |
---|
| 592 | for (int n = src.getInt(); n > 0; n--) |
---|
| 593 | t += getString(); |
---|
| 594 | setString(t); |
---|
| 595 | return; |
---|
| 596 | } |
---|
| 597 | default:; |
---|
| 598 | } |
---|
| 599 | break; |
---|
| 600 | case TObj: |
---|
[109] | 601 | { |
---|
[337] | 602 | VectorObject *vec = VectorObject::fromObject(getObject(), false); |
---|
| 603 | if (vec) |
---|
[109] | 604 | { |
---|
[337] | 605 | int n = src.getInt(); |
---|
| 606 | int orig_size = vec->data.size(); |
---|
| 607 | if (n <= 0) |
---|
[109] | 608 | { |
---|
[337] | 609 | vec->clear(); return; |
---|
| 610 | } |
---|
| 611 | for (; n > 1; n--) |
---|
| 612 | { |
---|
| 613 | for (int i = 0; i < orig_size; i++) |
---|
[109] | 614 | { |
---|
[337] | 615 | ExtValue *s = (ExtValue*)vec->data(i); |
---|
| 616 | ExtValue *d = s ? new ExtValue(*s) : NULL; |
---|
| 617 | vec->data += d; |
---|
[109] | 618 | } |
---|
[337] | 619 | } |
---|
[228] | 620 | return; |
---|
| 621 | } |
---|
[337] | 622 | } |
---|
[228] | 623 | //NO break; |
---|
[109] | 624 | default:; |
---|
| 625 | } |
---|
[375] | 626 | logPrintf("ExtValue", "multiply", LOG_WARN, "Can't multiply %s by %s", typeAndValue().c_str(), src.typeAndValue().c_str()); |
---|
[109] | 627 | } |
---|
| 628 | |
---|
| 629 | /*#include "fpu_control.h" |
---|
| 630 | #include <signal.h> |
---|
| 631 | |
---|
| 632 | static int fpuexception; |
---|
| 633 | void mathhandler(int sig) |
---|
| 634 | { |
---|
| 635 | printf("fpu exception!\n"); |
---|
| 636 | fpuexception=1; |
---|
| 637 | signal(SIGFPE,SIG_IGN); |
---|
| 638 | } */ |
---|
| 639 | |
---|
[337] | 640 | void ExtValue::divInt(paInt a) |
---|
| 641 | { |
---|
| 642 | if (a) |
---|
| 643 | idata() /= a; |
---|
| 644 | else |
---|
| 645 | { |
---|
[375] | 646 | logPrintf("ExtValue", "divide", LOG_CRITICAL, "Division by zero: %d/0", idata()); |
---|
[337] | 647 | setInvalid(); |
---|
| 648 | } |
---|
| 649 | } |
---|
| 650 | |
---|
| 651 | void ExtValue::divDouble(double a) |
---|
| 652 | { |
---|
| 653 | if (a == 0.0) |
---|
| 654 | { |
---|
[375] | 655 | logPrintf("ExtValue", "divide", LOG_CRITICAL, "Division by zero: %s/0.0", getString().c_str()); |
---|
[337] | 656 | setInvalid(); |
---|
| 657 | } |
---|
| 658 | else |
---|
| 659 | { |
---|
| 660 | fpExceptDisable(); |
---|
| 661 | double tmp = getDouble() / a; |
---|
| 662 | if (!finite(tmp)) |
---|
| 663 | { |
---|
[375] | 664 | logPrintf("ExtValue", "divide", LOG_CRITICAL, "Overflow %s/%g", getString().c_str(), a); setInvalid(); |
---|
[337] | 665 | } |
---|
| 666 | else |
---|
| 667 | setDouble(tmp); |
---|
| 668 | // niby dobrze ale lepiej byloby to robic bardziej systematycznie a nie tylko w dzieleniu? |
---|
| 669 | //if (isnan(ddata())) //http://www.digitalmars.com/d/archives/c++/Traping_divide_by_zero_5728.html |
---|
[375] | 670 | // { logPrintf("ExtValue","divide",LOG_ERROR,"not-a-number",(const char*)getString()); setInvalid(); } |
---|
[337] | 671 | fpExceptEnable(); |
---|
| 672 | } |
---|
| 673 | } |
---|
| 674 | |
---|
[109] | 675 | void ExtValue::operator/=(const ExtValue& src) |
---|
| 676 | { |
---|
[337] | 677 | switch (type) |
---|
[109] | 678 | { |
---|
| 679 | case TInt: |
---|
[337] | 680 | switch (src.getType()) |
---|
| 681 | { |
---|
| 682 | case TInt: |
---|
| 683 | divInt(src.idata()); |
---|
| 684 | return; |
---|
| 685 | case TDouble: |
---|
| 686 | divDouble(src.ddata()); |
---|
| 687 | return; |
---|
| 688 | default:; |
---|
| 689 | } |
---|
[109] | 690 | break; |
---|
| 691 | |
---|
| 692 | case TDouble: |
---|
[337] | 693 | switch (src.getType()) |
---|
[109] | 694 | { |
---|
[337] | 695 | case TInt: |
---|
| 696 | divDouble(src.getDouble()); |
---|
| 697 | return; |
---|
| 698 | case TDouble: |
---|
| 699 | divDouble(src.ddata()); |
---|
| 700 | return; |
---|
| 701 | default:; |
---|
[109] | 702 | } |
---|
| 703 | break; |
---|
| 704 | |
---|
| 705 | default:; |
---|
| 706 | } |
---|
[375] | 707 | logPrintf("ExtValue", "divide", LOG_ERROR, "Can't divide %s by %s", typeAndValue().c_str(), src.typeAndValue().c_str()); |
---|
[109] | 708 | } |
---|
| 709 | |
---|
[337] | 710 | SString ExtValue::format(SString& fmt, const ExtValue **values, int count) |
---|
[109] | 711 | { |
---|
[337] | 712 | SString ret; |
---|
| 713 | // "..........%.........%..........%........" |
---|
| 714 | // ^_cur ^_next |
---|
| 715 | // ^^^^^^^^^^___sub |
---|
| 716 | // |
---|
| 717 | // "..........%.........%..........%........" |
---|
| 718 | // ^-cur ^-next |
---|
| 719 | // ^^^^^^^^^^___sub |
---|
[348] | 720 | const char* begin = fmt.c_str(), *end = begin + fmt.len(), *curr = begin; |
---|
[337] | 721 | int type = 0; |
---|
[109] | 722 | |
---|
[337] | 723 | class Args |
---|
| 724 | { |
---|
| 725 | const ExtValue **values; |
---|
| 726 | int count; |
---|
| 727 | int arg; |
---|
| 728 | public: |
---|
| 729 | Args(const ExtValue **v, int c) :values(v), count(c), arg(0) {} |
---|
| 730 | bool finished() { return arg >= count; } |
---|
| 731 | const ExtValue *getNext() { const ExtValue *ret = NULL; if ((arg < count) && values[arg]) ret = values[arg]; arg++; return ret; } |
---|
| 732 | }; |
---|
| 733 | Args args(values, count); |
---|
[109] | 734 | |
---|
[337] | 735 | while (curr < end) |
---|
[109] | 736 | { |
---|
[337] | 737 | const char* next = strchr(curr, '%'); |
---|
| 738 | if (!next) next = end; else if ((next == curr) && (curr > begin)) |
---|
[109] | 739 | { |
---|
[337] | 740 | next = strchr(next + 1, '%'); if (!next) next = end; |
---|
[109] | 741 | } |
---|
[337] | 742 | type = 0; |
---|
| 743 | if (curr > begin) |
---|
[109] | 744 | { |
---|
[337] | 745 | type = 0; |
---|
| 746 | for (const char* t = curr; t < next; t++) |
---|
| 747 | switch (*t) |
---|
| 748 | { |
---|
| 749 | case 'd': case 'x': case 'X': case 'u': case 'p': case 'c': type = 'd'; t = next; break; |
---|
| 750 | case 'f': case 'g': case 'e': type = 'f'; t = next; break; |
---|
| 751 | case 's': type = 's'; t = next; break; |
---|
| 752 | case 't': case 'T': type = *t; t = next; break; |
---|
| 753 | case '%': if (t > begin) { type = *t; t = next; } break; |
---|
| 754 | } |
---|
[109] | 755 | } |
---|
[337] | 756 | if (curr > begin) curr--; |
---|
| 757 | const ExtValue *a; |
---|
| 758 | if (args.finished() && (type != 0) && (type != '%')) |
---|
[109] | 759 | { |
---|
[337] | 760 | ret += fmt.substr((int)(curr - begin)); |
---|
| 761 | break; |
---|
| 762 | } |
---|
| 763 | SString sub = fmt.substr((int)(curr - begin), (int)(next - curr)); |
---|
| 764 | switch (type) |
---|
| 765 | { |
---|
[348] | 766 | case 'd': a = args.getNext(); ret += SString::sprintf(sub.c_str(), a ? a->getInt() : 0); break; |
---|
| 767 | case 'f': a = args.getNext(); ret += SString::sprintf(sub.c_str(), a ? a->getDouble() : 0); break; |
---|
| 768 | case 's': {a = args.getNext(); SString tmp; if (a) tmp = a->getString(); ret += SString::sprintf(sub.c_str(), tmp.c_str()); } break; |
---|
[109] | 769 | case 't': case 'T': |
---|
[337] | 770 | { |
---|
| 771 | a = args.getNext(); |
---|
| 772 | time_t ti = a ? a->getInt() : 0; |
---|
| 773 | struct tm tm = Convert::localtime(ti); |
---|
[109] | 774 | SString timtxt; |
---|
[337] | 775 | if (type == 'T') |
---|
| 776 | timtxt = SString::sprintf("%04d-%02d-%02d %02d:%02d:%02d", 1900 + tm.tm_year, 1 + tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); |
---|
[109] | 777 | else |
---|
[337] | 778 | timtxt = Convert::asctime(tm).c_str(); |
---|
| 779 | ret += timtxt; |
---|
| 780 | ret += sub.substr(2); |
---|
| 781 | } |
---|
[109] | 782 | break; |
---|
[337] | 783 | case '%': ret += '%'; ret += sub.substr(2); break; |
---|
| 784 | case 0: ret += sub; break; |
---|
[109] | 785 | } |
---|
[337] | 786 | curr = next + 1; |
---|
[109] | 787 | } |
---|
[337] | 788 | return ret; |
---|
[109] | 789 | } |
---|
| 790 | |
---|
| 791 | |
---|
| 792 | void ExtValue::operator%=(const ExtValue& src) |
---|
| 793 | { |
---|
[337] | 794 | switch (type) |
---|
[109] | 795 | { |
---|
[337] | 796 | case TInt: idata() = idata() % src.getInt(); break; |
---|
| 797 | case TDouble: ddata() = fmod(ddata(), src.getDouble()); break; |
---|
[109] | 798 | |
---|
| 799 | case TString: |
---|
| 800 | { |
---|
[337] | 801 | VectorObject *vec = VectorObject::fromObject(src.getObject(), false); |
---|
| 802 | if (vec) |
---|
| 803 | sdata() = format(sdata(), (const ExtValue**)&vec->data.getref(0), vec->data.size()); |
---|
| 804 | else |
---|
| 805 | { |
---|
| 806 | const ExtValue *ptr = &src; sdata() = ExtValue::format(sdata(), &ptr, 1); |
---|
| 807 | } |
---|
[109] | 808 | } |
---|
[337] | 809 | break; |
---|
[109] | 810 | |
---|
[228] | 811 | case TObj: case TUnknown: case TInvalid: |
---|
[375] | 812 | logPrintf("ExtValue", "modulo", LOG_WARN, "Can't apply modulo to %s", typeDescription().c_str()); |
---|
[228] | 813 | |
---|
[109] | 814 | default:; |
---|
| 815 | } |
---|
| 816 | } |
---|
| 817 | |
---|
[326] | 818 | bool ExtValue::parseInt(const char* s, paInt &result, bool strict, bool error) |
---|
[144] | 819 | { |
---|
[325] | 820 | ExtValue tmp; |
---|
| 821 | const char* after = tmp.parseNumber(s, strict ? TInt : TUnknown); |
---|
[326] | 822 | if ((after == NULL) || (after[0] != 0)) |
---|
[337] | 823 | { |
---|
| 824 | if (error) |
---|
[375] | 825 | logPrintf("ExtValue", "parseInt", LOG_ERROR, "Could not parse '%s'%s", s, strict ? " (strict)" : ""); |
---|
[326] | 826 | return false; |
---|
[337] | 827 | } |
---|
[325] | 828 | result = tmp.getInt(); |
---|
| 829 | return true; |
---|
[144] | 830 | } |
---|
| 831 | |
---|
[326] | 832 | bool ExtValue::parseDouble(const char* s, double &result, bool error) |
---|
[325] | 833 | { |
---|
| 834 | ExtValue tmp; |
---|
| 835 | const char* after = tmp.parseNumber(s, TDouble); |
---|
[326] | 836 | if ((after == NULL) || (after[0] != 0)) |
---|
[337] | 837 | { |
---|
| 838 | if (error) |
---|
[375] | 839 | logPrintf("ExtValue", "parseDouble", LOG_ERROR, "Could not parse '%s'", s); |
---|
[326] | 840 | return false; |
---|
[337] | 841 | } |
---|
[325] | 842 | result = tmp.getDouble(); |
---|
| 843 | return true; |
---|
| 844 | } |
---|
| 845 | |
---|
[337] | 846 | paInt ExtValue::getInt(const char* s, bool strict) |
---|
[325] | 847 | { |
---|
| 848 | paInt result; |
---|
[326] | 849 | if (parseInt(s, result, strict, true)) |
---|
[325] | 850 | return result; |
---|
| 851 | return 0; |
---|
| 852 | } |
---|
| 853 | |
---|
[144] | 854 | double ExtValue::getDouble(const char* s) |
---|
| 855 | { |
---|
[325] | 856 | double result; |
---|
[326] | 857 | if (parseDouble(s, result, true)) |
---|
[325] | 858 | return result; |
---|
| 859 | return 0; |
---|
[144] | 860 | } |
---|
| 861 | |
---|
[247] | 862 | paInt ExtValue::getInt() const |
---|
[109] | 863 | { |
---|
[325] | 864 | switch (type) |
---|
[109] | 865 | { |
---|
| 866 | case TInt: return idata(); |
---|
| 867 | case TDouble: return (int)ddata(); |
---|
[348] | 868 | case TString: return getInt(sdata().c_str()); |
---|
[144] | 869 | case TObj: |
---|
[375] | 870 | logPrintf("ExtValue", "getInt", LOG_WARN, "Getting integer value from object reference (%s)", getString().c_str()); |
---|
[247] | 871 | return (paInt)(intptr_t)odata().param; |
---|
[109] | 872 | default:; |
---|
| 873 | } |
---|
[325] | 874 | return 0; |
---|
[109] | 875 | } |
---|
[144] | 876 | |
---|
[109] | 877 | double ExtValue::getDouble() const |
---|
| 878 | { |
---|
[325] | 879 | switch (type) |
---|
[109] | 880 | { |
---|
| 881 | case TDouble: return ddata(); |
---|
| 882 | case TInt: return (double)idata(); |
---|
[348] | 883 | case TString: return getDouble(sdata().c_str()); |
---|
[144] | 884 | case TObj: |
---|
[375] | 885 | logPrintf("ExtValue", "getDouble", LOG_WARN, "Getting floating point value from object reference (%s)", getString().c_str()); |
---|
[247] | 886 | return (double)(intptr_t)odata().param; |
---|
[109] | 887 | default:; |
---|
| 888 | } |
---|
[325] | 889 | return 0.0; |
---|
[109] | 890 | } |
---|
[325] | 891 | |
---|
[109] | 892 | SString ExtValue::getString() const |
---|
| 893 | { |
---|
[325] | 894 | switch (type) |
---|
[109] | 895 | { |
---|
| 896 | case TString: return sdata(); |
---|
| 897 | case TInt: return SString::valueOf(idata()); |
---|
| 898 | case TDouble: return SString::valueOf(ddata()); |
---|
| 899 | case TObj: return odata().toString(); |
---|
| 900 | case TInvalid: return SString("invalid"); |
---|
| 901 | default: return SString("null"); |
---|
| 902 | } |
---|
| 903 | } |
---|
| 904 | |
---|
| 905 | const SString* ExtValue::getStringPtr() const |
---|
| 906 | { |
---|
[325] | 907 | if (type == TString) |
---|
| 908 | return &sdata(); |
---|
| 909 | return NULL; |
---|
[109] | 910 | } |
---|
| 911 | |
---|
[464] | 912 | SString ExtValue::serialize(SerializationFormat format) const |
---|
[109] | 913 | { |
---|
[337] | 914 | switch (type) |
---|
[109] | 915 | { |
---|
| 916 | case TString: |
---|
[337] | 917 | { |
---|
| 918 | SString q = sdata(); |
---|
[109] | 919 | sstringQuote(q); |
---|
[337] | 920 | return SString("\"") + q + SString("\""); |
---|
| 921 | } |
---|
[109] | 922 | case TInt: |
---|
| 923 | return SString::valueOf(idata()); |
---|
| 924 | case TDouble: |
---|
| 925 | return SString::valueOf(ddata()); |
---|
| 926 | case TObj: |
---|
[464] | 927 | return odata().serialize(format); |
---|
[109] | 928 | case TInvalid: |
---|
[464] | 929 | if (format==NativeSerialization) |
---|
| 930 | return SString("invalid"); |
---|
| 931 | // else null --v |
---|
[109] | 932 | default: |
---|
| 933 | return SString("null"); |
---|
| 934 | } |
---|
| 935 | } |
---|
| 936 | |
---|
[325] | 937 | /// returns the first character after the parsed number, or NULL if not a number |
---|
| 938 | /// @param strict_type = restrict the allowed return value (TUnknown = unrestricted) |
---|
| 939 | const char* ExtValue::parseNumber(const char* in, ExtPType strict_type) |
---|
[109] | 940 | { |
---|
[325] | 941 | char* after; |
---|
| 942 | if (in == NULL) return NULL; |
---|
| 943 | if (in[0] == 0) return NULL; |
---|
| 944 | while (isspace(*in)) in++; |
---|
[326] | 945 | bool minus = (in[0] == '-'); |
---|
| 946 | bool plus = (in[0] == '+'); |
---|
[337] | 947 | if (((in[0] == '0') && ((in[1] == 'x') || (in[1] == 'X'))) |
---|
| 948 | || (((minus || plus) && (in[1] == '0') && ((in[2] == 'x') || (in[2] == 'X'))))) |
---|
[109] | 949 | { |
---|
[326] | 950 | in += (minus || plus) ? 3 : 2; |
---|
[325] | 951 | if (isspace(*in)) return NULL; |
---|
| 952 | errno = 0; |
---|
| 953 | unsigned long intvalue = strtoul(in, &after, 16); |
---|
| 954 | if ((after > in) && (errno == 0) && (intvalue <= 0xffffffff)) |
---|
[109] | 955 | { |
---|
[325] | 956 | if (strict_type == TDouble) |
---|
| 957 | setDouble(minus ? -(double)intvalue : (double)intvalue); |
---|
| 958 | else |
---|
| 959 | setInt(minus ? -intvalue : intvalue); |
---|
| 960 | return after; |
---|
[109] | 961 | } |
---|
[325] | 962 | else |
---|
| 963 | return NULL; |
---|
| 964 | } |
---|
[109] | 965 | |
---|
[325] | 966 | errno = 0; |
---|
| 967 | double fpvalue = strtod(in, &after); |
---|
| 968 | if ((after > in) && (errno == 0)) |
---|
| 969 | { |
---|
| 970 | if (strict_type != TDouble) |
---|
[109] | 971 | { |
---|
[325] | 972 | if ((memchr(in, '.', after - in) == NULL) && (memchr(in, 'e', after - in) == NULL) && (memchr(in, 'E', after - in) == NULL) // no "special" characters |
---|
| 973 | && (fpvalue == floor(fpvalue)) // value is integer |
---|
| 974 | && (fpvalue >= INT_MIN) && (fpvalue <= INT_MAX)) // within limits |
---|
| 975 | { |
---|
| 976 | setInt(fpvalue); |
---|
| 977 | return after; |
---|
| 978 | } |
---|
| 979 | else if (strict_type == TInt) |
---|
| 980 | return NULL; |
---|
[109] | 981 | } |
---|
[325] | 982 | setDouble(fpvalue); |
---|
| 983 | return after; |
---|
[109] | 984 | } |
---|
[325] | 985 | return NULL; |
---|
[109] | 986 | } |
---|
| 987 | |
---|
[222] | 988 | PtrListTempl<ParamInterface*> &ExtValue::getDeserializableClasses() |
---|
[109] | 989 | { |
---|
[337] | 990 | static PtrListTempl<ParamInterface*> classes; |
---|
| 991 | return classes; |
---|
[109] | 992 | } |
---|
| 993 | |
---|
| 994 | ParamInterface *ExtValue::findDeserializableClass(const char* name) |
---|
| 995 | { |
---|
[337] | 996 | FOREACH(ParamInterface*, cls, getDeserializableClasses()) |
---|
| 997 | if (!strcmp(cls->getName(), name)) |
---|
| 998 | return cls; |
---|
| 999 | return NULL; |
---|
[109] | 1000 | } |
---|
| 1001 | |
---|
| 1002 | static const char* skipWord(const char* in) |
---|
| 1003 | { |
---|
[337] | 1004 | while (isalpha(*in) || (*in == '_')) |
---|
| 1005 | in++; |
---|
| 1006 | return in; |
---|
[109] | 1007 | } |
---|
| 1008 | |
---|
| 1009 | //returns the first character after the parsed portion or NULL if invalid format |
---|
| 1010 | const char* ExtValue::deserialize_inner(const char* in) |
---|
| 1011 | { |
---|
[337] | 1012 | const char* ret = parseNumber(in); |
---|
| 1013 | if (ret) |
---|
| 1014 | return ret; |
---|
| 1015 | else if (*in == '\"') |
---|
[109] | 1016 | { |
---|
[337] | 1017 | ret = skipQuoteString(in + 1, NULL); |
---|
| 1018 | SString s(in + 1, (int)(ret - (in + 1))); |
---|
| 1019 | sstringUnquote(s); |
---|
| 1020 | setString(s); |
---|
| 1021 | if (*ret == '\"') |
---|
| 1022 | return ret + 1; |
---|
| 1023 | else |
---|
[333] | 1024 | { |
---|
[375] | 1025 | logPrintf("ExtValue", "deserialize", LOG_ERROR, "Missing '\"' in string: '%s'", ret); |
---|
[337] | 1026 | return NULL; |
---|
[333] | 1027 | } |
---|
[109] | 1028 | } |
---|
[337] | 1029 | else if (*in == '[') |
---|
[109] | 1030 | { |
---|
[337] | 1031 | VectorObject *vec = new VectorObject; |
---|
| 1032 | ExtObject o(&VectorObject::par, vec); |
---|
[371] | 1033 | tlsGetRef(ExtObject_serialization).add(o); |
---|
[337] | 1034 | const char* p = in + 1; |
---|
| 1035 | ExtValue tmp; |
---|
| 1036 | while (*p) |
---|
[109] | 1037 | { |
---|
[337] | 1038 | if (*p == ']') { p++; break; } |
---|
| 1039 | ret = tmp.deserialize(p); |
---|
| 1040 | if (ret) |
---|
[109] | 1041 | { |
---|
[337] | 1042 | vec->data += new ExtValue(tmp); |
---|
| 1043 | p = ret; |
---|
| 1044 | if (*p == ',') p++; |
---|
| 1045 | else if (*p != ']') |
---|
[333] | 1046 | { |
---|
[375] | 1047 | logPrintf("ExtValue", "deserialize", LOG_ERROR, "Missing ',' in Vector: '%s'", p); |
---|
[337] | 1048 | return NULL; |
---|
[333] | 1049 | } |
---|
[109] | 1050 | } |
---|
[337] | 1051 | else |
---|
[109] | 1052 | { |
---|
[337] | 1053 | p = NULL; |
---|
| 1054 | break; |
---|
[109] | 1055 | } |
---|
| 1056 | } |
---|
[337] | 1057 | setObject(o); |
---|
| 1058 | return p; |
---|
[109] | 1059 | } |
---|
[337] | 1060 | else if (*in == '{') |
---|
[109] | 1061 | { |
---|
[337] | 1062 | DictionaryObject *dic = new DictionaryObject; |
---|
| 1063 | ExtObject o(&DictionaryObject::par, dic); |
---|
[371] | 1064 | tlsGetRef(ExtObject_serialization).add(o); |
---|
[337] | 1065 | const char* p = in + 1; |
---|
| 1066 | ExtValue args[2]/*={value,key}*/, dummy_ret; |
---|
| 1067 | while (*p) |
---|
[109] | 1068 | { |
---|
[337] | 1069 | if (*p == '}') { p++; break; } |
---|
| 1070 | ret = args[1].deserialize(p); |
---|
| 1071 | if ((!ret) || (args[1].getType() != TString)) { p = NULL; break; } |
---|
| 1072 | p = ret; |
---|
[375] | 1073 | if (*p != ':') { logPrintf("ExtValue", "deserialize", LOG_ERROR, "Missing ':' in Dictionary: '%s'", p); p = NULL; break; } |
---|
[337] | 1074 | p++; |
---|
| 1075 | ret = args[0].deserialize(p); |
---|
| 1076 | if (!ret) { p = NULL; break; } |
---|
| 1077 | p = ret; |
---|
| 1078 | dic->p_set(args, &dummy_ret); |
---|
| 1079 | if (*p == ',') p++; |
---|
| 1080 | else if (*p != '}') |
---|
[333] | 1081 | { |
---|
[375] | 1082 | logPrintf("ExtValue", "deserialize", LOG_ERROR, "Missing ',' in Dictionary: '%s'", p); |
---|
[337] | 1083 | return NULL; |
---|
[333] | 1084 | } |
---|
[109] | 1085 | } |
---|
[337] | 1086 | setObject(o); |
---|
| 1087 | return p; |
---|
[109] | 1088 | } |
---|
[337] | 1089 | else if (!strncmp(in, "null", 4)) |
---|
[109] | 1090 | { |
---|
[337] | 1091 | setEmpty(); |
---|
| 1092 | return in + 4; |
---|
[109] | 1093 | } |
---|
[337] | 1094 | else if (!strncmp(in, "invalid", 9)) |
---|
[109] | 1095 | { |
---|
[337] | 1096 | setInvalid(); |
---|
| 1097 | return in + 9; |
---|
[109] | 1098 | } |
---|
[337] | 1099 | else if (*in == '<') |
---|
[109] | 1100 | { //unserializable object |
---|
[337] | 1101 | setInvalid(); |
---|
| 1102 | while (*in) |
---|
| 1103 | if (*in == '>') |
---|
| 1104 | return in + 1; |
---|
| 1105 | else in++; |
---|
| 1106 | return in; |
---|
[109] | 1107 | } |
---|
[337] | 1108 | else if (*in == '^') |
---|
[109] | 1109 | { |
---|
[337] | 1110 | in++; |
---|
| 1111 | ExtValue ref; |
---|
| 1112 | ret = ref.parseNumber(in, TInt); |
---|
| 1113 | if (ret && (ref.getType() == TInt)) |
---|
[109] | 1114 | { |
---|
[371] | 1115 | const ExtObject* o = tlsGetRef(ExtObject_serialization).get(ref.getInt()); |
---|
[337] | 1116 | if (o) |
---|
[109] | 1117 | { |
---|
[337] | 1118 | setObject(*o); |
---|
| 1119 | return ret; |
---|
[109] | 1120 | } |
---|
| 1121 | } |
---|
[375] | 1122 | logPrintf("ExtValue", "deserialize", LOG_ERROR, "Invalid reference: '%s'", in - 1); |
---|
[337] | 1123 | return NULL; |
---|
[109] | 1124 | } |
---|
[337] | 1125 | else if ((ret = skipWord(in)) && (ret != in)) |
---|
[109] | 1126 | { |
---|
[337] | 1127 | SString clsname(in, (int)(ret - in)); |
---|
| 1128 | ExtValue tmp; |
---|
| 1129 | ret = tmp.deserialize(ret); |
---|
[348] | 1130 | ParamInterface *cls = findDeserializableClass(clsname.c_str()); |
---|
[337] | 1131 | if (cls && (tmp.getType() != TUnknown) && (tmp.getType() != TInvalid)) |
---|
[109] | 1132 | { |
---|
[337] | 1133 | VectorObject *vec = VectorObject::fromObject(tmp.getObject(), false); |
---|
| 1134 | if (vec) |
---|
[109] | 1135 | { |
---|
[337] | 1136 | int m = cls->findId("newFromVector"); |
---|
| 1137 | if (m >= 0) |
---|
[109] | 1138 | { |
---|
[337] | 1139 | cls->call(m, &tmp, this); |
---|
[371] | 1140 | tlsGetRef(ExtObject_serialization).replace(tmp.getObject(), getObject()); |
---|
[337] | 1141 | return ret; |
---|
[109] | 1142 | } |
---|
| 1143 | } |
---|
[337] | 1144 | DictionaryObject *dic = DictionaryObject::fromObject(tmp.getObject(), false); |
---|
| 1145 | if (dic) |
---|
[109] | 1146 | { |
---|
[337] | 1147 | int m = cls->findId("newFromDictionary"); |
---|
| 1148 | if (m >= 0) |
---|
[109] | 1149 | { |
---|
[337] | 1150 | cls->call(m, &tmp, this); |
---|
[371] | 1151 | tlsGetRef(ExtObject_serialization).replace(tmp.getObject(), getObject()); |
---|
[337] | 1152 | return ret; |
---|
[109] | 1153 | } |
---|
| 1154 | } |
---|
[337] | 1155 | if (tmp.getType() == TString) |
---|
[109] | 1156 | { |
---|
[337] | 1157 | int m = cls->findId("newFromString"); |
---|
| 1158 | if (m >= 0) |
---|
[109] | 1159 | { |
---|
[337] | 1160 | cls->call(m, &tmp, this); |
---|
[371] | 1161 | tlsGetRef(ExtObject_serialization).replace(tmp.getObject(), getObject()); |
---|
[337] | 1162 | return ret; |
---|
[109] | 1163 | } |
---|
| 1164 | } |
---|
[371] | 1165 | tlsGetRef(ExtObject_serialization).remove(tmp.getObject()); |
---|
[337] | 1166 | setEmpty(); |
---|
| 1167 | } |
---|
[109] | 1168 | setEmpty(); |
---|
[375] | 1169 | logPrintf("ExtValue", "deserialize", LOG_WARN, "object of class \"%s\" could not be deserialized", clsname.c_str()); |
---|
[337] | 1170 | return ret; |
---|
| 1171 | } |
---|
[375] | 1172 | logPrintf("ExtValue", "deserialize", LOG_ERROR, "Bad syntax: '%s'", in); |
---|
[109] | 1173 | setEmpty(); |
---|
[337] | 1174 | return NULL; |
---|
[109] | 1175 | } |
---|
| 1176 | |
---|
| 1177 | const char* ExtValue::deserialize(const char* in) |
---|
| 1178 | { |
---|
[371] | 1179 | tlsGetRef(ExtObject_serialization).begin(); |
---|
[337] | 1180 | const char* ret = deserialize_inner(in); |
---|
[371] | 1181 | tlsGetRef(ExtObject_serialization).end(); |
---|
[337] | 1182 | return ret; |
---|
[109] | 1183 | } |
---|
| 1184 | |
---|
| 1185 | ExtObject ExtValue::getObject() const |
---|
| 1186 | { |
---|
[337] | 1187 | if (type == TObj) return odata(); |
---|
| 1188 | return ExtObject(); |
---|
[109] | 1189 | } |
---|
| 1190 | |
---|
| 1191 | ExtValue ExtValue::getExtType() |
---|
| 1192 | { |
---|
[337] | 1193 | static const char* typenames[] = { "null", "int", "float", "string", "", "invalid" }; |
---|
| 1194 | if (getType() != TObj) |
---|
| 1195 | return ExtValue(typenames[(int)getType()]); |
---|
| 1196 | ExtObject& o = odata(); |
---|
| 1197 | return ExtValue(SString(o.isEmpty() ? "" : o.interfaceName())); |
---|
[109] | 1198 | } |
---|
| 1199 | |
---|
| 1200 | SString SString::valueOf(const ExtValue& v) |
---|
| 1201 | { |
---|
[337] | 1202 | return v.getString(); |
---|
[109] | 1203 | } |
---|
| 1204 | SString SString::valueOf(const ExtObject& v) |
---|
| 1205 | { |
---|
[337] | 1206 | return v.toString(); |
---|
[109] | 1207 | } |
---|