home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 9.ddi / TVSRC.ZIP / TFILEDTR.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  7.9 KB  |  321 lines

  1. /*------------------------------------------------------------*/
  2. /* filename - tfiledtr.cpp                                    */
  3. /*                                                            */
  4. /* function(s)                                                */
  5. /*            TFileEditor member functions                    */
  6. /*------------------------------------------------------------*/
  7.  
  8. /*------------------------------------------------------------*/
  9. /*                                                            */
  10. /*    Turbo Vision -  Version 1.0                             */
  11. /*                                                            */
  12. /*                                                            */
  13. /*    Copyright (c) 1991 by Borland International             */
  14. /*    All Rights Reserved.                                    */
  15. /*                                                            */
  16. /*------------------------------------------------------------*/
  17.  
  18. #define Uses_TGroup
  19. #define Uses_TEditor
  20. #define Uses_TFileEditor
  21. #define Uses_TEvent
  22. #define Uses_opstream
  23. #define Uses_ipstream
  24. #include <tv.h>
  25.  
  26. #if !defined( __LIMITS_H )
  27. #include <Limits.h>
  28. #endif  // __LIMITS_H
  29.  
  30. #if !defined( __STRING_H )
  31. #include <string.h>
  32. #endif  // __STRING_H
  33.  
  34. #if !defined( __FSTREAM_H )
  35. #include <fstream.h>
  36. #endif  // __FSTREAM_H
  37.  
  38. #if !defined( __IO_H )
  39. #include <io.h>
  40. #endif  // __IO_H
  41.  
  42. #if !defined( __STDIO_H )
  43. #include <stdio.h>
  44. #endif  // __STDIO_H
  45.  
  46. inline ushort min( ushort u1, ushort u2 )
  47. {
  48.     return u1 < u2 ? u1 : u2;
  49. }
  50.  
  51. TFileEditor::TFileEditor( const TRect& bounds,
  52.                           TScrollBar *aHScrollBar,
  53.                           TScrollBar *aVScrollBar,
  54.                           TIndicator *aIndicator,
  55.                           const char *aFileName
  56.                         ) :
  57.     TEditor( bounds, aHScrollBar, aVScrollBar, aIndicator, 0 )
  58. {
  59.     if( aFileName == 0 )
  60.         fileName[0] = EOS;
  61.     else
  62.         {
  63.         strcpy( fileName, aFileName );
  64.         fexpand( fileName );
  65.         if( isValid )
  66.             isValid = loadFile();
  67.         }
  68. }
  69.  
  70. void TFileEditor::doneBuffer()
  71. {
  72.     delete buffer;
  73. }
  74.  
  75. void TFileEditor::handleEvent( TEvent& event )
  76. {
  77.     TEditor::handleEvent(event);
  78.     switch( event.what )
  79.         {
  80.         case evCommand:
  81.             switch( event.message.command )
  82.                 {
  83.                 case cmSave:
  84.                     save();
  85.                     break;
  86.                 case cmSaveAs:
  87.                     saveAs();
  88.                     break;
  89.                 default:
  90.                     return;
  91.                 }
  92.             break;
  93.         default:
  94.             return;
  95.         }
  96.     clearEvent(event);
  97. }
  98.  
  99. void TFileEditor::initBuffer()
  100. {
  101.     buffer = new char[bufSize];
  102. }
  103.  
  104. Boolean TFileEditor::loadFile()
  105. {
  106.     ifstream f( fileName, ios::in | ios::binary );
  107.     if( !f )
  108.         {
  109.         setBufLen( 0 );
  110.         return True;
  111.         }
  112.     else
  113.         {
  114.         long fSize = filelength( f.rdbuf()->fd() );
  115.         if( fSize > 0xFFE0L || setBufSize(ushort(fSize)) == False )
  116.             {
  117.             editorDialog( edOutOfMemory );
  118.             return False;
  119.             }
  120.         else
  121.             {
  122.             if ( fSize > INT_MAX )
  123.             {
  124.                f.read( &buffer[bufSize - ushort(fSize)], INT_MAX );
  125.                f.read( &buffer[bufSize - ushort(fSize) + INT_MAX],
  126.                                 ushort(fSize - INT_MAX) );
  127.  
  128.             }
  129.             else
  130.                f.read( &buffer[bufSize - ushort(fSize)], ushort(fSize) );
  131.             if( !f )
  132.                 {
  133.                 editorDialog( edReadError, fileName );
  134.                 return False;
  135.                 }
  136.             else
  137.                 {
  138.                 setBufLen(ushort(fSize));
  139.                 return True;
  140.                 }
  141.             }
  142.         }
  143. }
  144.  
  145. Boolean TFileEditor::save()
  146. {
  147.     if( *fileName == EOS )
  148.         return saveAs();
  149.     else
  150.         return saveFile();
  151. }
  152.  
  153. Boolean TFileEditor::saveAs()
  154. {
  155.     Boolean res = False;
  156.     if( editorDialog( edSaveAs, fileName ) != cmCancel )
  157.         {
  158.         fexpand( fileName );
  159.         message( owner, evBroadcast, cmUpdateTitle, 0 );
  160.         res = saveFile();
  161.         if( isClipboard() == True )
  162.             *fileName = EOS;
  163.         }
  164.     return res;
  165. }
  166.  
  167. static void writeBlock( ofstream& f, char *buf, unsigned len )
  168. {
  169.     while( len > 0 )
  170.         {
  171.         int l = len < INT_MAX ? len : INT_MAX;
  172.         f.write( buf, l );
  173.         buf += l;
  174.         len -= l;
  175.         }
  176. }
  177.  
  178. Boolean TFileEditor::saveFile()
  179. {
  180.     char drive[MAXDRIVE];
  181.     char dir[MAXDIR];
  182.     char file[MAXFILE];
  183.     char ext[MAXEXT];
  184.  
  185.     if( (editorFlags & efBackupFiles) != 0 )
  186.         {
  187.         fnsplit( fileName, drive, dir, file, ext );
  188.         char backupName[MAXPATH];
  189.         fnmerge( backupName, drive, dir, file, backupExt );
  190.         unlink( backupName );
  191.         rename( fileName, backupName );
  192.         }
  193.  
  194.     ofstream f( fileName, ios::out | ios::binary );
  195.  
  196.     if( !f )
  197.         {
  198.         editorDialog( edCreateError, fileName );
  199.         return False;
  200.         }
  201.     else
  202.         {
  203.         writeBlock( f, buffer, curPtr );
  204.         writeBlock( f, buffer+curPtr+gapLen, bufLen-curPtr );
  205.  
  206.         if( !f )
  207.             {
  208.             editorDialog( edWriteError, fileName );
  209.             return False;
  210.             }
  211.         else
  212.             {
  213.             modified = False;
  214.             update(ufUpdate);
  215.             }
  216.         }
  217.     return True;
  218. }
  219.  
  220. Boolean TFileEditor::setBufSize( ushort newSize )
  221. {
  222.     if( newSize > 0xF000 )
  223.         newSize = 0xFFE0;
  224.     else
  225.         newSize = (newSize + 0x0FFF) & 0xF000;
  226.     if( newSize != bufSize )
  227.         {
  228.         char *temp = buffer;
  229.         if( (buffer = new char[newSize]) == 0 )
  230.             {
  231.             delete temp;
  232.             return False;
  233.             }
  234.         ushort n = bufLen - curPtr + delCount;
  235.         memcpy( buffer, temp, min( newSize, bufSize ) );
  236.         memmove( &buffer[newSize - n], &temp[bufSize - n], n );
  237.         delete temp;
  238.         bufSize = newSize;
  239.         gapLen = bufSize - bufLen;
  240.         }
  241.     return True;
  242. }
  243.  
  244. void TFileEditor::shutDown()
  245. {
  246.     setCmdState(cmSave, False);
  247.     setCmdState(cmSaveAs, False);
  248.     TEditor::shutDown();
  249. }
  250.  
  251. void TFileEditor::updateCommands()
  252. {
  253.     TEditor::updateCommands();
  254.     setCmdState(cmSave, True);
  255.     setCmdState(cmSaveAs, True);
  256. }
  257.  
  258. Boolean TFileEditor::valid( ushort command )
  259. {
  260.     if( command == cmValid )
  261.         return isValid;
  262.     else
  263.         {
  264.         if( modified == True )
  265.             {
  266.             int d;
  267.             if( *fileName == EOS )
  268.                 d = edSaveUntitled;
  269.             else
  270.                 d = edSaveModify;
  271.  
  272.             switch( editorDialog( d, fileName ) )
  273.                 {
  274.                 case cmYes:
  275.                     return save();
  276.                 case cmNo:
  277.                     modified = False;
  278.                     return True;
  279.                 case cmCancel:
  280.                     return False;
  281.                 }
  282.             }
  283.         }
  284.     return True;
  285. }
  286.  
  287. void TFileEditor::write( opstream& os )
  288. {
  289.     TEditor::write( os );
  290.     os.writeString( fileName );
  291.     os << selStart << selEnd << curPtr;
  292. }
  293.  
  294. void *TFileEditor::read( ipstream& is )
  295. {
  296.     TEditor::read( is );
  297.     is.readString( fileName, sizeof( fileName ) );
  298.     if( isValid )
  299.         {
  300.         isValid = loadFile();
  301.         ushort sStart, sEnd, curs;
  302.         is >> sStart >> sEnd >> curs;
  303.         if( isValid && sEnd <= bufLen )
  304.             {
  305.             setSelect( sStart, sEnd, Boolean(curs == sStart) );
  306.             trackCursor( True );
  307.             }
  308.         }
  309.     return this;
  310. }
  311.  
  312. TStreamable *TFileEditor::build()
  313. {
  314.     return new TFileEditor( streamableInit );
  315. }
  316.  
  317. TFileEditor::TFileEditor( StreamableInit ) : TEditor( streamableInit )
  318. {
  319. }
  320.  
  321.