396 #endif |
396 #endif |
397 |
397 |
398 /* Get the length of the current object */ |
398 /* Get the length of the current object */ |
399 uint SlGetFieldLength(void) {return _sl.obj_len;} |
399 uint SlGetFieldLength(void) {return _sl.obj_len;} |
400 |
400 |
|
401 /** Return a signed-long version of the value of a setting |
|
402 * @param ptr pointer to the variable |
|
403 * @param conv type of variable, can be a non-clean |
|
404 * type, eg one with other flags because it is parsed |
|
405 * @return returns the value of the pointer-setting */ |
|
406 int64 ReadValue(const void *ptr, VarType conv) |
|
407 { |
|
408 switch (GetVarMemType(conv)) { |
|
409 case SLE_VAR_BL: return (*(bool*)ptr == 1); |
|
410 case SLE_VAR_I8: return *(int8* )ptr; |
|
411 case SLE_VAR_U8: return *(byte* )ptr; |
|
412 case SLE_VAR_I16: return *(int16* )ptr; |
|
413 case SLE_VAR_U16: return *(uint16*)ptr; |
|
414 case SLE_VAR_I32: return *(int32* )ptr; |
|
415 case SLE_VAR_U32: return *(uint32*)ptr; |
|
416 case SLE_VAR_I64: return *(int64* )ptr; |
|
417 case SLE_VAR_U64: return *(uint64*)ptr; |
|
418 case SLE_VAR_NULL:return 0; |
|
419 default: NOT_REACHED(); |
|
420 } |
|
421 |
|
422 /* useless, but avoids compiler warning this way */ |
|
423 return 0; |
|
424 } |
|
425 |
|
426 /** Write the value of a setting |
|
427 * @param ptr pointer to the variable |
|
428 * @param conv type of variable, can be a non-clean type, eg |
|
429 * with other flags. It is parsed upon read |
|
430 * @param var the new value being given to the variable */ |
|
431 void WriteValue(void *ptr, VarType conv, int64 val) |
|
432 { |
|
433 switch (GetVarMemType(conv)) { |
|
434 case SLE_VAR_BL: *(bool *)ptr = (val == 1); break; |
|
435 case SLE_VAR_I8: *(int8 *)ptr = val; break; |
|
436 case SLE_VAR_U8: *(byte *)ptr = val; break; |
|
437 case SLE_VAR_I16: *(int16 *)ptr = val; break; |
|
438 case SLE_VAR_U16: *(uint16*)ptr = val; break; |
|
439 case SLE_VAR_I32: *(int32 *)ptr = val; break; |
|
440 case SLE_VAR_U32: *(uint32*)ptr = val; break; |
|
441 case SLE_VAR_I64: *(int64 *)ptr = val; break; |
|
442 case SLE_VAR_U64: *(uint64*)ptr = val; break; |
|
443 case SLE_VAR_NULL: break; |
|
444 default: NOT_REACHED(); |
|
445 } |
|
446 } |
|
447 |
401 /** |
448 /** |
402 * Handle all conversion and typechecking of variables here. |
449 * Handle all conversion and typechecking of variables here. |
403 * In the case of saving, read in the actual value from the struct |
450 * In the case of saving, read in the actual value from the struct |
404 * and then write them to file, endian safely. Loading a value |
451 * and then write them to file, endian safely. Loading a value |
405 * goes exactly the opposite way |
452 * goes exactly the opposite way |
410 { |
457 { |
411 int64 x = 0; |
458 int64 x = 0; |
412 |
459 |
413 if (_sl.save) { /* SAVE values */ |
460 if (_sl.save) { /* SAVE values */ |
414 /* Read a value from the struct. These ARE endian safe. */ |
461 /* Read a value from the struct. These ARE endian safe. */ |
415 switch ((conv >> 4) & 0xF) { |
462 x = ReadValue(ptr, conv); |
416 case SLE_VAR_BL >> 4: |
463 |
417 case SLE_VAR_I8 >> 4: x = *(int8*)ptr; break; |
464 /* Write the value to the file and check if its value is in the desired range */ |
418 case SLE_VAR_U8 >> 4: x = *(byte*)ptr; break; |
465 switch (GetVarFileType(conv)) { |
419 case SLE_VAR_I16 >> 4: x = *(int16*)ptr; break; |
|
420 case SLE_VAR_U16 >> 4: x = *(uint16*)ptr; break; |
|
421 case SLE_VAR_I32 >> 4: x = *(int32*)ptr; break; |
|
422 case SLE_VAR_U32 >> 4: x = *(uint32*)ptr; break; |
|
423 case SLE_VAR_I64 >> 4: x = *(int64*)ptr; break; |
|
424 case SLE_VAR_U64 >> 4: x = *(uint64*)ptr; break; |
|
425 case SLE_VAR_NULL >> 4: x = 0; break; |
|
426 default: NOT_REACHED(); |
|
427 } |
|
428 |
|
429 // Write the value to the file and check if its value is in the desired range |
|
430 switch (conv & 0xF) { |
|
431 case SLE_FILE_I8: assert(x >= -128 && x <= 127); SlWriteByte(x);break; |
466 case SLE_FILE_I8: assert(x >= -128 && x <= 127); SlWriteByte(x);break; |
432 case SLE_FILE_U8: assert(x >= 0 && x <= 255); SlWriteByte(x);break; |
467 case SLE_FILE_U8: assert(x >= 0 && x <= 255); SlWriteByte(x);break; |
433 case SLE_FILE_I16:assert(x >= -32768 && x <= 32767); SlWriteUint16(x);break; |
468 case SLE_FILE_I16:assert(x >= -32768 && x <= 32767); SlWriteUint16(x);break; |
434 case SLE_FILE_STRINGID: |
469 case SLE_FILE_STRINGID: |
435 case SLE_FILE_U16:assert(x >= 0 && x <= 65535); SlWriteUint16(x);break; |
470 case SLE_FILE_U16:assert(x >= 0 && x <= 65535); SlWriteUint16(x);break; |
436 case SLE_FILE_I32: case SLE_FILE_U32: SlWriteUint32((uint32)x);break; |
471 case SLE_FILE_I32: |
437 case SLE_FILE_I64: case SLE_FILE_U64: SlWriteUint64(x);break; |
472 case SLE_FILE_U32: SlWriteUint32((uint32)x);break; |
|
473 case SLE_FILE_I64: |
|
474 case SLE_FILE_U64: SlWriteUint64(x);break; |
438 default: NOT_REACHED(); |
475 default: NOT_REACHED(); |
439 } |
476 } |
440 } else { /* LOAD values */ |
477 } else { /* LOAD values */ |
441 |
478 |
442 // Read a value from the file |
479 /* Read a value from the file */ |
443 switch (conv & 0xF) { |
480 switch (GetVarFileType(conv)) { |
444 case SLE_FILE_I8: x = (int8)SlReadByte(); break; |
481 case SLE_FILE_I8: x = (int8 )SlReadByte(); break; |
445 case SLE_FILE_U8: x = (byte)SlReadByte(); break; |
482 case SLE_FILE_U8: x = (byte )SlReadByte(); break; |
446 case SLE_FILE_I16: x = (int16)SlReadUint16(); break; |
483 case SLE_FILE_I16: x = (int16 )SlReadUint16(); break; |
447 case SLE_FILE_U16: x = (uint16)SlReadUint16(); break; |
484 case SLE_FILE_U16: x = (uint16)SlReadUint16(); break; |
448 case SLE_FILE_I32: x = (int32)SlReadUint32(); break; |
485 case SLE_FILE_I32: x = (int32 )SlReadUint32(); break; |
449 case SLE_FILE_U32: x = (uint32)SlReadUint32(); break; |
486 case SLE_FILE_U32: x = (uint32)SlReadUint32(); break; |
450 case SLE_FILE_I64: x = (int64)SlReadUint64(); break; |
487 case SLE_FILE_I64: x = (int64 )SlReadUint64(); break; |
451 case SLE_FILE_U64: x = (uint64)SlReadUint64(); break; |
488 case SLE_FILE_U64: x = (uint64)SlReadUint64(); break; |
452 case SLE_FILE_STRINGID: x = RemapOldStringID((uint16)SlReadUint16()); break; |
489 case SLE_FILE_STRINGID: x = RemapOldStringID((uint16)SlReadUint16()); break; |
453 default: NOT_REACHED(); |
490 default: NOT_REACHED(); |
454 } |
491 } |
455 |
492 |
456 /* Write The value to the struct. These ARE endian safe. */ |
493 /* Write The value to the struct. These ARE endian safe. */ |
457 switch ((conv >> 4) & 0xF) { |
494 WriteValue(ptr, conv, x); |
458 case SLE_VAR_BL >> 4: |
|
459 case SLE_VAR_I8 >> 4: *(int8*)ptr = x; break; |
|
460 case SLE_VAR_U8 >> 4: *(byte*)ptr = x; break; |
|
461 case SLE_VAR_I16 >> 4: *(int16*)ptr = x; break; |
|
462 case SLE_VAR_U16 >> 4: *(uint16*)ptr = x; break; |
|
463 case SLE_VAR_I32 >> 4: *(int32*)ptr = x; break; |
|
464 case SLE_VAR_U32 >> 4: *(uint32*)ptr = x; break; |
|
465 case SLE_VAR_I64 >> 4: *(int64*)ptr = x; break; |
|
466 case SLE_VAR_U64 >> 4: *(uint64*)ptr = x; break; |
|
467 case SLE_VAR_NULL >> 4: break; |
|
468 default: NOT_REACHED(); |
|
469 } |
|
470 } |
495 } |
471 } |
496 } |
472 |
497 |
473 static inline size_t SlCalcStringLen(const char *ptr, uint length) |
498 static inline size_t SlCalcStringLen(const char *ptr, uint length) |
474 { |
499 { |