Handle a variable with VAT functions.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Handle a variable with VAT functions.
  2. // Unlike "Create Variable", this example doesn't use any intermediate helper functions.
  3. #define OPTIMIZE_ROM_CALLS
  4. #define USE_TI89
  5. #define USE_TI92P
  6. #define USE_V200
  7. #define NO_CALC_DETECT
  8. #define MIN_AMS 100
  9. #define NO_AMS_CHECK
  10. #include <tigcclib.h>
  11. void _main(void)
  12. {
  13. HSym hs;
  14. SYM_ENTRY *SymPtr;
  15. unsigned short *VarPtr;
  16. unsigned short savednumber;
  17. unsigned char buffer[25];
  18. //
  19. // Read old score
  20. //
  21. // First, find the file. This returns an HSym which is HS_NULL if
  22. // the file didn't exist.
  23. hs = SymFind(SYMSTR("example"));
  24. if(hs.folder == 0)
  25. {
  26. savednumber = 0;
  27. }
  28. else
  29. {
  30. SymPtr = DerefSym(hs);
  31. VarPtr = (unsigned short*)(HeapDeref(SymPtr->handle) + 2);
  32. // The "+2" skips over the first 2 bytes of the file, which
  33. // store the file's size (and which we ignore).
  34. savednumber = *VarPtr;
  35. }
  36. //
  37. // Write new score
  38. //
  39. sprintf(buffer,"Runs before: %i.",savednumber);
  40. ST_helpMsg(buffer);
  41. savednumber++;
  42. SymDel(SYMSTR("example"));
  43. // NOTE: This completely lacks error checking. Checks should be
  44. // made for the following cases:
  45. // SymAdd returning HS_NULL;
  46. // HeapAlloc returning H_NULL;
  47. SymPtr = DerefSym(SymAdd(SYMSTR("example")));
  48. VarPtr = (unsigned short*)HeapDeref(SymPtr->handle = HeapAlloc(sizeof savednumber));
  49. // In a more complex case you'd cast this as a struct, not an array of
  50. // shorts, where the first field is the file size.
  51. VarPtr[0] = 2;
  52. // Size of the file, not counting the 2 bytes indicating the size
  53. VarPtr[1] = savednumber;
  54. }