home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_11_07 / 1107026b < prev    next >
Text File  |  1993-04-28  |  4KB  |  122 lines

  1. // MSTREAM.CPP
  2. // Defines the classes required to 
  3. // access a secondary monitor for 
  4. // debugging.
  5.  
  6. #ifdef _Windows
  7.     #include <windows.h>
  8.     extern WORD _B000H;        // pointer to global selector
  9. #else
  10.     #include <dos.h>
  11. #endif
  12.  
  13. #include "mstream.h"
  14.  
  15. #define CPL 80                 // Characters Per Line
  16. #define LPP 25                 // Lines Per Page
  17. #define ATTR 0x700             // white on black character attribute
  18.  
  19. mstreambuf::mstreambuf(char* s, int n) : streambuf(s,n)
  20. {
  21.     setp(base(),ebuf());       // Initialize the put pointers
  22.     if ( screen == 0 ) {       // screen pointer needs to be initialized
  23. #ifdef _Windows                // Windows will use the predefined global selector
  24.         screen = (unsigned int far*) MAKELONG( 0, &_B000H );
  25. #else                          // DOS will point directly to the MDA memory area
  26.         screen = (unsigned int far*) MK_FP( 0xB000, 0 );
  27. #endif
  28.                                // first time thru we clear the screen
  29.         for ( int i = 0; i < (CPL*LPP); i++ )
  30.             screen[i] = ' ' + ATTR;
  31.         column = row = 0;      // reset the current screen character position
  32.     }
  33. }
  34.  
  35. mstreambuf::~mstreambuf()
  36. {
  37.     sync();                    // flush the put buffer before we leave.
  38. }
  39.  
  40. void
  41. mstreambuf::newline()
  42. // This routine generates a new line on the mda monitor. It will scroll
  43. // all of the lines on the screen up one line and erases the bottom line
  44. // if it needs it.
  45. {
  46.     column = 0;                // reset the current column position
  47.     row++;                     // increment the current row pointer
  48.     if ( row == LPP ) {        // are we at the bottom of the screen?
  49.                                // move lines 2 thru 25 (LPP) up one line
  50.         _fmovmem( &screen[CPL], &screen[0], (CPL*(LPP-1))*2 );
  51.                                // clear the last line
  52.         for ( int i = CPL*(LPP-1); i < (CPL*LPP); i++ )
  53.             screen[i] = ATTR + ' ';
  54.         row--;                 // decrement the current row pointer
  55.     }
  56. }
  57.  
  58. int
  59. mstreambuf::overflow( int nChar )
  60. // This routine empties the put 
  61. // area and consumes the "nChar" 
  62. // character if it is not EOF.
  63. {
  64.     sync();                     // flush the buffer
  65.     if ( nChar != EOF )         // is there a character to consume?
  66.         return sputc( nChar );  // store it in the put area, returning the results
  67.     else
  68.         return 0;               // return success otherwise
  69. }
  70.  
  71. int
  72. mstreambuf::sync()
  73. // This routine flushes the put area.
  74. {
  75.     int count = out_waiting();  // get the number of characters in the put area
  76.     if ( count )    {
  77.         for ( int i=0; i< count; i++ ) {
  78.             char c = pbase()[i]; // get each character and send to mda
  79.             if ( column == CPL ) // are we at the end of the line?
  80.                 newline();       // then generate a CR/LF
  81.             if ( c == '\n' )     // is the character a CR/LF?
  82.                 newline();       // then generate a CR/LF here as well
  83.             else                 // send character and attribute to monitor
  84.                 screen[(CPL*row) + column++] = c + ATTR;
  85.         }
  86.         setp(base(),ebuf());     // reset the put area pointers
  87.     }
  88.    return 0;
  89. }
  90.  
  91. mstream::mstream() : ostream()
  92. // The class constructor. It will 
  93. // create a new streambuf using the 
  94. // classes character buffer.
  95. {
  96.     strbuf = new mstreambuf(msgs,bsize);
  97.                                  // do the actual initialization and associate
  98.                                  // strbuf with ostream
  99.     ostream::init(strbuf);
  100.                                  // set flag so that the stream to be flushed after
  101.                                  // each insertion
  102.     setf(ios::unitbuf);
  103. }
  104.  
  105. mstream::~mstream()
  106. // The class destructor.
  107. {
  108.     delete strbuf;                // delete the streambuf used by the class.
  109. }
  110.  
  111. // static class member allocation. 
  112. // This allocates space and initializes.
  113. unsigned int far* mstreambuf::screen=0; // mda screen memory area
  114. int mstreambuf::column=0;               // current screen column position
  115. int mstreambuf::row=0;                  // current screen line position
  116.  
  117. #ifndef __DLL__
  118.     mstream mout; // create a global instance of mstream for everyone to use.
  119. #endif
  120.  
  121.  
  122.