| 1 | // This file is a part of Framsticks GDK library. |
|---|
| 2 | // Copyright (C) 2002-2006 Szymon Ulatowski. See LICENSE.txt for details. |
|---|
| 3 | // Refer to http://www.frams.alife.pl/ for further information. |
|---|
| 4 | |
|---|
| 5 | #include "extvalue.h" |
|---|
| 6 | #include "param.h" |
|---|
| 7 | #include <math.h> |
|---|
| 8 | |
|---|
| 9 | SString ExtObject::toString() |
|---|
| 10 | { |
|---|
| 11 | if (isEmpty()) return SString("<empty object>"); |
|---|
| 12 | ParamInterface *p=getParamInterface(); |
|---|
| 13 | int tostr=p->findId("toString"); |
|---|
| 14 | if (tostr>=0) |
|---|
| 15 | { |
|---|
| 16 | return SString(p->getString(tostr)); |
|---|
| 17 | } |
|---|
| 18 | else |
|---|
| 19 | { |
|---|
| 20 | SString tmp("<"); |
|---|
| 21 | tmp+=p->getName(); |
|---|
| 22 | sprintf(tmp.directAppend(30)," object at %p>",object); |
|---|
| 23 | tmp.endAppend(); |
|---|
| 24 | return tmp; |
|---|
| 25 | } |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | /////////////////////////////////////// |
|---|
| 29 | |
|---|
| 30 | void ExtValue::set(const ExtValue& src) |
|---|
| 31 | { |
|---|
| 32 | switch(src.type) |
|---|
| 33 | { |
|---|
| 34 | case TString: sets(src.sdata()); break; |
|---|
| 35 | case TInt: seti(src.idata()); break; |
|---|
| 36 | case TDouble: setd(src.ddata()); break; |
|---|
| 37 | case TObj: seto(src.odata()); break; |
|---|
| 38 | } |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | void ExtValue::setEmpty() |
|---|
| 42 | { |
|---|
| 43 | switch(type) |
|---|
| 44 | { |
|---|
| 45 | #ifdef EXTVALUEUNION |
|---|
| 46 | case TString: sdata().~SString(); break; |
|---|
| 47 | case TObj: odata().~ExtObject(); break; |
|---|
| 48 | #else |
|---|
| 49 | case TString: delete s; break; |
|---|
| 50 | case TObj: delete o; break; |
|---|
| 51 | #endif |
|---|
| 52 | } |
|---|
| 53 | type=TUnknown; |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | static long longsign(long x) |
|---|
| 57 | { |
|---|
| 58 | if (x<0) return -1; |
|---|
| 59 | if (x>0) return 1; |
|---|
| 60 | return 0; |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | long ExtValue::compare(const ExtValue& src) const |
|---|
| 64 | { |
|---|
| 65 | if (type==TUnknown) |
|---|
| 66 | { |
|---|
| 67 | if (src.type==TDouble) |
|---|
| 68 | return (src.getDouble()!=0.0); |
|---|
| 69 | if (src.type==TString) |
|---|
| 70 | return 1; |
|---|
| 71 | return src.getInt()?1:0; |
|---|
| 72 | } |
|---|
| 73 | else if (src.type==TUnknown) |
|---|
| 74 | { |
|---|
| 75 | if (type==TDouble) |
|---|
| 76 | return (getDouble()!=0.0); |
|---|
| 77 | if (type==TString) |
|---|
| 78 | return 1; |
|---|
| 79 | return getInt()?1:0; |
|---|
| 80 | } |
|---|
| 81 | switch(type) |
|---|
| 82 | { |
|---|
| 83 | case TInt: |
|---|
| 84 | { |
|---|
| 85 | long t=src.getInt(); |
|---|
| 86 | if (idata()>0) |
|---|
| 87 | {if (t>0) return longsign(idata()-t); else return +1;} |
|---|
| 88 | else |
|---|
| 89 | {if (t<=0) return longsign(idata()-t); else return -1;} |
|---|
| 90 | } |
|---|
| 91 | case TDouble: |
|---|
| 92 | { |
|---|
| 93 | double t=ddata()-src.getDouble(); |
|---|
| 94 | if (t<0) return -1; |
|---|
| 95 | else if (t>0) return 1; |
|---|
| 96 | return 0; |
|---|
| 97 | } |
|---|
| 98 | case TString: |
|---|
| 99 | { |
|---|
| 100 | SString t=src.getString(); |
|---|
| 101 | SString& t2=sdata(); |
|---|
| 102 | const char* s1=(const char*)t2; |
|---|
| 103 | const char* s2=(const char*)t; |
|---|
| 104 | return longsign(strcmp(s1,s2)); |
|---|
| 105 | } |
|---|
| 106 | case TObj: |
|---|
| 107 | { |
|---|
| 108 | if (src.type==TObj) |
|---|
| 109 | return !(odata()==src.odata()); |
|---|
| 110 | return 1; |
|---|
| 111 | } |
|---|
| 112 | } |
|---|
| 113 | return 1; |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | int ExtValue::operator==(const ExtValue& src) const |
|---|
| 117 | { |
|---|
| 118 | if (type!=src.type) return 0; |
|---|
| 119 | switch(type) |
|---|
| 120 | { |
|---|
| 121 | case TInt: return idata()==src.idata(); |
|---|
| 122 | case TDouble: return ddata()==src.ddata(); |
|---|
| 123 | case TString: return sdata()==src.sdata(); |
|---|
| 124 | case TObj: return odata()==src.odata(); |
|---|
| 125 | } |
|---|
| 126 | return 1; |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | void ExtValue::operator+=(const ExtValue& src) |
|---|
| 130 | { |
|---|
| 131 | switch(type) |
|---|
| 132 | { |
|---|
| 133 | case TInt: idata()+=src.getInt(); break; |
|---|
| 134 | case TDouble: ddata()+=src.getDouble(); break; |
|---|
| 135 | case TString: sdata()+=src.getString(); break; |
|---|
| 136 | } |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | void ExtValue::operator-=(const ExtValue& src) |
|---|
| 140 | { |
|---|
| 141 | switch(type) |
|---|
| 142 | { |
|---|
| 143 | case TInt: idata()-=src.getInt(); break; |
|---|
| 144 | case TDouble: ddata()-=src.getDouble(); break; |
|---|
| 145 | } |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | void ExtValue::operator*=(const ExtValue& src) |
|---|
| 149 | { |
|---|
| 150 | switch(type) |
|---|
| 151 | { |
|---|
| 152 | case TInt: idata()*=src.getInt(); break; |
|---|
| 153 | case TDouble: ddata()*=src.getDouble(); break; |
|---|
| 154 | } |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | #include "framsg.h" |
|---|
| 158 | /*#include "fpu_control.h" |
|---|
| 159 | #include <signal.h> |
|---|
| 160 | |
|---|
| 161 | static int fpuexception; |
|---|
| 162 | void mathhandler(int sig) |
|---|
| 163 | { |
|---|
| 164 | printf("fpu exception!\n"); |
|---|
| 165 | fpuexception=1; |
|---|
| 166 | signal(SIGFPE,SIG_IGN); |
|---|
| 167 | } */ |
|---|
| 168 | |
|---|
| 169 | void ExtValue::operator/=(const ExtValue& src) |
|---|
| 170 | { |
|---|
| 171 | switch(type) |
|---|
| 172 | { |
|---|
| 173 | case TInt: |
|---|
| 174 | { int a=src.getInt(); |
|---|
| 175 | // idata()/=src.getInt(); |
|---|
| 176 | if (a) idata()/=a; |
|---|
| 177 | else FMprintf("ExtValue","divide",FMLV_ERROR,"%s/0",(const char*)getString()); |
|---|
| 178 | } |
|---|
| 179 | break; |
|---|
| 180 | |
|---|
| 181 | case TDouble: |
|---|
| 182 | // ugly ;-( |
|---|
| 183 | #ifdef FPU_THROWS_EXCEPTIONS |
|---|
| 184 | try |
|---|
| 185 | { |
|---|
| 186 | ddata()/=src.getDouble(); |
|---|
| 187 | } |
|---|
| 188 | catch(...) |
|---|
| 189 | { |
|---|
| 190 | FMprintf("ExtValue","divide",FMLV_ERROR,"%s/0.0",(const char*)getString()); |
|---|
| 191 | } |
|---|
| 192 | #else |
|---|
| 193 | { |
|---|
| 194 | double d=ddata(); |
|---|
| 195 | d/=src.getDouble(); |
|---|
| 196 | if (finite(d)) |
|---|
| 197 | ddata()=d; |
|---|
| 198 | else |
|---|
| 199 | FMprintf("ExtValue","divide",FMLV_ERROR,"%s/0.0",(const char*)getString()); |
|---|
| 200 | } |
|---|
| 201 | #endif |
|---|
| 202 | break; |
|---|
| 203 | } |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | void ExtValue::operator%=(const ExtValue& src) |
|---|
| 207 | { |
|---|
| 208 | switch(type) |
|---|
| 209 | { |
|---|
| 210 | case TInt: idata()=idata()%src.getInt(); break; |
|---|
| 211 | case TDouble: ddata()=fmod(ddata(),src.getDouble()); break; |
|---|
| 212 | } |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | long ExtValue::getInt() const |
|---|
| 216 | { |
|---|
| 217 | switch(type) |
|---|
| 218 | { |
|---|
| 219 | case TInt: return idata(); |
|---|
| 220 | case TDouble: return (int)ddata(); |
|---|
| 221 | case TString: |
|---|
| 222 | { |
|---|
| 223 | const char* s=(const char*)sdata(); |
|---|
| 224 | if ((s[0]=='0')&&(s[1]=='x')) |
|---|
| 225 | { |
|---|
| 226 | long val; |
|---|
| 227 | sscanf(s+2,"%lx",&val); |
|---|
| 228 | return val; |
|---|
| 229 | } |
|---|
| 230 | else |
|---|
| 231 | { |
|---|
| 232 | if (strchr(s,'e')||(strchr(s,'E'))) |
|---|
| 233 | return atof(s); |
|---|
| 234 | else |
|---|
| 235 | return atol(s); |
|---|
| 236 | } |
|---|
| 237 | } |
|---|
| 238 | case TObj: return (long)odata().param; |
|---|
| 239 | } |
|---|
| 240 | return 0; |
|---|
| 241 | } |
|---|
| 242 | double ExtValue::getDouble() const |
|---|
| 243 | { |
|---|
| 244 | switch(type) |
|---|
| 245 | { |
|---|
| 246 | case TDouble: return ddata(); |
|---|
| 247 | case TInt: return (double)idata(); |
|---|
| 248 | case TString: |
|---|
| 249 | { |
|---|
| 250 | const char* s=(const char*)sdata(); |
|---|
| 251 | if ((s[0]=='0')&&(s[1]=='x')) |
|---|
| 252 | { |
|---|
| 253 | long val; |
|---|
| 254 | sscanf(s+2,"%lx",&val); |
|---|
| 255 | return val; |
|---|
| 256 | } |
|---|
| 257 | else |
|---|
| 258 | return atof(s); |
|---|
| 259 | } |
|---|
| 260 | case TObj: return (double)(long)odata().param; |
|---|
| 261 | } |
|---|
| 262 | return 0.0; |
|---|
| 263 | } |
|---|
| 264 | SString ExtValue::getString() const |
|---|
| 265 | { |
|---|
| 266 | switch(type) |
|---|
| 267 | { |
|---|
| 268 | case TString: return sdata(); |
|---|
| 269 | case TInt: |
|---|
| 270 | { |
|---|
| 271 | SString tmp; |
|---|
| 272 | sprintf(tmp.directAppend(20),"%d",idata()); |
|---|
| 273 | tmp.endAppend(); |
|---|
| 274 | return tmp; |
|---|
| 275 | } |
|---|
| 276 | case TDouble: |
|---|
| 277 | { |
|---|
| 278 | SString tmp; |
|---|
| 279 | sprintf(tmp.directAppend(20),"%g",ddata()); |
|---|
| 280 | tmp.endAppend(); |
|---|
| 281 | if ((!strchr(tmp,'.'))&&(!strchr(tmp,'e'))) tmp+=".0"; |
|---|
| 282 | return tmp; |
|---|
| 283 | } |
|---|
| 284 | case TObj: |
|---|
| 285 | return odata().toString(); |
|---|
| 286 | default: |
|---|
| 287 | return SString("null"); |
|---|
| 288 | } |
|---|
| 289 | } |
|---|
| 290 | |
|---|
| 291 | ExtObject ExtValue::getObject() const |
|---|
| 292 | { |
|---|
| 293 | if (type==TObj) return odata(); |
|---|
| 294 | return ExtObject(); |
|---|
| 295 | } |
|---|
| 296 | |
|---|
| 297 | ExtValue ExtValue::getExtType() |
|---|
| 298 | { |
|---|
| 299 | if (getType()!=TObj) return ExtValue((long)getType()); |
|---|
| 300 | ExtObject& o=odata(); |
|---|
| 301 | return ExtValue(SString(o.isEmpty()?"":o.getParamInterface()->getName())); |
|---|
| 302 | } |
|---|