|
|
|
|
@ -19,6 +19,9 @@ const uint MAX_FILESIZE = 2 * 1024 * 1024; // 2MB
|
|
|
|
|
const uint MAX_STRING_LENGTH = 1024; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------
|
|
|
|
|
// C Strings
|
|
|
|
|
|
|
|
|
|
const char* |
|
|
|
|
utilBaseName(const char* path_str) |
|
|
|
|
{ |
|
|
|
|
@ -59,6 +62,45 @@ utilConcatPath(char* out, const char* base_dir, const char* file_name, uint max_
|
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool |
|
|
|
|
utilMatchPrefix(const char* lhs, const char* rhs, int sz) |
|
|
|
|
{ |
|
|
|
|
int rc = strncmp(lhs, rhs, sz); |
|
|
|
|
return (rc >= 0); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//-----------------
|
|
|
|
|
// Memory allocation
|
|
|
|
|
|
|
|
|
|
void * |
|
|
|
|
utilLogAlloc(uint item_count, uint type_size, const char* file_name, const int line) |
|
|
|
|
{ |
|
|
|
|
assert(item_count > 0); // that was a fun bug
|
|
|
|
|
|
|
|
|
|
void* mem = std::calloc(item_count, type_size); |
|
|
|
|
|
|
|
|
|
if (mem == nullptr) { |
|
|
|
|
LOG(ERROR) << "Memory allocation failed, called from " |
|
|
|
|
<< file_name << ":" << line; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
assert(mem != nullptr); // might as well stop execution here
|
|
|
|
|
|
|
|
|
|
return mem; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// TODO: search/replace other calls to std::free with this
|
|
|
|
|
void utilSafeFree(const void* mem) |
|
|
|
|
{ |
|
|
|
|
if (mem != nullptr) { |
|
|
|
|
std::free((void *) mem); |
|
|
|
|
mem = nullptr; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//-----------------
|
|
|
|
|
// File I/O
|
|
|
|
|
|
|
|
|
|
// TODO: don't use ftell() to get filesize
|
|
|
|
|
// https://wiki.sei.cmu.edu/confluence/display/c/FIO19-C.+Do+not+use+fseek%28%29+and+ftell%28%29+to+compute+the+size+of+a+regular+file
|
|
|
|
|
char * |
|
|
|
|
@ -112,31 +154,8 @@ utilWriteTextFile(const char* filename, const char* text)
|
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void * |
|
|
|
|
utilLogAlloc(uint item_count, uint type_size, const char* file_name, const int line) |
|
|
|
|
{ |
|
|
|
|
assert(item_count > 0); // that was a fun bug
|
|
|
|
|
|
|
|
|
|
void* mem = std::calloc(item_count, type_size); |
|
|
|
|
|
|
|
|
|
if (mem == nullptr) { |
|
|
|
|
LOG(ERROR) << "Memory allocation failed, called from " |
|
|
|
|
<< file_name << ":" << line; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
assert(mem != nullptr); // might as well stop execution here
|
|
|
|
|
|
|
|
|
|
return mem; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// TODO: search/replace other calls to std::free with this
|
|
|
|
|
void utilSafeFree(const void* mem) |
|
|
|
|
{ |
|
|
|
|
if (mem != nullptr) { |
|
|
|
|
std::free((void *) mem); |
|
|
|
|
mem = nullptr; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
//-----------------
|
|
|
|
|
// utilImage
|
|
|
|
|
|
|
|
|
|
util_image |
|
|
|
|
utilLoadImage(const char* base_dir, const char* filename) |
|
|
|
|
|