static void *mapPath(PathMapFn fn, string path, string filename, void *data) { char *p1, *p2; string localpath, tempname; void *result; if (filename == NULL) filename = ""; if (path == NULL || path[0] == '\0' || isAbsolutePath(filename)) { return fn(expandPathname(filename), data); } localpath = concat(path, ":"); result = NULL; p1 = localpath; while (result == NULL) { for (p2 = p1; (*p2 != '\0') && (*p2 != ':') && (*p2 != ';') ; p2++) { /* Empty */ } if (*p2 == '\0') break; *p2 = '\0'; if (p1 != p2 || strlen(localpath) == 1) { tempname = concat(p1, concat("/", filename)); result = fn(expandPathname(tempname), data); } p1 = p2 + 1; } return result; }
static void testExpandPathname(void) { if (stringEqual(getDirectoryPathSeparator(), "/")) { test(expandPathname("a/b"), "a/b"); test(expandPathname("a\\b"), "a/b"); } else { test(expandPathname("a/b"), "a\\b"); test(expandPathname("a\\b"), "a\\b"); } }
string *listDirectory(string path) { struct _finddata_t data; intptr_t fp; string *result; int i, n; path = expandPathname(path); if (!isDirectory(path)) error("listDirectory: Path is not a directory"); fp = _findfirst(concat(path, "\\*"), &data); if ((long) fp == -1L) { if (errno != ENOENT) { error("listDirectory: %s", strerror(errno)); } result = newArray(1, string); result[0] = NULL; return result; } n = 1; while (_findnext(fp, &data) == 0) n++; _findclose(fp); result = newArray(n + 1, string); fp = _findfirst(concat(path, "\\*"), &data); result[0] = copyString(data.name); for (i = 1; i < n; i++) { _findnext(fp, &data); result[i] = copyString(data.name); } result[n] = NULL; return result; }
void loadObject(string pathname) { HandleList hl; static char fullPath[MAX_PATH_NAME]; hl = newBlock(HandleList); switch (pathname[0]) { case '/': strcpy(fullPath, pathname); break; case '~': strcpy(fullPath, expandPathname(pathname)); break; default: getcwd(fullPath, MAX_PATH_NAME); strcat(fullPath, "/"); strcat(fullPath, pathname); break; } hl->handle = dlopen(fullPath, RTLD_NOW); if (hl->handle == NULL) { freeBlock(hl); error("loadObject: %s", dlerror()); } hl->link = handles; handles = hl; }
char* promptForFolder(const char *prompt, const char *defaultPath, char* folderPath, int folderPathLen) { char path[PATH_MAX]; OSStatus err; Boolean isDirectory; int len; FSRef ref; /* Display the prompt. */ if (prompt == NULL) prompt = "Please enter the path to a folder:"; printf("%s ", prompt); if (defaultPath != NULL) printf("[%s] ",defaultPath); fflush(stdout); /* Get user input, and trim trailing newlines. */ fgets(path,sizeof(path),stdin); for (len = strlen(path); len > 0 && path[len-1] == '\n';) path[--len] = 0; if (path[0] == 0) strcpy(path,defaultPath); /* Expand magic characters just like a shell (mostly so ~ will work) */ expandPathname(path); /* Convert the path into an FSRef, which is what the burn engine needs. */ err = FSPathMakeRef((const UInt8*)path,&ref,&isDirectory); if (err != noErr) { printf("Bad path. Aborting. (%d)\n", (int)err); exit(1); } if (!isDirectory) { printf("That's a file, not a directory! Aborting.\n"); exit(1); } return strncpy(folderPath, path, folderPathLen); }
void deleteFile(const std::string& filename) { remove(expandPathname(filename).c_str()); }
void renameFile(const std::string& oldname, const std::string& newname) { std::string oldExpand = expandPathname(oldname); std::string newExpand = expandPathname(newname); rename(oldExpand.c_str(), newExpand.c_str()); }
bool openFile(std::ofstream& stream, const std::string& filename) { stream.clear(); stream.open(expandPathname(filename).c_str()); return !stream.fail(); }