home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / FILEDLG6.ZIP / SOURCE.ZIP / PARSE.C < prev    next >
Text File  |  1990-10-15  |  7KB  |  139 lines

  1. /****************************************************************************
  2.  * USHORT ParseFileName( PSZ pszSource,PSZ pszDest,PSZ pszSearch );         *
  3.  * Purpose                  This function generates a fully                 *
  4.  *                          qualified file name or search spec.             *
  5.  *                          from the input value, and changes               *
  6.  *                          the current drive and directory to              *
  7.  *                          the drive/directory listed in the               *
  8.  *                          input file name.                                *
  9.  *                                                                          *
  10.  * Parameters               pszSource points to the input file name         *
  11.  *                          or search spec.                                 *
  12.  *                                                                          *
  13.  *                          pszDest points to the location where            *
  14.  *                          the fully qualified file name or                *
  15.  *                          search spec is to be stored. The memory         *
  16.  *                          buffer pointed to by pszDest must be at         *
  17.  *                          least the system maximum path length bytes      *
  18.  *                          long.                                           *
  19.  *                                                                          *
  20.  *                          pszSearch points to the current file search     *
  21.  *                          specification. This is used when the input      *
  22.  *                          string is just a drive or a drive:\directory    *
  23.  *                          without a file name.                            *
  24.  *                                                                          *
  25.  * Return Value             The return value is zero if the function        *
  26.  *                          is successful, otherwise it is an               *
  27.  *                          error value. The contents of pszDest is         *
  28.  *                          undefined if an error has occurred.             *
  29.  *                                                                          *
  30.  * Notes                    This function no longer modifies the contents   *
  31.  *                          of the string pointed to by pszSource.          *
  32.  *                                                                          *
  33.  *                          This function now detects illegal file          *
  34.  *                          names and search specifications.                *
  35.  *                                                                          *
  36.  *                                                                          *
  37.  * Modifications -                                                          *
  38.  *      17-Aug-1989 : Initial version.                                      *
  39.  *      11-Oct-1990 : Added long file name support.                         *
  40.  *                                                                          *
  41.  * (c)Copyright 1990 Rick Yoder                                             *
  42.  ****************************************************************************/
  43.  
  44.     #define INCL_DOSFILEMGR
  45.     #define INCL_DOSMISC
  46.     #define INCL_DOSERRORS
  47.     #include <os2.h>
  48.     #include <string.h>
  49.     #include <ctype.h>
  50.     #include "tools.h"
  51.     #include "static.h"
  52.  
  53. /****************************************************************************/
  54.     USHORT ParseFileName( PSZ pszSource,PSZ pszDest,PSZ pszSearch )
  55.     {
  56.         USHORT  usMaxPathLen;
  57.         USHORT  cbBuf;
  58.         USHORT  usResult;
  59.         PCHAR   pcLastSlash;
  60.  
  61.     /* Get maximum path length */
  62.         if ( usResult = DosQSysInfo(0,(PBYTE)&usMaxPathLen,sizeof(USHORT)) )
  63.             return usResult;
  64.  
  65.     /* Check whether input string just contains a drive letter.
  66.      * If true, then check whether drive is valid and create
  67.      * a string containing the new drive, current directory, and
  68.      * and search specification.
  69.      */
  70.         if ( strlen(pszSource)==2 && pszSource[1] == ':' && isalpha(pszSource[0]) )
  71.             {
  72.             strcpy( pszDest,pszSource );
  73.             pszDest[0] = (CHAR)toupper(pszDest[0]);
  74.             if ( usResult = DosSelectDisk(pszDest[0]-'@') ) return usResult;
  75.  
  76.             /* Append current directory and search spec. */
  77.             cbBuf = usMaxPathLen - 3;
  78.             if ( !(usResult = DosQCurDir(0,pszDest,&cbBuf)) )
  79.                 {
  80.                 if ( *(pszDest + strlen(pszDest) - 1) != '\\' )
  81.                     strcat( pszDest,szSlash );
  82.                 strcat( pszDest,pszSearch );
  83.                 }
  84.             return usResult;
  85.             }
  86.  
  87.     /* Generate fully qualified file/path name */
  88.     usResult = DosQPathInfo(pszSource,FIL_QUERYFULLNAME,pszDest,usMaxPathLen,0L);
  89.     if ( usResult ) return usResult;
  90.  
  91.     /* Change to specified drive */
  92.     if ( usResult = DosSelectDisk(toupper(pszDest[0])-'@') ) return usResult;
  93.  
  94.     /* Search for last backslash. */
  95.     pcLastSlash = strrchr(pszDest,'\\');
  96.  
  97.     /* If the only backslash is at beginning, pszDest contains either */
  98.     /* d:\filename or d:\dirname or d:\                               */
  99.         if ( &pszDest[2] == pcLastSlash )
  100.             {
  101.             switch ( usResult = DosChDir(pszDest,0L) ) {
  102.                 case NO_ERROR:  // pszDest contains d:\dirname or d:\
  103.                     if ( *(pszDest + strlen(pszDest) - 1) != '\\' )
  104.                         strcat( pszDest,szSlash );
  105.                     strcat( pszDest,pszSearch );
  106.                     return usResult;
  107.  
  108.                 case ERROR_FILE_NOT_FOUND:
  109.                 case ERROR_PATH_NOT_FOUND:
  110.                 case ERROR_ACCESS_DENIED:   // pszSource contains \filename
  111.                     if ( usResult = DosChDir(szSlash,0L) ) return usResult;
  112.                     return usResult;
  113.  
  114.                 default:
  115.                     return usResult;
  116.                 }
  117.             }
  118.  
  119.     /* Input has d:\dir\filename or d:\dir\dir */
  120.         switch ( usResult = DosChDir(pszDest,0L) ) {
  121.             case NO_ERROR:      // pszSource contains d:\dir\dir
  122.                 strcat( pszDest,szSlash );
  123.                 strcat( pszDest,pszSearch );
  124.                 return 0;
  125.  
  126.             case ERROR_FILE_NOT_FOUND:
  127.             case ERROR_PATH_NOT_FOUND:
  128.             case ERROR_ACCESS_DENIED:   // d:\dir\filename
  129.                 *pcLastSlash = '\0';
  130.                 if ( usResult = DosChDir(pszDest,0L) ) return usResult;
  131.                 *pcLastSlash = '\\';
  132.                 return 0;
  133.  
  134.             default:
  135.                 return usResult;
  136.             }
  137.     }
  138. /****************************************************************************/
  139.