home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / ZOO21E.EXE / PARSE.C < prev    next >
C/C++ Source or Header  |  1991-07-11  |  6KB  |  192 lines

  1. #ifndef LINT
  2. static char sccsid[]="@(#) parse.c 2.1 87/12/25 12:24:10";
  3. #endif /* LINT */
  4.  
  5. /*
  6. The contents of this file are hereby released to the public domain.
  7.  
  8.                                  -- Rahul Dhesi 1986/11/14
  9.  
  10. */
  11.  
  12. #include "options.h"
  13. #include "zoo.h"
  14. #include "zooio.h"
  15. #include "various.h"
  16. #include "zoofns.h"
  17.  
  18. #include "parse.h"
  19. #include "assert.h"
  20.  
  21. /*
  22. parse() accepts a filename and return its component parts in a structure.
  23. The component parts are:  disk drive, path prefix, root name of filename,
  24. and extension.
  25.  
  26. If DISK_CH is not defined, it is assumed that filenames may be
  27. preceded with a disk prefix terminated by the character DISK_CH.
  28. The first character of the disk prefix, followed by DISK_CH,
  29. is returned in the drive field.
  30.  
  31. If the symbol DISK_CH is defined, a null string is returned in the
  32. disk field.
  33. */
  34. void parse (path_st, fname)
  35. register struct path_st *path_st;
  36. char *fname;
  37. {
  38.    char tempname[LFNAMESIZE];       /* working copy of supplied fname */
  39.    char *namep;                   /* points to relevant part of tempname */
  40.  
  41.    char *p;
  42.    strcpy (tempname, fname);
  43.  
  44. #ifdef DEBUG
  45. printf ("parse:  supplied name is [%s].\n", tempname);
  46. #endif
  47.  
  48. #ifndef DISK_CH
  49.    path_st->drive[0] = '\0';
  50.    namep = tempname;           /* points to pathname+filename */
  51. #else
  52.    path_st->drive[0] = '\0';
  53.    p = strchr (tempname, DISK_CH);      /* point to first ':' */
  54.  
  55.    if (p != NULL) {
  56.       path_st->drive[0] = *tempname;/* use only first char of drive name */
  57.       path_st->drive[1] = DISK_CH;
  58.       path_st->drive[2] = '\0';
  59.       namep = ++p;                /* point to pathname+filename */
  60.    } else {
  61.       path_st->drive[0] = '\0';
  62.       namep = tempname;           /* points to pathname+filename */
  63.    }
  64. #endif /* end of not DISK_CH */
  65.  
  66.    /* Note:  findlast() finds last occurrence in the subject string of
  67.       any one of a set of chars */
  68.  
  69.    /* save the long filename */
  70.    p = findlast (namep, PATH_SEP);
  71.  
  72.    /* if path separator found, copy next char onwards; else entire string */
  73.    strncpy (path_st->lfname,
  74.                (p != NULL) ? p+1 : namep,
  75.                LFNAMESIZE);
  76.    path_st->lfname[LFNAMESIZE-1] = '\0';     /* force null termination */
  77.  
  78. #ifdef DEBUG
  79. printf ("parse:  path = [%s] long filename = [%s]\n",
  80.          namep, path_st->lfname);
  81. #endif
  82.  
  83. /* Separate out the extension */
  84. p = findlast (namep, EXT_SEP);                        /* look for . or /        */
  85. if (p != NULL && *p != EXT_CH)                        /* found .?                    */
  86.     p = NULL;                                                /* ... if not, ignore / */
  87.  
  88. #ifdef DEBUG
  89. if (p == NULL)
  90.    printf ("parse:  no extension found for [%s]\n", namep);
  91. else
  92.    printf ("parse:  extension for [%s] is [%s]\n", namep, p);
  93. #endif
  94.  
  95.    path_st->ext[0] = '\0';                      /* assume no extension  */
  96.    if (p != NULL) {                             /* found extension      */
  97.       strncpy (path_st->ext, (p+1), EXTLEN);    /* save extension       */
  98.       path_st->ext[EXTLEN] = '\0';              /* force termination    */
  99.       *p = '\0';                                /* null out extension   */
  100.    }
  101.  
  102.    /* separate out root of filename if any */
  103.    p = findlast (namep, PATH_SEP);
  104.  
  105.    if (p != NULL) {
  106.       ++p;
  107.       strncpy (path_st->fname, p, ROOTSIZE);  /* save filename        */
  108.       *p = '\0';               /* null out filename */
  109.    } else {
  110.       strncpy (path_st->fname, namep, ROOTSIZE);
  111.       *namep = '\0';                   /* null out filename    */
  112.    }
  113.    path_st->fname[ROOTSIZE] = '\0';           /* force termination    */
  114.  
  115.    /* what remains, whether null or not, is the path prefix */
  116.    path_st->dir[0] = '\0';             /* in case *namep is '\0' */
  117.  
  118.    strncpy (path_st->dir, namep, PATHSIZE);
  119.  
  120.    /* remove trailing path-separater from directory name, but don't
  121.       remove it if it is also the leading separater */
  122.    {
  123.       int n;
  124.       n = strlen(path_st->dir);
  125.       if (n != 1)
  126.          path_st->dir[n-1] = '\0';
  127.    }
  128.  
  129. #ifdef DEBUG
  130. printf ("parse:  path prefix = [%s].\n", namep);
  131. #endif
  132.    /* if extension is null, and if long filename contains more than
  133.       ROOTSIZE  characters, transfer some of them to extension */
  134.    if (path_st->ext[0] == '\0' && strlen(path_st->lfname) > ROOTSIZE) {
  135.       strncpy(path_st->ext, &path_st->lfname[ROOTSIZE], EXTLEN);
  136.       path_st->ext[3] = '\0';
  137.    }
  138. }
  139.  
  140. /*******************/
  141. /*
  142. findlast() finds last occurrence in provided string of any of the characters
  143. except the null character in the provided set.
  144.  
  145. If found, return value is pointer to character found, else it is NULL.
  146. */
  147.  
  148. char *findlast (str, set)
  149. register char *str;        /* subject string     */
  150. char *set;                 /* set of characters to look for */
  151.  
  152. {
  153.    register char *p;
  154.  
  155.    if (str == NULL || set == NULL || *str == '\0' || *set == '\0')
  156.       return (NULL);
  157.  
  158.    p = lastptr (str);   /* pointer to last char of string */
  159.    assert(p != NULL);
  160.  
  161.    while (p != str && strchr (set, *p) == NULL) {
  162.       --p;
  163.    }
  164.  
  165.    /* either p == str or we found a character or both */
  166.    if (strchr (set, *p) == NULL)
  167.       return (NULL);
  168.    else
  169.       return (p);
  170. }
  171.  
  172. /*******************/
  173. /*
  174. lastptr() returns a pointer to the last non-null character in the string, if
  175. any.  If the string is null it returns NULL
  176. */
  177.  
  178. char *lastptr (str)
  179. register char *str;                 /* string in which to find last char */
  180. {
  181.    register char *p;
  182.    if (str == NULL)
  183.       prterror ('f', "lastptr:  received null pointer\n");
  184.    if (*str == '\0')
  185.       return (NULL);
  186.    p = str;
  187.    while (*p != '\0')            /* find trailing null char */
  188.       ++p;
  189.    --p;                          /* point to just before it */
  190.    return (p);
  191. }
  192.