lib/filePath.c

Go to the documentation of this file.
00001 /* filePath - stuff to handle file name parsing. */
00002 #include "common.h"
00003 #include "filePath.h"
00004 
00005 void undosPath(char *path)
00006 /* Convert '\' to '/' in path. */
00007 {
00008 subChar(path, '\\', '/');
00009 }
00010 
00011 void splitPath(char *path, char dir[256], char name[128], char extension[64])
00012 /* Split a full path into components.  The dir component will include the
00013  * trailing / if any.  The extension component will include the starting
00014  * . if any.   Pass in NULL for dir, name, or extension if you don't care about
00015  * that part. */
00016 {
00017 char *dirStart, *nameStart, *extStart, *extEnd;
00018 int dirSize, nameSize, extSize;
00019 
00020 undosPath(path);
00021 dirStart = path;
00022 nameStart = strrchr(path,'/');
00023 if (nameStart == NULL)
00024     nameStart = path;
00025 else
00026     nameStart += 1;
00027 extStart = strrchr(nameStart, '.');
00028 if (extStart == NULL)
00029     extStart = nameStart + strlen(nameStart);
00030 extEnd = extStart + strlen(extStart);
00031 if ((dirSize = (nameStart - dirStart)) >= 256)
00032     errAbort("Directory too long in %s", path);
00033 if ((nameSize = (extStart - nameStart)) >= 128)
00034     errAbort("Name too long in %s", path);
00035 if ((extSize = (extEnd - extStart)) >= 64)
00036     errAbort("Extension too long in %s", path);
00037 if (dir != NULL)
00038     {
00039     memcpy(dir, dirStart, dirSize);
00040     dir[dirSize] = 0;
00041     }
00042 if (name != NULL)
00043     {
00044     memcpy(name, nameStart, nameSize);
00045     name[nameSize] = 0;
00046     }
00047 if (extension != NULL)
00048     {
00049     memcpy(extension, extStart, extSize);
00050     extension[extSize] = 0;
00051     }
00052 }
00053 
00054 static char *findSlashBefore(char *start, char *e)
00055 /* Return first slash before s (but not before start) */
00056 {
00057 while (--e >= start)
00058     {
00059     if (*e == '/')
00060          return e;
00061     }
00062 return start;
00063 }
00064 
00065 char *expandRelativePath(char *baseDir, char *relPath)
00066 /* Expand relative path to more absolute one. */
00067 {
00068 char *e = baseDir + strlen(baseDir);
00069 int slashCount;
00070 char *rel = relPath;
00071 char *result;
00072 int size, baseSize;
00073 undosPath(baseDir);
00074 undosPath(relPath);
00075 slashCount = countChars(baseDir, '/');
00076 if (baseDir[0] == 0)
00077     slashCount = -1;
00078 while (startsWith("../", rel))
00079     {
00080     if (slashCount < 0)
00081         {
00082         warn("More ..'s in \"%s\" than directories in \"%s\"", relPath, baseDir);
00083         return NULL;
00084         }
00085     else if (slashCount == 0)
00086         e = baseDir;
00087     else
00088         e = findSlashBefore(baseDir, e);
00089     slashCount -= 1;
00090     rel += 3;
00091     }
00092 baseSize = e - baseDir;
00093 size = strlen(rel) + 1;
00094 if (baseSize > 0)
00095     size += baseSize + 1;
00096 if (baseSize > 0)
00097     {
00098     result = needMem(size);
00099     memcpy(result, baseDir, baseSize);
00100     result[baseSize] = '/';
00101     strcpy(result + baseSize + 1, rel);
00102     }
00103 else
00104     result = cloneString(rel);
00105 return result;
00106 }
00107 

Generated on Tue Dec 25 18:39:30 2007 for blat by  doxygen 1.5.2