This commit is contained in:
alessandrobason 2022-09-09 12:46:20 +01:00
parent e3cc2fcf5e
commit 87751fad5f
5 changed files with 92 additions and 10 deletions

View file

@ -164,3 +164,41 @@ void dirCreate(const char *path) {
} }
#endif #endif
#include <stdio.h>
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
}

View file

@ -27,6 +27,7 @@ bool dirValid(dir_t ctx);
dir_entry_t *dirNext(dir_t ctx); dir_entry_t *dirNext(dir_t ctx);
void dirCreate(const char *path); void dirCreate(const char *path);
bool dirRemove(const char *path);
#ifdef __cplusplus #ifdef __cplusplus
} // extern "C" } // extern "C"

View file

@ -411,9 +411,11 @@ usize strvFindView(strview_t ctx, strview_t view, usize from) {
usize strvRFind(strview_t ctx, char c, usize from) { usize strvRFind(strview_t ctx, char c, usize from) {
if(from >= ctx.len) { if(from >= ctx.len) {
from = ctx.len - 1; from = ctx.len;
} }
from = ctx.len - from;
const char *buf = ctx.buf + from; const char *buf = ctx.buf + from;
for(; buf >= ctx.buf; --buf) { for(; buf >= ctx.buf; --buf) {
if(*buf == c) return (buf - ctx.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) { usize strvRFindView(strview_t ctx, strview_t view, usize from) {
from = min(from, ctx.len);
if(view.len > 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; const char *buf = ctx.buf + from;
for(; buf >= ctx.buf; --buf) { for(; buf >= ctx.buf; --buf) {
if(memcmp(buf, view.buf, view.len) == 0) return (buf - ctx.buf); if(memcmp(buf, view.buf, view.len) == 0) return (buf - ctx.buf);

View file

@ -72,14 +72,16 @@ void traceLog(int level, const char *fmt, ...) {
va_end(args); va_end(args);
} }
#ifdef TLOG_MUTEX
#include "cthreads.h" #include "cthreads.h"
static cmutex_t g_mtx = 0; static cmutex_t g_mtx = 0;
#endif
void traceLogVaList(int level, const char *fmt, va_list args) { void traceLogVaList(int level, const char *fmt, va_list args) {
#ifdef TLOG_MUTEX
if (!g_mtx) g_mtx = mtxInit(); if (!g_mtx) g_mtx = mtxInit();
mtxLock(g_mtx); mtxLock(g_mtx);
#endif
char buffer[MAX_TRACELOG_MSG_LENGTH]; char buffer[MAX_TRACELOG_MSG_LENGTH];
memset(buffer, 0, sizeof(buffer)); memset(buffer, 0, sizeof(buffer));
@ -124,9 +126,28 @@ void traceLogVaList(int level, const char *fmt, va_list args) {
if (level == LogFatal) exit(1); if (level == LogFatal) exit(1);
#endif #endif
#ifdef TLOG_MUTEX
mtxUnlock(g_mtx); mtxUnlock(g_mtx);
#endif
} }
void traceUseNewline(bool newline) { void traceUseNewline(bool newline) {
use_newline = 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
}

View file

@ -8,6 +8,8 @@ extern "C" {
* -> TLOG_NO_COLOURS: print without using colours * -> TLOG_NO_COLOURS: print without using colours
* -> TLOG_VS: print to visual studio console, also turns on TLOG_NO_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_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 <stdbool.h> #include <stdbool.h>
@ -17,11 +19,24 @@ enum {
LogAll, LogTrace, LogDebug, LogInfo, LogWarning, LogError, LogFatal 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 traceLog(int level, const char *fmt, ...);
void traceLogVaList(int level, const char *fmt, va_list args); void traceLogVaList(int level, const char *fmt, va_list args);
void traceUseNewline(bool use_newline); void traceUseNewline(bool use_newline);
void traceSetColour(colour_e colour);
#ifdef NO_LOG #ifdef TLOG_DISABLE
#define tall(...) #define tall(...)
#define trace(...) #define trace(...)
#define debug(...) #define debug(...)