1 #ifndef B3_FILE_UTILS_H
2 #define B3_FILE_UTILS_H
6 #include <stddef.h> //ptrdiff_h
14 virtual ~b3FileUtils()
18 static bool findFile(const char* orgFileName, char* relativeFileName, int maxRelativeFileNameMaxLen)
21 f = fopen(orgFileName, "rb");
24 //printf("original file found: [%s]\n", orgFileName);
25 sprintf(relativeFileName, "%s", orgFileName);
30 //printf("Trying various directories, relative to current working directory\n");
31 const char* prefix[] = {"./", "./data/", "../data/", "../../data/", "../../../data/", "../../../../data/"};
32 int numPrefixes = sizeof(prefix) / sizeof(const char*);
35 bool fileFound = false;
37 for (int i = 0; !f && i < numPrefixes; i++)
40 sprintf_s(relativeFileName, maxRelativeFileNameMaxLen, "%s%s", prefix[i], orgFileName);
42 sprintf(relativeFileName, "%s%s", prefix[i], orgFileName);
44 f = fopen(relativeFileName, "rb");
59 static const char* strip2(const char* name, const char* pattern)
61 size_t const patlen = strlen(pattern);
65 // find how many times the pattern occurs in the original string
66 for (oriptr = name; (patloc = strstr(oriptr, pattern)); oriptr = patloc + patlen)
73 static int extractPath(const char* fileName, char* path, int maxPathLength)
75 const char* stripped = strip2(fileName, "/");
76 stripped = strip2(stripped, "\\");
78 ptrdiff_t len = stripped - fileName;
79 b3Assert((len + 1) < maxPathLength);
81 if (len && ((len + 1) < maxPathLength))
83 for (int i = 0; i < len; i++)
85 path[i] = fileName[i];
92 b3Assert(maxPathLength > 0);
93 if (maxPathLength > 0)
101 static char toLowerChar(const char t)
103 if (t >= (char)'A' && t <= (char)'Z')
104 return t + ((char)'a' - (char)'A');
109 static void toLower(char* str)
111 int len = strlen(str);
112 for (int i = 0; i < len; i++)
114 str[i] = toLowerChar(str[i]);
118 /*static const char* strip2(const char* name, const char* pattern)
120 size_t const patlen = strlen(pattern);
124 // find how many times the pattern occurs in the original string
125 for (oriptr = name; patloc = strstr(oriptr, pattern); oriptr = patloc + patlen)
133 #endif //B3_FILE_UTILS_H