home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / Epoc / Palmtime / files / FrotzCE2_src.ZIP / FrotzCE / FILES.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-09-12  |  9.5 KB  |  583 lines

  1. /*
  2.  * files.c
  3.  *
  4.  * Transscription, recording and playback
  5.  *
  6.  */
  7.  
  8. #ifndef _WIN32_WCE
  9. #include <stdio.h>
  10. #else
  11. #include "FrotzCEIO.h"
  12. #endif
  13.  
  14. #include <string.h>
  15.  
  16. #ifndef _WIN32_WCE
  17. #include "frotz.h"
  18. #else
  19. #include "Frotz\Frotz.h"
  20. #endif
  21.  
  22. #ifndef SEEK_SET
  23. #define SEEK_SET 0
  24. #define SEEK_CUR 1
  25. #define SEEK_END 2
  26. #endif
  27.  
  28. char script_name[MAX_FILE_NAME + 1] = DEFAULT_SCRIPT_NAME;
  29. char command_name[MAX_FILE_NAME + 1] = DEFAULT_COMMAND_NAME;
  30.  
  31. char euro_substitute[] =
  32. "\
  33. aeoeueAeOeUess>><<e \
  34. i y E I a e i o u y \
  35. A E I O U Y a e i o \
  36. u A E I O U a e i o \
  37. u A E I O U a A o O \
  38. a n o A N O aeAEc C \
  39. ththThThL oeOE! ? ";
  40.  
  41. static script_width = 0;
  42.  
  43. static FILE *sfp = NULL;
  44. static FILE *rfp = NULL;
  45. static FILE *pfp = NULL;
  46.  
  47. /*
  48.  * script_open
  49.  *
  50.  * Open the transscript file. 'AMFV' makes this more complicated as it
  51.  * turns transscription on/off several times to exclude some text from
  52.  * the transscription file. This wasn't a problem for the original V4
  53.  * interpreters which always sent transscription to the printer, but it
  54.  * means a problem to modern interpreters that offer to open a new file
  55.  * every time transscription is turned on. Our solution is to append to
  56.  * the old transscription file in V1 to V4, and to ask for a new file
  57.  * name in V5+.
  58.  *
  59.  */
  60.  
  61. void script_open (void)
  62. {
  63.     static script_valid = 0;
  64.  
  65.     char new_name[MAX_FILE_NAME + 1];
  66.  
  67.     h_flags &= ~SCRIPTING_FLAG;
  68.  
  69.     if (h_version >= V5 || !script_valid) {
  70.  
  71.     if (!os_read_file_name (new_name, script_name, FILE_SCRIPT))
  72.         goto done;
  73.  
  74.     strcpy (script_name, new_name);
  75.  
  76.     }
  77.  
  78.     sfp = fopen (script_name, "at");
  79.  
  80.     if (sfp != NULL) {
  81.  
  82.     h_flags |= SCRIPTING_FLAG;
  83.  
  84.     script_valid = 1;
  85.     ostream_script = 1;
  86.  
  87.     } else print_string ("Cannot open file\n");
  88.  
  89. done:
  90.  
  91.     SET_WORD (H_FLAGS, h_flags)
  92.  
  93.     script_width = 0;
  94.  
  95. }/* script_open */
  96.  
  97. /*
  98.  * script_close
  99.  *
  100.  * Stop transscription.
  101.  *
  102.  */
  103.  
  104. void script_close (void)
  105. {
  106.  
  107.     h_flags &= ~SCRIPTING_FLAG;
  108.     SET_WORD (H_FLAGS, h_flags)
  109.  
  110.     fclose (sfp);
  111.     sfp = NULL;
  112.  
  113.     ostream_script = 0;
  114.  
  115. }/* script_close */
  116.  
  117. /*
  118.  * script_char
  119.  *
  120.  * Write a single character to the transscript file.
  121.  *
  122.  */
  123.  
  124. void script_char (int c)
  125. {
  126.  
  127. /*   if (c >= EURO_MIN && c <= EURO_MAX) {
  128.  
  129.     const char *ptr = euro_substitute + 2 * (c - EURO_MIN);
  130.  
  131.     char c1 = *ptr++;
  132.     char c2 = *ptr;
  133.  
  134.     fputc (c1, sfp);
  135.  
  136.     if (c2 != ' ')
  137.         fputc (c2, sfp);
  138.  
  139.     script_width += (c2 == ' ') ? 1 : 2;
  140.  
  141.     } else if (c == 9) {
  142.  
  143.     fputc (' ', sfp);
  144.     fputc (' ', sfp);
  145.  
  146.     script_width += 2;
  147.  
  148.     } else */if (c == 11) {
  149.  
  150.     fputc (' ', sfp);
  151.     fputc (' ', sfp);
  152.     fputc (' ', sfp);
  153.  
  154.     script_width += 3;
  155.  
  156.     } else {
  157.  
  158.     fputc (c, sfp);
  159.  
  160.     script_width += 1;
  161.  
  162.     }
  163.  
  164. }/* script_char */
  165.  
  166. /*
  167.  * script_word
  168.  *
  169.  * Write a string to the transscript file.
  170.  *
  171.  */
  172.  
  173. void script_word (const char *s)
  174. {
  175.     int width = 0;
  176.     int i, c;
  177.  
  178.     /* Calculate the width of the string */
  179.  
  180.     for (i = 0; (c = (unsigned char) s[i]) != 0; i++)
  181.  
  182. /*    if (c >= EURO_MIN && c <= EURO_MAX)
  183.         width += (euro_substitute[2 * (c - EURO_MIN) + 1] == ' ') ? 1 : 2;
  184.     else*/ if (c == 9)
  185.         width += 2;
  186.     else if (c == 11)
  187.         width += 3;
  188.     else if (c != NEW_STYLE && c != NEW_FONT)
  189.         width += 1;
  190.     else
  191.         i++;
  192.  
  193.     /* Insert a newline if the word doesn't fit */
  194.  
  195.     if (script_width != 0 && script_width + width > option_script_cols) {
  196.  
  197.     while (*s == ' ' || *s == 9 || *s == 11)
  198.         s++;
  199.  
  200.     script_new_line ();
  201.  
  202.     }
  203.  
  204.     /* Send the string to the transscript file */
  205.  
  206.     for (i = 0; (c = (unsigned char) s[i]) != 0; i++)
  207.  
  208.     if (c != NEW_FONT && c != NEW_STYLE)
  209.         script_char (c);
  210.     else
  211.         i++;
  212.  
  213. }/* script_word */
  214.  
  215. /*
  216.  * script_new_line
  217.  *
  218.  * Write a newline to the transscript file.
  219.  *
  220.  */
  221.  
  222. void script_new_line (void)
  223. {
  224.  
  225.     if (fputc ('\n', sfp) == EOF)
  226.     script_close ();
  227.  
  228.     script_width = 0;
  229.  
  230. }/* script_new_line */
  231.  
  232. /*
  233.  * script_write_input
  234.  *
  235.  * Send an input line to the transscript file.
  236.  *
  237.  */
  238.  
  239. void script_write_input (const char *buf, int key)
  240. {
  241.     int width = 0;
  242.     int i, c;
  243.  
  244.     /* Calculate the width of the input line */
  245.  
  246.     for (i = 0; (c = (unsigned char) buf[i]) != 0; i++)
  247.  
  248. /*    if (c >= EURO_MIN && c <= EURO_MAX)
  249.         width += (euro_substitute[2 * (c - EURO_MIN) + 1] == ' ') ? 1 : 2;
  250.     else*/
  251.         width++;
  252.  
  253.     /* Insert a newline if the line doesn't fit */
  254.  
  255.     if (script_width != 0 && script_width + width > option_script_cols)
  256.     script_new_line ();
  257.  
  258.     /* Send the input line to the transscript file */
  259.  
  260.     for (i = 0; (c = (unsigned char) buf[i]) != 0; i++)
  261.     script_char (c);
  262.  
  263.     /* Add a newline if the input was terminated by the return key */
  264.  
  265.     if (key == 13)
  266.     script_new_line ();
  267.  
  268. }/* script_write_input */
  269.  
  270. /*
  271.  * script_erase_input
  272.  *
  273.  * Remove an input line from the transscript file.
  274.  *
  275.  */
  276.  
  277. void script_erase_input (const char *buf)
  278. {
  279.     int width = 0;
  280.     int i, c;
  281.  
  282.     /* Calculate the width of the input line */
  283.  
  284.     for (i = 0; (c = (unsigned char) buf[i]) != 0; i++)
  285.  
  286. /*    if (c >= EURO_MIN && c <= EURO_MAX)
  287.         width += (euro_substitute[2 * (c - EURO_MIN) + 1] == ' ') ? 1 : 2;
  288.     else*/
  289.         width++;
  290.  
  291.     /* Remove the input line from the transscript file */
  292.  
  293.     fseek (sfp, -width, SEEK_CUR);
  294.  
  295.     script_width -= width;
  296.  
  297. }/* script_erase_input */
  298.  
  299. /*
  300.  * script_mssg_on
  301.  *
  302.  * Start sending a "debugging" message to the transscript file.
  303.  *
  304.  */
  305.  
  306. void script_mssg_on (void)
  307. {
  308.  
  309.     if (script_width != 0)
  310.     script_new_line ();
  311.  
  312.     script_char (9);
  313.  
  314. }/* script_mssg_on */
  315.  
  316. /*
  317.  * script_mssg_off
  318.  *
  319.  * Stop writing a "debugging" message.
  320.  *
  321.  */
  322.  
  323. void script_mssg_off (void)
  324. {
  325.  
  326.     script_new_line ();
  327.  
  328. }/* script_mssg_off */
  329.  
  330. /*
  331.  * record_open
  332.  *
  333.  * Open a file to record the player's input.
  334.  *
  335.  */
  336.  
  337. void record_open (void)
  338. {
  339.     char new_name[MAX_FILE_NAME + 1];
  340.  
  341.     if (os_read_file_name (new_name, command_name, FILE_RECORD)) {
  342.  
  343.     strcpy (command_name, new_name);
  344.  
  345.     if ((rfp = fopen (new_name, "wt")) != NULL)
  346.         ostream_record = 1;
  347.     else
  348.         print_string ("Cannot open file\n");
  349.  
  350.     }
  351.  
  352. }/* record_open */
  353.  
  354. /*
  355.  * record_close
  356.  *
  357.  * Stop recording the player's input.
  358.  *
  359.  */
  360.  
  361. void record_close (void)
  362. {
  363.  
  364.     fclose (rfp);
  365.     rfp = NULL;
  366.  
  367.     ostream_record = 0;
  368.  
  369. }/* record_close */
  370.  
  371. /*
  372.  * record_char
  373.  *
  374.  * Helper function for record_write_key and record_write_input.
  375.  *
  376.  */
  377.  
  378. static void record_char (int c, int force_encoding)
  379. {
  380.     int i;
  381.  
  382.     if (c < 32 || c > 126 || c == '[' || force_encoding) {
  383.  
  384.     fputc ('[', rfp);
  385.  
  386.     for (i = 10000; i != 0; i /= 10)
  387.         if (c >= i || i == 1)
  388.         record_char ('0' + (c / i) % 10, 0);
  389.  
  390.     fputc (']', rfp);
  391.  
  392.     } else fputc (c, rfp);
  393.  
  394. }/* record_char */
  395.  
  396. /*
  397.  * record_write_key
  398.  *
  399.  * Copy a keystroke to the command file.
  400.  *
  401.  */
  402.  
  403. void record_write_key (int key)
  404. {
  405.  
  406.     if (key != 13)
  407.     record_char (key, 0);
  408.  
  409.     if (key == 253 || key == 254) {
  410.     record_char (mouse_x, 1);
  411.     record_char (mouse_y, 1);
  412.     }
  413.  
  414.     if (fputc ('\n', rfp) == EOF)
  415.     record_close ();
  416.  
  417. }/* record_write_key */
  418.  
  419. /*
  420.  * record_write_input
  421.  *
  422.  * Copy a line of input to a command file.
  423.  *
  424.  */
  425.  
  426. void record_write_input (const char *buf, int key)
  427. {
  428.     int c;
  429.  
  430.     while ((c = (unsigned char) *buf++) != 0)
  431.     record_char (c, 0);
  432.  
  433.     record_write_key (key);
  434.  
  435. }/* record_write_input */
  436.  
  437. /*
  438.  * replay_open
  439.  *
  440.  * Open a file of commands that are executed as if they were typed
  441.  * in by the player.
  442.  *
  443.  */
  444.  
  445. void replay_open (void)
  446. {
  447.     char new_name[MAX_FILE_NAME + 1];
  448.  
  449.     if (os_read_file_name (new_name, command_name, FILE_PLAYBACK)) {
  450.  
  451.     strcpy (command_name, new_name);
  452.  
  453.     if ((pfp = fopen (new_name, "rt")) != NULL) {
  454.  
  455.         set_more_prompts (read_yes_or_no ("Do you want MORE prompts"));
  456.  
  457.         istream_replay = 1;
  458.  
  459.     } else print_string ("Cannot open file\n");
  460.  
  461.     }
  462.  
  463. }/* replay_open */
  464.  
  465. /*
  466.  * replay_close
  467.  *
  468.  * Stop playback of commands.
  469.  *
  470.  */
  471.  
  472. void replay_close (void)
  473. {
  474.  
  475.     fclose (pfp);
  476.     pfp = NULL;
  477.  
  478.     set_more_prompts (1);
  479.  
  480.     istream_replay = 0;
  481.  
  482. }/* replay_close */
  483.  
  484. /*
  485.  * replay_char
  486.  *
  487.  * Helper function for replay_key and replay_line.
  488.  *
  489.  */
  490.  
  491. static int replay_char (void)
  492. {
  493.     int c1, c2;
  494.  
  495.     c1 = fgetc (pfp);
  496.  
  497.     if (c1 == '[') {
  498.  
  499.     c1 = 0;
  500.  
  501.     while ((c2 = fgetc (pfp)) != EOF && c2 != ']')
  502.         if (c2 >= '0' && c2 <= '9')
  503.         c1 = 10 * c1 + c2 - '0';
  504.  
  505.     }
  506.  
  507.     return c1;
  508.  
  509. }/* replay_char */
  510.  
  511. /*
  512.  * _replay_key
  513.  *
  514.  * Read a keystroke from a command file. The first character, however,
  515.  * is not taken from the file; it is given as an argument instead.
  516.  *
  517.  */
  518.  
  519. static int _replay_read_key (int c)
  520. {
  521.  
  522.     if (c == '\n')
  523.     return 13;
  524.  
  525.     if (c == 253 || c == 254) {
  526.     mouse_x = replay_char ();
  527.     mouse_y = replay_char ();
  528.     }
  529.  
  530.     if (fgetc (pfp) != '\n') {
  531.  
  532.     replay_close ();
  533.     return -1;
  534.  
  535.     } else return c;
  536.  
  537. }/* _replay_read_key */
  538.  
  539. /*
  540.  * replay_read_key
  541.  *
  542.  * Read a keystroke from a command file.
  543.  *
  544.  */
  545.  
  546. int replay_read_key (void)
  547. {
  548.  
  549.     return _replay_read_key (replay_char ());
  550.  
  551. }/* replay_read_key */
  552.  
  553. /*
  554.  * replay_read_input
  555.  *
  556.  * Read a line of input from a command file.
  557.  *
  558.  */
  559.  
  560. int replay_read_input (char *buf)
  561. {
  562.     char *ptr = buf;
  563.     int key, c;
  564.  
  565.     do {
  566.  
  567.     c = replay_char ();
  568.  
  569.     if (c == EOF || c == '\n' || is_terminator (c))
  570.         break;
  571.  
  572.     *ptr++ = c;
  573.  
  574.     } while (c);
  575.  
  576.     *ptr = 0;
  577.  
  578.     key = _replay_read_key (c);
  579.  
  580.     return key;
  581.  
  582. }/* replay_read_input */
  583.