diff --git a/dir.c b/dir.c index b50d646..e2e8316 100644 --- a/dir.c +++ b/dir.c @@ -81,6 +81,15 @@ dir_t dirOpen(const char *path) { return dir; } +void dirClose(dir_t ctx) { + free(ctx); +} + +bool dirValid(dir_t ctx) { + _dir_internal_t *dir = (_dir_internal_t*)ctx; + return dir->handle != INVALID_HANDLE_VALUE; +} + dir_entry_t *dirNext(dir_t ctx) { _dir_internal_t *dir = (_dir_internal_t*)ctx; strFree(&dir->cur.name); @@ -90,10 +99,6 @@ dir_entry_t *dirNext(dir_t ctx) { return &dir->cur; } -void dirClose(dir_t ctx) { - free(ctx); -} - #else #include @@ -127,6 +132,11 @@ void dirClose(dir_t ctx) { } } +bool dirValid(dir_t ctx) { + _dir_internal_t *dir = (_dir_internal_t*)ctx; + return dir->handle != NULL; +} + dir_entry_t *dirNext(dir_t ctx) { if(!ctx) return NULL; _dir_internal_t *in = (_dir_internal_t *)ctx; diff --git a/dir.h b/dir.h index 961509d..4d91bae 100644 --- a/dir.h +++ b/dir.h @@ -22,6 +22,8 @@ enum { dir_t dirOpen(const char *path); void dirClose(dir_t ctx); +bool dirValid(dir_t ctx); + dir_entry_t *dirNext(dir_t ctx); #ifdef __cplusplus diff --git a/file.c b/file.c index 82336b6..4c97989 100644 --- a/file.c +++ b/file.c @@ -7,21 +7,20 @@ #include static DWORD _toWin32Access(int mode) { - switch(mode) { - case FILE_READ: return GENERIC_READ; - case FILE_WRITE: return GENERIC_WRITE; - case FILE_BOTH: return GENERIC_READ | GENERIC_WRITE; - default: fatal("unrecognized mode: %d", mode); return 0; - } + if(mode & FILE_READ) return GENERIC_READ; + if(mode & FILE_WRITE) return GENERIC_WRITE; + if(mode & FILE_BOTH) return GENERIC_READ | GENERIC_WRITE; + fatal("unrecognized access mode: %d", mode); + return 0; } static DWORD _toWin32Creation(int mode) { - switch(mode) { - case FILE_READ: return OPEN_EXISTING; - case FILE_WRITE: return OPEN_ALWAYS; - case FILE_BOTH: return OPEN_ALWAYS; - default: fatal("unrecognized mode: %d", mode); return 0; - } + if(mode & FILE_READ) return OPEN_EXISTING; + if(mode == (FILE_WRITE | FILE_CLEAR)) return CREATE_ALWAYS; + if(mode & FILE_WRITE) return OPEN_ALWAYS; + if(mode & FILE_BOTH) return OPEN_ALWAYS; + fatal("unrecognized creation mode: %d", mode); + return 0; } file_t fileOpen(const char *fname, int mode) { diff --git a/file.h b/file.h index 332fd37..f4502a7 100644 --- a/file.h +++ b/file.h @@ -8,7 +8,10 @@ extern "C" { #include "str.h" enum { - FILE_READ, FILE_WRITE, FILE_BOTH + FILE_READ = 1 << 0, + FILE_WRITE = 1 << 1, + FILE_CLEAR = 1 << 2, + FILE_BOTH = 1 << 3 }; typedef struct { diff --git a/str.c b/str.c index 9feb60a..87383ee 100644 --- a/str.c +++ b/str.c @@ -263,6 +263,18 @@ str_t strToLower(str_t ctx) { return str; } +void strUpper(str_t *ctx) { + for(size_t i = 0; i < ctx->len; ++i) { + ctx->buf[i] = (char)toupper(ctx->buf[i]); + } +} + +str_t strToUpper(str_t ctx) { + str_t str = strDup(ctx); + strUpper(&str); + return str; +} + // == STRVIEW_T ==================================================== strview_t strvInit(const char *cstr) { diff --git a/str.h b/str.h index 305d637..1950b72 100644 --- a/str.h +++ b/str.h @@ -68,6 +68,9 @@ strview_t strSubview(str_t *ctx, size_t pos, size_t len); void strLower(str_t *ctx); str_t strToLower(str_t ctx); +void strUpper(str_t *ctx); +str_t strToUpper(str_t ctx); + #ifdef STR_TESTING void strTest(void); #endif diff --git a/strstream.c b/strstream.c index 76f56e8..8639f52 100644 --- a/strstream.c +++ b/strstream.c @@ -534,7 +534,7 @@ void ostrAppenddouble(str_ostream_t *ctx, double val) { } void ostrAppendview(str_ostream_t *ctx, strview_t view) { - if((ctx->allocated - ctx->size) < view.len) { + if((ctx->allocated - ctx->size) <= view.len) { _ostrRealloc(ctx, view.len + 1); } memcpy(ctx->buf + ctx->size, view.buf, view.len);