home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / Epoc / Palmtime / files / FrotzS5_src.ZIP / STREAM.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-10-12  |  7.7 KB  |  364 lines

  1. /*
  2.  * stream.c
  3.  *
  4.  * IO stream implementation
  5.  *
  6.  */
  7.  
  8. #include "frotz.h"
  9. #include "s5api.h"
  10.  
  11. extern short handle_hot_key (struct sg *g, zchar);
  12. extern short validate_click (struct sg *g);
  13. extern void replay_open (struct sg *g);
  14. extern void replay_close (struct sg *g);
  15. extern void memory_open (struct sg *g, zword, zword, short);
  16. extern void memory_close (struct sg *g);
  17. extern void record_open (struct sg *g);
  18. extern void record_close (struct sg *g);
  19. extern void script_open (struct sg *g);
  20. extern void script_close (struct sg *g);
  21. extern void memory_word (struct sg *g, const zchar *);
  22. extern void memory_new_line (struct sg *g);
  23. extern void record_write_key (struct sg *g, short);
  24. extern void record_write_input (struct sg *g, const zchar *, zchar);
  25. extern void script_char (struct sg *g, zchar);
  26. extern void script_word (struct sg *g, const zchar *);
  27. extern void script_new_line (struct sg *g);
  28. extern void script_write_input (struct sg *g, const zchar *, zchar);
  29. extern void script_erase_input (struct sg *g, const zchar *);
  30. extern void script_mssg_on (struct sg *g);
  31. extern void script_mssg_off (struct sg *g);
  32. extern void screen_char (struct sg *g, zchar);
  33. extern void screen_word (struct sg *g, const zchar *);
  34. extern void screen_new_line (struct sg *g);
  35. extern void screen_write_input (struct sg *g, const zchar *, zchar);
  36. extern void screen_erase_input (struct sg *g, const zchar *);
  37. extern void screen_mssg_on (struct sg *g);
  38. extern void screen_mssg_off (struct sg *g);
  39. extern zchar replay_read_key (struct sg *g);
  40. extern zchar replay_read_input (struct sg *g, zchar *);
  41. extern zchar console_read_key (struct sg *g, zword);
  42. extern zchar console_read_input (struct sg *g, short, zchar *, zword, short);
  43. extern short direct_call (struct sg *g, zword);
  44.  
  45. /*
  46.  * stream_mssg_on
  47.  *
  48.  * Start printing a "debugging" message.
  49.  *
  50.  */
  51.  
  52. void stream_mssg_on (struct sg *g)
  53. {
  54.  
  55.     flush_buffer (g);
  56.  
  57.     if (g->ostream_screen)
  58.     screen_mssg_on (g);
  59.     if (g->ostream_script && g->enable_scripting)
  60.     script_mssg_on (g);
  61.  
  62.     g->message = TRUE;
  63.  
  64. }/* stream_mssg_on */
  65.  
  66. /*
  67.  * stream_mssg_off
  68.  *
  69.  * Stop printing a "debugging" message.
  70.  *
  71.  */
  72.  
  73. void stream_mssg_off (struct sg *g)
  74. {
  75.  
  76.     flush_buffer (g);
  77.  
  78.     if (g->ostream_screen)
  79.     screen_mssg_off (g);
  80.     if (g->ostream_script && g->enable_scripting)
  81.     script_mssg_off (g);
  82.  
  83.     g->message = FALSE;
  84.  
  85. }/* stream_mssg_off */
  86.  
  87. /*
  88.  * z_output_stream, open or close an output stream.
  89.  *
  90.  *    zargs[0] = stream to open (positive) or close (negative)
  91.  *    zargs[1] = address to redirect output to (stream 3 only)
  92.  *    zargs[2] = width of redirected output (stream 3 only, optional)
  93.  *
  94.  */
  95.  
  96. void z_output_stream (struct sg *g)
  97. {
  98.  
  99.     flush_buffer (g);
  100.  
  101.     switch ((short) (g->zargs[0])) {
  102.  
  103.     case  1: g->ostream_screen = TRUE;
  104.          break;
  105.     case -1: g->ostream_screen = FALSE;
  106.          break;
  107.     case  2: if (!g->ostream_script) script_open (g);
  108.          break;
  109.     case -2: if (g->ostream_script) script_close (g);
  110.          break;
  111.     case  3: memory_open (g, g->zargs[1], g->zargs[2], g->zargc >= 3);
  112.          break;
  113.     case -3: memory_close (g);
  114.          break;
  115.     case  4: if (!g->ostream_record) record_open (g);
  116.          break;
  117.     case -4: if (g->ostream_record) record_close (g);
  118.          break;
  119.  
  120.     }
  121.  
  122. }/* z_output_stream */
  123.  
  124. /*
  125.  * stream_char
  126.  *
  127.  * Send a single character to the output stream.
  128.  *
  129.  */
  130.  
  131. void stream_char (struct sg *g, zchar c)
  132. {
  133.  
  134.     if (g->ostream_screen)
  135.     screen_char (g,c);
  136.     if (g->ostream_script && g->enable_scripting)
  137.     script_char (g,c);
  138.  
  139. }/* stream_char */
  140.  
  141. /*
  142.  * stream_word
  143.  *
  144.  * Send a string of characters to the output streams.
  145.  *
  146.  */
  147.  
  148. void stream_word (struct sg *g, const zchar *s)
  149. {
  150.  
  151.     if (g->ostream_memory && !g->message)
  152.  
  153.     memory_word (g,s);
  154.  
  155.     else {
  156.  
  157.     if (g->ostream_screen)
  158.         screen_word (g,s);
  159.     if (g->ostream_script && g->enable_scripting)
  160.         script_word (g,s);
  161.  
  162.     }
  163.  
  164. }/* stream_word */
  165.  
  166. /*
  167.  * stream_new_line
  168.  *
  169.  * Send a newline to the output streams.
  170.  *
  171.  */
  172.  
  173. void stream_new_line (struct sg *g)
  174. {
  175.  
  176.     if (g->ostream_memory && !g->message)
  177.  
  178.     memory_new_line (g);
  179.  
  180.     else {
  181.  
  182.     if (g->ostream_screen)
  183.         screen_new_line (g);
  184.     if (g->ostream_script && g->enable_scripting)
  185.         script_new_line (g);
  186.  
  187.     }
  188.  
  189. }/* stream_new_line */
  190.  
  191. /*
  192.  * z_input_stream, select an input stream.
  193.  *
  194.  *    zargs[0] = input stream to be selected
  195.  *
  196.  */
  197.  
  198. void z_input_stream (struct sg *g)
  199. {
  200.  
  201.     flush_buffer (g);
  202.  
  203.     if (g->zargs[0] == 0 && g->istream_replay)
  204.     replay_close (g);
  205.     if (g->zargs[0] == 1 && !g->istream_replay)
  206.     replay_open (g);
  207.  
  208. }/* z_input_stream */
  209.  
  210. /*
  211.  * stream_read_key
  212.  *
  213.  * Read a single keystroke from the current input stream.
  214.  *
  215.  */
  216.  
  217. zchar stream_read_key ( struct sg *g, zword timeout, zword routine,
  218.             short hot_keys )
  219. {
  220.     zchar key = ZC_BAD;
  221.  
  222.     flush_buffer (g);
  223.  
  224.     /* Read key from current input stream */
  225.  
  226. continue_input:
  227.  
  228.     do {
  229.  
  230.     if (g->istream_replay)
  231.         key = replay_read_key (g);
  232.     else
  233.         key = console_read_key (g,timeout);
  234.  
  235.     } while (key == ZC_BAD);
  236.  
  237.     /* Verify mouse clicks */
  238.  
  239.     if (key == ZC_SINGLE_CLICK || key == ZC_DOUBLE_CLICK)
  240.     if (!validate_click (g))
  241.         goto continue_input;
  242.  
  243.     /* Copy key to the command file */
  244.  
  245.     if (g->ostream_record && !g->istream_replay)
  246.     record_write_key (g,key);
  247.  
  248.     /* Handle timeouts */
  249.  
  250.     if (key == ZC_TIME_OUT)
  251.     if (direct_call (g,routine) == 0)
  252.         goto continue_input;
  253.  
  254.     /* Handle hot keys */
  255.     if(hot_keys)
  256.           {
  257.           switch(key)
  258.           {
  259.           case 18: case 16:
  260.           case 19: case 21:
  261.           case 14: case 24:
  262.           case 4:
  263.               {
  264.             if (g->h_version == V4 && key == ZC_HKEY_UNDO)
  265.                 goto continue_input;
  266.             if (!handle_hot_key (g,key))
  267.                 goto continue_input;
  268.  
  269.             return ZC_BAD;
  270.             }
  271.           default:break;
  272.           }
  273.           }
  274.  
  275.     /* Return key */
  276.  
  277.     return key;
  278.  
  279. }/* stream_read_key */
  280.  
  281. /*
  282.  * stream_read_input
  283.  *
  284.  * Read a line of input from the current input stream.
  285.  *
  286.  */
  287.  
  288. zchar stream_read_input (struct sg *g, short max, zchar *buf,
  289.               zword timeout, zword routine,
  290.               short hot_keys,
  291.               short no_scripting )
  292. {
  293.     zchar key = ZC_BAD;
  294.  
  295.     flush_buffer (g);
  296.  
  297.     /* Remove initial input from the transscript file or from the screen */
  298.  
  299.     if (g->ostream_script && g->enable_scripting && !no_scripting)
  300.     script_erase_input (g,buf);
  301.     if (g->istream_replay)
  302.     screen_erase_input (g,buf);
  303.  
  304.     /* Read input line from current input stream */
  305.  
  306. continue_input:
  307.  
  308.     do {
  309.     if (g->istream_replay)
  310.         key = replay_read_input (g,buf);
  311.     else
  312.         key = console_read_input (g,max, buf, timeout, key != ZC_BAD);
  313.  
  314.     } while (key == ZC_BAD);
  315.  
  316.     /* Verify mouse clicks */
  317.  
  318.     if (key == ZC_SINGLE_CLICK || key == ZC_DOUBLE_CLICK)
  319.     if (!validate_click (g))
  320.         goto continue_input;
  321.  
  322.     /* Copy input line to the command file */
  323.  
  324.     if (g->ostream_record && !g->istream_replay)
  325.     record_write_input (g,buf, key);
  326.  
  327.     /* Handle timeouts */
  328.  
  329.     if (key == ZC_TIME_OUT)
  330.     if (direct_call (g,routine) == 0)
  331.         goto continue_input;
  332.  
  333.     /* Handle hot keys */
  334.  
  335.     if (hot_keys)
  336.         {
  337.         switch(key)
  338.           {
  339.           case 18: case 16:
  340.           case 19: case 21:
  341.           case 14: case 24:
  342.           case 4:  case 8:
  343.             {
  344.             if (!handle_hot_key (g,key))
  345.               goto continue_input;
  346.             return ZC_BAD;
  347.             }
  348.           default: break;
  349.           }
  350.         }
  351.  
  352.     /* Copy input line to transscript file or to the screen */
  353.  
  354.     if (g->ostream_script && g->enable_scripting && !no_scripting)
  355.     script_write_input (g,buf, key);
  356.     if (g->istream_replay)
  357.     screen_write_input (g,buf, key);
  358.  
  359.     /* Return terminating key */
  360.  
  361.     return key;
  362.  
  363. }/* stream_read_input */
  364.