Merge remote-tracking branch 'refs/remotes/origin/master'

This commit is contained in:
alessandrobason 2021-11-04 00:11:09 +00:00
commit 7fdc8b5301
7 changed files with 47 additions and 18 deletions

18
dir.c
View file

@ -81,6 +81,15 @@ dir_t dirOpen(const char *path) {
return dir; 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_entry_t *dirNext(dir_t ctx) {
_dir_internal_t *dir = (_dir_internal_t*)ctx; _dir_internal_t *dir = (_dir_internal_t*)ctx;
strFree(&dir->cur.name); strFree(&dir->cur.name);
@ -90,10 +99,6 @@ dir_entry_t *dirNext(dir_t ctx) {
return &dir->cur; return &dir->cur;
} }
void dirClose(dir_t ctx) {
free(ctx);
}
#else #else
#include <dirent.h> #include <dirent.h>
@ -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) { dir_entry_t *dirNext(dir_t ctx) {
if(!ctx) return NULL; if(!ctx) return NULL;
_dir_internal_t *in = (_dir_internal_t *)ctx; _dir_internal_t *in = (_dir_internal_t *)ctx;

2
dir.h
View file

@ -22,6 +22,8 @@ enum {
dir_t dirOpen(const char *path); dir_t dirOpen(const char *path);
void dirClose(dir_t ctx); void dirClose(dir_t ctx);
bool dirValid(dir_t ctx);
dir_entry_t *dirNext(dir_t ctx); dir_entry_t *dirNext(dir_t ctx);
#ifdef __cplusplus #ifdef __cplusplus

23
file.c
View file

@ -7,21 +7,20 @@
#include <windows.h> #include <windows.h>
static DWORD _toWin32Access(int mode) { static DWORD _toWin32Access(int mode) {
switch(mode) { if(mode & FILE_READ) return GENERIC_READ;
case FILE_READ: return GENERIC_READ; if(mode & FILE_WRITE) return GENERIC_WRITE;
case FILE_WRITE: return GENERIC_WRITE; if(mode & FILE_BOTH) return GENERIC_READ | GENERIC_WRITE;
case FILE_BOTH: return GENERIC_READ | GENERIC_WRITE; fatal("unrecognized access mode: %d", mode);
default: fatal("unrecognized mode: %d", mode); return 0; return 0;
}
} }
static DWORD _toWin32Creation(int mode) { static DWORD _toWin32Creation(int mode) {
switch(mode) { if(mode & FILE_READ) return OPEN_EXISTING;
case FILE_READ: return OPEN_EXISTING; if(mode == (FILE_WRITE | FILE_CLEAR)) return CREATE_ALWAYS;
case FILE_WRITE: return OPEN_ALWAYS; if(mode & FILE_WRITE) return OPEN_ALWAYS;
case FILE_BOTH: return OPEN_ALWAYS; if(mode & FILE_BOTH) return OPEN_ALWAYS;
default: fatal("unrecognized mode: %d", mode); return 0; fatal("unrecognized creation mode: %d", mode);
} return 0;
} }
file_t fileOpen(const char *fname, int mode) { file_t fileOpen(const char *fname, int mode) {

5
file.h
View file

@ -8,7 +8,10 @@ extern "C" {
#include "str.h" #include "str.h"
enum { 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 { typedef struct {

12
str.c
View file

@ -263,6 +263,18 @@ str_t strToLower(str_t ctx) {
return str; 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 ====================================================
strview_t strvInit(const char *cstr) { strview_t strvInit(const char *cstr) {

3
str.h
View file

@ -68,6 +68,9 @@ strview_t strSubview(str_t *ctx, size_t pos, size_t len);
void strLower(str_t *ctx); void strLower(str_t *ctx);
str_t strToLower(str_t ctx); str_t strToLower(str_t ctx);
void strUpper(str_t *ctx);
str_t strToUpper(str_t ctx);
#ifdef STR_TESTING #ifdef STR_TESTING
void strTest(void); void strTest(void);
#endif #endif

View file

@ -534,7 +534,7 @@ void ostrAppenddouble(str_ostream_t *ctx, double val) {
} }
void ostrAppendview(str_ostream_t *ctx, strview_t view) { 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); _ostrRealloc(ctx, view.len + 1);
} }
memcpy(ctx->buf + ctx->size, view.buf, view.len); memcpy(ctx->buf + ctx->size, view.buf, view.len);