home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / os2 / filedlg5 / source / parse.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-10-11  |  8.3 KB  |  180 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.                    *
  16.  *                                                                          *
  17.  *                          pszSearch points to the current file search     *
  18.  *                          specification. This is used when the input      *
  19.  *                          string is just a drive or a drive:\directory    *
  20.  *                          without a file name.                            *
  21.  *                                                                          *
  22.  * Return Value             The return value is zero if the function        *
  23.  *                          is successful, otherwise it is an               *
  24.  *                          error value. The contents of pszDest is         *
  25.  *                          undefined if an error has occurred.             *
  26.  *                                                                          *
  27.  * Notes                    This function modifies the contents of          *
  28.  *                          the string pointed to by pszSource.             *
  29.  *                                                                          *
  30.  *                          This function will not detect an illegal        *
  31.  *                          filename or search specification.               *
  32.  *                                                                          *
  33.  *                                                                          *
  34.  * Modifications -                                                          *
  35.  *      17-Aug-1989 : Initial version.                                      *
  36.  *                                                                          *
  37.  * (c)Copyright 1989 Rick Yoder                                             *
  38.  ****************************************************************************/
  39.  
  40.     #define INCL_DOSFILEMGR
  41.     #define INCL_DOSMISC
  42.     #define INCL_DOSERRORS
  43.     #include <os2.h>
  44.     #include <string.h>
  45.     #include "tools.h"
  46.     #include "static.h"
  47.  
  48. /****************************************************************************/
  49.     USHORT ParseFileName( PSZ pszSource,PSZ pszDest,PSZ pszSearch )
  50.     {
  51.         USHORT  usMaxPathLen;
  52.         USHORT  usDrive;
  53.         ULONG   ulMap;
  54.         USHORT  cbBuf;
  55.         USHORT  usResult;
  56.         PCHAR   pcLastSlash,pcFileOnly;
  57.  
  58.     /* Get maximum path length */
  59.         if ( usResult = DosQSysInfo(0,(PBYTE)&usMaxPathLen,sizeof(USHORT)) )
  60.             return usResult;
  61.  
  62.     /* Convert input string to upper case */
  63.         strupr( pszSource );
  64.  
  65.     /* Get drive from input string or current drive */
  66.         if ( pszSource[1] == ':' && pszSource[0] != '\0' )
  67.             {
  68.             if ( usResult = DosSelectDisk(pszSource[0]-'@') ) return usResult;
  69.             pszSource += 2;
  70.             }
  71.         if ( usResult = DosQCurDisk(&usDrive,&ulMap) ) return usResult;
  72.         *pszDest++ = (CHAR)usDrive + '@';
  73.         *pszDest++ = ':';
  74.         *pszDest++ = '\\';
  75.         *pszDest   = '\0';
  76.  
  77.     /* If rest of input string is empty, append curdir\search and return */
  78.         if ( *pszSource == '\0' )
  79.             {
  80.             cbBuf = usMaxPathLen - 3;
  81.             if ( !(usResult = DosQCurDir(0,pszDest,&cbBuf)) )
  82.                 {
  83.                 if ( *(pszDest + strlen(pszDest) - 1) != '\\' )
  84.                     strcat( pszDest,szSlash );
  85.                 strcat( pszDest,pszSearch );
  86.                 }
  87.             return usResult;
  88.             }
  89.  
  90.     /* Search for last backslash. If none then source could be directory. */
  91.         if ( NULL == (pcLastSlash = strrchr(pszSource,'\\')) )
  92.             {
  93.             switch ( usResult = DosChDir(pszSource,0L) ) {
  94.                 case NO_ERROR:
  95.                     cbBuf = usMaxPathLen - 3;
  96.                     if ( usResult = DosQCurDir(0,pszDest,&cbBuf) )
  97.                         return usResult;
  98.                     if ( *(pszDest + strlen(pszDest) - 1) != '\\' )
  99.                         strcat( pszDest,szSlash );
  100.                     strcat( pszDest,pszSearch );
  101.                     return 0;
  102.  
  103.                 case ERROR_PATH_NOT_FOUND:
  104.                 case ERROR_FILE_NOT_FOUND:
  105.                 case ERROR_ACCESS_DENIED:
  106.                     cbBuf = usMaxPathLen - 3;
  107.                     if ( usResult = DosQCurDir(0,pszDest,&cbBuf) )
  108.                         return usResult;
  109.                     if ( *(pszDest + strlen(pszDest) - 1) != '\\' )
  110.                         strcat( pszDest,szSlash );
  111.                     strcat( pszDest,pszSource );
  112.                     return 0;
  113.  
  114.                 default:
  115.                     return usResult;
  116.                 }
  117.             }
  118.  
  119.     /* If the only backslash is at beginning, pszSource contains either */
  120.     /* \filename or \dirname or \                                       */
  121.         if ( pszSource == pcLastSlash )
  122.             {
  123.             switch ( usResult = DosChDir(pszSource,0L) ) {
  124.                 case NO_ERROR:  // pszSource contains \dirname
  125.                     cbBuf = usMaxPathLen - 3;
  126.                     if ( !(usResult = DosQCurDir(0,pszDest,&cbBuf)) )
  127.                         {
  128.                         if ( *(pszDest + strlen(pszDest) - 1) != '\\' )
  129.                             strcat( pszDest,szSlash );
  130.                         strcat( pszDest,pszSearch );
  131.                         }
  132.                     return usResult;
  133.  
  134.                 case ERROR_FILE_NOT_FOUND:
  135.                 case ERROR_PATH_NOT_FOUND:
  136.                 case ERROR_ACCESS_DENIED:   // pszSource contains \filename
  137.                     if ( usResult = DosChDir(szSlash,0L) ) return usResult;
  138.                     strcpy( pszDest,pszSource+1 );
  139.                     return 0;
  140.  
  141.                 default:
  142.                     return usResult;
  143.                 }
  144.             }
  145.  
  146.     /* Input has d:\dir\filename or d:\dir\dir or d:\dir\ */
  147.         switch ( usResult = DosChDir(pszSource,0L) ) {
  148.             case NO_ERROR:      // pszSource contains d:\dir\dir
  149.                 cbBuf = usMaxPathLen - 3;
  150.                 if ( !(usResult = DosQCurDir(0,pszDest,&cbBuf)) )
  151.                     {
  152.                     if ( *(pszDest + strlen(pszDest) - 1) != '\\' )
  153.                         strcat( pszDest,szSlash );
  154.                     strcat( pszDest,pszSearch );
  155.                     }
  156.                 return usResult;
  157.  
  158.             case ERROR_FILE_NOT_FOUND:
  159.             case ERROR_PATH_NOT_FOUND:
  160.             case ERROR_ACCESS_DENIED:   // d:\dir\filename or d:\dir\
  161.                 *pcLastSlash = '\0';
  162.                 cbBuf = usMaxPathLen - 3;
  163.                 if (   !(usResult = DosChDir(pszSource,0L))
  164.                     && !(usResult = DosQCurDir(0,pszDest,&cbBuf)) )
  165.                     {
  166.                     /* Append input filename, if any */
  167.                     pcFileOnly = pcLastSlash + 1;
  168.                     if ( *(pszDest + strlen(pszDest) - 1) != '\\' )
  169.                         strcat( pszDest,szSlash );
  170.                     strcat( pszDest,
  171.                             (*pcFileOnly=='\0') ? pszSearch : pcFileOnly );
  172.                     }
  173.                 return usResult;
  174.  
  175.             default:
  176.                 return usResult;
  177.             }
  178.     }
  179. /****************************************************************************/
  180.