2017-08-01 14:30:58 +02:00
|
|
|
#ifndef B3_FILE_UTILS_H
|
|
|
|
#define B3_FILE_UTILS_H
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "b3Scalar.h"
|
2019-01-03 14:26:51 +01:00
|
|
|
#include <stddef.h> //ptrdiff_h
|
2017-08-01 14:30:58 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
struct b3FileUtils
|
|
|
|
{
|
|
|
|
b3FileUtils()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
virtual ~b3FileUtils()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool findFile(const char* orgFileName, char* relativeFileName, int maxRelativeFileNameMaxLen)
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
FILE* f = 0;
|
|
|
|
f = fopen(orgFileName, "rb");
|
|
|
|
if (f)
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
//printf("original file found: [%s]\n", orgFileName);
|
2019-01-03 14:26:51 +01:00
|
|
|
sprintf(relativeFileName, "%s", orgFileName);
|
2017-08-01 14:30:58 +02:00
|
|
|
fclose(f);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
//printf("Trying various directories, relative to current working directory\n");
|
|
|
|
const char* prefix[] = {"./", "./data/", "../data/", "../../data/", "../../../data/", "../../../../data/"};
|
|
|
|
int numPrefixes = sizeof(prefix) / sizeof(const char*);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
f = 0;
|
|
|
|
bool fileFound = false;
|
|
|
|
|
|
|
|
for (int i = 0; !f && i < numPrefixes; i++)
|
|
|
|
{
|
2018-09-07 16:11:04 +02:00
|
|
|
#ifdef _MSC_VER
|
2019-01-03 14:26:51 +01:00
|
|
|
sprintf_s(relativeFileName, maxRelativeFileNameMaxLen, "%s%s", prefix[i], orgFileName);
|
2017-08-01 14:30:58 +02:00
|
|
|
#else
|
2019-01-03 14:26:51 +01:00
|
|
|
sprintf(relativeFileName, "%s%s", prefix[i], orgFileName);
|
2017-08-01 14:30:58 +02:00
|
|
|
#endif
|
2019-01-03 14:26:51 +01:00
|
|
|
f = fopen(relativeFileName, "rb");
|
2017-08-01 14:30:58 +02:00
|
|
|
if (f)
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
fileFound = true;
|
|
|
|
break;
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
}
|
|
|
|
if (f)
|
|
|
|
{
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
return fileFound;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char* strip2(const char* name, const char* pattern)
|
|
|
|
{
|
|
|
|
size_t const patlen = strlen(pattern);
|
|
|
|
size_t patcnt = 0;
|
2019-01-03 14:26:51 +01:00
|
|
|
const char* oriptr;
|
|
|
|
const char* patloc;
|
2017-08-01 14:30:58 +02:00
|
|
|
// find how many times the pattern occurs in the original string
|
|
|
|
for (oriptr = name; (patloc = strstr(oriptr, pattern)); oriptr = patloc + patlen)
|
|
|
|
{
|
|
|
|
patcnt++;
|
|
|
|
}
|
|
|
|
return oriptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int extractPath(const char* fileName, char* path, int maxPathLength)
|
|
|
|
{
|
|
|
|
const char* stripped = strip2(fileName, "/");
|
|
|
|
stripped = strip2(stripped, "\\");
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
ptrdiff_t len = stripped - fileName;
|
|
|
|
b3Assert((len + 1) < maxPathLength);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
if (len && ((len + 1) < maxPathLength))
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
for (int i = 0; i < len; i++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
path[i] = fileName[i];
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
path[len] = 0;
|
|
|
|
}
|
|
|
|
else
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
len = 0;
|
2019-01-03 14:26:51 +01:00
|
|
|
b3Assert(maxPathLength > 0);
|
|
|
|
if (maxPathLength > 0)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
path[len] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char toLowerChar(const char t)
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
if (t >= (char)'A' && t <= (char)'Z')
|
2017-08-01 14:30:58 +02:00
|
|
|
return t + ((char)'a' - (char)'A');
|
|
|
|
else
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void toLower(char* str)
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
int len = strlen(str);
|
|
|
|
for (int i = 0; i < len; i++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
str[i] = toLowerChar(str[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*static const char* strip2(const char* name, const char* pattern)
|
|
|
|
{
|
|
|
|
size_t const patlen = strlen(pattern);
|
|
|
|
size_t patcnt = 0;
|
|
|
|
const char * oriptr;
|
|
|
|
const char * patloc;
|
|
|
|
// find how many times the pattern occurs in the original string
|
|
|
|
for (oriptr = name; patloc = strstr(oriptr, pattern); oriptr = patloc + patlen)
|
|
|
|
{
|
|
|
|
patcnt++;
|
|
|
|
}
|
|
|
|
return oriptr;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
};
|
2019-01-03 14:26:51 +01:00
|
|
|
#endif //B3_FILE_UTILS_H
|