cc79022c128caab81ba5686174b0085ce480259f kent Wed Oct 27 17:53:19 2010 -0700 Adding new function pathRelativeToFile diff --git src/lib/filePath.c src/lib/filePath.c index 9972139..50019dd 100644 --- src/lib/filePath.c +++ src/lib/filePath.c @@ -56,30 +56,34 @@ static char *findSlashBefore(char *start, char *e) /* Return first slash before s (but not before start) */ { while (--e >= start) { if (*e == '/') return e; } return start; } char *expandRelativePath(char *baseDir, char *relPath) /* Expand relative path to more absolute one. */ { +if (relPath[0] == '/') + // hey, it's absolute actually... + return cloneString(relPath); + char *e = baseDir + strlen(baseDir); int slashCount; char *rel = relPath; char *result; int size, baseSize; undosPath(baseDir); undosPath(relPath); slashCount = countChars(baseDir, '/'); if (baseDir[0] == 0) slashCount = -1; while (startsWith("../", rel)) { if (slashCount < 0) { warn("More ..'s in \"%s\" than directories in \"%s\"", relPath, baseDir); @@ -96,15 +100,32 @@ size = strlen(rel) + 1; if (baseSize > 0) size += baseSize + 1; if (baseSize > 0) { result = needMem(size); memcpy(result, baseDir, baseSize); result[baseSize] = '/'; strcpy(result + baseSize + 1, rel); } else result = cloneString(rel); return result; } +char *pathRelativeToFile(char *baseFile, char *relPath) +/* Given a base file name and a path relative to that, return + * relative path interpreted as if it were seen from the + * same directory holding the baseFile. + * An example of using this would be in processing include + * files. In this case the baseFile would be the current + * source file, and the relPath would be from the include + * statement. The returned result could then be used to + * open the include file. */ +{ +char dir[PATH_LEN]; +splitPath(baseFile, dir, NULL, NULL); +int dirLen = strlen(dir); +if (dirLen > 0 && dir[dirLen-1] == '/') + dir[dirLen-1] = 0; +return expandRelativePath(dir, relPath); +}