| 1 | #ifdef RCSID |
| 2 | static char RCSid[] = |
| 3 | "$Header$"; |
| 4 | #endif |
| 5 | |
| 6 | /* |
| 7 | * Copyright (c) 2012 Michael J. Roberts. All Rights Reserved. |
| 8 | * |
| 9 | * Please see the accompanying license file, LICENSE.TXT, for information |
| 10 | * on using and copying this software. |
| 11 | */ |
| 12 | /* |
| 13 | Name |
| 14 | vmtzobj.cpp - CVmObjTimeZone object |
| 15 | Function |
| 16 | |
| 17 | Notes |
| 18 | |
| 19 | Modified |
| 20 | 02/06/12 MJRoberts - Creation |
| 21 | */ |
| 22 | |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <assert.h> |
| 26 | |
| 27 | #include "t3std.h" |
| 28 | #include "vmtzobj.h" |
| 29 | #include "vmerr.h" |
| 30 | #include "vmerrnum.h" |
| 31 | #include "vmtype.h" |
| 32 | #include "vmglob.h" |
| 33 | #include "vmobj.h" |
| 34 | #include "vmundo.h" |
| 35 | #include "vmmcreg.h" |
| 36 | #include "vmfile.h" |
| 37 | #include "vmbif.h" |
| 38 | #include "vmmeta.h" |
| 39 | #include "vmstack.h" |
| 40 | #include "vmrun.h" |
| 41 | #include "vmstr.h" |
| 42 | #include "vmlst.h" |
| 43 | #include "vmtz.h" |
| 44 | #include "vmdate.h" |
| 45 | #include "utf8.h" |
| 46 | |
| 47 | |
| 48 | /* ------------------------------------------------------------------------ */ |
| 49 | /* |
| 50 | * Allocate an extension structure |
| 51 | */ |
| 52 | vm_tzobj_ext *vm_tzobj_ext::alloc_ext(VMG_ CVmObjTimeZone *self, |
| 53 | CVmTimeZone *tz) |
| 54 | { |
| 55 | /* calculate how much space we need */ |
| 56 | size_t siz = sizeof(vm_tzobj_ext); |
| 57 | |
| 58 | /* allocate the memory */ |
| 59 | vm_tzobj_ext *ext = (vm_tzobj_ext *)G_mem->get_var_heap()->alloc_mem( |
| 60 | siz, self); |
| 61 | |
| 62 | /* save our TimeZone object */ |
| 63 | ext->tz = tz; |
| 64 | |
| 65 | /* return the new extension */ |
| 66 | return ext; |
| 67 | } |
| 68 | |
| 69 | /* ------------------------------------------------------------------------ */ |
| 70 | /* |
| 71 | * CVmObjTimeZone object statics |
| 72 | */ |
| 73 | |
| 74 | /* metaclass registration object */ |
| 75 | static CVmMetaclassTimeZone metaclass_reg_obj; |
| 76 | CVmMetaclass *CVmObjTimeZone::metaclass_reg_ = &metaclass_reg_obj; |
| 77 | |
| 78 | /* function table */ |
| 79 | int (CVmObjTimeZone::*CVmObjTimeZone::func_table_[])( |
| 80 | VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc) = |
| 81 | { |
| 82 | &CVmObjTimeZone::getp_undef, /* 0 */ |
| 83 | &CVmObjTimeZone::getp_getNames, /* 1 */ |
| 84 | &CVmObjTimeZone::getp_getHistory, /* 2 */ |
| 85 | &CVmObjTimeZone::getp_getRules, /* 3 */ |
| 86 | &CVmObjTimeZone::getp_getLocation /* 4 */ |
| 87 | }; |
| 88 | |
| 89 | |
| 90 | /* ------------------------------------------------------------------------ */ |
| 91 | /* |
| 92 | * construction |
| 93 | */ |
| 94 | CVmObjTimeZone::CVmObjTimeZone(VMG_ CVmTimeZone *tz) |
| 95 | { |
| 96 | /* allocate our extension structure */ |
| 97 | ext_ = (char *)vm_tzobj_ext::alloc_ext(vmg_ this, tz); |
| 98 | } |
| 99 | |
| 100 | /* ------------------------------------------------------------------------ */ |
| 101 | /* |
| 102 | * create dynamically using stack arguments |
| 103 | */ |
| 104 | vm_obj_id_t CVmObjTimeZone::create_from_stack( |
| 105 | VMG_ const uchar **pc_ptr, uint argc) |
| 106 | { |
| 107 | /* parse the constructor arguments */ |
| 108 | CVmTimeZone *tz = parse_ctor_args(vmg_ G_stk->get(0), argc); |
| 109 | |
| 110 | /* allocate the object ID */ |
| 111 | vm_obj_id_t id = vm_new_id(vmg_ FALSE, FALSE, FALSE); |
| 112 | |
| 113 | /* create the object */ |
| 114 | new (vmg_ id) CVmObjTimeZone(vmg_ tz); |
| 115 | |
| 116 | /* return the new ID */ |
| 117 | return id; |
| 118 | } |
| 119 | |
| 120 | /* |
| 121 | * Parse stack arguments for a timezone constructor, returning a |
| 122 | * CVmTimeZone object if the arguments specify a valid timezone. If not, |
| 123 | * throws an error - we never return null. This doesn't create the garbage |
| 124 | * collected TimeZone object; it just parses the arguments and creates the |
| 125 | * underlying cache object. |
| 126 | */ |
| 127 | CVmTimeZone *CVmObjTimeZone::parse_ctor_args( |
| 128 | VMG_ const vm_val_t *argp, int argc) |
| 129 | { |
| 130 | /* check arguments */ |
| 131 | const char *str; |
| 132 | CVmTimeZone *tz = 0; |
| 133 | if (argc <= 0) |
| 134 | { |
| 135 | /* no arguments; create a local timezone object */ |
| 136 | tz = G_tzcache->get_local_zone(vmg0_); |
| 137 | } |
| 138 | else if (argc == 1 && (str = argp->get_as_string(vmg0_)) != 0) |
| 139 | { |
| 140 | /* get the string buffer and length */ |
| 141 | size_t len = vmb_get_len(str); |
| 142 | str += VMB_LEN; |
| 143 | |
| 144 | /* parse the zone name */ |
| 145 | tz = G_tzcache->parse_zone(vmg_ str, len); |
| 146 | } |
| 147 | else if (argc == 1 && argp->is_numeric(vmg0_)) |
| 148 | { |
| 149 | /* construct from a gmt offset */ |
| 150 | tz = G_tzcache->get_gmtofs_zone(vmg_ argp->num_to_int(vmg0_)); |
| 151 | } |
| 152 | else if (argc > 1) |
| 153 | { |
| 154 | /* wrong number of arguments */ |
| 155 | err_throw(VMERR_WRONG_NUM_OF_ARGS); |
| 156 | } |
| 157 | else |
| 158 | { |
| 159 | /* wrong type */ |
| 160 | err_throw(VMERR_BAD_TYPE_BIF); |
| 161 | } |
| 162 | |
| 163 | /* if we didn't find a zone, it's an error */ |
| 164 | if (tz == 0) |
| 165 | err_throw(VMERR_BAD_VAL_BIF); |
| 166 | |
| 167 | /* return the zone */ |
| 168 | return tz; |
| 169 | } |
| 170 | |
| 171 | /* ------------------------------------------------------------------------ */ |
| 172 | /* |
| 173 | * Create from a given CVmTimeZone object. |
| 174 | */ |
| 175 | vm_obj_id_t CVmObjTimeZone::create(VMG_ CVmTimeZone *tz) |
| 176 | { |
| 177 | /* allocate the object ID and create the object */ |
| 178 | vm_obj_id_t id = vm_new_id(vmg_ FALSE, FALSE, FALSE); |
| 179 | new (vmg_ id) CVmObjTimeZone(vmg_ tz); |
| 180 | |
| 181 | /* return the new ID */ |
| 182 | return id; |
| 183 | } |
| 184 | |
| 185 | /* ------------------------------------------------------------------------ */ |
| 186 | /* |
| 187 | * notify of deletion |
| 188 | */ |
| 189 | void CVmObjTimeZone::notify_delete(VMG_ int /*in_root_set*/) |
| 190 | { |
| 191 | /* free our additional data, if we have any */ |
| 192 | if (ext_ != 0) |
| 193 | G_mem->get_var_heap()->free_mem(ext_); |
| 194 | } |
| 195 | |
| 196 | /* ------------------------------------------------------------------------ */ |
| 197 | /* |
| 198 | * set a property |
| 199 | */ |
| 200 | void CVmObjTimeZone::set_prop(VMG_ class CVmUndo *undo, |
| 201 | vm_obj_id_t self, vm_prop_id_t prop, |
| 202 | const vm_val_t *val) |
| 203 | { |
| 204 | /* no settable properties - throw an error */ |
| 205 | err_throw(VMERR_INVALID_SETPROP); |
| 206 | } |
| 207 | |
| 208 | /* ------------------------------------------------------------------------ */ |
| 209 | /* |
| 210 | * get a property |
| 211 | */ |
| 212 | int CVmObjTimeZone::get_prop(VMG_ vm_prop_id_t prop, vm_val_t *retval, |
| 213 | vm_obj_id_t self, vm_obj_id_t *source_obj, |
| 214 | uint *argc) |
| 215 | { |
| 216 | uint func_idx; |
| 217 | |
| 218 | /* translate the property into a function vector index */ |
| 219 | func_idx = G_meta_table |
| 220 | ->prop_to_vector_idx(metaclass_reg_->get_reg_idx(), prop); |
| 221 | |
| 222 | /* call the appropriate function */ |
| 223 | if ((this->*func_table_[func_idx])(vmg_ self, retval, argc)) |
| 224 | { |
| 225 | *source_obj = metaclass_reg_->get_class_obj(vmg0_); |
| 226 | return TRUE; |
| 227 | } |
| 228 | |
| 229 | /* inherit default handling from our base class */ |
| 230 | return CVmObject::get_prop(vmg_ prop, retval, self, source_obj, argc); |
| 231 | } |
| 232 | |
| 233 | /* ------------------------------------------------------------------------ */ |
| 234 | /* |
| 235 | * load from an image file |
| 236 | */ |
| 237 | void CVmObjTimeZone::load_from_image(VMG_ vm_obj_id_t self, |
| 238 | const char *ptr, size_t siz) |
| 239 | { |
| 240 | /* load our image data */ |
| 241 | load_image_data(vmg_ self, ptr, siz); |
| 242 | |
| 243 | /* |
| 244 | * save our image data pointer in the object table, so that we can |
| 245 | * access it (without storing it ourselves) during a reload |
| 246 | */ |
| 247 | G_obj_table->save_image_pointer(self, ptr, siz); |
| 248 | } |
| 249 | |
| 250 | /* |
| 251 | * reload from the image file |
| 252 | */ |
| 253 | void CVmObjTimeZone::reload_from_image(VMG_ vm_obj_id_t self, |
| 254 | const char *ptr, size_t siz) |
| 255 | { |
| 256 | /* load our image data */ |
| 257 | load_image_data(vmg_ self, ptr, siz); |
| 258 | } |
| 259 | |
| 260 | /* |
| 261 | * load or reload data from the image |
| 262 | */ |
| 263 | void CVmObjTimeZone::load_image_data( |
| 264 | VMG_ vm_obj_id_t self, const char *ptr, size_t siz) |
| 265 | { |
| 266 | /* free our existing extension, if we have one */ |
| 267 | if (ext_ != 0) |
| 268 | G_mem->get_var_heap()->free_mem(ext_); |
| 269 | |
| 270 | /* look up our timezone object */ |
| 271 | CVmTimeZone *tz = G_tzcache->parse_zone(vmg_ ptr+9, osrp1(ptr+8)); |
| 272 | |
| 273 | /* if we didn't find it, create a dummy entry for it */ |
| 274 | if (tz == 0) |
| 275 | { |
| 276 | /* |
| 277 | * Read the default GMT offset and daylight savings offset. Note |
| 278 | * that the saved values are in milliseconds, and os_tzinfo_t uses |
| 279 | * seconds, so adjust accordingly. |
| 280 | */ |
| 281 | os_tzinfo_t desc; |
| 282 | memset(&desc, 0, sizeof(desc)); |
| 283 | desc.std_ofs = osrp4s(ptr) / 1000; |
| 284 | desc.dst_ofs = desc.std_ofs + osrp4s(ptr+4) / 1000; |
| 285 | |
| 286 | /* read the default abbreviation */ |
| 287 | const char *abbr = ptr + 8 + osrp1(ptr+8) + 1; |
| 288 | lib_strcpy(desc.std_abbr, sizeof(desc.std_abbr), abbr+1, osrp1(abbr)); |
| 289 | lib_strcpy(desc.dst_abbr, sizeof(desc.dst_abbr), abbr+1, osrp1(abbr)); |
| 290 | |
| 291 | /* create the dummy */ |
| 292 | tz = G_tzcache->create_missing_zone(vmg_ ptr+9, osrp1(ptr+8), &desc); |
| 293 | } |
| 294 | |
| 295 | /* allocate the extension */ |
| 296 | vm_tzobj_ext *ext = vm_tzobj_ext::alloc_ext(vmg_ this, tz); |
| 297 | ext_ = (char *)ext; |
| 298 | } |
| 299 | |
| 300 | |
| 301 | /* ------------------------------------------------------------------------ */ |
| 302 | /* |
| 303 | * save to a file |
| 304 | */ |
| 305 | void CVmObjTimeZone::save_to_file(VMG_ class CVmFile *fp) |
| 306 | { |
| 307 | /* get our timezone name */ |
| 308 | CVmTimeZone *tz = get_ext()->tz; |
| 309 | size_t namelen; |
| 310 | const char *name = tz->get_name(namelen); |
| 311 | |
| 312 | /* if this is the system's local zone, use ":local" as the name */ |
| 313 | if (G_tzcache->is_local_zone(tz)) |
| 314 | name = ":local", namelen = 6; |
| 315 | |
| 316 | /* query the zone data */ |
| 317 | vmtzquery q; |
| 318 | tz->query(&q); |
| 319 | |
| 320 | /* write our GMT offset, DST offset, abbreviation, and timezone name */ |
| 321 | fp->write_int4(q.gmtofs); |
| 322 | fp->write_int4(q.save); |
| 323 | fp->write_str_byte_prefix(q.abbr); |
| 324 | fp->write_str_byte_prefix(name, namelen); |
| 325 | } |
| 326 | |
| 327 | /* ------------------------------------------------------------------------ */ |
| 328 | /* |
| 329 | * restore from a file |
| 330 | */ |
| 331 | void CVmObjTimeZone::restore_from_file(VMG_ vm_obj_id_t self, |
| 332 | CVmFile *fp, CVmObjFixup *fixups) |
| 333 | { |
| 334 | /* free our existing extension, if we have one */ |
| 335 | if (ext_ != 0) |
| 336 | G_mem->get_var_heap()->free_mem(ext_); |
| 337 | |
| 338 | /* |
| 339 | * Read the GMT and DST offsets (in case we need a synthetic entry). |
| 340 | * Note that the saved values are in milliseconds, but os_tzinfo_t |
| 341 | * works in seconds, so adjust accordingly. |
| 342 | */ |
| 343 | os_tzinfo_t desc; |
| 344 | memset(&desc, 0, sizeof(desc)); |
| 345 | desc.std_ofs = fp->read_int4() / 1000; |
| 346 | desc.dst_ofs = desc.std_ofs + fp->read_int4() / 1000; |
| 347 | |
| 348 | /* read the abbreviation */ |
| 349 | fp->read_str_byte_prefix(desc.std_abbr, sizeof(desc.std_abbr)); |
| 350 | strcpy(desc.dst_abbr, desc.std_abbr); |
| 351 | |
| 352 | /* read the length and name */ |
| 353 | char name[256]; |
| 354 | size_t len = fp->read_str_byte_prefix(name, sizeof(name)); |
| 355 | |
| 356 | /* look up our timezone object */ |
| 357 | CVmTimeZone *tz = G_tzcache->parse_zone(vmg_ name, len); |
| 358 | |
| 359 | /* if we didn't find it, create a dummy entry for it */ |
| 360 | if (tz == 0) |
| 361 | tz = G_tzcache->create_missing_zone(vmg_ name, len, &desc); |
| 362 | |
| 363 | /* allocate the extension structure */ |
| 364 | vm_tzobj_ext *ext = vm_tzobj_ext::alloc_ext(vmg_ this, tz); |
| 365 | ext_ = (char *)ext; |
| 366 | } |
| 367 | |
| 368 | /* ------------------------------------------------------------------------ */ |
| 369 | /* |
| 370 | * cast to a string - returns the time zone name |
| 371 | */ |
| 372 | const char *CVmObjTimeZone::cast_to_string( |
| 373 | VMG_ vm_obj_id_t self, vm_val_t *newstr) const |
| 374 | { |
| 375 | /* get the time zone name */ |
| 376 | size_t len; |
| 377 | const char *name = get_tz()->get_name(len); |
| 378 | |
| 379 | /* create the return string */ |
| 380 | newstr->set_obj(CVmObjString::create(vmg_ FALSE, name, len)); |
| 381 | return newstr->get_as_string(vmg0_); |
| 382 | } |
| 383 | |
| 384 | /* ------------------------------------------------------------------------ */ |
| 385 | /* |
| 386 | * getNames method |
| 387 | */ |
| 388 | int CVmObjTimeZone::getp_getNames(VMG_ vm_obj_id_t self, |
| 389 | vm_val_t *retval, uint *oargc) |
| 390 | { |
| 391 | /* check arguments */ |
| 392 | uint argc = (oargc != 0 ? *oargc : 0); |
| 393 | static CVmNativeCodeDesc desc(0); |
| 394 | if (get_prop_check_argc(retval, oargc, &desc)) |
| 395 | return TRUE; |
| 396 | |
| 397 | /* create the name list */ |
| 398 | retval->set_obj(get_tz()->get_name_list(vmg0_)); |
| 399 | |
| 400 | /* discard arguments */ |
| 401 | G_stk->discard(argc); |
| 402 | |
| 403 | /* handled */ |
| 404 | return TRUE; |
| 405 | } |
| 406 | |
| 407 | /* ------------------------------------------------------------------------ */ |
| 408 | /* |
| 409 | * getHistory method |
| 410 | */ |
| 411 | int CVmObjTimeZone::getp_getHistory(VMG_ vm_obj_id_t self, |
| 412 | vm_val_t *retval, uint *oargc) |
| 413 | { |
| 414 | /* check arguments */ |
| 415 | uint argc = (oargc != 0 ? *oargc : 0); |
| 416 | static CVmNativeCodeDesc desc(0, 1); |
| 417 | if (get_prop_check_argc(retval, oargc, &desc)) |
| 418 | return TRUE; |
| 419 | |
| 420 | /* check for a Date argument */ |
| 421 | if (argc >= 1 && G_stk->get(0)->typ != VM_NIL) |
| 422 | { |
| 423 | /* there's a date - retrieve it */ |
| 424 | CVmObjDate *date = vm_val_cast(CVmObjDate, G_stk->get(0)); |
| 425 | if (date == 0) |
| 426 | err_throw(VMERR_BAD_TYPE_BIF); |
| 427 | |
| 428 | /* get the history for the given date only */ |
| 429 | retval->set_obj(get_tz()->get_history_item( |
| 430 | vmg_ date->get_dayno(), date->get_daytime())); |
| 431 | } |
| 432 | else |
| 433 | { |
| 434 | /* no date argument - get the full history */ |
| 435 | retval->set_obj(get_tz()->get_history_list(vmg0_)); |
| 436 | } |
| 437 | |
| 438 | /* discard arguments */ |
| 439 | G_stk->discard(argc); |
| 440 | |
| 441 | /* handled */ |
| 442 | return TRUE; |
| 443 | } |
| 444 | |
| 445 | /* ------------------------------------------------------------------------ */ |
| 446 | /* |
| 447 | * getRules method |
| 448 | */ |
| 449 | int CVmObjTimeZone::getp_getRules(VMG_ vm_obj_id_t self, |
| 450 | vm_val_t *retval, uint *oargc) |
| 451 | { |
| 452 | /* check arguments */ |
| 453 | uint argc = (oargc != 0 ? *oargc : 0); |
| 454 | static CVmNativeCodeDesc desc(0); |
| 455 | if (get_prop_check_argc(retval, oargc, &desc)) |
| 456 | return TRUE; |
| 457 | |
| 458 | /* get the rule list */ |
| 459 | retval->set_obj(get_tz()->get_rule_list(vmg0_)); |
| 460 | |
| 461 | /* discard arguments */ |
| 462 | G_stk->discard(argc); |
| 463 | |
| 464 | /* handled */ |
| 465 | return TRUE; |
| 466 | } |
| 467 | |
| 468 | |
| 469 | /* ------------------------------------------------------------------------ */ |
| 470 | /* |
| 471 | * getLocation method |
| 472 | */ |
| 473 | int CVmObjTimeZone::getp_getLocation(VMG_ vm_obj_id_t self, |
| 474 | vm_val_t *retval, uint *oargc) |
| 475 | { |
| 476 | /* check arguments */ |
| 477 | uint argc = (oargc != 0 ? *oargc : 0); |
| 478 | static CVmNativeCodeDesc desc(0); |
| 479 | if (get_prop_check_argc(retval, oargc, &desc)) |
| 480 | return TRUE; |
| 481 | |
| 482 | /* get our underlying CVmTimeZone object */ |
| 483 | CVmTimeZone *tz = get_ext()->tz; |
| 484 | |
| 485 | /* create the return list - [country, lat, long, desc] */ |
| 486 | retval->set_obj(CVmObjList::create(vmg_ FALSE, 4)); |
| 487 | CVmObjList *lst = (CVmObjList *)vm_objp(vmg_ retval->val.obj); |
| 488 | G_stk->push(retval); |
| 489 | lst->cons_clear(); |
| 490 | |
| 491 | /* set the country string */ |
| 492 | vm_val_t ele; |
| 493 | const char *str = tz->country(); |
| 494 | ele.set_obj(CVmObjString::create(vmg_ FALSE, str, strlen(str))); |
| 495 | lst->cons_set_element(0, &ele); |
| 496 | |
| 497 | /* get the lat/long string */ |
| 498 | str = tz->coords(); |
| 499 | |
| 500 | /* find the +/- that separates the lat/long portions */ |
| 501 | const char *p = str; |
| 502 | if (*p == '+' || *p == '-') |
| 503 | ++p; |
| 504 | for ( ; *p != '\0' && *p != '-' && *p != '+' ; ++p) ; |
| 505 | |
| 506 | /* set the two substrings */ |
| 507 | ele.set_obj(CVmObjString::create(vmg_ FALSE, str, p - str)); |
| 508 | lst->cons_set_element(1, &ele); |
| 509 | ele.set_obj(CVmObjString::create(vmg_ FALSE, p, strlen(p))); |
| 510 | lst->cons_set_element(2, &ele); |
| 511 | |
| 512 | /* set the description string */ |
| 513 | str = tz->desc(); |
| 514 | if (str != 0 && str[0] != '\0') |
| 515 | ele.set_obj(CVmObjString::create(vmg_ FALSE, str, strlen(str))); |
| 516 | else |
| 517 | ele.set_nil(); |
| 518 | lst->cons_set_element(3, &ele); |
| 519 | |
| 520 | /* discard arguments and gc protection */ |
| 521 | G_stk->discard(argc + 1); |
| 522 | |
| 523 | /* handled */ |
| 524 | return TRUE; |
| 525 | } |