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

  1. /*
  2.  * buffer.c
  3.  *
  4.  * Text buffering and word wrapping
  5.  *
  6.  */
  7.  
  8. #include "Frotz/frotz.h"
  9.  
  10. static char buffer[TEXT_BUFFER_SIZE];
  11.  
  12. int bufpos = 0;
  13. int prev_c = 0;
  14.  
  15. int locked = 0;
  16.  
  17. /*
  18.  * flush_buffer
  19.  *
  20.  * Copy the contents of the text buffer to the output streams.
  21.  *
  22.  */
  23.  
  24. void flush_buffer (void)
  25. {
  26.     /* Make sure we stop when flush_buffer is called from flush_buffer.
  27.        Note that this is difficult to avoid as we might print a newline
  28.        during flush_buffer, which might cause a newline interrupt, that
  29.        might execute any arbitrary opcode, which might flush the buffer. */
  30.  
  31.     if (locked || bufpos == 0)
  32.     return;
  33.  
  34.     buffer[bufpos] = 0;
  35.  
  36.     /* Send the buffer to the output streams */
  37.  
  38.     locked = 1;
  39.  
  40.     stream_word (buffer);
  41.  
  42.     locked = 0;
  43.  
  44.     /* Reset the buffer */
  45.  
  46.     bufpos = 0;
  47.     prev_c = 0;
  48.  
  49. }/* flush_buffer */
  50.  
  51. /*
  52.  * print_char
  53.  *
  54.  * High level character output function.
  55.  *
  56.  */
  57.  
  58. void print_char (int c)
  59. {
  60.     static flag = 0;
  61.  
  62.     if (!flag) {
  63.  
  64.     /* Character 0 prints nothing, character 13 prints newline */
  65.  
  66.     if (c == 13)
  67.         new_line ();
  68.     if (c == 0 || c == 13)
  69.         return;
  70.  
  71.     /* Flush the buffer before a whitespace or after a hyphen */
  72.  
  73.     if (c == ' ' || c == 9 || c == 11 || prev_c == '-' && c != '-')
  74.         flush_buffer ();
  75.  
  76.     /* Set the flag if this is part one of a style or font change */
  77.  
  78.     if (c == NEW_FONT || c == NEW_STYLE)
  79.         flag = 1;
  80.  
  81.     /* Remember the current character code */
  82.  
  83.     prev_c = c;
  84.  
  85.     } else flag = 0;
  86.  
  87.     /* Insert the character into the buffer */
  88.  
  89.     buffer[bufpos++] = (char) c;
  90.  
  91.     if (bufpos == TEXT_BUFFER_SIZE)
  92.     runtime_error ("Text buffer overflow");
  93.  
  94. }/* print_char */
  95.  
  96. /*
  97.  * new_line
  98.  *
  99.  * High level newline function.
  100.  *
  101.  */
  102.  
  103. void new_line (void)
  104. {
  105.  
  106.     flush_buffer ();
  107.  
  108.     stream_new_line ();
  109.  
  110. }/* new_line */
  111.