From 87751fad5fef06cb8e263088b2238d2127d278c0 Mon Sep 17 00:00:00 2001 From: alessandrobason <1902881@uad.ac.uk> Date: Fri, 9 Sep 2022 12:46:20 +0100 Subject: [PATCH] . --- colla/dir.c | 38 ++++++++++++++++++++++++++++++++++++++ colla/dir.h | 1 + colla/str.c | 15 +++++++++++---- colla/tracelog.c | 25 +++++++++++++++++++++++-- colla/tracelog.h | 23 +++++++++++++++++++---- 5 files changed, 92 insertions(+), 10 deletions(-) diff --git a/colla/dir.c b/colla/dir.c index deaab1d..b6771d2 100644 --- a/colla/dir.c +++ b/colla/dir.c @@ -164,3 +164,41 @@ void dirCreate(const char *path) { } #endif + +#include + +bool dirRemove(const char *path) { + dir_t dir = dirOpen(path); + if (!dirValid(dir)) return false; + dir_entry_t *it = NULL; + while((it = dirNext(dir))) { + if (it->type == FS_TYPE_FILE) { + str_t file_path = strFromFmt("%s/%s", path, it->name.buf); + if (remove(file_path.buf)) { + err("couldn't remove %s > %s", file_path.buf, strerror(errno)); + } + strFree(file_path); + } + else if (it->type == FS_TYPE_DIR) { + if (strcmp(it->name.buf, ".") == 0 || strcmp(it->name.buf, "..") == 0) { + continue; + } + str_t new_path = strFromFmt("%s/%s", path, it->name.buf); + info("new path: %s--%s -> %s", path, it->name.buf, new_path.buf); + if (!dirRemove(new_path.buf)) { + err("couldn't delete folder %s", new_path.buf); + break; + } + strFree(new_path); + } + else { + err("%d -> %s", it->type, it->name.buf); + } + } + dirClose(dir); +#ifdef _WIN32 + return RemoveDirectoryA(path); +#else + return rmdir(path) == 0; +#endif +} diff --git a/colla/dir.h b/colla/dir.h index d3011ed..372b01d 100644 --- a/colla/dir.h +++ b/colla/dir.h @@ -27,6 +27,7 @@ bool dirValid(dir_t ctx); dir_entry_t *dirNext(dir_t ctx); void dirCreate(const char *path); +bool dirRemove(const char *path); #ifdef __cplusplus } // extern "C" diff --git a/colla/str.c b/colla/str.c index da33fca..570a1d9 100644 --- a/colla/str.c +++ b/colla/str.c @@ -411,9 +411,11 @@ usize strvFindView(strview_t ctx, strview_t view, usize from) { usize strvRFind(strview_t ctx, char c, usize from) { if(from >= ctx.len) { - from = ctx.len - 1; + from = ctx.len; } + from = ctx.len - from; + const char *buf = ctx.buf + from; for(; buf >= ctx.buf; --buf) { if(*buf == c) return (buf - ctx.buf); @@ -423,12 +425,17 @@ usize strvRFind(strview_t ctx, char c, usize from) { } usize strvRFindView(strview_t ctx, strview_t view, usize from) { - from = min(from, ctx.len); - if(view.len > ctx.len) { - from -= view.len; + return SIZE_MAX; } + if(from > ctx.len) { + from = ctx.len; + } + + from = ctx.len - from; + from -= view.len; + const char *buf = ctx.buf + from; for(; buf >= ctx.buf; --buf) { if(memcmp(buf, view.buf, view.len) == 0) return (buf - ctx.buf); diff --git a/colla/tracelog.c b/colla/tracelog.c index af02cad..bd126d0 100644 --- a/colla/tracelog.c +++ b/colla/tracelog.c @@ -72,14 +72,16 @@ void traceLog(int level, const char *fmt, ...) { va_end(args); } +#ifdef TLOG_MUTEX #include "cthreads.h" - static cmutex_t g_mtx = 0; +#endif void traceLogVaList(int level, const char *fmt, va_list args) { + #ifdef TLOG_MUTEX if (!g_mtx) g_mtx = mtxInit(); - mtxLock(g_mtx); + #endif char buffer[MAX_TRACELOG_MSG_LENGTH]; memset(buffer, 0, sizeof(buffer)); @@ -124,9 +126,28 @@ void traceLogVaList(int level, const char *fmt, va_list args) { if (level == LogFatal) exit(1); #endif + #ifdef TLOG_MUTEX mtxUnlock(g_mtx); + #endif } void traceUseNewline(bool newline) { use_newline = newline; } + +void traceSetColour(colour_e colour) { +#ifdef TLOG_WIN32_NO_VS + SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colour); +#else + switch (colour) { + case COL_RESET: printf(RESET); break; + case COL_BLACK: printf(BLACK); break; + case COL_BLUE: printf(BLUE); break; + case COL_GREEN: printf(GREEN); break; + case COL_CYAN: printf(CYAN); break; + case COL_RED: printf(RED); break; + case COL_MAGENTA: printf(MAGENTA); break; + case COL_YELLOW: printf(YELLOW); break; + } +#endif +} \ No newline at end of file diff --git a/colla/tracelog.h b/colla/tracelog.h index b97b95b..7e10cd2 100644 --- a/colla/tracelog.h +++ b/colla/tracelog.h @@ -5,9 +5,11 @@ extern "C" { #endif /* Define any of this to turn on the option - * -> TLOG_NO_COLOURS: print without using colours - * -> TLOG_VS: print to visual studio console, also turns on TLOG_NO_COLOURS - * -> TLOG_DONT_EXIT_ON_FATAL: don't call 'exit(1)' when using LogFatal + * -> TLOG_NO_COLOURS: print without using colours + * -> TLOG_VS: print to visual studio console, also turns on TLOG_NO_COLOURS + * -> TLOG_DONT_EXIT_ON_FATAL: don't call 'exit(1)' when using LogFatal + * -> TLOG_DISABLE: turn off all logging, useful for release builds + * -> TLOG_MUTEX: use a mutex on every traceLog call */ #include @@ -17,11 +19,24 @@ enum { LogAll, LogTrace, LogDebug, LogInfo, LogWarning, LogError, LogFatal }; +typedef enum { + COL_RESET = 15, + COL_BLACK = 8, + COL_BLUE = 9, + COL_GREEN = 10, + COL_CYAN = 11, + COL_RED = 12, + COL_MAGENTA = 13, + COL_YELLOW = 14, + COL_WHITE = 15 +} colour_e; + void traceLog(int level, const char *fmt, ...); void traceLogVaList(int level, const char *fmt, va_list args); void traceUseNewline(bool use_newline); +void traceSetColour(colour_e colour); -#ifdef NO_LOG +#ifdef TLOG_DISABLE #define tall(...) #define trace(...) #define debug(...)