home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / prof_c / 12sbuf / sb_put.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-11  |  2.2 KB  |  104 lines

  1. /*
  2.  *    sb_put -- routines to put characters and strings into the
  3.  *    screen buffer; the cursor location is altered
  4.  */
  5.  
  6. #include <local\sbuf.h>
  7. #include <ctype.h>
  8.  
  9. extern struct BUFFER Sbuf;
  10. extern union CELL Scrnbuf[SB_ROWS][SB_COLS];
  11.  
  12. /*
  13.  *    sb_putc -- put a character into a screen buffer window
  14.  */
  15.  
  16. int
  17. sb_putc(win, ch)
  18. struct REGION *win;
  19. unsigned char ch;
  20. {
  21.     short cmax, rmax;
  22.     short lim;
  23.     short noscroll = 0, puterr = 0;
  24.  
  25.     /* calculate screen buffer position and limits */
  26.     cmax = win->c1 - win->c0;
  27.     rmax = win->r1 - win->r0;
  28.     Sbuf.row = win->r0 + win->row;
  29.     Sbuf.col = win->c0 + win->col;
  30.  
  31.     /* process the character */
  32.     switch (ch) {
  33.     case '\b':
  34.         /* non-destructive backspace */
  35.         if (win->col > 0) {
  36.             --win->col;
  37.             Sbuf.col = win->c0 + win->col;
  38.             return SB_OK;
  39.         }
  40.         else
  41.             return SB_ERR;
  42.     case '\n':
  43.         /* clear trailing line segment */
  44.         while (win->col < cmax)
  45.             if (sb_putc(win, ' ') == SB_ERR)
  46.                 ++puterr;
  47.         break;
  48.     case '\t':
  49.         /* convert tab to required number of spaces */
  50.         lim = win->col + 8 - (win->col & 0x7);
  51.         while (win->col < lim)
  52.             if (sb_putc(win, ' ') == SB_ERR)
  53.                 ++puterr;
  54.         break;
  55.     default:
  56.         /* if printable ASCII, place the character in the buffer */
  57.         if (isascii(ch) && isprint(ch))
  58.             Scrnbuf[Sbuf.row][Sbuf.col].b.ch = ch;
  59.         if (Sbuf.col < Sbuf.lcol[Sbuf.row])
  60.             Sbuf.lcol[Sbuf.row] = Sbuf.col;
  61.         if (Sbuf.col > Sbuf.rcol[Sbuf.row])
  62.             Sbuf.rcol[Sbuf.row] = Sbuf.col;
  63.         break;
  64.     }
  65.  
  66.     /* update the cursor position */
  67.     if (win->col < cmax)
  68.         ++win->col;
  69.     else if (win->row < rmax) {
  70.         win->col = 0;
  71.         ++win->row;
  72.     }
  73.     else if ((win->wflags & SB_SCROLL) == SB_SCROLL) {
  74.         sb_scrl(win, 1);
  75.         win->col = 0;
  76.         win->row = rmax;
  77.     }
  78.     else
  79.         ++noscroll;
  80.  
  81.     /* update screen buffer position */
  82.     Sbuf.row = win->r0 + win->row;
  83.     Sbuf.col = win->c0 + win->col;
  84.     Sbuf.flags |= SB_DELTA;
  85.  
  86.     return ((noscroll || puterr) ? SB_ERR : SB_OK);
  87. } /* end sb_putc() */
  88.  
  89.  
  90. /*
  91.  *    sb_puts -- put a string into the screen buffer
  92.  */
  93.  
  94. int
  95. sb_puts(win, s)
  96. struct REGION *win;
  97. unsigned char *s;
  98. {
  99.     while (*s)
  100.         if (sb_putc(win, *s++) == SB_ERR)
  101.             return SB_ERR;
  102.     return SB_OK;
  103. } /* end sb_puts() */
  104.