home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 2.ddi / TVDEMOS.ZIP / TVTXTDMO.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  4.1 KB  |  146 lines

  1. /*----------------------------------------------------------*/
  2. /*                                                          */
  3. /*   Turbo Vision 1.0                                       */
  4. /*   Copyright (c) 1991 by Borland International            */
  5. /*                                                          */
  6. /*   Turbo Vision TVTXTDMD source file                      */
  7. /*----------------------------------------------------------*/
  8.  
  9. #define Uses_TWindow
  10. #define Uses_TApplication
  11. #define Uses_TRect
  12. #define Uses_TTerminal
  13. #define Uses_MsgBox
  14. #define Uses_otstream
  15. #define Uses_TDeskTop
  16. #include <tv.h>
  17.  
  18. #include <string.h>
  19. #include <stdlib.h>
  20. #include <io.h>
  21. #include <alloc.h>
  22. #include <fcntl.h>
  23. #include <strstrea.h>
  24. #include <fstream.h>
  25.  
  26. class TTerminalWindow : public TWindow
  27. {
  28.  
  29. public:
  30.  
  31.     TTerminalWindow( TRect bounds,
  32.                      const char *winTitle,
  33.                      ushort windowNo,
  34.                      TTerminal *&interior,
  35.                      ushort aBufSize
  36.                    );
  37.     TTerminal *makeInterior( TRect bounds, ushort aBufSize );
  38. };
  39.  
  40. class TMyApp : public TApplication
  41. {
  42.  
  43. public:
  44.  
  45.     TMyApp( int argc, char *argv[] );
  46.     void showTerminalWindow( char *fileName );
  47. };
  48.  
  49. void checkParamList( int argc, char *argv[] )
  50. {
  51.     if( argc != 2 )
  52.         {
  53.         cerr << "Syntax: TVTXTDMO <file to view>" << endl;
  54.         exit(1);
  55.         }
  56.  
  57.     if( access( argv[1], 4 ) != 0 ) // check for read permission
  58.         {
  59.         cerr << "Cannot open file (" << argv[1] << ")" << endl;
  60.         exit(1);
  61.         }
  62. }
  63.  
  64. TTerminalWindow::TTerminalWindow( TRect bounds,
  65.                                   const char *winTitle,
  66.                                   ushort windowNo,
  67.                                   TTerminal *&interior,
  68.                                   ushort aBufSize
  69.                                 ) :
  70.     TWindowInit( &TTerminalWindow::initFrame ),
  71.     TWindow(bounds, winTitle, windowNo )
  72. {
  73.     interior = makeInterior( bounds, aBufSize );
  74.     insert( interior );
  75. }
  76.  
  77. TTerminal *TTerminalWindow::makeInterior( TRect bounds, ushort aBufSize )
  78. {
  79.     bounds = getExtent();
  80.     bounds.grow( -1, -1 );
  81.     return new TTerminal( bounds,
  82.         0,             // no horizontal scrollbar
  83.         standardScrollBar( sbVertical | sbHandleKeyboard ),
  84.         aBufSize );
  85. };
  86.  
  87. TMyApp::TMyApp( int argc, char *argv[] ) :
  88.     TProgInit( &TMyApp::initStatusLine,
  89.                &TMyApp::initMenuBar,
  90.                &TMyApp::initDeskTop
  91.              )
  92. {
  93.     checkParamList( argc, argv );
  94.     showTerminalWindow( argv[1] );
  95. };
  96.  
  97. void TMyApp::showTerminalWindow( char *fileName )
  98. {
  99.     int handle = open( fileName, O_RDONLY );
  100.     unsigned buffSize = 8192;
  101.     if( coreleft() - 1024 < buffSize )
  102.         buffSize = unsigned(coreleft() - 1024);
  103.  
  104.     if( filelength( handle ) > buffSize )
  105.         {
  106.         char buf[128];
  107.         ostrstream os( buf, sizeof buf );
  108.         os << "File is too big to fit in a TTerminal buffer. "
  109.               "Only the first " << buffSize << " bytes of the file "
  110.               "will be displayed." << ends;
  111.         messageBox( buf, mfOKButton | mfWarning );
  112.         }
  113.     else
  114.         buffSize = unsigned(filelength( handle ));
  115.     close( handle );
  116.  
  117.     // Initialize the terminal window object
  118.     TTerminal *interior;
  119.     TTerminalWindow *demo = new TTerminalWindow( TRect( 10, 1, 70, 18 ),
  120.                                                  fileName,
  121.                                                  wnNoNumber,
  122.                                                  interior,
  123.                                                  buffSize
  124.                                                );
  125.     deskTop->insert( demo );
  126.  
  127.     // Assign the TTerminal interior text device driver to a text "file"
  128.     otstream os( interior );
  129.     
  130.     ifstream is( fileName );
  131.     is.unsetf( ios::skipws );
  132.     char st[128];
  133.  
  134.     while( is.getline( st, sizeof st ) && interior->canInsert(strlen(st)) )
  135.         os << st << endl;
  136.  
  137.     interior->scrollTo(0, 0);
  138. };
  139.  
  140. int main( int argc, char *argv[] )
  141. {
  142.     TMyApp app( argc, argv );
  143.     app.run();
  144.     return 0;
  145. }
  146.