void file_removeDoubleSlashes(char *path) { unsigned int i = 0; int previousslash = 0; unsigned int len = strlen(path); unsigned int skipbeforeindex = 0; #ifdef WIN // don't remove network folder stuff if (path[0] == '\\' && strlen(path) > 1) { if (path[1] == '\\') { skipbeforeindex = 1; // this will retain \\ but shorten \\\ to \\. } } #endif while (i < len) { if (i >= skipbeforeindex) { if (file_IsDirectorySeparator(path[i])) { if (previousslash) { memmove(path+i, path+i+1, len-i); len--; continue; } previousslash = 1; } else { previousslash = 0; } } i++; } }
int file_Cwd(const char* path) { while (path[0] == '.' && strlen(path) > 1 && file_IsDirectorySeparator(path[1])) { path += 2; } if (strcasecmp(path, "") == 0 || strcasecmp(path, ".") == 0) { return 1; } char* pathcopy = strdup(path); if (!pathcopy) { return 0; } file_MakeSlashesNative(pathcopy); #ifdef WINDOWS if (SetCurrentDirectory(pathcopy) == 0) { free(pathcopy); return 0; } #else if (chdir(pathcopy) != 0) { free(pathcopy); return 0; } #endif free(pathcopy); return 1; }
void file_MakeSlashesNative(char* path) { unsigned int i = 0; while (i < strlen(path)) { if (file_IsDirectorySeparator(path[i])) { path[i] = file_NativeSlash(); } i++; } }
static int file_LatestSlash(const char* path) { int i = strlen(path)-1; while (!file_IsDirectorySeparator(path[i]) && i > 0) { i--; } if (i <= 0) { i = -1; } return i; }
int file_IsPathRelative(const char* path) { #ifdef WINDOWS if (PathIsRelative(path) == TRUE) { return 1; } return 0; #else if (file_IsDirectorySeparator(path[0])) { return 0; } return 1; #endif }
void file_makeSlashesCrossplatform(char *path) { unsigned int skipbeforeindex = 0; #ifdef WIN if (strlen(path) >= 2 && path[0] == '\\' && path[1] == '\\') { // skip network folder \\ stuff since that isn't really cross-platform skipbeforeindex = 2; } #endif unsigned int i = 0; while (i < strlen(path)) { if (i >= skipbeforeindex) { if (file_IsDirectorySeparator(path[i])) { path[i] = '/'; } } i++; } }
char* file_AddComponentToPath(const char* path, const char* component) { int addslash = 0; if (strlen(path) > 0) { if (!file_IsDirectorySeparator(path[strlen(path)-1])) { addslash = 1; } } char* newpath = malloc(strlen(path)+addslash+1+strlen(component)); if (!newpath) { return NULL; } memcpy(newpath, path, strlen(path)); if (addslash) { newpath[strlen(path)] = file_NativeSlash(); } memcpy(newpath + strlen(path) + addslash, component, strlen(component)); newpath[strlen(path) + addslash + strlen(component)] = 0; return newpath; }
int file_IsPathRelative(const char *path) { #ifdef WINDOWS // check network paths: if (strlen(path) >= 2 && path[0] == '\\' && path[1] == '\\') { // network path -> absolute return 1; } // check regular paths: if (PathIsRelative(path) == TRUE) { return 1; } return 0; #else if (file_IsDirectorySeparator(path[0])) { return 0; } return 1; #endif }
unsigned int file_CountPathComponents(const char *path) { unsigned int c = 0; unsigned int i = 0; int previousslash = 0; int absolutepathslash = 0; while (i < strlen(path)) { if (file_IsDirectorySeparator(path[i])) { if (!previousslash) { #ifdef WINDOWS // don't increment for first absolute path slash if (c > 0 || (i > 0 && path[i-1] != ':')) { c++; } else { absolutepathslash = 1; } #else if (i == 0) { absolutepathslash = 1; } else { c++; } #endif } previousslash = 1; } else { previousslash = 0; } i++; } if (!previousslash) { // add folder at the very end: if (c > 1 || absolutepathslash) { c++; } } return c; }