home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / FNSPLIT.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  11KB  |  271 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /************************************************************************/
  4. /*                                                                      */
  5. /*  FNSPLIT.C                                                           */
  6. /*                                                                      */
  7. /*  Contains routines for parsing file/path names.                      */
  8. /*                                                                      */
  9. /*  Original Copyright 1989-96 by Robert B. Stout as part of            */
  10. /*  the MicroFirm Function Library (MFL)                                */
  11. /*                                                                      */
  12. /*  The user is granted a free limited license to use this source file  */
  13. /*  to create royalty-free programs, subject to the terms of the        */
  14. /*  license restrictions specified in the LICENSE.MFL file.             */
  15. /*                                                                      */
  16. /************************************************************************/
  17.  
  18. #include <string.h>
  19. #include <stdlib.h>
  20. #include "filnames.h"
  21.  
  22. /******************************************************************/
  23. /*                                                                */
  24. /*  has_wild()                                                    */
  25. /*                                                                */
  26. /*  Checks a string for wildcard characters ('?' and '*')         */
  27. /*                                                                */
  28. /*  Arguments 1 - String to check                                 */
  29. /*                                                                */
  30. /*  Returns: True_ if string contains wildcards, else False_      */
  31. /*                                                                */
  32. /*  Side Effects: None                                            */
  33. /*                                                                */
  34. /******************************************************************/
  35.  
  36. Boolean_T has_wild(char *pname)
  37. {
  38.       if (NULL != strchr(pname, '*') || NULL != strchr(pname, '?'))
  39.             return True_;
  40.       else  return False_;
  41. }
  42.  
  43. /******************************************************************/
  44. /*                                                                */
  45. /*  fnSplit()                                                     */
  46. /*                                                                */
  47. /*  Splits file specifications into component parts. Similar to   */
  48. /*  compiler-specific fnsplit() or _splitpath().                  */
  49. /*                                                                */
  50. /*  Arguments 1 - Original file specification                     */
  51. /*            2 - Buffer to receive drive spec                    */
  52. /*            3 - Buffer to receive drive/path spec               */
  53. /*            4 - Buffer to receive path spec                     */
  54. /*            5 - Buffer to receive name.ext spec                 */
  55. /*            6 - Buffer to receive name spec                     */
  56. /*            7 - Buffer to receive ext spec                      */
  57. /*                                                                */
  58. /*  Returns: Bit map as follows (see defines in RBS.H):           */
  59. /*           Extension_ - File spec included extension            */
  60. /*           Filename_  - File spec did not end in '\'            */
  61. /*           Directory_ - File spec included a path               */
  62. /*           Drive_     - File spec included a drive spec         */
  63. /*           Wildname_  - File name included wildcards (*.?)      */
  64. /*           Wildpath_  - File path included wildcards (*.?)      */
  65. /*                                                                */
  66. /*  Side Effects: Calls unix2dos() to convert '/' to '\'          */
  67. /*                                                                */
  68. /*  Notes: Passing NULL in arguments 2-7 causes fnsplit() to      */
  69. /*         not save the corresponding portion of the path.        */
  70. /*                                                                */
  71. /******************************************************************/
  72.  
  73. int fnSplit(char *spec,             /* Original file spec         */
  74.             char *drive,            /* Drive spec                 */
  75.             char *pname,            /* Path w/ drive spec         */
  76.             char *path,             /* Path spec                  */
  77.             char *fname,            /* File name + extension      */
  78.             char *name,             /* File name                  */
  79.             char *ext)              /* File extension             */
  80. {
  81.       int ret_code = 0;
  82.       char *d = spec, *p, *e;
  83.  
  84.       unix2dos(spec);
  85.  
  86.       if (':' == spec[1])
  87.       {
  88.             if (drive)
  89.                   strncpy(drive, spec, 2);
  90.             drive[2] = NUL;
  91.             d += 2;
  92.             ret_code |= Drive_;
  93.       }
  94.       else
  95.       {
  96.             if (drive)
  97.                   *drive = NUL;
  98.       }
  99.  
  100.       if (NULL != (p = strrchr(d, '\\')))
  101.       {
  102.             char ch;
  103.  
  104.             ch = *(++p);
  105.             *p = NUL;
  106.             if (path)
  107.                   strcpy(path, d);
  108.             if (pname)
  109.                   strcpy(pname, spec);
  110.             if (has_wild(d))
  111.                   ret_code |= Wildpath_;
  112.             *p = ch;
  113.             ret_code |= Directory_;
  114.       }
  115.       else
  116.       {
  117.             if (path)
  118.                   *path = NUL;
  119.             if (pname)
  120.             {
  121.                   if (drive)
  122.                         strcpy(pname, drive);
  123.                   else  *pname = NUL;
  124.             }
  125.             p = d;
  126.  
  127.             if ('.' == *p)
  128.             {
  129.                   size_t dot_length;
  130.  
  131.                   ret_code |= Directory_;
  132.                   for (dot_length = 0; '.' == p[dot_length]; ++dot_length)
  133.                         ;
  134.                   if (path)
  135.                   {
  136.                         strncat(path, p, dot_length);
  137.                         strcat(path, "\\");
  138.                   }
  139.                   if (pname)
  140.                   {
  141.                         strncat(pname, p, dot_length);
  142.                         strcat(pname, "\\");
  143.                   }
  144.                   if (fname)
  145.                         *fname = NUL;
  146.                   if (name)
  147.                         *name  = NUL;
  148.                   if (ext)
  149.                         *ext   = NUL;
  150.                   return ret_code;
  151.             }
  152.       }
  153.       if (fname)
  154.             strcpy (fname, p);
  155.       if (has_wild(p))
  156.             ret_code |= Wildname_;
  157.       if (*p)
  158.             ret_code |= Filename_;
  159.       if (NULL != (e = strrchr(p, '.')))
  160.       {
  161.             *e = NUL;
  162.             if (name)
  163.                   strcpy(name, p);
  164.             *e = '.';
  165.             if (ext)
  166.                   strcpy(ext, e);
  167.             ret_code |= Extension_;
  168.       }
  169.       else
  170.       {
  171.             if (name)
  172.                   strcpy(name,p);
  173.             if (ext)
  174.                   *ext = NUL;
  175.       }
  176.       return ret_code;
  177. }
  178.  
  179. /******************************************************************/
  180. /*                                                                */
  181. /*  fnMerge()                                                     */
  182. /*                                                                */
  183. /*  Creates file specification from component parts. Similar to   */
  184. /*  compiler-specific fnmerge() or _makepath().                   */
  185. /*                                                                */
  186. /*  Arguments 1 - Buffer to receive file specification            */
  187. /*            2 - drive specification                             */
  188. /*            3 - drive/path specification                        */
  189. /*            4 - path specification                              */
  190. /*            5 - name.ext specification                          */
  191. /*            6 - name specification                              */
  192. /*            7 - ext specification                               */
  193. /*                                                                */
  194. /*  Returns: Reassembled name                                     */
  195. /*                                                                */
  196. /*  Side Effects: None                                            */
  197. /*                                                                */
  198. /******************************************************************/
  199.  
  200. char *fnMerge(char *spec,                 /* File spec buffer     */
  201.               char *drive,                /* Drive spec           */
  202.               char *pname,                /* Path w/ drive spec   */
  203.               char *path,                 /* Path spec            */
  204.               char *fname,                /* File name + extension*/
  205.               char *name,                 /* File name            */
  206.               char *ext)                  /* File extension       */
  207. {
  208.       *spec = NUL;
  209.  
  210.       if (pname && *pname)
  211.             strcpy(spec, pname);
  212.       else
  213.       {
  214.             if (drive && *drive)
  215.                   strcpy(spec, drive);
  216.             if (path && *path)
  217.                   strcpy(spec, path);
  218.       }
  219.  
  220.       unix2dos(spec);
  221.  
  222.       if (*spec && '\\' != LAST_CHAR(spec) && (':' != LAST_CHAR(spec)))
  223.             strcat(spec, "\\");
  224.  
  225.       if (fname && *fname)
  226.             strcat(spec, fname);
  227.       else
  228.       {
  229.             if (name && *name)
  230.                   strcat(spec, name);
  231.             else  return spec;
  232.             if (ext && *ext)
  233.             {
  234.                   if ('.' != *ext)
  235.                         strcat(spec, ".");
  236.                   strcat(spec, ext);
  237.             }
  238.       }
  239.       return strupr(spec);
  240. }
  241.  
  242. #ifdef TEST
  243.  
  244. #include <stdio.h>
  245.  
  246. main(int argc, char *argv[])
  247. {
  248.       char pname[FILENAME_MAX], drive[3], path[FILENAME_MAX];
  249.       char fname[13], name[9], ext[4], fullname[FILENAME_MAX];
  250.  
  251.       if (2 > argc)
  252.       {
  253.             puts("\aUsage: FNSTST [d:][path\\][file][.ext]");
  254.             abort();
  255.       }
  256.       printf("fnSplit(%s) returned \n", argv[1]);
  257.       printf("%X\n",
  258.             fnSplit(argv[1], drive, pname, path, fname, name, ext));
  259.       printf("drive: %s\n", drive);
  260.       printf("pname: %s\n", pname);
  261.       printf("path : %s\n", path);
  262.       printf("fname: %s\n", fname);
  263.       printf("name : %s\n", name);
  264.       printf("ext  : %s\n", ext);
  265.  
  266.       printf("\nCalling fnMerge() to reassemble everything returned %s\n",
  267.              fnMerge(fullname, drive, pname, path, fname, name, ext));
  268. }
  269.  
  270. #endif /* TEST */
  271.