home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c500 / 4.ddi / FILEDLL.WEX / FILEDLL.C next >
Encoding:
C/C++ Source or Header  |  1992-05-28  |  6.9 KB  |  286 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.  * FILEDLL.C
  22.  *
  23.  * Windows file dialog  - this is a 16-bit example only
  24.  *
  25.  */
  26. #include <windows.h>
  27. #include <dos.h>
  28. #include <string.h>
  29. #include <ctype.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <fcntl.h>
  33. #include <io.h>
  34. #include <direct.h>
  35. #include <errno.h>
  36. #include "filedll.h"
  37. #include "filerc.h"
  38.  
  39. HANDLE        Inst;
  40. LPFILEOPEN    Ofs;
  41. char        HasFile;
  42. char        _ext[128];
  43. char        Buff[256];
  44.  
  45. /*
  46.  * LibMain - main DLL entry point
  47.  */
  48. int PASCAL LibMain( HANDLE inst, WORD data, WORD heapsize,
  49.                 LPSTR cmdline )
  50. {
  51.     cmdline = cmdline;
  52.     data = data;
  53.     heapsize = heapsize;
  54.  
  55.     Inst = inst;
  56.     return( 1 );
  57.  
  58. } /* LibMain */
  59.  
  60. /*
  61.  * WEP - dll is exiting
  62.  */
  63. int __export FAR PASCAL WEP( int sys_exit )
  64. {
  65.     sys_exit = sys_exit;
  66.     return( 1 );
  67.  
  68. } /* WEP */
  69.  
  70.  
  71. /*
  72.  * CheckSaveVerify - see if save-verify request is needed
  73.  */
  74. static BOOL CheckSaveVerify( void )
  75. {
  76.     WORD    rc;
  77.  
  78.     if( Ofs->type == FILE_SAVE_VERIFY ) {
  79.     rc = MessageBox( NULL,Buff,"Really overwrite?",
  80.         MB_OKCANCEL | MB_TASKMODAL );
  81.     if( rc == IDOK ) return( TRUE );
  82.     return( FALSE );
  83.     }
  84.     return( TRUE );
  85.  
  86. } /* CheckSaveVerify */
  87.  
  88. /*
  89.  * BuildDirectory - build the directory boxes
  90.  */
  91. static void BuildDirectory( HWND hwnd )
  92. {
  93.     WORD    handle;
  94.  
  95.     SendDlgItemMessage( hwnd, DRIVE_BOX, LB_RESETCONTENT, 0, 0L );
  96.     SendDlgItemMessage( hwnd, FILE_BOX, LB_RESETCONTENT, 0, 0L );
  97.     SendDlgItemMessage( hwnd, DIR_BOX, LB_RESETCONTENT, 0, 0L );
  98.     DlgDirList( hwnd, _ext, DRIVE_BOX, NULL, 0x8000 | 0x4000 );
  99.     DlgDirList( hwnd, _ext, FILE_BOX, NULL, 0x0000 );
  100.     DlgDirList( hwnd, _ext, DIR_BOX, NULL, 0x8000 | 0x0010 );
  101.     handle = GetDlgItem( hwnd, FILE_EDIT );
  102.     SetWindowText( handle, (LPSTR) _ext );
  103.     handle = GetDlgItem( hwnd, FILE_CWD );
  104.     getcwd( Buff, _MAX_PATH );
  105.     SetWindowText( handle, (LPSTR) Buff );
  106.  
  107. } /* BuildDirectory */
  108.  
  109. /*
  110.  * TryTheFile - try the current file
  111.  */
  112. static BOOL TryTheFile( HWND hwnd )
  113. {
  114.     char    tmp[_MAX_PATH];
  115.     char     drive[_MAX_DRIVE],directory[_MAX_DIR];
  116.     char    name[_MAX_FNAME],ext[_MAX_EXT];
  117.     unsigned    total;
  118.     int        rc;
  119.     int        h;
  120.     int        i;
  121.     int        len;
  122.     BOOL    isdir = FALSE;
  123.     BOOL    iswild = FALSE;
  124.  
  125.     GetDlgItemText( hwnd, FILE_EDIT, (LPSTR) Buff, _MAX_PATH );
  126.     rc = access( Buff, ACCESS_RD );
  127.     if( !rc ) {
  128.         /*
  129.      * see if this is a directory
  130.      */
  131.         strcpy( tmp,Buff );
  132.     if( tmp[strlen(tmp)-1] != '\\' ) strcat( tmp,"\\" );
  133.     strcat( tmp,"NUL" );
  134.     h = open( tmp, O_RDONLY );
  135.     if( h < 0 ) {
  136.         return( CheckSaveVerify() );
  137.     }
  138.     close( h );
  139.     isdir = TRUE;
  140.     }
  141.     len = strlen( Buff );
  142.     for( i=0; i<len; i++ ) {
  143.     if( Buff[i] == '*' || Buff[i] == '?' ) {
  144.         iswild = TRUE;
  145.         break;
  146.     }
  147.     }
  148.     if( errno == ENOENT && !iswild ) return( TRUE );
  149.  
  150.     _splitpath( Buff, drive, directory, name, ext );
  151.     if( drive[0] != 0 ) {
  152.     _dos_setdrive( tolower( drive[0] ) - 'a' + 1, &total );
  153.     }
  154.     if( isdir ) {
  155.     strcat( directory, name );
  156.     strcat( directory, ext );
  157.     name[0] = 0;
  158.     ext[0] = 0;
  159.     }
  160.     if( directory[0] != 0 ) {
  161.         i = strlen( directory );
  162.     if( directory[i-1] == '\\' ) directory[i-1] = 0;
  163.         chdir( directory );
  164.     }
  165.     if( name[0] == '*' || ext[1] == '*' ) {
  166.         strcpy( _ext, name );
  167.     strcat( _ext, ext );
  168.     } else {
  169.     strcpy( _ext, Ofs->ext );
  170.     }
  171.     BuildDirectory( hwnd );
  172.     return( FALSE );
  173.  
  174. } /* TryTheFile */
  175.  
  176. /*
  177.  * GetFile - get file dialog
  178.  */
  179. BOOL __export FAR PASCAL GetFile( HWND hwnd, unsigned msg, WORD wparam,
  180.                             LONG lparam )
  181. {
  182.     WORD    notify;
  183.     HANDLE    handle;
  184.     WORD    index;
  185.     unsigned    total;
  186.     char    str[128];
  187.  
  188.     switch( msg ) {
  189.     case WM_INITDIALOG:
  190.         if( Ofs->title != NULL ) {
  191.         SetWindowText( hwnd, Ofs->title );
  192.     } else {
  193.         if( Ofs->type == FILE_OPEN ) {
  194.         SetWindowText( hwnd, "OPEN" );
  195.         } else {
  196.         SetWindowText( hwnd, "SAVE" );
  197.         }
  198.     }
  199.         BuildDirectory( hwnd );
  200.     return( TRUE );
  201.  
  202.     case WM_COMMAND:
  203.     notify = HIWORD( lparam );
  204.     handle = LOWORD( lparam );
  205.     switch( wparam ) {
  206.     case DRIVE_BOX:
  207.         if( notify == LBN_DBLCLK ) {
  208.         index = SendMessage( handle, LB_GETCURSEL, 0 , 0L );
  209.         SendMessage( handle, LB_GETTEXT, index, (LONG) ((LPSTR) str) );
  210.         _dos_setdrive( str[2] -'a'+1,&total );
  211.         BuildDirectory( hwnd );
  212.         }
  213.         break;
  214.     case FILE_BOX:
  215.         if( notify == LBN_DBLCLK ) {
  216.         index = SendMessage( handle, LB_GETCURSEL, 0 , 0L );
  217.         SendMessage( handle, LB_GETTEXT, index, (LONG) ((LPSTR) str) );
  218.         getcwd( Buff, _MAX_PATH );
  219.         if( Buff[strlen(Buff)-1] != '\\' ) strcat( Buff,"\\" );
  220.         strcat( Buff, str );
  221.         if( !CheckSaveVerify() ) break;
  222.         EndDialog( hwnd, TRUE );
  223.         }
  224.         break;
  225.     case DIR_BOX:
  226.         if( notify == LBN_DBLCLK ) {
  227.         index = SendMessage( handle, LB_GETCURSEL, 0 , 0L );
  228.         SendMessage( handle, LB_GETTEXT, index, (LONG) ((LPSTR) str) );
  229.         /*
  230.          * skip []'s
  231.          */
  232.         str[ strlen( str )-1 ] = 0;
  233.         chdir( str+1 );
  234.         BuildDirectory( hwnd );
  235.         }
  236.         break;
  237.     case FILE_OK:
  238.         if( !TryTheFile( hwnd ) ) return( FALSE );
  239.         EndDialog( hwnd, TRUE );
  240.         return( TRUE );
  241.         break;
  242.     case FILE_CANCEL:
  243.         EndDialog( hwnd, FALSE );
  244.         return( TRUE );
  245.         break;
  246.     } 
  247.     }
  248.     return( FALSE );
  249.  
  250. } /* GetFile */
  251.  
  252. /*
  253.  * GetFileName - get a save file name
  254.  */
  255. int __export FAR PASCAL GetFileName( LPFILEOPEN of )
  256. {
  257.     FARPROC    fp;
  258.     BOOL    rc;
  259.     int        i;
  260.     char    *ptr;
  261.  
  262.     if( of == NULL || Ofs != NULL ) return( 1 );
  263.     fp = MakeProcInstance( GetFile, Inst );
  264.     Ofs = of;
  265.     strcpy( _ext, Ofs->ext );
  266.     rc = DialogBox( Inst, (LPSTR) "FileOpen", of->hwnd, fp );
  267.     FreeProcInstance( fp );
  268.     if( rc ) {
  269.         i = 0;
  270.     ptr = Buff;
  271.     while( 1 ) {
  272.         Ofs->name[i] = *ptr;
  273.         if( *ptr == 0 ) break;
  274.         ptr++;
  275.         i++;
  276.         if( i == Ofs->namelen ) {
  277.             Ofs->name[i-1] = 0;
  278.         break;
  279.         }
  280.     }
  281.     }
  282.     Ofs = NULL;
  283.     return( rc );
  284.  
  285. } /* GetFileName */
  286.