home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / makepath.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  3KB  |  118 lines

  1. /***
  2. *makepath.c - create path name from components
  3. *
  4. *       Copyright (c) 1987-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       To provide support for creation of full path names from components
  8. *
  9. *******************************************************************************/
  10.  
  11. #include <cruntime.h>
  12. #include <stdlib.h>
  13. #ifdef _MBCS
  14. #include <mbdata.h>
  15. #include <mbstring.h>
  16. #endif  /* _MBCS */
  17. #include <tchar.h>
  18.  
  19. /***
  20. *void _makepath() - build path name from components
  21. *
  22. *Purpose:
  23. *       create a path name from its individual components
  24. *
  25. *Entry:
  26. *       _TSCHAR *path  - pointer to buffer for constructed path
  27. *       _TSCHAR *drive - pointer to drive component, may or may not contain
  28. *                     trailing ':'
  29. *       _TSCHAR *dir   - pointer to subdirectory component, may or may not include
  30. *                     leading and/or trailing '/' or '\' characters
  31. *       _TSCHAR *fname - pointer to file base name component
  32. *       _TSCHAR *ext   - pointer to extension component, may or may not contain
  33. *                     a leading '.'.
  34. *
  35. *Exit:
  36. *       path - pointer to constructed path name
  37. *
  38. *Exceptions:
  39. *
  40. *******************************************************************************/
  41.  
  42. void __cdecl _tmakepath (
  43.         register _TSCHAR *path,
  44.         const _TSCHAR *drive,
  45.         const _TSCHAR *dir,
  46.         const _TSCHAR *fname,
  47.         const _TSCHAR *ext
  48.         )
  49. {
  50.         register const _TSCHAR *p;
  51.  
  52.         /* we assume that the arguments are in the following form (although we
  53.          * do not diagnose invalid arguments or illegal filenames (such as
  54.          * names longer than 8.3 or with illegal characters in them)
  55.          *
  56.          *  drive:
  57.          *      A           ; or
  58.          *      A:
  59.          *  dir:
  60.          *      \top\next\last\     ; or
  61.          *      /top/next/last/     ; or
  62.          *      either of the above forms with either/both the leading
  63.          *      and trailing / or \ removed.  Mixed use of '/' and '\' is
  64.          *      also tolerated
  65.          *  fname:
  66.          *      any valid file name
  67.          *  ext:
  68.          *      any valid extension (none if empty or null )
  69.          */
  70.  
  71.         /* copy drive */
  72.  
  73.         if (drive && *drive) {
  74.                 *path++ = *drive;
  75.                 *path++ = _T(':');
  76.         }
  77.  
  78.         /* copy dir */
  79.  
  80.         if ((p = dir) && *p) {
  81.                 do {
  82.                         *path++ = *p++;
  83.                 }
  84.                 while (*p);
  85. #ifdef _MBCS
  86.                 if (*(p=_mbsdec(dir,p)) != _T('/') && *p != _T('\\')) {
  87. #else  /* _MBCS */
  88.                 if (*(p-1) != _T('/') && *(p-1) != _T('\\')) {
  89. #endif  /* _MBCS */
  90.                         *path++ = _T('\\');
  91.                 }
  92.         }
  93.  
  94.         /* copy fname */
  95.  
  96.         if (p = fname) {
  97.                 while (*p) {
  98.                         *path++ = *p++;
  99.                 }
  100.         }
  101.  
  102.         /* copy ext, including 0-terminator - check to see if a '.' needs
  103.          * to be inserted.
  104.          */
  105.  
  106.         if (p = ext) {
  107.                 if (*p && *p != _T('.')) {
  108.                         *path++ = _T('.');
  109.                 }
  110.                 while (*path++ = *p++)
  111.                         ;
  112.         }
  113.         else {
  114.                 /* better add the 0-terminator */
  115.                 *path = _T('\0');
  116.         }
  117. }
  118.