PRODUCT : Borland C++ NUMBER : 1397 VERSION : 3.x OS : DOS DATE : October 25, 1993 PAGE : 1/4 TITLE : Example of constream input/output //---------------------------------------------------------- // // Constream example program // // Description: // // This example shows use of multiple constream windows // and how to get user input within a window while // maintaining the color attributes set for that window. // // Ideally, one would derive from constream to create // windows with additional functionality. To avoid // confusing the subject, this example takes a more // procedural approach to using the objects and is not // intended to be an example of good OOP design. // // Usage notes: // // On construction, constreams tie() to the input stream // cin. For this reason, constreams should not be // destructed during program execution - doing so will // eventually result in a program crash. Declaring global // objects will appear to work since they will not be // destructed until program termination, but may leave an // unstable DOS environment. For best results, allocate // constream instances from the heap and do not delete // them. Each instance will take 112 bytes in large model // and 64 bytes in small. However, constream instances // can be used for multiple purposes, as in the Input and // Display functions in this example. Direct calls to // conio functions work on the active window, so calling // clrscr() would clear the currently active window. However, // by calling the constream object's member function for // these purposes, we ensure the intended window will be // active before the window before conio operations occur. // When a constream object puts text to the screen, it // actual writes to video memory. Thus, the window and // its text don't get erased, they can only be // overwritten. We can reuse a constream object, redefine // it by calling its window() member, and give the // appearance of overlapping windows. //------------------------------------------------------------ PRODUCT : Borland C++ NUMBER : 1397 VERSION : 3.x OS : DOS DATE : October 25, 1993 PAGE : 2/4 TITLE : Example of constream input/output #include #include #include #include char * Input( constream& win ); void Display( constream& win, const char * text ); const bfrLen = 500; // Using references lets us use the simpler syntax of // accessing object members as opposed to a pointer to an // object. constream& statusWin = *new constream; //------------------------------------------------------------ int main( int argc, char ** argv ) { constream& screen = *new constream; constream& utilityWin = *new constream; // Since screen is instantiated before any calls to set a // specific window, it's constructor initializes it with // the start-up video attributes. Thus, we can always act // on the entire CRT screen by operating upon it. screen.clrscr(); // Initialize the status line and display greeting statusWin.window( 1, 25, 80, 25 ); statusWin << setattr( (BLUE<<4) | LIGHTRED ) << setcrsrtype( _NOCURSOR ); statusWin.clrscr(); statusWin << "Welcome to " << argv[0]; statusWin << " Press any key to start!"; getch(); // Get some input from the user char * textBfr = Input( utilityWin ); // And show the results. Display( utilityWin, textBfr ); PRODUCT : Borland C++ NUMBER : 1397 VERSION : 3.x OS : DOS DATE : October 25, 1993 PAGE : 3/4 TITLE : Example of constream input/output // Say goodbye and clean up statusWin.clrscr(); statusWin << "Thank you for playing! Press any key..."; getch(); screen.clrscr(); delete textBfr; return argc; } //----------------------------------------------------------- // // function Input // // Description: // // Get input from user and return pointer to a buffer // allocated on the heap. // // Simply collects printable characters in the order input // and ignores non-printable characters. Stops saving // input if buffer fills. Input is terminated by receipt // of a carriage return. Aborts process and returns NULL // if allocation cannot be made for input buffer. // //------------------------------------------------------------ char * Input( constream& win ) { unsigned count = 0; char * bfr = new char[ bfrLen ]; if( bfr ) { statusWin.clrscr(); statusWin << "Press 'Enter' to terminate input."; win.window( 10, 5, 50, 18 ); win << setattr( (CYAN<<4) | BLACK ); win.clrscr(); win << "Enter some text:\n"; win << setclr( BLUE ); char c=getch(); PRODUCT : Borland C++ NUMBER : 1397 VERSION : 3.x OS : DOS DATE : October 25, 1993 PAGE : 4/4 TITLE : Example of constream input/output while( c!='\r' ) { if( isprint( c ) ) { win.put( c ); if( count < bfrLen ) bfr[count++]=c; } c=getch(); } } // Ensure our string has a null terminator and throw away // the last character if necessary. bfr[ (count==bfrLen) ? count-1 : count ] = '\0'; return bfr; } //------------------------------------------------------------ // // function Display // // Description: // // Accepts a null-terminated character buffer and // unceremoniously blasts text into a constream window. // //------------------------------------------------------------ void Display( constream& win, const char * text ) { win.window( 30, 12, 70, 20 ); win << setattr( (BLUE<<4) | LIGHTMAGENTA ); win.clrscr(); win << "Display window:\n" << setclr( YELLOW ) << text; } DISCLAIMER: You have the right to use this technical information subject to the terms of the No-Nonsense License Statement that you received with the Borland product to which this information pertains.