home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tv20os2.zip / src / TFileEditor.cpp < prev    next >
C/C++ Source or Header  |  1999-05-20  |  8KB  |  342 lines

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