home *** CD-ROM | disk | FTP | other *** search
/ Boldly Go Collection / version40.iso / TS / 17A / DRWIN101.ZIP / TBUTIL.CPP < prev    next >
C/C++ Source or Header  |  1991-07-16  |  10KB  |  263 lines

  1.  
  2. // Text buffer (quasi-window) functions
  3.  
  4. #include <stdio.h>
  5. #include <conio.h>
  6. #include <string.h>
  7. #include <stdarg.h>
  8.  
  9. #include "tbutil.hpp"
  10.  
  11.  
  12. TBuf::TBuf(void)
  13. {
  14.   rows=MAX_ROWS;                       //max rows
  15.   cols=MAX_COLS;                       //max cols
  16.   txt=new (WORD[rows*cols]);           //allocate space for text
  17.   if (txt==NULL) {                     //if no room..
  18.     rows=cols=0;                       //..reset things
  19.     return;                            //..exit
  20.   }
  21.   row=0;                               //start at upper
  22.   col=0;                               //left corner
  23.   att=DEF_ATT;                         //use default attributes
  24.   fil=DEF_FIL;                         //use default fill character
  25.   nam[0]=0;                            //don't set up a name
  26.   fl.wrap=1;                           //wrapping
  27.   fl.scroll=1;                         //scrolling
  28.   Clr();                               //clear the whole buffer
  29. }   //TBuf default constructor
  30.  
  31.  
  32.  
  33. TBuf::TBuf(int rs,int cs)
  34. {
  35.   rows=cols=0;                         //first indicate failure
  36.   if ((rs<MIN_ROWS)||(rs>MAX_ROWS)) return;   //fail
  37.   if ((cs<MIN_COLS)||(cs>MAX_COLS)) return;   //fail
  38.   rows=rs;                             //set rows
  39.   cols=cs;                             //set columns
  40.   txt=new (WORD[rows*cols]);           //allocate space for text
  41.   if (txt==NULL) {                     //if no room..
  42.     rows=cols=0;                       //..reset things
  43.     return;                            //..exit
  44.   }
  45.   row=0;                               //start at upper
  46.   col=0;                               //left corner
  47.   att=DEF_ATT;                         //use default attributes
  48.   fil=DEF_FIL;                         //use default fill character
  49.   nam[0]=0;                            //don't set up a name
  50.   fl.wrap=1;                           //wrapping
  51.   fl.scroll=1;                         //scrolling
  52.   Clr();                               //clear the buffer
  53. }   //TBuf constructor given size
  54.  
  55.  
  56. TBuf::TBuf(int rs,int cs,BYTE a)
  57. {
  58.   rows=cols=0;                         //first indicate failure
  59.   if ((rs<MIN_ROWS)||(rs>MAX_ROWS)) return;   //fail
  60.   if ((cs<MIN_COLS)||(cs>MAX_COLS)) return;   //fail
  61.   rows=rs;                             //set rows
  62.   cols=cs;                             //set columns
  63.   txt=new (WORD[rows*cols]);           //allocate space for text
  64.   if (txt==NULL) {                     //if no room..
  65.     rows=cols=0;                       //..reset things
  66.     return;                            //..exit
  67.   }
  68.   row=0;                               //start at upper
  69.   col=0;                               //left corner
  70.   fil=DEF_FIL;                         //use default fill character
  71.   nam[0]=0;                            //don't set up a name
  72.   fl.wrap=1;                           //wrapping
  73.   fl.scroll=1;                         //scrolling
  74.   att=a;                               //set attribute to passed value
  75.   Clr();                               //clear the buffer
  76. }   //TBuf constructor given corners
  77.  
  78.  
  79. TBuf::~TBuf(void)
  80. {
  81.   delete txt;                          //delete the text from memory
  82. }   //TBuf::~Tbuf
  83.  
  84.  
  85. void TBuf::Name(char* s)               //sets text-buffer name
  86. {
  87.   int l=strlen(s);                     //length of name
  88.   if (l>=MAX_NAM) l=MAX_NAM-1;         //make sure length is ok
  89.   strncpy(nam,s,l-1);                  //copy into window's name
  90.   nam[l]=0;                            //terminate string
  91. }   //TBuf::Name(char*)
  92.  
  93.  
  94. void TBuf::Pos(int r,int c=0)
  95. {
  96.   if ((r>=rows)||(r<0)) r=0;           //check row
  97.   if ((c>=cols)||(c<0)) c=0;           //check column
  98.   row=r;
  99.   col=c;
  100. }   //TBuf::Pos(int,int)
  101.  
  102.  
  103. void TBuf::Clr(int dir)                //clear buffer (beg=-1,all=0,end=+1)
  104. {
  105.   WORD ca=(att<<8)+(BYTE)fil;          //fill char and attribute
  106.   WORD* wp;                            //pointer into text window
  107.   WORD n;                              //number of cells
  108.  
  109.   if (!dir) {                          //0==entire screen
  110.     wp=txt;                            //start at beginning
  111.     n=rows*cols;                       //go for entire screen
  112.     row=col=0;                         //reset cursor position
  113.   }
  114.   else if (dir<0) {                    //-1==from beginning
  115.     wp=txt;                            //start at beginning
  116.     n=row*cols+col;                    //go to current row and column
  117.   }
  118.   else {                               //+1==to end
  119.     wp=&txt[row*cols+col];             //set start to current row and column
  120.     n=(rows-row)*cols-col;             //go to end of text buffer
  121.   }
  122.  
  123.   while (n--) *wp++=ca;                //clear the whole thing
  124. }   //TBuf::Clr(int)
  125.  
  126.  
  127. void TBuf::ClrRow(int dir)             //clear row (beg=-1,all=0,end=+1)
  128. {
  129.   WORD ca=(att<<8)+(BYTE)fil;          //fill char and attribute
  130.   WORD* wp;                            //pointer into text window
  131.   WORD n;                              //number of cells
  132.  
  133.   if (!dir) {                          //0==entire row
  134.     wp=&txt[row*cols];                 //start at beginning of row
  135.     n=cols;                            //go for entire row
  136.     col=0;                             //reset cursor position
  137.   }
  138.   else if (dir<0) {                    //-1==from beginning of row
  139.     wp=&txt[row*cols];                 //start at beginning of row
  140.     n=col;                             //go to current column
  141.   }
  142.   else {                               //+1==to end of row
  143.     wp=&txt[row*cols+col];             //set start to current row and column
  144.     n=cols-col;                        //go to end of row
  145.   }
  146.  
  147.   while (n--) *wp++=ca;                //clear the whole thing
  148. }   //TBuf::ClrRow(int)
  149.  
  150.  
  151. void TBuf::ScrollUp(int n)
  152. {
  153.   WORD* wp1;                           //leading pointer
  154.   WORD* wp2;                           //trailing pointer
  155.   WORD  nb=cols<<1;                    //number of bytes per row
  156.  
  157.   if (!n) return;                      //no scroll!
  158.   int r=row;                           //save the row,column
  159.   int c=col;
  160.   if (n>0) {                           //check for scroll-up/scroll-down
  161.     if (n>=rows) { Clr(); Pos(r,c); return; }    //huge scroll
  162.     wp1=txt;                           //point to beginning of buffer
  163.     wp2=&txt[n*cols];                  //point to next available row
  164.     for (int i=n;i<rows;i++) {         //go through each row to be scrolled
  165.       memcpy(wp1,wp2,nb);              //copy it over
  166.       wp1+=cols;                       //increment row pointers
  167.       wp2+=cols;
  168.     }   //for
  169.     Pos(rows-n,0);                     //go to first new row
  170.     Clr(CLR_END);                      //clear to end of buffer
  171.     Pos(r,c);                          //restore cursor
  172.   }   //if up
  173.   else {                               //scroll down
  174.     n=-n;                              //invert if scrolling down
  175.     if (n>=rows) { Clr(); Pos(r,c); return; }    //huge scroll
  176.     wp1=&txt[(rows-n-1)*cols];         //point to last row of buffer
  177.     wp2=&txt[(rows-1)*cols];           //point to next available row
  178.     for (int i=n;i<rows;i++) {         //go through each row to be scrolled
  179.       memcpy(wp2,wp1,nb);              //copy it over
  180.       wp1-=cols;                       //decrement row pointers
  181.       wp2-=cols;
  182.     }   //for
  183.     Pos(n,0);                          //go to row
  184.     Clr(CLR_BEG);                      //clear from beginning of buffer
  185.     Pos(r,c);                          //restore cursor
  186.   }   //if scrolling down
  187. }   //TBuf::ScrollUp(int)
  188.  
  189.  
  190. void TBuf::IncCol(void)
  191. {
  192.   col++;                               //increment column pointer
  193.   if (col<cols) return;                //done if column is small enough
  194.   col--;                               //back down
  195.   if (!fl.wrap) return;                //if not wrapping, leave hangin'
  196.   col=0;
  197.   IncRow();                            //wrap to next row
  198. }   //TBuf::IncCol(void)
  199.  
  200.  
  201. void TBuf::IncRow(void)
  202. {
  203.   int c=col;                           //save column position
  204.   col=0;                               //reset column for clears
  205.   row++;                               //wrap to next row
  206.   if (row<rows) return;                //if row ok, great
  207.   if (!fl.scroll) {                    //if not scrolling, just wrap..
  208.     row=0;                             //set to first row
  209.     ClrRow(CLR_END);                   //clear the first row
  210.     col=c;                             //restore column
  211.     return;                            //all done
  212.   }
  213.   ScrollUp(1);                         //scroll up one row
  214.   row=rows-1;                          //set to bottom row
  215.   ClrRow(CLR_END);                     //clear bottom row
  216.   col=c;                               //restore column pointer
  217. }   //TBuf::IncRow(void)
  218.  
  219.  
  220. void TBuf::Put(char c)
  221. {
  222.   switch (c) {                         //see if special character
  223.   case '\r':                           //if CR, reset column
  224.     col=0;
  225.     break;
  226.   case '\n':                           //if LF, newline
  227.     col=0;                             //reset column
  228.     IncRow();                          //go to next row
  229.     break;
  230.   default:
  231.     txt[row*cols+col]=(WORD)(att<<8)+(WORD)c;  //put char into buffer
  232.     IncCol();                          //point to next location
  233.   }   //switch
  234. }   //TBuf::Put(char)
  235.  
  236.  
  237. void TBuf::Put(char* s)
  238. {
  239.   while (*s) Put(*s++);                //just write each character
  240. }   //TBuf::Put(char*)
  241.  
  242.  
  243. void TBuf::Put(char* s,int n)
  244. {
  245.   int i;                               //local index
  246.   for (i=0;i<n;i++) Put(*s++);         //just write each character
  247. }   //TBuf:Put(char*,int)
  248.  
  249.  
  250. void TBuf::Printf(char *fmt,...)       //formatted print
  251. {
  252.   va_list va;                          //variable argument list
  253.   int l;                               //length of string
  254.   char b[256];                         //buffer to hold string
  255.  
  256.   va_start(va,fmt);                    //open va list
  257.   l=vsprintf(b,fmt,va);                //write into a string
  258.   va_end(va);                          //close va list
  259.   Put(b,l);                            //put out string of l chars
  260. }   //TBuf::Printf
  261.  
  262.  
  263.