home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / SCRINTRP.CPP < prev    next >
C/C++ Source or Header  |  1997-07-05  |  2KB  |  125 lines

  1. // +++Date last modified: 05-Jul-1997
  2.  
  3. // Base class for screen interpreter
  4. // public domain
  5. // by David Nugent <davidn@unique.blaze.net.au>
  6. // 3:632/348@fidonet
  7.  
  8. #include <iostream.h>
  9. #include "scrintrp.hpp"
  10.  
  11. const int scrinterp::CBUFSZ = 127;
  12.  
  13. scrinterp::scrinterp(video & v)
  14.   : vs(v),
  15.     chrbuf(new char [CBUFSZ+1]),
  16.     chridx(0),
  17.     flushevery(0),
  18.     insertmode(0)
  19. {}
  20.  
  21. scrinterp::~scrinterp()
  22. {
  23.   flushbuf();
  24.   delete[] chrbuf;
  25. }
  26.  
  27. void scrinterp::reset()
  28. {
  29.   chridx = 0;
  30. }
  31.  
  32.   // This is the basic get-a-character/display-it
  33.   // loop that is used to drive the engine
  34.  
  35. void
  36. scrinterp::display(istream & is)
  37. {
  38.   int ch =0;
  39.   reset();
  40.   while (is.good() && (ch = is.get()) != EOF)
  41.     putch(ch);
  42.   flushbuf();
  43. }
  44.  
  45. void
  46. scrinterp::flushbuf()
  47. {
  48.   if (chridx)
  49.   {
  50.     chrbuf[chridx] = '\0';
  51.     vs.put(chrbuf);
  52.     chridx = 0;
  53.   }
  54. }
  55.  
  56. void
  57. scrinterp::putbuf(int ch)
  58. {
  59.   if (chridx >= CBUFSZ)
  60.     flushbuf();
  61.   chrbuf[chridx++] = char(ch);
  62. }
  63.  
  64. void
  65. scrinterp::putch(int ch)
  66. {
  67.   cell_t x, y, mx, my;
  68.  
  69.   // We can handle all of the standard control chrs here
  70.   switch (ch)
  71.   {
  72.   case '\r':                  // CR
  73.     flushbuf();
  74.     vs.wherexy(x, y);
  75.     vs.gotoxy(0, y);
  76.     break;
  77.  
  78.   case '\t':                  // TAB
  79.     flushbuf();
  80.     vs.wherexy(x, y);
  81.     vs.maxxy(mx, my);
  82.     x = cell_t((((x + 8) / 8) + 1) * 8);
  83.     if (x >= mx)
  84.     {
  85.       x = cell_t(x % mx);
  86.       goto dolf;
  87.     }
  88.     vs.gotoxy(x, y);
  89.     break;
  90.  
  91.   case '\n':                  // NL
  92.     flushbuf();
  93.     vs.wherexy(x, y);
  94.     vs.maxxy(mx, my);
  95.   dolf:
  96.     if (y == cell_t(my - 1))  // On last line
  97.       vs.scroll(0, 0, mx, my, 1);
  98.     else
  99.       ++y;
  100.     vs.gotoxy(x, y);
  101.     break;
  102.  
  103.   case '\007':                 // BELL
  104.     // beep() !
  105.     break;
  106.  
  107.   case '\b':
  108.     vs.wherexy(x, y);
  109.     if (x > 0)
  110.       vs.gotoxy(--x, y);
  111.     break;
  112.  
  113.   case '\x0c':                // LF
  114.     // Should to a cls here with home, perhaps...
  115.     break;
  116.  
  117.   default:
  118.     putbuf(ch);
  119.     if (flushevery)
  120.       flushbuf();
  121.     break;
  122.   }
  123. }
  124.  
  125.