home *** CD-ROM | disk | FTP | other *** search
/ Informática Multimedia: Special Games / INFESPGAMES.mdf / os2 / ribble / support / makepath.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-28  |  1.7 KB  |  92 lines

  1. #include <CSupport.h>
  2. #include <Makepath.h>
  3.  
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <sys/stat.h>
  8. #include <errno.h>
  9. #if defined(__OS2__) || defined(__WATCOMC__)
  10. #include <direct.h>
  11. #else
  12. #include <dir.h>
  13. #endif
  14.  
  15. // Returns 1 if path found
  16. // Returns 2 if path created
  17. // Returns 0 if failed
  18.  
  19. int CSExport
  20. makePath(const char* newPath)
  21. {
  22.   if (isValidPath(newPath) == 0)
  23.     return 0;
  24.  
  25.   char*   path = new char[strlen(newPath)+1];
  26.   char*   p = path;
  27.     int     success = 1;
  28.  
  29.   if (path == 0)
  30.     return 0;
  31.   else
  32.     strcpy(path, newPath);
  33.  
  34.   if (*p == '\\' || *p == '/')  // skip leading root spec.
  35.     p++;
  36.  
  37.   if (*p == 0)
  38.     return 1;
  39.  
  40.   while (success)
  41.     {
  42.       if ((*p == '\\' || *p == '/' || *p == '\0') && (p - path > 0 && p[-1] != ':'))
  43.         {
  44.           char oldCh = *p;
  45.           *p = '\0';
  46.  
  47.                     struct stat info;
  48.  
  49.           if (stat(path, &info) == -1)
  50.             if (errno == ENOENT)
  51.               if (mkdir(path) == -1)
  52.                 success = 0;
  53.               else
  54.                 success = 2;    // worked & dir made
  55.  
  56.           *p = oldCh;
  57.  
  58.           if (*p == 0)
  59.             break;
  60.         }
  61.       p++;
  62.         }
  63.     return success;
  64. }
  65.  
  66. int CSExport
  67. isValidPath(const char* _path)
  68. {
  69.   // Check that dir being made is <= 8 chars in length
  70.  
  71.   const char* pch = _path + strlen(_path) - 1;
  72.   int len = 0;
  73.  
  74.   while (pch != _path)
  75.     {
  76.       char ch = *pch--;
  77.       if (ch == ':' || ch == '\\' || ch == '/' || ch == '.')
  78.         len = 0;
  79.       else if (ch == '\0')
  80.         break;
  81.       else
  82.         len++;
  83.  
  84.       if (len > 8)
  85.         return 0;
  86.     }
  87.  
  88.   return 1;
  89. }
  90.  
  91.  
  92.