added readWholeFile and more generic tracelog with va_list
This commit is contained in:
parent
4cc2159793
commit
aa39e2563d
4 changed files with 88 additions and 10 deletions
62
file.c
62
file.c
|
|
@ -158,3 +158,65 @@ uint64_t fileTell(file_t *ctx) {
|
|||
return (uint64_t)ftell((FILE*)ctx->handle);
|
||||
}
|
||||
#endif
|
||||
|
||||
static fread_buf_t _readWholeInternal(file_t *ctx, bool null_terminated) {
|
||||
fread_buf_t contents = {0};
|
||||
uint64_t fsize = 0;
|
||||
|
||||
if(!fileSeekEnd(ctx)) {
|
||||
err("file: couldn't read until end");
|
||||
goto failed;
|
||||
}
|
||||
|
||||
fsize = fileTell(ctx);
|
||||
fileRewind(ctx);
|
||||
|
||||
contents.len = fsize;
|
||||
contents.buf = malloc(fsize + null_terminated);
|
||||
if(!contents.buf) {
|
||||
err("file: couldn't allocate buffer");
|
||||
goto failed;
|
||||
}
|
||||
|
||||
size_t read = fileRead(ctx, contents.buf, fsize);
|
||||
if(read != fsize) {
|
||||
err("file: read wrong amount of bytes: %zu instead of %zu", read, fsize);
|
||||
goto failed_free;
|
||||
}
|
||||
|
||||
if(null_terminated) {
|
||||
contents.buf[contents.len] = '\0';
|
||||
}
|
||||
|
||||
failed:
|
||||
return contents;
|
||||
failed_free:
|
||||
free(contents.buf);
|
||||
return contents;
|
||||
}
|
||||
|
||||
fread_buf_t fileReadWhole(const char *fname) {
|
||||
file_t fp = fileOpen(fname, FILE_READ);
|
||||
fread_buf_t contents = fileReadWholeFP(&fp);
|
||||
fileClose(&fp);
|
||||
return contents;
|
||||
}
|
||||
|
||||
fread_buf_t fileReadWholeFP(file_t *ctx) {
|
||||
return _readWholeInternal(ctx, false);
|
||||
}
|
||||
|
||||
str_t fileReadWholeText(const char *fname) {
|
||||
file_t fp = fileOpen(fname, FILE_READ);
|
||||
str_t contents = fileReadWholeFPText(&fp);
|
||||
fileClose(&fp);
|
||||
return contents;
|
||||
}
|
||||
|
||||
str_t fileReadWholeFPText(file_t *ctx) {
|
||||
fread_buf_t contents = _readWholeInternal(ctx, true);
|
||||
return (str_t) {
|
||||
.buf = contents.buf,
|
||||
.len = contents.len
|
||||
};
|
||||
}
|
||||
11
file.h
11
file.h
|
|
@ -15,6 +15,11 @@ typedef struct {
|
|||
void *handle;
|
||||
} file_t;
|
||||
|
||||
typedef struct {
|
||||
char *buf;
|
||||
size_t len;
|
||||
} fread_buf_t;
|
||||
|
||||
file_t fileOpen(const char *fname, int mode);
|
||||
void fileClose(file_t *ctx);
|
||||
|
||||
|
|
@ -33,6 +38,12 @@ void fileRewind(file_t *ctx);
|
|||
|
||||
uint64_t fileTell(file_t *ctx);
|
||||
|
||||
fread_buf_t fileReadWhole(const char *fname);
|
||||
fread_buf_t fileReadWholeFP(file_t *ctx);
|
||||
|
||||
str_t fileReadWholeText(const char *fname);
|
||||
str_t fileReadWholeFPText(file_t *ctx);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
#endif
|
||||
13
tracelog.c
13
tracelog.c
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
|
|
@ -50,7 +49,14 @@
|
|||
|
||||
bool use_newline = true;
|
||||
|
||||
void traceLog(LogLevel level, const char *fmt, ...) {
|
||||
void traceLog(int level, const char *fmt, ...) {
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
traceLogVaList(level, fmt, args);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
void traceLogVaList(int level, const char *fmt, va_list args) {
|
||||
char buffer[MAX_TRACELOG_MSG_LENGTH];
|
||||
memset(buffer, 0, sizeof(buffer));
|
||||
|
||||
|
|
@ -68,10 +74,7 @@ void traceLog(LogLevel level, const char *fmt, ...) {
|
|||
size_t offset = strlen(beg);
|
||||
strncpy(buffer, beg, sizeof(buffer));
|
||||
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
vsnprintf(buffer + offset, sizeof(buffer) - offset, fmt, args);
|
||||
va_end(args);
|
||||
|
||||
#ifdef TLOG_VS
|
||||
OutputDebugStringA(buffer);
|
||||
|
|
|
|||
|
|
@ -11,12 +11,14 @@ extern "C" {
|
|||
*/
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
typedef enum {
|
||||
enum {
|
||||
LogAll, LogTrace, LogDebug, LogInfo, LogWarning, LogError, LogFatal
|
||||
} LogLevel;
|
||||
};
|
||||
|
||||
void traceLog(LogLevel level, const char *fmt, ...);
|
||||
void traceLog(int level, const char *fmt, ...);
|
||||
void traceLogVaList(int level, const char *fmt, va_list args);
|
||||
void traceUseNewline(bool use_newline);
|
||||
|
||||
#define tall(...) traceLog(LogAll, __VA_ARGS__)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue