home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c500 / 4.ddi / EDIT.WEX / EFILE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-28  |  6.9 KB  |  277 lines

  1. /*
  2.  *%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3.  *%                                       %
  4.  *%    Copyright (C) 1991, by WATCOM Systems Inc. All rights reserved.    %
  5.  *%                                       %
  6.  *%     Permission is granted to anyone to use this example program for       %
  7.  *%     any purpose on any computer system, subject to the following       %
  8.  *%    restrictions:                               %
  9.  *%                                       %
  10.  *%     1. This example is provided on an "as is" basis, without warranty. %
  11.  *%       You indemnify, hold harmless and defend WATCOM from and against %
  12.  *%       any claims or lawsuits, including attorney's, that arise or       %
  13.  *%       result from the use or distribution of this example, or any     %
  14.  *%       modification thereof.                       %
  15.  *%                                       %
  16.  *%     2. You may not remove, alter or suppress this notice from this       %
  17.  *%        example program or any modification thereof.               %
  18.  *%                                       %
  19.  *%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  20.  *
  21.  * EFILE.C
  22.  *
  23.  * Windows edit program: file functions
  24.  *
  25.  */
  26. #include <windows.h>
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. #include <io.h>
  30. #include <fcntl.h>
  31. #include <dos.h>
  32. #include <string.h>
  33. #include "edit.h"
  34. #include "filedll.h"
  35.  
  36. static char _ext[] = "*.*";
  37. static char _savemsg[] = "Save current changes";
  38. #ifdef __WINDOWS_386__
  39. static char _fmt[] = "%s: %s";
  40. #else
  41. static char _fmt[] = "%s: %Fs";
  42. #endif
  43.  
  44. /*
  45.  * CheckFileSave - check if file needs to be saved; if so,
  46.  *            ask the user if it should be or not
  47.  */
  48. BOOL CheckFileSave( LPEDATA ed )
  49. {
  50. char     str[128];
  51. int    rc;
  52.  
  53.     if( ed->needs_saving ) {
  54.     if( ed->filename == NULL ) {
  55.         strcpy( str, _savemsg );
  56.     } else {
  57.         sprintf( str, _fmt, _savemsg, ed->filename );
  58.     }
  59.     rc = MessageBox( ed->hwnd, str, EditTitle,  MB_YESNOCANCEL );
  60.         if( rc == IDYES ) {
  61.         if( FileSave( ed, FALSE ) ) return( TRUE );
  62.     } else if( rc == IDNO ) {
  63.         return( TRUE );
  64.     }
  65.     return( FALSE );
  66.     }
  67.     return( TRUE );
  68.  
  69. } /* CheckFileSave */
  70.  
  71. /*
  72.  * GetFileName - get a file name using common dialog stuff
  73.  */
  74. static BOOL GetFileName( LPEDATA ed, int type, char *fname )
  75. {
  76.     FILEOPEN        of;
  77.     HANDLE        dll;
  78. #if defined(__WINDOWS_386__)
  79.     HINDIR        h;
  80. #else
  81.     BOOL          (PASCAL FAR *gsn)( LPFILEOPEN );
  82. #endif
  83.     FARPROC        farp;
  84.     DWORD        a1,a2;
  85.     BOOL        rc;
  86.  
  87.     dll = LoadLibrary( "filedll.dll" );
  88.     if( dll < 32 ) {
  89.     MessageBox( NULL, "Could not find filedll.dll!", EditTitle, MB_OK );
  90.     return( FALSE );
  91.     }
  92.     farp = (FARPROC) GetProcAddress( dll,"GetFileName" );
  93. #if defined(__WINDOWS_386__)
  94.     h = GetIndirectFunctionHandle( farp, INDIR_PTR, INDIR_ENDLIST );
  95. #else
  96.     gsn = (void FAR *) farp;
  97. #endif
  98.  
  99.     fname[ 0 ] = 0;
  100.     of.hwnd = ed->hwnd;
  101.     a1 = AllocAlias16( _ext );
  102.     of.ext = (LPSTR) a1;
  103.     a2 = AllocAlias16( fname );
  104.     of.name = (LPSTR) a2;
  105.     of.namelen = _MAX_PATH;
  106.     of.title = (LPSTR) NULL;
  107.     of.type = type;
  108. #if defined(__WINDOWS_386__)
  109.     rc = InvokeIndirectFunction( h, &of );
  110. #else
  111.     rc = gsn( &of );
  112. #endif
  113.     FreeAlias16( a1 );
  114.     FreeAlias16( a2 );
  115.     FreeLibrary( dll );
  116.     return( rc );
  117.  
  118. } /* GetFileName */
  119.  
  120. /*
  121.  * NewFileName - set a new file name
  122.  */
  123. static void NewFileName( LPEDATA ed, char *fname )
  124. {
  125. char     str[256];
  126. int    len;
  127.  
  128.     MemFree( ed->filename );
  129.     len = strlen( fname );
  130.     if( len != 0 ) {
  131.         ed->filename = MemAlloc( len + 1 );
  132.         _memcpy( ed->filename, fname, len + 1 );
  133.         sprintf( str,"%s - %s", EditTitle, fname );
  134.     } else {
  135.         ed->filename = NULL;
  136.         strcpy( str, EditTitle );
  137.     }
  138.     SetWindowText( ed->hwnd, str );
  139.  
  140. } /* NewFileName */
  141.  
  142.  
  143. /*
  144.  * FileSave - save a file
  145.  */
  146. BOOL FileSave( LPEDATA ed, BOOL saveas )
  147. {
  148.     char    fname[_MAX_PATH];
  149.     char     _FAR *tmp;
  150.     int        h;
  151.     OFSTRUCT     ofs;
  152.     char    str[128];
  153.     LOCALHANDLE    hbuff;
  154.     char     FAR *buffptr;
  155.     int        len;
  156.     int        rc;
  157.     unsigned    bytes;
  158.  
  159.     if( !ed->needs_saving && !saveas ) return( TRUE );
  160.  
  161.     /*
  162.      * get filename to save
  163.      */
  164.     if( ed->filename == NULL || saveas ) {
  165.     if( !GetFileName( ed, FILE_SAVE, fname ) ) return( FALSE );
  166.     NewFileName( ed, fname );
  167.     }
  168.  
  169.     /*
  170.      * try to open the file
  171.      */
  172.     h = OpenFile( ed->filename, &ofs, OF_PROMPT | OF_CANCEL | OF_CREATE );
  173.     if( h < 0 ) {
  174.         sprintf(str, "Cannot save  %s", ed->filename );
  175.         MessageBox( ed->hwnd, str, NULL, MB_OK );
  176.     return( FALSE );
  177.     }
  178.  
  179.     /*
  180.      * get buffer, and make a copy into near memory, so that
  181.      * we can write it
  182.      */
  183.     hbuff = SendMessage( ed->editwnd, EM_GETHANDLE, 0, 0L );
  184.     buffptr = (char FAR *) MK_LOCAL32( LocalLock( hbuff ) );
  185.     len = _fstrlen( buffptr );
  186.     tmp = MemAlloc( len+1 );
  187.     _fmemcpy( tmp, buffptr, len );
  188.     LocalUnlock( hbuff );
  189.  
  190.     /*
  191.      * save file, and check results
  192.      */
  193.     rc = _dos_write( h, tmp, len, &bytes );
  194.     MemFree( tmp );
  195.     close( h );
  196.  
  197.     if( rc || bytes != len ) {
  198.         sprintf( str, "Error writing to %s", ed->filename );
  199.         MessageBox( ed->hwnd, str, EditTitle, MB_OK );
  200.     return( FALSE );
  201.     } else {
  202.         ed->needs_saving = FALSE;
  203.     return( TRUE );
  204.     }
  205.  
  206. } /* FileSave */
  207.  
  208. /*
  209.  * FileEdit - edit a new file
  210.  */
  211. void FileEdit( LPEDATA ed, BOOL openfile )
  212. {
  213.     LOCALHANDLE hbuff_new,hbuff_old;
  214.     char     fname[_MAX_PATH];
  215.     char    str[128];
  216.     long int    len=0;
  217.     char     _FAR *buff;
  218.     int        rc;
  219.     int        h;
  220.     unsigned    bytes;
  221.  
  222.     if( !CheckFileSave( ed ) ) return;
  223.     if( openfile ) {
  224.     if( !GetFileName( ed, FILE_OPEN, fname ) ) return;
  225.     } else {
  226.         fname[0] = 0;
  227.     }
  228.  
  229.     /*
  230.      * get file size, and get a buffer
  231.      */
  232.     if( fname[0] != 0 ) {
  233.     h = open( fname, O_RDONLY | O_BINARY );
  234.     if( h < 0 ) {
  235.         sprintf( str,"Could not open file %s", fname );
  236.         MessageBox( ed->hwnd, str, EditTitle, MB_OK );
  237.         return;
  238.     }
  239.     len = filelength( h );
  240.     }
  241.     if( len >= 0xFFFFL || (hbuff_new = LocalAlloc( LMEM_MOVEABLE |
  242.             LMEM_ZEROINIT, (WORD) len+1 ) ) == NULL ) {
  243.     sprintf( str, "File %s is too big (%ld bytes)", fname, len+1 );
  244.     MessageBox( ed->hwnd, str, EditTitle, MB_OK );
  245.     return;
  246.     }
  247.  
  248.     /*
  249.      * read into temp. buffer, then copy to edit area
  250.      */
  251.     if( fname[0] != 0 ) {
  252.     buff = MemAlloc( len + 1 );
  253.     rc = _dos_read( h, buff, len, &bytes );
  254.     close( h );
  255.     if( rc || bytes != len ) {
  256.         sprintf( str,"Could not read file %s", fname );
  257.         MessageBox( ed->hwnd, str, EditTitle, MB_OK );
  258.         LocalFree( hbuff_new );
  259.         MemFree( buff );
  260.         return;
  261.     }
  262.     _fmemcpy( MK_LOCAL32( LocalLock( hbuff_new ) ), buff, len );
  263.     LocalUnlock( hbuff_new );
  264.     MemFree( buff );
  265.     }
  266.  
  267.     /*
  268.      * clear out old buffer, and set new one
  269.      */
  270.     hbuff_old = SendMessage( ed->editwnd, EM_GETHANDLE, 0, 0L );
  271.     LocalFree( hbuff_old );
  272.     SendMessage( ed->editwnd, EM_SETHANDLE, hbuff_new, 0L );
  273.     NewFileName( ed, fname );
  274.     ed->needs_saving = FALSE;
  275.  
  276. } /* FileEdit */
  277.