[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> |
---|
[109] | 15 | |
---|
| 16 | #ifndef NO_BARRIER |
---|
| 17 | #include <frams/simul/barrier.h> |
---|
| 18 | #include <common/threads.h> |
---|
| 19 | #endif |
---|
| 20 | |
---|
| 21 | #ifdef MULTITHREADED |
---|
| 22 | #include <pthread.h> |
---|
| 23 | //this lock only protects against ref.counter corruption caused by concurrent reads. |
---|
| 24 | //read/write conficts and nonatomicity are handled by BarrierObject (at least in theory ;-)) |
---|
| 25 | static pthread_mutex_t extobject_ref_lock=PTHREAD_MUTEX_INITIALIZER; |
---|
| 26 | #define REF_LOCK pthread_mutex_lock(&extobject_ref_lock) |
---|
| 27 | #define REF_UNLOCK pthread_mutex_unlock(&extobject_ref_lock) |
---|
| 28 | #else |
---|
| 29 | #define REF_LOCK |
---|
| 30 | #define REF_UNLOCK |
---|
| 31 | #endif |
---|
| 32 | |
---|
| 33 | void ExtObject::incref() const |
---|
| 34 | { |
---|
| 35 | if (subtype&1) |
---|
| 36 | { |
---|
| 37 | REF_LOCK; |
---|
| 38 | dbobject->refcount++; |
---|
| 39 | REF_UNLOCK; |
---|
| 40 | } |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | void ExtObject::decref() const |
---|
| 44 | { |
---|
| 45 | if (subtype&1) |
---|
| 46 | { |
---|
| 47 | REF_LOCK; |
---|
| 48 | bool destroy=!--dbobject->refcount; |
---|
| 49 | REF_UNLOCK; |
---|
| 50 | //another thread can now access the object while we are deleting it |
---|
| 51 | //but this is not a bug since we only guarantee read/read safety |
---|
| 52 | if (destroy) delete dbobject; |
---|
| 53 | } |
---|
| 54 | } |
---|
| 55 | |
---|
[257] | 56 | bool ExtObject::operator==(const ExtObject& src) const |
---|
| 57 | { |
---|
| 58 | if (object!=src.object) return false; |
---|
| 59 | const char* n1=interfaceName(); |
---|
| 60 | const char* n2=src.interfaceName(); |
---|
| 61 | return (n1==n2) || (strcmp(n1,n2)==0); |
---|
| 62 | } |
---|
| 63 | |
---|
[109] | 64 | bool ExtObject::makeUnique() |
---|
| 65 | { |
---|
| 66 | if (!(subtype&1)) return false; |
---|
| 67 | if (dbobject->refcount==1) return false; |
---|
[171] | 68 | VectorObject* v=VectorObject::fromObject(*this,false); |
---|
[109] | 69 | if (v) |
---|
| 70 | { |
---|
| 71 | VectorObject* n=new VectorObject; |
---|
| 72 | n->data.setSize(n->data.size()); |
---|
| 73 | for(int i=0;i<v->data.size();i++) |
---|
| 74 | { |
---|
| 75 | ExtValue *x=(ExtValue*)v->data(i); |
---|
| 76 | n->data.set(i,x?new ExtValue(*x):NULL); |
---|
| 77 | } |
---|
| 78 | operator=(n->makeObject()); |
---|
| 79 | return true; |
---|
| 80 | } |
---|
| 81 | return false; |
---|
| 82 | } |
---|
| 83 | |
---|
[171] | 84 | void* ExtObject::getTarget(const char* classname, bool through_barrier, bool warn) const |
---|
[109] | 85 | { |
---|
| 86 | if (!strcmp(interfaceName(),classname)) |
---|
| 87 | return getTarget(); |
---|
| 88 | #ifndef NO_BARRIER |
---|
| 89 | if (through_barrier) |
---|
| 90 | { |
---|
| 91 | BarrierObject *bo=BarrierObject::fromObject(*this); |
---|
| 92 | if (bo) |
---|
[171] | 93 | return bo->getSourceObject().getTarget(classname,true,warn); |
---|
[109] | 94 | } |
---|
| 95 | #endif |
---|
[171] | 96 | |
---|
| 97 | if (warn) |
---|
| 98 | { |
---|
| 99 | FMprintf("ExtValue","getObjectTarget",FMLV_WARN,"%s object expected, %s found",classname,interfaceName()); |
---|
| 100 | } |
---|
| 101 | |
---|
[109] | 102 | return NULL; |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | |
---|
| 106 | SString ExtObject::toString() const |
---|
| 107 | { |
---|
[273] | 108 | if (isEmpty()) return SString("null"); |
---|
[109] | 109 | Param tmp_param; |
---|
| 110 | ParamInterface *p=getParamInterface(tmp_param); |
---|
| 111 | int tostr=p->findId("toString"); |
---|
| 112 | if (tostr>=0) |
---|
| 113 | { |
---|
| 114 | return SString(p->getString(tostr)); |
---|
| 115 | } |
---|
| 116 | else |
---|
| 117 | { |
---|
| 118 | SString tmp("<"); |
---|
| 119 | tmp+=p->getName(); |
---|
| 120 | tmp+=SString::sprintf(" object at %p>",object?object:paraminterface); |
---|
| 121 | return tmp; |
---|
| 122 | } |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | THREAD_LOCAL_DEF(ExtObject::Serialization,ExtObject::serialization); |
---|
| 126 | |
---|
| 127 | void ExtObject::Serialization::begin() |
---|
| 128 | { |
---|
| 129 | if (level==0) |
---|
| 130 | refs.clear(); |
---|
| 131 | level++; |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | int ExtObject::Serialization::add(const ExtObject &o) |
---|
| 135 | { |
---|
| 136 | if (o.isEmpty()) return -1; |
---|
| 137 | for(int i=0;i<(int)refs.size();i++) |
---|
| 138 | { |
---|
| 139 | ExtObject& r=refs[i]; |
---|
| 140 | if (r==o) return i; |
---|
| 141 | } |
---|
| 142 | refs.push_back(o); |
---|
| 143 | return -1; |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | void ExtObject::Serialization::replace(const ExtObject& o,const ExtObject& other) |
---|
| 147 | { |
---|
| 148 | if (o.isEmpty()) return; |
---|
| 149 | for(int i=0;i<(int)refs.size();i++) |
---|
| 150 | { |
---|
| 151 | ExtObject& r=refs[i]; |
---|
| 152 | if (r==o) |
---|
| 153 | { |
---|
| 154 | r=other; |
---|
| 155 | return; |
---|
| 156 | } |
---|
| 157 | } |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | void ExtObject::Serialization::remove(const ExtObject& o) |
---|
| 161 | { |
---|
| 162 | if (o.isEmpty()) return; |
---|
| 163 | for(int i=0;i<(int)refs.size();i++) |
---|
| 164 | { |
---|
| 165 | ExtObject& r=refs[i]; |
---|
| 166 | if (o==r) refs.erase(refs.begin()+i); |
---|
| 167 | } |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | const ExtObject* ExtObject::Serialization::get(int ref) |
---|
| 171 | { |
---|
| 172 | if (ref<0) return NULL; |
---|
| 173 | if (ref>=(int)refs.size()) return NULL; |
---|
| 174 | return &refs[ref]; |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | void ExtObject::Serialization::end() |
---|
| 178 | { |
---|
| 179 | level--; |
---|
| 180 | if (level==0) |
---|
| 181 | refs.clear(); |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | SString ExtObject::serialize_inner() const |
---|
| 185 | { |
---|
| 186 | int ref=tlsGetRef(serialization).add(*this); |
---|
| 187 | if (ref>=0) |
---|
| 188 | return SString::sprintf("^%d",ref); |
---|
| 189 | |
---|
[273] | 190 | if (isEmpty()) return SString("null"); |
---|
[171] | 191 | VectorObject *vec=VectorObject::fromObject(*this,false); |
---|
[109] | 192 | if (vec) |
---|
| 193 | return vec->serialize(); |
---|
[171] | 194 | DictionaryObject *dic=DictionaryObject::fromObject(*this,false); |
---|
[109] | 195 | if (dic) |
---|
| 196 | return dic->serialize(); |
---|
| 197 | Param tmp_param; |
---|
| 198 | ParamInterface *p=getParamInterface(tmp_param); |
---|
| 199 | int m=p->findId("toVector"); |
---|
| 200 | if (m<0) |
---|
| 201 | m=p->findId("toDictionary"); |
---|
| 202 | if (m>=0) |
---|
| 203 | { |
---|
| 204 | ExtObject o(p->getObject(m)); |
---|
| 205 | SString ret=SString(interfaceName())+o.serialize(); |
---|
| 206 | return ret; |
---|
| 207 | } |
---|
| 208 | m=p->findId("toString"); |
---|
| 209 | if (m>=0) |
---|
| 210 | { |
---|
| 211 | SString str=p->getString(m); |
---|
| 212 | sstringQuote(str); |
---|
| 213 | SString ret=SString(interfaceName())+"\""+str+"\""; |
---|
| 214 | return ret; |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | tlsGetRef(serialization).remove(*this);//undo nonserializable reference |
---|
| 218 | SString ret=interfaceName(); |
---|
| 219 | ret+=SString::sprintf("<%p>",object?object:paraminterface); |
---|
| 220 | return ret; |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | SString ExtObject::serialize() const |
---|
| 224 | { |
---|
| 225 | tlsGetRef(serialization).begin(); |
---|
| 226 | SString ret=serialize_inner(); |
---|
| 227 | tlsGetRef(serialization).end(); |
---|
| 228 | return ret; |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | /////////////////////////////////////// |
---|
| 232 | |
---|
[228] | 233 | SString ExtValue::typeDescription() const |
---|
| 234 | { |
---|
| 235 | switch(type) |
---|
| 236 | { |
---|
| 237 | case TInt: return SString("integer value"); |
---|
| 238 | case TDouble: return SString("floating point value"); |
---|
| 239 | case TString: return SString("text string"); |
---|
| 240 | case TUnknown: return SString("null value"); |
---|
| 241 | case TInvalid: return SString("invalid value"); |
---|
| 242 | case TObj: return getObject().isEmpty()?SString("null"):(SString(getObject().interfaceName())+" object"); |
---|
| 243 | } |
---|
| 244 | return SString::empty(); |
---|
| 245 | } |
---|
| 246 | |
---|
[171] | 247 | void *ExtValue::getObjectTarget(const char* classname,bool warn) const |
---|
| 248 | { |
---|
| 249 | if (type!=TObj) |
---|
| 250 | { |
---|
| 251 | if (warn) |
---|
| 252 | { |
---|
| 253 | SString tmp=getString(); |
---|
| 254 | if (tmp.len()>30) tmp=tmp.substr(0,30)+"..."; |
---|
| 255 | if (type==TString) tmp=SString("\"")+tmp+SString("\""); |
---|
| 256 | FMprintf("ExtValue","getObjectTarget",FMLV_WARN,"%s object expected, %s found",classname,(const char*)tmp); |
---|
| 257 | } |
---|
| 258 | return NULL; |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | return getObject().getTarget(classname,true,warn); |
---|
| 262 | } |
---|
| 263 | |
---|
[109] | 264 | void ExtValue::set(const ExtValue& src) |
---|
| 265 | { |
---|
| 266 | switch(src.type) |
---|
| 267 | { |
---|
| 268 | case TString: sets(src.sdata()); break; |
---|
| 269 | case TInt: seti(src.idata()); break; |
---|
| 270 | case TDouble: setd(src.ddata()); break; |
---|
| 271 | case TObj: seto(src.odata()); break; |
---|
| 272 | default:type=src.type; break; |
---|
| 273 | } |
---|
| 274 | } |
---|
| 275 | |
---|
| 276 | void ExtValue::setEmpty() |
---|
| 277 | { |
---|
| 278 | switch(type) |
---|
| 279 | { |
---|
| 280 | #ifdef EXTVALUEUNION |
---|
| 281 | case TString: sdata().~SString(); break; |
---|
| 282 | case TObj: odata().~ExtObject(); break; |
---|
| 283 | #else |
---|
| 284 | case TString: delete s; break; |
---|
| 285 | case TObj: delete o; break; |
---|
| 286 | #endif |
---|
| 287 | default:; |
---|
| 288 | } |
---|
| 289 | type=TUnknown; |
---|
| 290 | } |
---|
| 291 | |
---|
[247] | 292 | static int longsign(paInt x) |
---|
[109] | 293 | { |
---|
| 294 | if (x<0) return -1; |
---|
| 295 | if (x>0) return 1; |
---|
| 296 | return 0; |
---|
| 297 | } |
---|
| 298 | |
---|
[247] | 299 | static int compareNull(const ExtValue& v) |
---|
[144] | 300 | { |
---|
| 301 | switch(v.type) |
---|
| 302 | { |
---|
| 303 | case TDouble: return v.getDouble()!=0.0; |
---|
| 304 | case TInt: return v.getInt()?1:0; |
---|
| 305 | case TString: return 1; |
---|
| 306 | default: return !v.isNull(); |
---|
| 307 | } |
---|
| 308 | } |
---|
| 309 | |
---|
[247] | 310 | int ExtValue::compare(const ExtValue& src) const |
---|
[109] | 311 | { |
---|
[247] | 312 | if (isNull()) |
---|
[144] | 313 | return compareNull(src); |
---|
[247] | 314 | else if (src.isNull()) |
---|
[144] | 315 | return compareNull(*this); |
---|
[109] | 316 | switch(type) |
---|
| 317 | { |
---|
| 318 | case TInt: |
---|
| 319 | { |
---|
[247] | 320 | paInt t=src.getInt(); |
---|
[109] | 321 | if (idata()>0) |
---|
| 322 | {if (t>0) return longsign(idata()-t); else return +1;} |
---|
| 323 | else |
---|
| 324 | {if (t<=0) return longsign(idata()-t); else return -1;} |
---|
| 325 | } |
---|
| 326 | case TDouble: |
---|
| 327 | { |
---|
| 328 | double t=ddata()-src.getDouble(); |
---|
| 329 | if (t<0) return -1; |
---|
| 330 | else if (t>0) return 1; |
---|
| 331 | return 0; |
---|
| 332 | } |
---|
| 333 | case TString: |
---|
| 334 | { |
---|
| 335 | SString t=src.getString(); |
---|
| 336 | SString& t2=sdata(); |
---|
| 337 | const char* s1=(const char*)t2; |
---|
| 338 | const char* s2=(const char*)t; |
---|
| 339 | return longsign(strcmp(s1,s2)); |
---|
| 340 | } |
---|
| 341 | case TObj: |
---|
| 342 | { |
---|
| 343 | if (src.type==TObj) |
---|
| 344 | return !(odata()==src.odata()); |
---|
| 345 | return 1; |
---|
| 346 | } |
---|
| 347 | default:; |
---|
| 348 | } |
---|
| 349 | return 1; |
---|
| 350 | } |
---|
| 351 | |
---|
| 352 | int ExtValue::operator==(const ExtValue& src) const |
---|
| 353 | { |
---|
| 354 | if (type!=src.type) return 0; |
---|
| 355 | switch(type) |
---|
| 356 | { |
---|
| 357 | case TInt: return idata()==src.idata(); |
---|
| 358 | case TDouble: return ddata()==src.ddata(); |
---|
| 359 | case TString: return sdata()==src.sdata(); |
---|
| 360 | case TObj: return odata()==src.odata(); |
---|
| 361 | default:; |
---|
| 362 | } |
---|
| 363 | return 1; |
---|
| 364 | } |
---|
| 365 | |
---|
| 366 | void ExtValue::operator+=(const ExtValue& src) |
---|
| 367 | { |
---|
| 368 | switch(type) |
---|
| 369 | { |
---|
| 370 | case TInt: idata()+=src.getInt(); break; |
---|
| 371 | case TDouble: ddata()+=src.getDouble(); break; |
---|
| 372 | case TString: sdata()+=src.getString(); break; |
---|
| 373 | case TObj: |
---|
| 374 | { |
---|
[171] | 375 | VectorObject *vec=VectorObject::fromObject(getObject(),false); |
---|
| 376 | VectorObject *vec2=VectorObject::fromObject(src.getObject(),false); |
---|
[109] | 377 | if (vec && vec2) |
---|
| 378 | { |
---|
| 379 | for(int i=0;i<vec2->data.size();i++) |
---|
| 380 | { |
---|
| 381 | ExtValue *s=(ExtValue*)vec2->data(i); |
---|
| 382 | ExtValue *d=s?new ExtValue(*s):NULL; |
---|
| 383 | vec->data+=d; |
---|
| 384 | } |
---|
[228] | 385 | return; |
---|
[109] | 386 | } |
---|
| 387 | } |
---|
[228] | 388 | //NO break; |
---|
| 389 | case TUnknown: |
---|
| 390 | case TInvalid: |
---|
| 391 | FMprintf("ExtValue","operator+=",FMLV_WARN,"Can't add %s to %s",(const char*)src.typeDescription(),(const char*)typeDescription()); |
---|
[109] | 392 | default:; |
---|
| 393 | } |
---|
| 394 | } |
---|
| 395 | |
---|
| 396 | void ExtValue::operator-=(const ExtValue& src) |
---|
| 397 | { |
---|
| 398 | switch(type) |
---|
| 399 | { |
---|
| 400 | case TInt: idata()-=src.getInt(); break; |
---|
| 401 | case TDouble: ddata()-=src.getDouble(); break; |
---|
[228] | 402 | case TString: case TObj: case TUnknown: case TInvalid: |
---|
| 403 | FMprintf("ExtValue","operator-=",FMLV_WARN,"Can't subtract %s from %s",(const char*)src.typeDescription(),(const char*)typeDescription()); |
---|
| 404 | break; |
---|
[109] | 405 | default:; |
---|
| 406 | } |
---|
| 407 | } |
---|
| 408 | |
---|
| 409 | void ExtValue::operator*=(const ExtValue& src) |
---|
| 410 | { |
---|
| 411 | switch(type) |
---|
| 412 | { |
---|
| 413 | case TInt: idata()*=src.getInt(); break; |
---|
| 414 | case TDouble: ddata()*=src.getDouble(); break; |
---|
| 415 | case TString: |
---|
| 416 | { |
---|
| 417 | SString t; |
---|
| 418 | for(int n=src.getInt();n>0;n--) |
---|
| 419 | t+=getString(); |
---|
| 420 | setString(t); |
---|
| 421 | break; |
---|
| 422 | } |
---|
| 423 | case TObj: |
---|
| 424 | { |
---|
[171] | 425 | VectorObject *vec=VectorObject::fromObject(getObject(),false); |
---|
[109] | 426 | if (vec) |
---|
| 427 | { |
---|
| 428 | int n=src.getInt(); |
---|
| 429 | int orig_size=vec->data.size(); |
---|
| 430 | if (n<=0) |
---|
| 431 | {vec->clear();return;} |
---|
| 432 | for(;n>1;n--) |
---|
| 433 | { |
---|
| 434 | for(int i=0;i<orig_size;i++) |
---|
| 435 | { |
---|
| 436 | ExtValue *s=(ExtValue*)vec->data(i); |
---|
| 437 | ExtValue *d=s?new ExtValue(*s):NULL; |
---|
| 438 | vec->data+=d; |
---|
| 439 | } |
---|
| 440 | } |
---|
[228] | 441 | return; |
---|
[109] | 442 | } |
---|
[228] | 443 | } |
---|
| 444 | //NO break; |
---|
| 445 | case TUnknown: case TInvalid: |
---|
| 446 | FMprintf("ExtValue","operator*=",FMLV_WARN,"Can't multiply %s by %s",(const char*)typeDescription(),(const char*)src.typeDescription()); |
---|
[109] | 447 | break; |
---|
| 448 | default:; |
---|
| 449 | } |
---|
| 450 | } |
---|
| 451 | |
---|
| 452 | #include <common/framsg.h> |
---|
| 453 | /*#include "fpu_control.h" |
---|
| 454 | #include <signal.h> |
---|
| 455 | |
---|
| 456 | static int fpuexception; |
---|
| 457 | void mathhandler(int sig) |
---|
| 458 | { |
---|
| 459 | printf("fpu exception!\n"); |
---|
| 460 | fpuexception=1; |
---|
| 461 | signal(SIGFPE,SIG_IGN); |
---|
| 462 | } */ |
---|
| 463 | |
---|
| 464 | void ExtValue::operator/=(const ExtValue& src) |
---|
| 465 | { |
---|
| 466 | switch(type) |
---|
| 467 | { |
---|
| 468 | case TInt: |
---|
| 469 | { int a=src.getInt(); |
---|
| 470 | // idata()/=src.getInt(); |
---|
| 471 | if (a) idata()/=a; |
---|
| 472 | else {FMprintf("ExtValue","divide",FMLV_CRITICAL,"%d/0",idata()); setInvalid();} |
---|
| 473 | } |
---|
| 474 | break; |
---|
| 475 | |
---|
| 476 | case TDouble: |
---|
| 477 | { |
---|
| 478 | double d=src.getDouble(); |
---|
| 479 | if (d==0.0) |
---|
| 480 | { |
---|
| 481 | FMprintf("ExtValue","divide",FMLV_CRITICAL,"%s/0.0",(const char*)getString()); |
---|
| 482 | setInvalid(); |
---|
| 483 | } |
---|
| 484 | else |
---|
| 485 | { |
---|
| 486 | fpExceptDisable(); |
---|
| 487 | double tmp=ddata()/d; |
---|
| 488 | if (!finite(tmp)) |
---|
| 489 | { FMprintf("ExtValue","divide",FMLV_CRITICAL,"overflow %s/%s",(const char*)getString(),(const char*)src.getString()); setInvalid(); } |
---|
| 490 | else |
---|
| 491 | ddata()=tmp; |
---|
| 492 | // niby dobrze ale lepiej byloby to robic bardziej systematycznie a nie tylko w dzieleniu? |
---|
| 493 | //if (isnan(ddata())) //http://www.digitalmars.com/d/archives/c++/Traping_divide_by_zero_5728.html |
---|
| 494 | // { FMprintf("ExtValue","divide",FMLV_ERROR,"not-a-number",(const char*)getString()); setInvalid(); } |
---|
| 495 | fpExceptEnable(); |
---|
| 496 | } |
---|
| 497 | } |
---|
| 498 | break; |
---|
| 499 | |
---|
[228] | 500 | case TString: case TObj: case TUnknown: case TInvalid: |
---|
| 501 | FMprintf("ExtValue","operator/=",FMLV_WARN,"Can't divide %s by %s",(const char*)typeDescription(),(const char*)src.typeDescription()); |
---|
| 502 | break; |
---|
| 503 | |
---|
[109] | 504 | default:; |
---|
| 505 | } |
---|
| 506 | } |
---|
| 507 | |
---|
| 508 | SString ExtValue::format(SString& fmt,const ExtValue **values,int count) |
---|
| 509 | { |
---|
| 510 | SString ret; |
---|
| 511 | // "..........%.........%..........%........" |
---|
| 512 | // ^_cur ^_next |
---|
| 513 | // ^^^^^^^^^^___sub |
---|
| 514 | // |
---|
| 515 | // "..........%.........%..........%........" |
---|
| 516 | // ^-cur ^-next |
---|
| 517 | // ^^^^^^^^^^___sub |
---|
| 518 | const char* begin=(const char*)fmt, *end=begin+fmt.len(), *curr=begin; |
---|
| 519 | int type=0; |
---|
| 520 | |
---|
| 521 | class Args |
---|
| 522 | { |
---|
| 523 | const ExtValue **values; |
---|
| 524 | int count; |
---|
| 525 | int arg; |
---|
| 526 | public: |
---|
| 527 | Args(const ExtValue **v,int c):values(v),count(c),arg(0) {} |
---|
| 528 | bool finished() {return arg>=count;} |
---|
| 529 | const ExtValue *getNext() {const ExtValue *ret=NULL; if ((arg<count)&&values[arg]) ret=values[arg]; arg++; return ret;} |
---|
| 530 | }; |
---|
| 531 | Args args(values,count); |
---|
| 532 | |
---|
| 533 | while(curr<end) |
---|
| 534 | { |
---|
| 535 | const char* next=strchr(curr,'%'); |
---|
| 536 | if (!next) next=end; else if ((next==curr)&&(curr>begin)) |
---|
| 537 | {next=strchr(next+1,'%'); if (!next) next=end;} |
---|
| 538 | type=0; |
---|
| 539 | if (curr>begin) |
---|
| 540 | { |
---|
| 541 | type=0; |
---|
| 542 | for(const char* t=curr;t<next;t++) |
---|
| 543 | switch(*t) |
---|
| 544 | { |
---|
| 545 | case 'd': case 'x': case 'X': case 'u': case 'p': case 'c': type='d'; t=next; break; |
---|
| 546 | case 'f': case 'g': case 'e': type='f'; t=next; break; |
---|
| 547 | case 's': type='s'; t=next; break; |
---|
| 548 | case 't': case 'T': type=*t; t=next; break; |
---|
| 549 | case '%': if (t>begin) {type=*t; t=next;} break; |
---|
| 550 | } |
---|
| 551 | } |
---|
| 552 | if (curr>begin) curr--; |
---|
| 553 | const ExtValue *a; |
---|
| 554 | if (args.finished() && (type!=0) && (type!='%')) |
---|
| 555 | { |
---|
[247] | 556 | ret+=fmt.substr((int)(curr-begin)); |
---|
[109] | 557 | break; |
---|
| 558 | } |
---|
[247] | 559 | SString sub=fmt.substr((int)(curr-begin),(int)(next-curr)); |
---|
[109] | 560 | switch(type) |
---|
| 561 | { |
---|
| 562 | case 'd': a=args.getNext(); ret+=SString::sprintf((const char*)sub,a?a->getInt():0); break; |
---|
| 563 | case 'f': a=args.getNext(); ret+=SString::sprintf((const char*)sub,a?a->getDouble():0); break; |
---|
| 564 | case 's': {a=args.getNext(); SString tmp; if (a) tmp=a->getString(); ret+=SString::sprintf((const char*)sub,(const char*)tmp);} break; |
---|
| 565 | case 't': case 'T': |
---|
| 566 | { |
---|
| 567 | a=args.getNext(); |
---|
| 568 | time_t ti=a?a->getInt():0; |
---|
| 569 | struct tm tm=Convert::localtime(ti); |
---|
| 570 | SString timtxt; |
---|
| 571 | if (type=='T') |
---|
| 572 | 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); |
---|
| 573 | else |
---|
| 574 | timtxt=Convert::asctime(tm).c_str(); |
---|
| 575 | ret+=timtxt; |
---|
| 576 | ret+=sub.substr(2); |
---|
| 577 | } |
---|
| 578 | break; |
---|
| 579 | case '%': ret+='%'; ret+=sub.substr(2); break; |
---|
| 580 | case 0: ret+=sub; break; |
---|
| 581 | } |
---|
| 582 | curr=next+1; |
---|
| 583 | } |
---|
| 584 | return ret; |
---|
| 585 | } |
---|
| 586 | |
---|
| 587 | |
---|
| 588 | void ExtValue::operator%=(const ExtValue& src) |
---|
| 589 | { |
---|
| 590 | switch(type) |
---|
| 591 | { |
---|
| 592 | case TInt: idata()=idata()%src.getInt(); break; |
---|
| 593 | case TDouble: ddata()=fmod(ddata(),src.getDouble()); break; |
---|
| 594 | |
---|
| 595 | case TString: |
---|
| 596 | { |
---|
[171] | 597 | VectorObject *vec=VectorObject::fromObject(src.getObject(),false); |
---|
[109] | 598 | if (vec) |
---|
| 599 | sdata()=format(sdata(),(const ExtValue**)&vec->data.getref(0),vec->data.size()); |
---|
| 600 | else |
---|
| 601 | {const ExtValue *ptr=&src; sdata()=ExtValue::format(sdata(),&ptr,1);} |
---|
| 602 | } |
---|
| 603 | break; |
---|
| 604 | |
---|
[228] | 605 | case TObj: case TUnknown: case TInvalid: |
---|
| 606 | FMprintf("ExtValue","operator%=",FMLV_WARN,"Can't apply modulo to %s",(const char*)typeDescription()); |
---|
| 607 | |
---|
[109] | 608 | default:; |
---|
| 609 | } |
---|
| 610 | } |
---|
| 611 | |
---|
[326] | 612 | bool ExtValue::parseInt(const char* s, paInt &result, bool strict, bool error) |
---|
[144] | 613 | { |
---|
[325] | 614 | ExtValue tmp; |
---|
| 615 | const char* after = tmp.parseNumber(s, strict ? TInt : TUnknown); |
---|
[326] | 616 | if ((after == NULL) || (after[0] != 0)) |
---|
| 617 | { |
---|
| 618 | if (error) |
---|
| 619 | FMprintf("ExtValue", "parseInt", FMLV_ERROR, "Could not parse '%s'%s", s, strict?" (strict)":""); |
---|
| 620 | return false; |
---|
| 621 | } |
---|
[325] | 622 | result = tmp.getInt(); |
---|
| 623 | return true; |
---|
[144] | 624 | } |
---|
| 625 | |
---|
[326] | 626 | bool ExtValue::parseDouble(const char* s, double &result, bool error) |
---|
[325] | 627 | { |
---|
| 628 | ExtValue tmp; |
---|
| 629 | const char* after = tmp.parseNumber(s, TDouble); |
---|
[326] | 630 | if ((after == NULL) || (after[0] != 0)) |
---|
| 631 | { |
---|
| 632 | if (error) |
---|
| 633 | FMprintf("ExtValue", "parseDouble", FMLV_ERROR, "Could not parse '%s'", s); |
---|
| 634 | return false; |
---|
| 635 | } |
---|
[325] | 636 | result = tmp.getDouble(); |
---|
| 637 | return true; |
---|
| 638 | } |
---|
| 639 | |
---|
| 640 | paInt ExtValue::getInt(const char* s,bool strict) |
---|
| 641 | { |
---|
| 642 | paInt result; |
---|
[326] | 643 | if (parseInt(s, result, strict, true)) |
---|
[325] | 644 | return result; |
---|
| 645 | return 0; |
---|
| 646 | } |
---|
| 647 | |
---|
[144] | 648 | double ExtValue::getDouble(const char* s) |
---|
| 649 | { |
---|
[325] | 650 | double result; |
---|
[326] | 651 | if (parseDouble(s, result, true)) |
---|
[325] | 652 | return result; |
---|
| 653 | return 0; |
---|
[144] | 654 | } |
---|
| 655 | |
---|
[247] | 656 | paInt ExtValue::getInt() const |
---|
[109] | 657 | { |
---|
[325] | 658 | switch (type) |
---|
[109] | 659 | { |
---|
| 660 | case TInt: return idata(); |
---|
| 661 | case TDouble: return (int)ddata(); |
---|
[144] | 662 | case TString: return getInt((const char*)sdata()); |
---|
| 663 | case TObj: |
---|
[325] | 664 | FMprintf("ExtValue", "getInt", FMLV_WARN, "Getting integer value from object reference (%s)", (const char*)getString()); |
---|
[247] | 665 | return (paInt)(intptr_t)odata().param; |
---|
[109] | 666 | default:; |
---|
| 667 | } |
---|
[325] | 668 | return 0; |
---|
[109] | 669 | } |
---|
[144] | 670 | |
---|
[109] | 671 | double ExtValue::getDouble() const |
---|
| 672 | { |
---|
[325] | 673 | switch (type) |
---|
[109] | 674 | { |
---|
| 675 | case TDouble: return ddata(); |
---|
| 676 | case TInt: return (double)idata(); |
---|
[144] | 677 | case TString: return getDouble((const char*)sdata()); |
---|
| 678 | case TObj: |
---|
[325] | 679 | FMprintf("ExtValue", "getDouble", FMLV_WARN, "Getting floating point value from object reference (%s)", (const char*)getString()); |
---|
[247] | 680 | return (double)(intptr_t)odata().param; |
---|
[109] | 681 | default:; |
---|
| 682 | } |
---|
[325] | 683 | return 0.0; |
---|
[109] | 684 | } |
---|
[325] | 685 | |
---|
[109] | 686 | SString ExtValue::getString() const |
---|
| 687 | { |
---|
[325] | 688 | switch (type) |
---|
[109] | 689 | { |
---|
| 690 | case TString: return sdata(); |
---|
| 691 | case TInt: return SString::valueOf(idata()); |
---|
| 692 | case TDouble: return SString::valueOf(ddata()); |
---|
| 693 | case TObj: return odata().toString(); |
---|
| 694 | case TInvalid: return SString("invalid"); |
---|
| 695 | default: return SString("null"); |
---|
| 696 | } |
---|
| 697 | } |
---|
| 698 | |
---|
| 699 | const SString* ExtValue::getStringPtr() const |
---|
| 700 | { |
---|
[325] | 701 | if (type == TString) |
---|
| 702 | return &sdata(); |
---|
| 703 | return NULL; |
---|
[109] | 704 | } |
---|
| 705 | |
---|
| 706 | SString ExtValue::serialize() const |
---|
| 707 | { |
---|
| 708 | switch(type) |
---|
| 709 | { |
---|
| 710 | case TString: |
---|
| 711 | { |
---|
| 712 | SString q=sdata(); |
---|
| 713 | sstringQuote(q); |
---|
| 714 | return SString("\"")+q+SString("\""); |
---|
| 715 | } |
---|
| 716 | case TInt: |
---|
| 717 | return SString::valueOf(idata()); |
---|
| 718 | case TDouble: |
---|
| 719 | return SString::valueOf(ddata()); |
---|
| 720 | case TObj: |
---|
| 721 | return odata().serialize(); |
---|
| 722 | case TInvalid: |
---|
| 723 | return SString("invalid"); |
---|
| 724 | default: |
---|
| 725 | return SString("null"); |
---|
| 726 | } |
---|
| 727 | } |
---|
| 728 | |
---|
[325] | 729 | /// returns the first character after the parsed number, or NULL if not a number |
---|
| 730 | /// @param strict_type = restrict the allowed return value (TUnknown = unrestricted) |
---|
| 731 | const char* ExtValue::parseNumber(const char* in, ExtPType strict_type) |
---|
[109] | 732 | { |
---|
[325] | 733 | char* after; |
---|
| 734 | if (in == NULL) return NULL; |
---|
| 735 | if (in[0] == 0) return NULL; |
---|
| 736 | while (isspace(*in)) in++; |
---|
[326] | 737 | bool minus = (in[0] == '-'); |
---|
| 738 | bool plus = (in[0] == '+'); |
---|
| 739 | if (((in[0] == '0') && ((in[1] == 'x') || (in[1]=='X'))) |
---|
| 740 | || (((minus || plus) && (in[1] == '0') && ((in[2] == 'x') || (in[2] == 'X'))))) |
---|
[109] | 741 | { |
---|
[326] | 742 | in += (minus || plus) ? 3 : 2; |
---|
[325] | 743 | if (isspace(*in)) return NULL; |
---|
| 744 | errno = 0; |
---|
| 745 | unsigned long intvalue = strtoul(in, &after, 16); |
---|
| 746 | if ((after > in) && (errno == 0) && (intvalue <= 0xffffffff)) |
---|
[109] | 747 | { |
---|
[325] | 748 | if (strict_type == TDouble) |
---|
| 749 | setDouble(minus ? -(double)intvalue : (double)intvalue); |
---|
| 750 | else |
---|
| 751 | setInt(minus ? -intvalue : intvalue); |
---|
| 752 | return after; |
---|
[109] | 753 | } |
---|
[325] | 754 | else |
---|
| 755 | return NULL; |
---|
| 756 | } |
---|
[109] | 757 | |
---|
[325] | 758 | errno = 0; |
---|
| 759 | double fpvalue = strtod(in, &after); |
---|
| 760 | if ((after > in) && (errno == 0)) |
---|
| 761 | { |
---|
| 762 | if (strict_type != TDouble) |
---|
[109] | 763 | { |
---|
[325] | 764 | if ((memchr(in, '.', after - in) == NULL) && (memchr(in, 'e', after - in) == NULL) && (memchr(in, 'E', after - in) == NULL) // no "special" characters |
---|
| 765 | && (fpvalue == floor(fpvalue)) // value is integer |
---|
| 766 | && (fpvalue >= INT_MIN) && (fpvalue <= INT_MAX)) // within limits |
---|
| 767 | { |
---|
| 768 | setInt(fpvalue); |
---|
| 769 | return after; |
---|
| 770 | } |
---|
| 771 | else if (strict_type == TInt) |
---|
| 772 | return NULL; |
---|
[109] | 773 | } |
---|
[325] | 774 | setDouble(fpvalue); |
---|
| 775 | return after; |
---|
[109] | 776 | } |
---|
[325] | 777 | return NULL; |
---|
[109] | 778 | } |
---|
| 779 | |
---|
[222] | 780 | PtrListTempl<ParamInterface*> &ExtValue::getDeserializableClasses() |
---|
[109] | 781 | { |
---|
[222] | 782 | static PtrListTempl<ParamInterface*> classes; |
---|
| 783 | return classes; |
---|
[109] | 784 | } |
---|
| 785 | |
---|
| 786 | ParamInterface *ExtValue::findDeserializableClass(const char* name) |
---|
| 787 | { |
---|
[222] | 788 | FOREACH(ParamInterface*,cls,getDeserializableClasses()) |
---|
[109] | 789 | if (!strcmp(cls->getName(),name)) |
---|
| 790 | return cls; |
---|
| 791 | return NULL; |
---|
| 792 | } |
---|
| 793 | |
---|
| 794 | static const char* skipWord(const char* in) |
---|
| 795 | { |
---|
| 796 | while(isalpha(*in)||(*in=='_')) |
---|
| 797 | in++; |
---|
| 798 | return in; |
---|
| 799 | } |
---|
| 800 | |
---|
| 801 | //returns the first character after the parsed portion or NULL if invalid format |
---|
| 802 | const char* ExtValue::deserialize_inner(const char* in) |
---|
| 803 | { |
---|
| 804 | const char* ret=parseNumber(in); |
---|
| 805 | if (ret) |
---|
| 806 | return ret; |
---|
| 807 | else if (*in=='\"') |
---|
| 808 | { |
---|
| 809 | ret=skipQuoteString(in+1,NULL); |
---|
[247] | 810 | SString s(in+1,(int)(ret-(in+1))); |
---|
[109] | 811 | sstringUnquote(s); |
---|
| 812 | setString(s); |
---|
| 813 | if (*ret=='\"') |
---|
| 814 | return ret+1; |
---|
| 815 | else |
---|
| 816 | return NULL; |
---|
| 817 | } |
---|
| 818 | else if (*in=='[') |
---|
| 819 | { |
---|
| 820 | VectorObject *vec=new VectorObject; |
---|
| 821 | ExtObject o(&VectorObject::par,vec); |
---|
| 822 | tlsGetRef(ExtObject::serialization).add(o); |
---|
| 823 | const char* p=in+1; |
---|
| 824 | ExtValue tmp; |
---|
| 825 | while(*p) |
---|
| 826 | { |
---|
| 827 | if (*p==']') {p++;break;} |
---|
| 828 | ret=tmp.deserialize(p); |
---|
| 829 | if (ret) |
---|
| 830 | { |
---|
| 831 | vec->data+=new ExtValue(tmp); |
---|
| 832 | p=ret; |
---|
| 833 | if (*p==',') p++; |
---|
| 834 | } |
---|
| 835 | else |
---|
| 836 | { |
---|
| 837 | p=NULL; |
---|
| 838 | break; |
---|
| 839 | } |
---|
| 840 | } |
---|
| 841 | setObject(o); |
---|
| 842 | return p; |
---|
| 843 | } |
---|
| 844 | else if (*in=='{') |
---|
| 845 | { |
---|
| 846 | DictionaryObject *dic=new DictionaryObject; |
---|
| 847 | ExtObject o(&DictionaryObject::par,dic); |
---|
| 848 | tlsGetRef(ExtObject::serialization).add(o); |
---|
| 849 | const char* p=in+1; |
---|
| 850 | ExtValue args[2]/*={value,key}*/, dummy_ret; |
---|
| 851 | while(*p) |
---|
| 852 | { |
---|
| 853 | if (*p=='}') {p++;break;} |
---|
| 854 | ret=args[1].deserialize(p); |
---|
| 855 | if ((!ret)||(args[1].getType()!=TString)) {p=NULL;break;} |
---|
| 856 | p=ret; |
---|
| 857 | if (*p!=':') {p=NULL;break;} |
---|
| 858 | p++; |
---|
| 859 | ret=args[0].deserialize(p); |
---|
| 860 | if (!ret) {p=NULL;break;} |
---|
| 861 | p=ret; |
---|
| 862 | dic->p_set(args,&dummy_ret); |
---|
| 863 | if (*p==',') p++; |
---|
| 864 | } |
---|
| 865 | setObject(o); |
---|
| 866 | return p; |
---|
| 867 | } |
---|
| 868 | else if (!strncmp(in,"null",4)) |
---|
| 869 | { |
---|
| 870 | setEmpty(); |
---|
| 871 | return in+4; |
---|
| 872 | } |
---|
| 873 | else if (!strncmp(in,"invalid",9)) |
---|
| 874 | { |
---|
| 875 | setInvalid(); |
---|
| 876 | return in+9; |
---|
| 877 | } |
---|
| 878 | else if (*in=='<') |
---|
| 879 | { //unserializable object |
---|
| 880 | setInvalid(); |
---|
| 881 | while(*in) |
---|
| 882 | if (*in=='>') |
---|
| 883 | return in+1; |
---|
| 884 | else in++; |
---|
| 885 | return in; |
---|
| 886 | } |
---|
| 887 | else if (*in=='^') |
---|
| 888 | { |
---|
| 889 | in++; |
---|
| 890 | ExtValue ref; |
---|
[325] | 891 | ret=ref.parseNumber(in,TInt); |
---|
[109] | 892 | if (ret && (ref.getType()==TInt)) |
---|
| 893 | { |
---|
| 894 | const ExtObject* o=tlsGetRef(ExtObject::serialization).get(ref.getInt()); |
---|
| 895 | if (o) |
---|
| 896 | { |
---|
| 897 | setObject(*o); |
---|
| 898 | return ret; |
---|
| 899 | } |
---|
| 900 | } |
---|
| 901 | return NULL; |
---|
| 902 | } |
---|
| 903 | else if ((ret=skipWord(in))&&(ret!=in)) |
---|
| 904 | { |
---|
[247] | 905 | SString clsname(in,(int)(ret-in)); |
---|
[109] | 906 | ExtValue tmp; |
---|
| 907 | ret=tmp.deserialize(ret); |
---|
| 908 | ParamInterface *cls=findDeserializableClass(clsname); |
---|
| 909 | if (cls && (tmp.getType()!=TUnknown) && (tmp.getType()!=TInvalid)) |
---|
| 910 | { |
---|
[171] | 911 | VectorObject *vec=VectorObject::fromObject(tmp.getObject(),false); |
---|
[109] | 912 | if (vec) |
---|
| 913 | { |
---|
| 914 | int m=cls->findId("newFromVector"); |
---|
| 915 | if (m>=0) |
---|
| 916 | { |
---|
| 917 | cls->call(m,&tmp,this); |
---|
| 918 | tlsGetRef(ExtObject::serialization).replace(tmp.getObject(),getObject()); |
---|
| 919 | return ret; |
---|
| 920 | } |
---|
| 921 | } |
---|
[171] | 922 | DictionaryObject *dic=DictionaryObject::fromObject(tmp.getObject(),false); |
---|
[109] | 923 | if (dic) |
---|
| 924 | { |
---|
| 925 | int m=cls->findId("newFromDictionary"); |
---|
| 926 | if (m>=0) |
---|
| 927 | { |
---|
| 928 | cls->call(m,&tmp,this); |
---|
| 929 | tlsGetRef(ExtObject::serialization).replace(tmp.getObject(),getObject()); |
---|
| 930 | return ret; |
---|
| 931 | } |
---|
| 932 | } |
---|
| 933 | if (tmp.getType()==TString) |
---|
| 934 | { |
---|
| 935 | int m=cls->findId("newFromString"); |
---|
| 936 | if (m>=0) |
---|
| 937 | { |
---|
| 938 | cls->call(m,&tmp,this); |
---|
| 939 | tlsGetRef(ExtObject::serialization).replace(tmp.getObject(),getObject()); |
---|
| 940 | return ret; |
---|
| 941 | } |
---|
| 942 | } |
---|
| 943 | tlsGetRef(ExtObject::serialization).remove(tmp.getObject()); |
---|
| 944 | setEmpty(); |
---|
| 945 | } |
---|
| 946 | setEmpty(); |
---|
| 947 | FMprintf("ExtValue","deserialize",FMLV_WARN,"object of class \"%s\" could not be deserialized",(const char*)clsname); |
---|
| 948 | return ret; |
---|
| 949 | } |
---|
| 950 | setEmpty(); |
---|
| 951 | return NULL; |
---|
| 952 | } |
---|
| 953 | |
---|
| 954 | const char* ExtValue::deserialize(const char* in) |
---|
| 955 | { |
---|
| 956 | tlsGetRef(ExtObject::serialization).begin(); |
---|
| 957 | const char* ret=deserialize_inner(in); |
---|
| 958 | tlsGetRef(ExtObject::serialization).end(); |
---|
| 959 | return ret; |
---|
| 960 | } |
---|
| 961 | |
---|
| 962 | ExtObject ExtValue::getObject() const |
---|
| 963 | { |
---|
| 964 | if (type==TObj) return odata(); |
---|
| 965 | return ExtObject(); |
---|
| 966 | } |
---|
| 967 | |
---|
| 968 | ExtValue ExtValue::getExtType() |
---|
| 969 | { |
---|
[247] | 970 | if (getType()!=TObj) return ExtValue((paInt)getType()); |
---|
[109] | 971 | ExtObject& o=odata(); |
---|
| 972 | return ExtValue(SString(o.isEmpty()?"":o.interfaceName())); |
---|
| 973 | } |
---|
| 974 | |
---|
| 975 | SString SString::valueOf(const ExtValue& v) |
---|
| 976 | { |
---|
| 977 | return v.getString(); |
---|
| 978 | } |
---|
| 979 | SString SString::valueOf(const ExtObject& v) |
---|
| 980 | { |
---|
| 981 | return v.toString(); |
---|
| 982 | } |
---|