home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 2.ddi / CLIBSRC3.ZIP / SPLITPTH.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  8.7 KB  |  252 lines

  1. /*------------------------------------------------------------------------
  2.  * filename - spltpath.c
  3.  *
  4.  * function(s)
  5.  *        CopyIt   - copies a string to another
  6.  *        DotFound - checks for special directory names
  7.  *        _fnsplit - splits a full path name into its components
  8.  *        _splitpath - split a full path name (MSC compatible)
  9.  *-----------------------------------------------------------------------*/
  10.  
  11. /*
  12.  *      C/C++ Run Time Library - Version 5.0
  13.  *
  14.  *      Copyright (c) 1987, 1992 by Borland International
  15.  *      All Rights Reserved.
  16.  *
  17.  */
  18.  
  19.  
  20. #include <dir.h>
  21. #include <string.h>
  22.  
  23. /*---------------------------------------------------------------------*
  24.  
  25. Name            CopyIt - copies a string to another
  26.  
  27. Usage           void pascal near CopyIt(char *dst, const char *src,
  28.                                         unsigned maxlen)
  29.  
  30. Prototype in    local to this module
  31.  
  32. Description     copies string scr to string dst.
  33.  
  34. Return value    nothing
  35.  
  36. *---------------------------------------------------------------------*/
  37. static void pascal near CopyIt(char *dst, const char *src, unsigned maxlen)
  38. {
  39.         if (dst) {
  40.                 if(strlen(src) >= maxlen)
  41.                 {
  42.                         strncpy(dst, src, maxlen);
  43.                         dst[maxlen] = 0;
  44.                 }
  45.                 else
  46.                         strcpy(dst, src);
  47.         }
  48. }
  49.  
  50. /*---------------------------------------------------------------------*
  51.  
  52. Name            DotFound - checks for special dir name cases
  53.  
  54. Usage           int pascal near DotFound(char *pB);
  55.  
  56. Prototype in    local to this module
  57.  
  58. Description     checks for special directory names
  59.  
  60. *---------------------------------------------------------------------*/
  61. static  int pascal near DotFound(char *pB)
  62. {
  63.         if (*(pB-1) == '.')
  64.                 pB--;
  65.         switch (*--pB) {
  66.         case ':'  :
  67.                 if (*(pB-2) != '\0')
  68.                         break;
  69.         case '/'  :
  70.         case '\\' :
  71.         case '\0' :
  72.                 return 1;
  73.         }
  74.         return 0;
  75. }
  76.  
  77. /*---------------------------------------------------------------------*
  78.  
  79. Name            _fnsplit - splits a full path name into its components
  80.  
  81. Usage           #include <dir.h>
  82.                 int _fnsplit(const char *path, char * drive, char * dir,
  83.                              char * name, char * ext);
  84.  
  85.                 void _splitpath(const char *path, char * drive, char * dir,
  86.                              char * name, char * ext);
  87.  
  88. Related
  89. functions usage void _makepath(char *path, const char *drive, const char *dir,
  90.                             const char *name, const char *ext);
  91.  
  92. Prototype in    dir.h
  93.  
  94. Description     _fnsplit takes a file's full path name (path) as a string
  95.                 in the form
  96.  
  97.                         X:\DIR\SUBDIR\NAME.EXT
  98.  
  99.                 and splits path into its four components. It then stores
  100.                 those components in the strings pointed to by drive, dir,
  101.                 name and ext. (Each component is required but can be a
  102.                 NULL, which means the corresponding component will be
  103.                 parsed but not stored.)
  104.  
  105.                 The maximum sizes for these strings are given by the
  106.                 constants MAXDRIVE, MAXDIR, MAXPATH, MAXNAME and MAXEXT,
  107.                 (defined in dir.h) and each size includes space for the
  108.                 null-terminator.
  109.  
  110.                         Constant        (Max.)  String
  111.  
  112.                         MAXPATH         (80)    path
  113.                         MAXDRIVE        (3)     drive; includes colon (:)
  114.                         MAXDIR          (66)    dir; includes leading and
  115.                                                 trailing backslashes (\)
  116.                         MAXFILE         (9)     name
  117.                         MAXEXT          (5)     ext; includes leading dot (.)
  118.  
  119.                 _fnsplit assumes that there is enough space to store each
  120.                 non-NULL component. fnmerge assumes that there is enough
  121.                 space for the constructed path name. The maximum constructed
  122.                 length is MAXPATH.
  123.  
  124.                 When _fnsplit splits path, it treats the punctuation as
  125.                 follows:
  126.  
  127.                 * drive keeps the colon attached (C:, A:, etc.)
  128.  
  129.                 * dir keeps the leading and trailing backslashes
  130.                   (\turboc\include\,\source\, etc.)
  131.  
  132.                 * ext keeps the dot preceding the extension (.c, .exe, etc.)
  133.  
  134.                 _splitpath is an MSC-compatible function that is identical
  135.                 to _fnsplit, except that is doesn't return a value.
  136.  
  137.                 _splitpath (or _fnsplit) and _makepath are invertible; if you
  138.                 split a given path with _splitpath (or _fnsplit), then
  139.                 merge the resultant components with _makepath, you end up
  140.                 with path.
  141.  
  142. Return value    _fnsplit returns an integer (composed of five flags,
  143.                 defined in dir.h) indicating which of the full path name
  144.                 components were present in path; these flags and the components
  145.                 they represent are:
  146.  
  147.                         EXTENSION       an extension
  148.                         FILENAME        a filename
  149.                         DIRECTORY       a directory (and possibly
  150.                                         sub-directories)
  151.                         DRIVE           a drive specification (see dir.h)
  152.                         WILDCARDS       wildcards (* or ? cards)
  153.  
  154.                 _splitpath does not return a value.
  155.  
  156. *---------------------------------------------------------------------*/
  157. int _CType _FARFUNC _fnsplit(const char *pathP, char *driveP, char *dirP,
  158.                              char *nameP, char *extP)
  159. {
  160.         register char   *pB;
  161.         register int    Wrk;
  162.         int     Ret;
  163.  
  164.         char buf[ MAXPATH+2 ];
  165.  
  166.         /*
  167.           Set all string to default value zero
  168.         */
  169.         Ret = 0;
  170.         if (driveP)
  171.                 *driveP = 0;
  172.         if (dirP)
  173.                 *dirP = 0;
  174.         if (nameP)
  175.                 *nameP = 0;
  176.         if (extP)
  177.                 *extP = 0;
  178.  
  179.         /*
  180.           Copy filename into template up to MAXPATH characters
  181.         */
  182.         pB = buf;
  183.         while (*pathP == ' ')
  184.                 pathP++;
  185.         if ((Wrk = strlen(pathP)) > MAXPATH)
  186.                 Wrk = MAXPATH;
  187.         *pB++ = 0;
  188.         strncpy(pB, pathP, Wrk);
  189.         *(pB += Wrk) = 0;
  190.  
  191.         /*
  192.           Split the filename and fill corresponding nonzero pointers
  193.         */
  194.         Wrk = 0;
  195.         for (; ; ) {
  196.                 switch (*--pB) {
  197.                 case '.'  :
  198.                         if (!Wrk && (*(pB+1) == '\0'))
  199.                                 Wrk = DotFound(pB);
  200.                         if ((!Wrk) && ((Ret & EXTENSION) == 0)) {
  201.                                 Ret |= EXTENSION;
  202.                                 CopyIt(extP, pB, MAXEXT - 1);
  203.                                 *pB = 0;
  204.                         }
  205.                         continue;
  206.                 case ':'  :
  207.                         if (pB != &buf[2])
  208.                                 continue;
  209.                 case '\0' :
  210.                         if (Wrk) {
  211.                                 if (*++pB)
  212.                                         Ret |= DIRECTORY;
  213.                                 CopyIt(dirP, pB, MAXDIR - 1);
  214.                                 *pB-- = 0;
  215.                                 break;
  216.                         }
  217.                 case '/'  :
  218.                 case '\\' :
  219.                         if (!Wrk) {
  220.                                 Wrk++;
  221.                                 if (*++pB)
  222.                                         Ret |= FILENAME;
  223.                                 CopyIt(nameP, pB, MAXFILE - 1);
  224.                                 *pB-- = 0;
  225.                                 if (*pB == 0 || (*pB == ':' && pB == &buf[2]))
  226.                                         break;
  227.                         }
  228.                         continue;
  229.                 case '*'  :
  230.                 case '?'  :
  231.                         if (!Wrk)
  232.                                 Ret |= WILDCARDS;
  233.                 default :
  234.                         continue;
  235.                 }
  236.                 break;
  237.         }
  238.         if (*pB == ':') {
  239.                 if (buf[1])
  240.                         Ret |= DRIVE;
  241.                 CopyIt(driveP, &buf[1], MAXDRIVE - 1);
  242.         }
  243.  
  244.         return (Ret);
  245. }
  246.  
  247. void _FARFUNC _splitpath(const char *pathP, char *driveP, char *dirP,
  248.                          char *nameP, char *extP)
  249. {
  250.         (void)_fnsplit(pathP,driveP,dirP,nameP,extP);
  251. }
  252.