home *** CD-ROM | disk | FTP | other *** search
/ Macmaailma Vuosikerta 1994 to 1996 / TK1996_3CD.ISO / pc / k_portti / tk0395 / fmexpand.c next >
C/C++ Source or Header  |  1995-04-18  |  4KB  |  167 lines

  1. #include <windows.h>
  2. #include <wfext.h>
  3. #include <lzexpand.h>
  4. #include <dir.h>
  5. #include "fmexpand.h"
  6.  
  7. BOOL CALLBACK _export ExpandDlgProc(HWND, UINT, WPARAM, LPARAM);
  8. BOOL ExpandFile(LPSTR, LPSTR);
  9. LPSTR lstrrchr(LPSTR, char);
  10.  
  11. HINSTANCE ghInst;
  12. HMENU ghMenu;    
  13. WORD gwMenuDelta;
  14. FMS_GETFILESEL gFileSel;
  15. char gszSrcName[256];   
  16. char gszDestName[256];  
  17.  
  18. #pragma argsused
  19. int CALLBACK LibMain(HINSTANCE hInstance, WORD wDataSeg, WORD wHeapSize, LPSTR lpszCmdLine)
  20. {
  21.     ghInst = hInstance;
  22.     return 1;
  23. }
  24.  
  25. HMENU CALLBACK _export FMExtensionProc(HWND hwnd, UINT msg, LPARAM lParam)
  26. {
  27.     LPFMS_LOAD lpload;
  28.     short nFiles;
  29.  
  30.     switch (msg)
  31.     {
  32.         case FMEVENT_LOAD:
  33.             lpload = (LPFMS_LOAD) lParam;
  34.             ghMenu = LoadMenu( ghInst, "FMExpand" );
  35.             lpload->hMenu = ghMenu;
  36.             gwMenuDelta = lpload->wMenuDelta;
  37.             lpload->dwSize = sizeof (FMS_LOAD);
  38.             lstrcpy( lpload->szMenuName, "L&aajenna" );
  39.             return ghMenu;
  40.  
  41.         case FMEVENT_INITMENU:
  42.             gwMenuDelta = LOWORD(lParam);
  43.             ghMenu = (HMENU) HIWORD(lParam);
  44.             nFiles = (short) SendMessage( hwnd, FM_GETSELCOUNT, FMFOCUS_DIR, 0L );
  45.             EnableMenuItem(ghMenu, gwMenuDelta + IDM_EXPAND, nFiles ? MF_ENABLED : MF_GRAYED);
  46.             break;
  47.  
  48.         case IDM_EXPAND:
  49.             SendMessage( hwnd, FM_GETFILESEL, 0, (LPARAM)(LPFMS_GETFILESEL)&gFileSel );
  50.             lstrcpy( gszSrcName, gFileSel.szName );
  51.             if ( DialogBox( ghInst, "DlgExpand", hwnd, ExpandDlgProc ) == 0 )
  52.                 if ( !ExpandFile( gszSrcName, gszDestName ) )
  53.                        MessageBox( hwnd, "Virhe laajennettaessa tiedostoa", 
  54.                            "Laajenna", MB_ICONSTOP | MB_OK );
  55.             break;
  56.  
  57.         default:
  58.             break;
  59.     }
  60.     return NULL;
  61. }
  62.  
  63. BOOL CALLBACK _export ExpandDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
  64. {
  65.     BOOL fResult = FALSE;
  66.     LPSTR lp;
  67.     char szDir[128];
  68.     char szName[14];
  69.  
  70.     switch (msg)
  71.     {
  72.         case WM_INITDIALOG:
  73.             EnableWindow( GetDlgItem(hDlg, IDOK), FALSE );
  74.         
  75.             lstrcpy( szDir, gszSrcName );
  76.             lp = lstrrchr(szDir, '\\');
  77.             lstrcpy( szName, AnsiNext(lp) );
  78.             *lp = '\0';
  79.             if ( lstrlen(szDir) == 2 )        /* esim. A: */
  80.                 lstrcat( szDir, "\\" );
  81.  
  82.             OemToAnsi( szDir, szDir );
  83.             SetWindowText( GetDlgItem(hDlg, IDD_CURRENT), (LPSTR)szDir );
  84.          
  85.             OemToAnsi( szName, szName );
  86.             SetWindowText( GetDlgItem(hDlg, IDD_FROM), (LPSTR)szName );
  87.         
  88.             SendDlgItemMessage( hDlg, IDD_TO, EM_LIMITTEXT, 256, 0L );
  89.             SetFocus( GetDlgItem(hDlg, IDD_TO) );
  90.             break;
  91.  
  92.         case WM_COMMAND:
  93.             switch (wParam)
  94.             {
  95.                 case IDD_TO:
  96.                      if ( HIWORD(lParam) == EN_CHANGE )
  97.                          EnableWindow( GetDlgItem(hDlg, IDOK),
  98.                             (BOOL) SendMessage( (HWND) LOWORD(lParam),
  99.                                 WM_GETTEXTLENGTH, 0, 0L) );
  100.                     fResult = TRUE;
  101.                     break;
  102.  
  103.                 case IDOK:
  104.                     GetWindowText( GetDlgItem(hDlg, IDD_TO), gszDestName, 256 );
  105.                     if ( gszDestName[0] != '\0' )
  106.                     {
  107.                         AnsiToOem( gszDestName, gszDestName );
  108.                         EndDialog( hDlg, 0 );   /* 0 = kaikki OK */
  109.                     }
  110.                     else
  111.                     {
  112.                         MessageBeep( MB_ICONASTERISK );
  113.                         SetFocus( GetDlgItem(hDlg, IDD_TO) );
  114.                         SendDlgItemMessage( hDlg, IDD_TO, EM_SETSEL, TRUE, MAKELONG(0,-1) );
  115.                     }
  116.                     fResult = TRUE;
  117.                     break;
  118.  
  119.                 case IDCANCEL:
  120.                     EndDialog( hDlg, 1 );    /* 1 = peruutettu */
  121.                     fResult = TRUE;
  122.                     break;
  123.             } /* switch (wParam) */
  124.             break;
  125.     } /* switch (msg) */
  126.     return fResult;
  127. }
  128.  
  129. BOOL ExpandFile(LPSTR lpszSrc, LPSTR lpszDest)
  130. {
  131.     OFSTRUCT ofSrc, ofDest;
  132.     HFILE hfSrc, hfDest;
  133.     BOOL fSuccess = TRUE;
  134.  
  135.     hfSrc = LZOpenFile( lpszSrc, &ofSrc, OF_READ );
  136.     if ( hfSrc == HFILE_ERROR )
  137.         return FALSE;
  138.  
  139.     hfDest = LZOpenFile( lpszDest, &ofDest, OF_CREATE );
  140.     if ( hfDest == HFILE_ERROR )
  141.     {
  142.         LZClose( hfSrc );
  143.         return FALSE;
  144.     }
  145.  
  146.     if ( LZCopy( hfSrc, hfDest ) < 0 )
  147.         fSuccess = FALSE;
  148.  
  149.     LZClose( hfSrc );
  150.     LZClose( hfDest );
  151.     return fSuccess;
  152. }
  153.  
  154. LPSTR lstrrchr( LPSTR str, char ch )
  155. {
  156.     LPSTR strl = str + lstrlen(str);
  157.  
  158.     do
  159.     {
  160.         if ( ch == *strl ) return strl;
  161.         strl = AnsiPrev( str, strl );
  162.     }
  163.     while (strl > str);
  164.  
  165.     return NULL;
  166. }
  167.