• Support
  • Articles
  • Resources
  • Products

saving and restoring system variables

Moderator: boris

1 postPage 1 of 1

Trevor
Guru

Posts:
468
Joined: Wed Jul 26, 2006 8:47 am
by Trevor » Mon Feb 22, 2010 11:01 am
I wrote these functions because I was frustrated with system functions or my own functions changing the value of result and/or timeout leading to annoying bugs etc. These allow you to save and restore result and timeout even if nested calls are used. Those familiar with push and pop will understand, others may not get it straight away. This method could be extended to any other variable or even to save and restore all system variables in one go.

Code: Select all

  1.  
  2. result_sp = 0
  3. timeout_sp = 0
  4. .
  5. .
  6. .
  7. ;-------------------------------------------------------------------------------
  8. ; Save a copy of result that can be restored later.
  9. ; This uses a "stack" type approach so that nested calls are supported. The "stack"
  10. ; is just an array containing result1, result2, result3............resultn where n
  11. ; grows to the maximum number of nested calls - 1.
  12. ;-------------------------------------------------------------------------------
  13. :push_result
  14.     push_pop_result_copy = result           ; save result as sprintf will alter it
  15.     sprintf "result%d = result" result_sp   ; build the command - we place result on the stack and inc the stack pointer
  16.     result_sp = result_sp + 1               ; increment the stack pointer (points to where next push will be stored)
  17.     result = push_pop_result_copy           ; restore result to what it was before sprintf changed it
  18.     execcmnd inputstr                       ; execute the command
  19. return
  20. ;-------------------------------------------------------------------------------
  21. ; Restore result to the value it contained at the last call to push_result
  22. ;-------------------------------------------------------------------------------
  23. :pop_result
  24.     if result_sp = 0 then
  25.         errorStr = 'pop_result called when nothing to pop'  ; should never happen
  26.         goto error
  27.     endif
  28.     result_sp = result_sp - 1               ; so that stack pointer points to last pushed result value
  29.     sprintf "result = result%d" result_sp   ; build the command to get value pointed to by stack pointer into result
  30.     execcmnd inputstr                       ; execute the command
  31. return
  32. ;-------------------------------------------------------------------------------
  33. ;-------------------------------------------------------------------------------
  34. :push_timeout
  35.     push_pop_result_copy = result
  36.     sprintf "timeout%d = timeout" timeout_sp
  37.     timeout_sp = timeout_sp + 1
  38.     execcmnd inputstr
  39. ;      sprintf "push: 'execcmnd %s' timeout_sp = %d" inputstr timeout_sp
  40. ;      messagebox inputstr 'debug'
  41.     result = push_pop_result_copy
  42. return
  43. ;-------------------------------------------------------------------------------
  44. :pop_timeout
  45.     push_pop_result_copy = result
  46.     if timeout_sp = 0 then
  47.         errorStr = 'pop_timeout called when nothing to pop' ; should never happen
  48.         goto error
  49.     endif
  50.     timeout_sp = timeout_sp - 1
  51.     sprintf "timeout = timeout%d" timeout_sp
  52.     execcmnd inputstr
  53. ;      sprintf "pop: 'execcmnd %s' timeout_sp = %d" inputstr timeout_sp
  54. ;      messagebox inputstr 'debug'
  55.     result = push_pop_result_copy
  56. return
  57. ;-------------------------------------------------------------------------------
  58.  

1 postPage 1 of 1

Users browsing this forum: No registered users