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

  1. /*
  2.  * buffer.c
  3.  *
  4.  * Text buffering and word wrapping
  5.  *
  6.  */
  7.  
  8. #include "frotz.h"
  9. #include "s5api.h"
  10.  
  11. extern void stream_char (struct sg *g, zchar);
  12. extern void stream_word (struct sg *g, const zchar *);
  13. extern void stream_new_line (struct sg *g);
  14.  
  15.  
  16. /*
  17.  * flush_buffer
  18.  *
  19.  * Copy the contents of the text buffer to the output streams.
  20.  *
  21.  */
  22.  
  23. void flush_buffer (struct sg *g)
  24. {
  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 (g->locked || g->bufpos == 0)
  32.     return;
  33.  
  34.     /* Send the buffer to the output streams */
  35.  
  36.     g->buffer[g->bufpos] = 0;
  37.  
  38.     g->locked = TRUE; stream_word (g, g->buffer); g->locked = FALSE;
  39.  
  40.     /* Reset the buffer */
  41.  
  42.     g->bufpos = 0;
  43.     g->prev_c = 0;
  44.  
  45. }/* flush_buffer */
  46.  
  47. /*
  48.  * print_char
  49.  *
  50.  * High level output function.
  51.  *
  52.  */
  53.  
  54. void print_char (struct sg *g, zchar c)
  55. {
  56.     if (g->message || g->ostream_memory || g->enable_buffering) {
  57.  
  58.     if (!g->flag) {
  59.  
  60.         /* Characters 0 and ZC_RETURN are special cases */
  61.  
  62.         if (c == ZC_RETURN)
  63.         { new_line (g); return; }
  64.         if (c == 0)
  65.         return;
  66.  
  67.         /* Flush the buffer before a whitespace or after a hyphen */
  68.  
  69.         if (c == ' ' || c == ZC_INDENT || c == ZC_GAP || g->prev_c == '-' && c != '-')
  70.         flush_buffer (g);
  71.  
  72.         /* Set the flag if this is part one of a style or font change */
  73.  
  74.         if (c == ZC_NEW_FONT || c == ZC_NEW_STYLE)
  75.         g->flag = TRUE;
  76.  
  77.         /* Remember the current character code */
  78.  
  79.         g->prev_c = c;
  80.  
  81.     } else g->flag = FALSE;
  82.  
  83.     /* Insert the character into the buffer */
  84.  
  85.     g->buffer[(g->bufpos)++] = c;
  86.  
  87.     if (g->bufpos == TEXT_BUFFER_SIZE)
  88.         runtime_error (g, "Text buffer overflow");
  89.  
  90.     } else stream_char (g, c);
  91.  
  92. }/* print_char */
  93.  
  94. /*
  95.  * new_line
  96.  *
  97.  * High level newline function.
  98.  *
  99.  */
  100.  
  101. void new_line (struct sg *g)
  102. {
  103.  
  104.     flush_buffer (g); stream_new_line (g);
  105.  
  106. }/* new_line */
  107.