home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / sdktools / winnt / walker / pwalkio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-10-05  |  4.9 KB  |  209 lines

  1.  
  2. /******************************************************************************\
  3. *       This is a part of the Microsoft Source Code Samples. 
  4. *       Copyright (C) 1993-1997 Microsoft Corporation.
  5. *       All rights reserved. 
  6. *       This source code is only intended as a supplement to 
  7. *       Microsoft Development Tools and/or WinHelp documentation.
  8. *       See these sources for detailed information regarding the 
  9. *       Microsoft samples programs.
  10. \******************************************************************************/
  11.  
  12. #include "pwalk.h"
  13. #include <commdlg.h>
  14.  
  15.  
  16. /* global file name variables */
  17. char    szExeFilterSpec[128] = "Executable Files (*.EXE)\0*.EXE\0";
  18.  
  19.  
  20. /* call the OpenFile common dialog to get a filename */
  21. BOOL WINAPI GetFileName (
  22.     HWND    hWnd,
  23.     char    *lpszFilePath,
  24.     char    *lpszExt)
  25. {
  26.     OPENFILENAME    ofn;
  27.     char        szFileOpen[25];
  28.     char        szExt[10];
  29.     char        szTitle[MAX_PATH];
  30.  
  31.     *szTitle = 0;
  32.  
  33.     if (lpszExt && *lpszExt)
  34.     strcpy (szExt, lpszExt);
  35.     else
  36.     LoadString ((HANDLE)GetModuleHandle (NULL),
  37.         IDS_EXEFILEEXT,
  38.         szExt,
  39.         sizeof (szExt));
  40.  
  41.     LoadString ((HANDLE)GetModuleHandle (NULL),
  42.         IDS_FILEOPENTITLE,
  43.     szFileOpen,
  44.     sizeof (szFileOpen));
  45.  
  46.     ofn.lStructSize      = sizeof (OPENFILENAME);
  47.     ofn.hwndOwner      = NULL;
  48.     ofn.lpstrFilter      = szExeFilterSpec;
  49.     ofn.lpstrCustomFilter = NULL;
  50.     ofn.nMaxCustFilter    = 0;
  51.     ofn.nFilterIndex      = 0;
  52.     ofn.lpstrFile      = lpszFilePath;
  53.     ofn.nMaxFile      = MAX_PATH;
  54.     ofn.lpstrInitialDir   = NULL;
  55.     ofn.lpstrFileTitle      = szTitle;
  56.     ofn.nMaxFileTitle     = MAX_PATH;
  57.     ofn.lpstrTitle      = szFileOpen;
  58.     ofn.lpstrDefExt      = szExt;
  59.     ofn.Flags          = OFN_FILEMUSTEXIST;
  60.  
  61.     /* call common open dialog and return result */
  62.     return (GetOpenFileName ((LPOPENFILENAME)&ofn));
  63. }
  64.  
  65.  
  66.  
  67. /* invokes the saveas common dialog to retrieve a file name */
  68. /* function retrieves the filename from the path */
  69. void WINAPI GetFileFromPath (
  70.     char    *lpszFullPath,
  71.     char    *lpszFile)
  72. {
  73.     char    *lpPtr = lpszFullPath + strlen (lpszFullPath);
  74.  
  75.     /* file is at end of path, so search backwards to first \ or : char */
  76.     while (lpPtr > lpszFullPath)
  77.     {
  78.     if (*lpPtr == '\\' ||
  79.         *lpPtr == ':')
  80.         {
  81.         lpPtr++;
  82.         break;
  83.         }
  84.     lpPtr = CharPrev(lpszFullPath, lpPtr);
  85.     }
  86.  
  87.     /* return filename if found, or full path passed in */
  88.     strcpy (lpszFile, lpPtr);
  89. }
  90.  
  91.  
  92.  
  93.  
  94. /* validate filename as executable image  */
  95. BOOL WINAPI IsValidFile (
  96.     char    *lpszFilename)
  97. {
  98.     OFSTRUCT    of;
  99.     int     nLen;
  100.     char    *pStr;
  101.  
  102.     /* validate filename pointer */
  103.     if (lpszFilename == NULL ||
  104.     !*lpszFilename)
  105.     return FALSE;
  106.  
  107.     /* open the file for existance */
  108.     if (OpenFile (lpszFilename, &of, OF_EXIST) == -1)
  109.     /* fail validation */
  110.     return FALSE;
  111.  
  112.     /* test the extension is .EXE */
  113.     nLen = strlen (lpszFilename);
  114.     pStr = lpszFilename + nLen - 4;
  115.     if (!stricmp (pStr, ".EXE"))
  116.     /* pass validation */
  117.     return TRUE;
  118.  
  119.     /* fail validation */
  120.     return FALSE;
  121. }
  122.  
  123.  
  124.  
  125.  
  126. /* get win32 command line parameters */
  127. BOOL WINAPI GetCmdLine(
  128.     char    *lpStr,
  129.     char    *lpszCmdLine,
  130.     BOOL    *bBkgnd)
  131. {
  132.     if (*lpStr)
  133.     {
  134.     /* skip application name which precedes parameters */
  135.     while (*lpStr != ' ' && *lpStr != 0)
  136.         lpStr = CharNext(lpStr);
  137.  
  138.     /* skip spaces */
  139.     while (*lpStr == ' ' && *lpStr != 0)
  140.         lpStr = CharNext(lpStr);
  141.  
  142.     /* indeed command line parameter(s) present */
  143.     if (*lpStr != 0)
  144.         {
  145.         /* if background switch, set flag and remove switch from command line */
  146.         if ((*lpStr == '/' || *lpStr == '-') &&
  147.         (*(lpStr+1) == 'b' || *(lpStr+1) == 'B'))
  148.         {
  149.         *bBkgnd = TRUE;
  150.         lpStr += 2;
  151.  
  152.         if (*lpStr == 0)
  153.             *lpszCmdLine = 0;
  154.         else
  155.             strcpy (lpszCmdLine, lpStr);
  156.         }
  157.         /* maybe switch is embedded in parameter(s) somewhere */
  158.         else
  159.         {
  160.         char    *pStr = lpStr;
  161.         char    *pCmdLine = lpszCmdLine;
  162.         int    i, nCnt;
  163.  
  164.         while (*pStr != 0)
  165.             {
  166.             /* background switch is set, so prepare parameters and set flag */
  167.             if ((*pStr == '/' || *pStr == '-') &&
  168.             (*(pStr+1) == 'b' || *(pStr+1) == 'B'))
  169.             {
  170.             *bBkgnd = TRUE;
  171.  
  172.             /* copy from beg. of lpStr to *pStr to lpszCmdLine */
  173.             nCnt = pStr - lpStr;
  174.             for (i=0; i<nCnt; i++)
  175.                 lpszCmdLine[i] = lpStr[i];
  176.             lpszCmdLine[i] = 0;
  177.             strcat (lpszCmdLine, (pStr+2));
  178.  
  179.             /* break from loop */
  180.             break;
  181.             }
  182.  
  183.             pStr = CharNext(pStr);
  184.             }
  185.  
  186.         /* no switch found, can only edit one file, remove extra parameters */
  187.         if (*pStr == 0)
  188.             {
  189.             pStr = lpStr;
  190.  
  191.             while (*pStr != ' ' && *pStr != 0)
  192.             pStr = CharNext(pStr);
  193.  
  194.             if (*pStr == ' ')
  195.             *pStr = 0;
  196.  
  197.             strcpy (lpszCmdLine, lpStr);
  198.             }
  199.         }
  200.         }
  201.     else
  202.         return FALSE;
  203.     }
  204.     else
  205.     return FALSE;
  206.  
  207.     return TRUE;
  208. }
  209.