added readWholeFile and more generic tracelog with va_list

This commit is contained in:
alessandrobason 2021-10-31 00:36:05 +01:00
parent 4cc2159793
commit aa39e2563d
4 changed files with 88 additions and 10 deletions

64
file.c
View file

@ -157,4 +157,66 @@ void fileRewind(file_t *ctx) {
uint64_t fileTell(file_t *ctx) { uint64_t fileTell(file_t *ctx) {
return (uint64_t)ftell((FILE*)ctx->handle); return (uint64_t)ftell((FILE*)ctx->handle);
} }
#endif #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
View file

@ -15,6 +15,11 @@ typedef struct {
void *handle; void *handle;
} file_t; } file_t;
typedef struct {
char *buf;
size_t len;
} fread_buf_t;
file_t fileOpen(const char *fname, int mode); file_t fileOpen(const char *fname, int mode);
void fileClose(file_t *ctx); void fileClose(file_t *ctx);
@ -33,6 +38,12 @@ void fileRewind(file_t *ctx);
uint64_t fileTell(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 #ifdef __cplusplus
} // extern "C" } // extern "C"
#endif #endif

View file

@ -2,7 +2,6 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stdarg.h>
#include <stdlib.h> #include <stdlib.h>
#ifdef _WIN32 #ifdef _WIN32
@ -50,7 +49,14 @@
bool use_newline = true; 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]; char buffer[MAX_TRACELOG_MSG_LENGTH];
memset(buffer, 0, sizeof(buffer)); memset(buffer, 0, sizeof(buffer));
@ -68,10 +74,7 @@ void traceLog(LogLevel level, const char *fmt, ...) {
size_t offset = strlen(beg); size_t offset = strlen(beg);
strncpy(buffer, beg, sizeof(buffer)); strncpy(buffer, beg, sizeof(buffer));
va_list args;
va_start(args, fmt);
vsnprintf(buffer + offset, sizeof(buffer) - offset, fmt, args); vsnprintf(buffer + offset, sizeof(buffer) - offset, fmt, args);
va_end(args);
#ifdef TLOG_VS #ifdef TLOG_VS
OutputDebugStringA(buffer); OutputDebugStringA(buffer);

View file

@ -11,15 +11,17 @@ extern "C" {
*/ */
#include <stdbool.h> #include <stdbool.h>
#include <stdarg.h>
typedef enum { enum {
LogAll, LogTrace, LogDebug, LogInfo, LogWarning, LogError, LogFatal 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); void traceUseNewline(bool use_newline);
#define tall(...) traceLog(LogAll, __VA_ARGS__) #define tall(...) traceLog(LogAll, __VA_ARGS__)
#define trace(...) traceLog(LogTrace, __VA_ARGS__) #define trace(...) traceLog(LogTrace, __VA_ARGS__)
#define debug(...) traceLog(LogDebug, __VA_ARGS__) #define debug(...) traceLog(LogDebug, __VA_ARGS__)
#define info(...) traceLog(LogInfo, __VA_ARGS__) #define info(...) traceLog(LogInfo, __VA_ARGS__)