home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / sdktools / porttool / ptfile.c < prev    next >
C/C++ Source or Header  |  1995-10-28  |  9KB  |  352 lines

  1. #include "PortTool.h"
  2. #include <string.h>
  3.  
  4. /* global file name variables */
  5. char    lpszFilePath[MAX_PATH];
  6. char    lpszFilterSpec[128] = "";    // pickup eventually from string table
  7.  
  8. char    lpszPortIniFilePath[MAX_PATH];
  9. char    lpszPortIniFilterSpec[128] = "";  // pickup eventually from string table
  10.  
  11. /* call the OpenFile common dialog to get a porttool Information filename */
  12.  
  13.  
  14. /* load file filter spec strings from resource */
  15. VOID LoadFilterSpecString(LPTSTR lpDest, UINT StartID, UINT EndID)
  16. {
  17.    CHAR    szBuf[MAX_PATH];
  18.    UINT    wID;
  19.    INT     nOffset;
  20.    HMODULE hModule = GetModuleHandle(NULL);
  21.  
  22.     if(!*lpDest) {
  23.         for(wID = StartID, nOffset = 0; wID <= EndID; wID++) {
  24.             LoadString((HANDLE)hModule, wID, szBuf, sizeof(szBuf));
  25.             lstrcpy(lpDest + nOffset, szBuf);
  26.             nOffset += lstrlen(szBuf) + 1;
  27.         }
  28.     }
  29.     return;
  30. }
  31.  
  32.  
  33. /* call the OpenFile common dialog to get a filename */
  34. BOOL WINAPI GetFileName (
  35.     HWND    hWnd,
  36.     char    *lpszFileTitle,
  37.     char    *lpszFilePath)
  38. {
  39.     OPENFILENAME    ofn;
  40.     char        lpszFileOpen[25];
  41.     char        lpszExt[10];
  42.  
  43.     memset(&ofn, 0, sizeof(OPENFILENAME));
  44.     strcpy (lpszFileTitle, "");
  45.  
  46.     LoadString ((HANDLE)GetModuleHandle (NULL),
  47.     IDS_DEFAULTFILEEXT,
  48.     lpszExt,
  49.     sizeof (lpszExt));
  50.  
  51.     LoadString ((HANDLE)GetModuleHandle (NULL),
  52.     IDS_FILEOPENTITLE,
  53.     lpszFileOpen,
  54.     sizeof (lpszFileOpen));
  55.     LoadFilterSpecString(lpszFilterSpec, IDS_FILE_FILTER_SPEC1, IDS_EXT_FILTER_SPEC3);
  56.  
  57.     /* fill in non-variant fields of OPENFILENAME struct. */
  58.     ofn.lStructSize       = sizeof(OPENFILENAME);
  59.     ofn.hwndOwner          = hWnd;
  60.     ofn.lpstrFilter       = lpszFilterSpec;
  61.     ofn.lpstrCustomFilter = NULL;
  62.     ofn.nMaxCustFilter    = 0;
  63.     ofn.nFilterIndex      = 0;
  64.     ofn.lpstrFile          = lpszFilePath;
  65.     ofn.nMaxFile          = MAX_PATH;
  66.     ofn.lpstrInitialDir   = NULL;
  67.     ofn.lpstrFileTitle    = lpszFileTitle;
  68.     ofn.nMaxFileTitle     = MAX_PATH;
  69.     ofn.lpstrTitle          = lpszFileOpen;
  70.     ofn.lpstrDefExt       = lpszExt;
  71.     ofn.Flags              = OFN_FILEMUSTEXIST;
  72.  
  73.     /* call common open dialog and return result */
  74.     return (GetOpenFileName ((LPOPENFILENAME)&ofn));
  75. }
  76.  
  77. BOOL WINAPI GetPortIniFileName (
  78.     HWND    hWnd,
  79.     char    *lpszFileTitle,
  80.     char    *lpszFilePath)
  81. {
  82.     OPENFILENAME    ofn;
  83.     char        lpszFileOpen[64];
  84.     char        lpszExt[10];
  85.  
  86.     memset(&ofn, 0, sizeof(OPENFILENAME));
  87.     strcpy (lpszFileTitle, "");
  88.  
  89.     /* load strings from resource string table */
  90.     LoadString ((HANDLE)GetModuleHandle (NULL),
  91.     IDS_DEFAULTPORTEXT,
  92.     lpszExt,
  93.     sizeof (lpszExt));
  94.  
  95.     LoadString ((HANDLE)GetModuleHandle (NULL),
  96.     IDS_PORTFILEOPENTITLE,
  97.     lpszFileOpen,
  98.     sizeof (lpszFileOpen));
  99.  
  100.     LoadFilterSpecString(lpszPortIniFilterSpec, IDS_FILE_FILTER_SPEC4, IDS_EXT_FILTER_SPEC5);
  101.  
  102.     /* fill in non-variant fields of OPENFILENAME struct. */
  103.     ofn.lStructSize       = sizeof(OPENFILENAME);
  104.     ofn.hwndOwner          = hWnd;
  105.     ofn.lpstrFilter       = lpszPortIniFilterSpec;
  106.     ofn.lpstrCustomFilter = NULL;
  107.     ofn.nMaxCustFilter    = 0;
  108.     ofn.nFilterIndex      = 0;
  109.     ofn.lpstrFile          = lpszFilePath;
  110.     ofn.nMaxFile          = MAX_PATH;
  111.     ofn.lpstrInitialDir   = NULL;
  112.     ofn.lpstrFileTitle    = lpszFileTitle;
  113.     ofn.nMaxFileTitle     = MAX_PATH;
  114.     ofn.lpstrTitle          = lpszFileOpen;
  115.     ofn.lpstrDefExt       = lpszExt;
  116.     ofn.Flags              = 0;
  117.  
  118.     /* call common open dialog and return result */
  119.     return (GetOpenFileName ((LPOPENFILENAME)&ofn));
  120. }
  121.  
  122.  
  123. /* function retrieves the filename from the path */
  124. BOOL WINAPI GetFileFromPath (
  125.     char    *lpszFullPath,
  126.     char    *lpszFile)
  127. {
  128.     char    *lpPtr = lpszFullPath + strlen (lpszFullPath);
  129.  
  130.     /* file is at end of path, so search backwards to first \ or : char */
  131.     while (lpPtr > lpszFullPath)
  132.     {
  133.     if (*lpPtr == '\\' ||
  134.         *lpPtr == ':')
  135.         {
  136.         lpPtr++;
  137.         break;
  138.         }
  139.     lpPtr = CharPrev(lpszFullPath, lpPtr);
  140.     }
  141.  
  142.     /* return filename if found, or full path passed in */
  143.     strcpy (lpszFile, lpPtr);
  144.  
  145.     return (lpPtr > lpszFullPath);
  146. }
  147.  
  148.  
  149.  
  150.  
  151. /* open a file and load into edit control */
  152. int WINAPI LoadFile (
  153.     HWND    hWnd,
  154.     char    *lpszName)
  155. {
  156.     LONG    lLength;
  157.     HWND    hWndEdit = (HWND)GetWindowLong (hWnd, WL_HWNDEDIT);
  158.     HANDLE  hEditData;
  159.     char    *lpEditData;
  160.     HFILE    hFile;
  161.  
  162.     /* open file for read & write */
  163.     if ((hFile = (HFILE)CreateFile (lpszName, GENERIC_READ | GENERIC_WRITE,
  164.                                     0, NULL, OPEN_EXISTING,
  165.                                     FILE_ATTRIBUTE_NORMAL, NULL))
  166.                == (HFILE)INVALID_HANDLE_VALUE) {
  167.     /* return error */
  168.     return 0-IDS_OPENFAILED;
  169.     }
  170.  
  171.     /* get file length */
  172.     if (lLength = _llseek(hFile, 0L, 2))
  173.        _llseek(hFile, 0L, 0);
  174.     else
  175.     {
  176.     /* close file and return error */
  177.     CloseHandle ((HANDLE)hFile);
  178.     return 0-IDS_NOSIZE;
  179.     }
  180.  
  181. #if !defined (WIN32)
  182.  
  183.     /* get the edit control's memory handle */
  184.     if (!(hEditData = (HANDLE)SendMessage (hWndEdit, EM_GETHANDLE, 0, 0L)))
  185.     {
  186.     /* close file and return error */
  187.     CloseHandle ((HANDLE)hFile);
  188.     return 0-IDS_GETHANDLEFAILED;
  189.     }
  190.  
  191.     /* realloc the memory to fit the new file size */
  192.     if (((hEditData = LocalReAlloc(hEditData, lLength+1, LHND)) == NULL) ||
  193.  
  194. #else
  195.  
  196.     if (((hEditData = LocalAlloc (LHND, lLength+1)) == NULL) ||
  197.  
  198. #endif
  199.  
  200.     (!(lpEditData = (char *)LocalLock (hEditData))))
  201.     {
  202.     /* close file and return error */
  203.     CloseHandle ((HANDLE)hFile);
  204.     return 0-IDS_REALLOCFAILED;
  205.     }
  206.  
  207.  
  208.     /* read the file into hEditData buffer */
  209.     if (_lread(hFile, lpEditData, lLength) == -1)
  210.     {
  211.     /* close file and return error */
  212.     CloseHandle ((HANDLE)hFile);
  213.     return 0-IDS_READFAILED;
  214.     }
  215.  
  216.     /* null terminate edit buffer */
  217.     lpEditData[lLength] = 0;
  218.     LocalUnlock (hEditData);
  219.  
  220.     /* load buffer into edit control and close file */
  221.  
  222. #if !defined(WIN32)
  223.  
  224.     SendMessage (hWndEdit, EM_SETHANDLE, (UINT)hEditData, 0L);
  225.  
  226. #else
  227.  
  228.     lpEditData = LocalLock (hEditData);
  229.     SendMessage (hWndEdit, WM_SETTEXT, 0, (LPARAM)lpEditData);
  230.     GetLastError();
  231.     LocalUnlock (hEditData);
  232.     // LocalFree (hEditData); // Isn't there a synchronization issue with this?
  233.  
  234. #endif
  235.  
  236.     CloseHandle ((HANDLE)hFile);
  237.  
  238.     /* return success */
  239.     return TRUE;
  240. }
  241.  
  242.  
  243.  
  244. /* save file to disk */
  245. int WINAPI SaveFile (
  246.     HWND    hWnd,
  247.     char    *lpszFile)
  248. {
  249.     HANDLE   hEditData;
  250.     int      nLength;
  251.     DWORD    dwWritten;
  252.     HANDLE   hFile;
  253.     HWND     hWndEdit = (HWND)GetWindowLong (hWnd, WL_HWNDEDIT);
  254.     char     *lpEditData;
  255.  
  256.  
  257.     /* open the file for writing */
  258.     hFile = CreateFile (lpszFile,
  259.         GENERIC_WRITE,
  260.         0,
  261.         NULL,
  262.         CREATE_ALWAYS,
  263.         FILE_ATTRIBUTE_NORMAL,
  264.         NULL);
  265.  
  266.     /* validate file handle */
  267.     if (!hFile)
  268.     return IDS_WRITEOPENFAILED;
  269.  
  270.     /* find out the length of the text in the edit control */
  271.     nLength = GetWindowTextLength (hWndEdit);
  272.  
  273. #if !defined (WIN32)
  274.  
  275.     /* get handle to Edit text and lock pointer */
  276.     hEditData  = (HANDLE)SendMessage (hWndEdit, EM_GETHANDLE, 0, 0);
  277.     lpEditData = (char *)LocalLock (hEditData);
  278.  
  279. #else
  280.  
  281.     hEditData = LocalAlloc (LHND, nLength+1);
  282.     lpEditData = (char *) LocalLock (hEditData);
  283.     GetWindowText (hWndEdit, lpEditData, nLength+1);
  284.  
  285. #endif
  286.  
  287.     /* write edit data to file. */
  288.     if (!WriteFile(hFile, lpEditData, nLength, &dwWritten, NULL))
  289.     {
  290.     /* unlock memory, restore edit handle, close file and return error */
  291.     LocalUnlock (hEditData);
  292.     CloseHandle (hFile);
  293.     return IDS_WRITEFAILED;
  294.     }
  295.  
  296.     /* clean up and go away */
  297.     LocalUnlock (hEditData);
  298.     CloseHandle (hFile);
  299.  
  300.     return TRUE;
  301. }
  302.  
  303.  
  304. /* invokes the saveas common dialog to retrieve a file name */
  305. BOOL WINAPI SaveAsFileName (
  306.     HWND    hWnd,
  307.     char    *lpszFileTitle,
  308.     char    *lpszFilePath)
  309. {
  310.     OPENFILENAME    ofn;
  311.     char        lpszSaveAs[25];
  312.     char        lpszExt[10];
  313.     BOOL        nResult;
  314.  
  315.     memset(&ofn, 0, sizeof(OPENFILENAME));
  316.     *lpszFileTitle = 0;
  317.     *lpszFilePath = 0;
  318.  
  319.     /* load strings from resource string table */
  320.     LoadString ((HANDLE)GetModuleHandle (NULL),
  321.     IDS_DEFAULTFILEEXT,
  322.     lpszExt,
  323.     sizeof (lpszExt));
  324.     LoadString ((HANDLE)GetModuleHandle (NULL),
  325.     IDS_SAVEASTITLE,
  326.     lpszSaveAs,
  327.     sizeof (lpszSaveAs));
  328.     LoadFilterSpecString(lpszFilterSpec, IDS_FILE_FILTER_SPEC1, IDS_EXT_FILTER_SPEC3);
  329.  
  330.     /* fill in non-variant fields of OPENFILENAME struct. */
  331.     ofn.lStructSize   = sizeof (OPENFILENAME);
  332.     ofn.hwndOwner     = hWnd;
  333.     ofn.lpstrFilter   = lpszFilterSpec;
  334.     ofn.lpstrCustomFilter = NULL;
  335.     ofn.nMaxCustFilter    = 0;
  336.     ofn.nFilterIndex      = 0;
  337.     ofn.lpstrFile     = lpszFilePath;
  338.     ofn.nMaxFile      = MAX_PATH;
  339.     ofn.lpstrInitialDir   = NULL;
  340.     ofn.lpstrFileTitle    = lpszFileTitle;
  341.     ofn.nMaxFileTitle     = MAX_PATH;
  342.     ofn.lpstrTitle    = lpszSaveAs;
  343.     ofn.lpstrDefExt   = lpszExt;
  344.     ofn.Flags         = 0;
  345.  
  346.     /* call common saveas dialog and return success */
  347.     if(nResult = GetSaveFileName ((LPOPENFILENAME)&ofn)) {
  348.         lstrcpy(lpszFileTitle,ofn.lpstrFileTitle); // save File Title
  349.     }
  350.     return nResult; 
  351. }
  352.