home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0040 - 0049 / ibm0040-0049 / ibm0040.tar / ibm0040 / ZINC_6.ZIP / DOSSRC.ZIP / PATH.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-01  |  2.9 KB  |  140 lines

  1. //    Zinc Interface Library - PATH.CPP
  2. //    COPYRIGHT (C) 1990, 1991.  All Rights Reserved.
  3. //    Zinc Software Incorporated.  Pleasant Grove, Utah  USA
  4.  
  5. #include "ui_gen.hpp"
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <io.h>
  10. #include <fcntl.h>
  11. #include <dir.h>
  12.  
  13. UI_PATH::UI_PATH()
  14. {
  15.     envPath = getenv("PATH");
  16.     prgPath = 0;
  17.     cwdPath = 0;
  18.     fileName = 0;
  19.     pathName = 0;
  20.     pathPtr  = 0;
  21. }
  22.  
  23. UI_PATH::UI_PATH(char *programPath, int rememberCwd)
  24. {
  25.     envPath = getenv("PATH");
  26.     prgPath = 0;
  27.     if (_osmajor >= 3 && programPath)
  28.     {
  29.         char *tail = strrchr(programPath, '\\');
  30.         if (tail)
  31.         {
  32.             if (*(tail - 1) == ':')
  33.                 tail++;                    // Leave in the slash if root dir.
  34.             int prgLen = (int) (tail - programPath);
  35.             prgPath = new char[prgLen + 1];
  36.             memcpy(prgPath, programPath, prgLen);
  37.             *(prgPath + prgLen) = '\0';
  38.         }
  39.     }
  40.     cwdPath = 0;
  41.     if (rememberCwd)
  42.     {
  43.         char cwd[128];
  44.         getcwd(cwd, sizeof(cwd));
  45.         if ( !prgPath || strcmp(prgPath, cwd) != 0 )
  46.             cwdPath = ui_strdup(cwd);
  47.     }
  48.     fileName = 0;
  49.     pathName = 0;
  50.     pathPtr  = 0;
  51. }
  52.  
  53. UI_PATH::~UI_PATH()
  54. {
  55.     delete prgPath;
  56.     delete cwdPath;
  57.     delete fileName;
  58.     delete pathName;
  59. }
  60.  
  61. void UI_PATH::SetFileName(const char *a_fileName)
  62. {
  63.     delete fileName;
  64.     if (a_fileName)
  65.         fileName = ui_strdup(a_fileName);
  66.     else
  67.         fileName = 0;
  68.     pathPtr = envPath;
  69.     pathCnt = (prgPath || cwdPath) ? 1 : 3;
  70. }
  71.  
  72. char *UI_PATH::NextPathName()
  73. {
  74.     delete pathName;
  75.     pathName = 0;
  76.     int len;
  77.     int addSlash;
  78.     int fileNameLen = fileName ? strlen(fileName) : 0;
  79.     while (pathCnt == 1 || pathCnt == 2)
  80.     {
  81.         char *ptr = (pathCnt == 1) ? prgPath : cwdPath;
  82.         pathCnt++;
  83.         if (ptr)
  84.         {
  85.             len = strlen(ptr);
  86.             addSlash = *(ptr + len - 1) == '\\' ? 0 : 1;
  87.             pathName = new char[len + addSlash + fileNameLen + 1];
  88.             strcpy(pathName, ptr);
  89.             if (fileName)
  90.             {
  91.                 if (addSlash)
  92.                     strcat(pathName, "\\");
  93.                 strcat(pathName, fileName);
  94.             }
  95.             return pathName;
  96.         }
  97.     }
  98.     if (!pathPtr)
  99.         return 0;
  100.     char *s = strchr(pathPtr, ';');
  101.     if (!s)
  102.         len = strlen(pathPtr);
  103.     else
  104.         len = (int) (s - pathPtr);
  105.     addSlash = 0;
  106.     if (fileName && pathPtr[len - 1] != '\\')
  107.         addSlash = 1;
  108.     pathName = new char[len + addSlash + fileNameLen + 1];
  109.     memcpy(pathName, pathPtr, len);
  110.     if (addSlash)
  111.         pathName[len++] = '\\';
  112.     if (fileName)
  113.         strcpy(pathName + len, fileName);
  114.     else
  115.         pathName[len] = '\0';
  116.     pathPtr = s ? s + 1 : 0;
  117.     return pathName;
  118. }
  119.  
  120. int UI_PATH::OpenFile(const char *file, unsigned int mode)
  121. {
  122.     int handle;
  123.  
  124.     if (_osmajor >= 3 && mode == O_RDONLY)
  125.         mode = O_DENYWRITE; 
  126.     /* try specification as is */
  127.     if ((handle = open(file, mode)) >= 0)
  128.         return (handle);
  129.  
  130.     if (file[0] == '\\' || strchr(file, ':'))
  131.         return (-1);                 // Absolute file spec. or drive specifier.
  132.  
  133.     SetFileName(file);
  134.     char *pathName;
  135.     while ((pathName = NextPathName()) != 0)
  136.         if ((handle = open(pathName, mode)) >= 0)
  137.             return (handle);
  138.     return (-1);
  139. }
  140.