You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
801 B
43 lines
801 B
|
|
#include <cstdlib> |
|
#include <cstdio> |
|
|
|
#include "aixlog.hpp" |
|
|
|
#include "util.h" |
|
|
|
const uint MAX_FILESIZE = 2 * 1024 * 1024; // 2MB |
|
|
|
char * |
|
utilDumpTextFile(const char* filename) |
|
{ |
|
// log('info: loading filename, blablabla); |
|
// TODO: log errors |
|
std::FILE* fp = std::fopen(filename, "rb"); |
|
assert(fp); |
|
|
|
std::fseek(fp, 0, SEEK_END); |
|
uint length = std::ftell(fp); |
|
std::fseek(fp, 0, SEEK_SET); |
|
assert(length < MAX_FILESIZE); |
|
// TODO: check error codes for fseek and ftell |
|
|
|
char* buf = (char *) std::calloc(length, sizeof(char)); |
|
assert(buf); |
|
|
|
std::fread(buf, sizeof(char), length, fp); |
|
// TODO: check fp w/ ferror() here |
|
|
|
return buf; |
|
} |
|
|
|
// TODO: search/replace other calls to std::free with this |
|
void |
|
utilSafeFree(void* mem) |
|
{ |
|
if (mem != nullptr) { |
|
std::free(mem); |
|
mem = nullptr; |
|
} |
|
} |
|
|
|
|