From 5724c8ffa8c54dcb00d0e6ed251bc97dfd5855a1 Mon Sep 17 00:00:00 2001 From: kamkow1 Date: Sun, 1 Jun 2025 23:34:31 +0200 Subject: [PATCH] Add sb_read_file() --- example/sb.c | 17 +++++++++++++++++ gebs.c | 10 +++++++--- gebs.h | 36 +++++++++++++++++++++++++++++++++++- 3 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 example/sb.c diff --git a/example/sb.c b/example/sb.c new file mode 100644 index 0000000..cbc81ce --- /dev/null +++ b/example/sb.c @@ -0,0 +1,17 @@ +#define GEBS_NO_PREFIX +#define GEBS_IMPLEMENTATION +#include "../gebs.h" + +int main(int argc, char ** argv) +{ + String_Builder sb_c; + if (!sb_read_file(&sb_c, "sb.c")) { + LOGE("Could not read sb.c. sb.c is in example/\n"); + return 1; + } + sb_finish(&sb_c); + + printf("%s\n", sb_c.items); + + return 0; +} diff --git a/gebs.c b/gebs.c index 057bd49..dafb5ed 100644 --- a/gebs.c +++ b/gebs.c @@ -11,15 +11,19 @@ int main(int argc, char ** argv) } RULE("build/self_rebuild", "example/self_rebuild.c", "gebs.h") { - CMD("gcc", "-ggdb", "-o", "build/self_rebuild", "example/self_rebuild.c"); + CMD("cc", "-ggdb", "-o", "build/self_rebuild", "example/self_rebuild.c"); } RULE("build/arena", "example/arena.c", "gebs.h") { - CMD("gcc", "-ggdb", "-o", "build/arena", "example/arena.c"); + CMD("cc", "-ggdb", "-o", "build/arena", "example/arena.c"); } RULE("build/commands", "example/commands.c", "gebs.h") { - CMD("gcc", "-ggdb", "-o", "build/commands", "example/commands.c"); + CMD("cc", "-ggdb", "-o", "build/commands", "example/commands.c"); + } + + RULE("build/sb", "example/sb.c", "gebs.h") { + CMD("cc", "-ggdb", "-o", "build/sb", "example/sb.c"); } return 0; diff --git a/gebs.h b/gebs.h index 56bbc8d..3ba1ccb 100644 --- a/gebs.h +++ b/gebs.h @@ -88,6 +88,7 @@ int main(int argc, char ** argv) #include #include #include +#include #include #include @@ -283,6 +284,28 @@ typedef struct { #define gebs_sb_append_nstr(sb, nstr) \ gebs_sb_append_nstr_alloc(&gebs_default_allocator, (sb), (nstr)) +#define gebs_sb_read_file_alloc(alloc, sb, path) \ +({ \ + bool __ret = false; \ + FILE *__f = fopen((path), "r"); \ + if (__f != nil) { \ + __ret = true; \ + fseek(__f, 0L, SEEK_END); \ + size_t __size = ftell(__f); \ + rewind(__f); \ + char *__buf = gebs_malloc((alloc), __size); \ + fread(__buf, __size, 1, __f); \ + fclose(__f); \ + for (size_t __i = 0; __i < __size; __i++) { \ + gebs_sb_append_char_alloc((alloc), (sb), __buf[__i]); \ + } \ + } \ + __ret; \ +}) + +#define gebs_sb_read_file(sb, path) \ + gebs_sb_read_file_alloc(&default_allocator, (sb), (path)) + // ---------------------------------------------------------------------------- // Filesystem operations // ---------------------------------------------------------------------------- @@ -382,8 +405,9 @@ void gebs_rebuild_self1_alloc(Gebs_Allocator *alloc, int argc, char ** argv, extern Gebs_Arena gebs_scratch_arena; -char *gebs_fmt(const char *fmt, ...); void gebs_scratch_arena_reset(void); +char *gebs_fmt(const char *fmt, ...); +char *gebs_cwd(void); #ifdef GEBS_IMPLEMENTATION @@ -649,6 +673,15 @@ void gebs_scratch_arena_reset(void) gebs_arena_reset(&gebs_scratch_arena); } +char *gebs_cwd(void) +{ + char *cwd = gebs_malloc(&gebs_scratch_arena, PATH_MAX); + if (getcwd(cwd, PATH_MAX) == nil) { + return nil; + } + return cwd; +} + // ---------------------------------------------------------------------------- // Filesystem operations // ---------------------------------------------------------------------------- @@ -774,6 +807,7 @@ void gebs_rebuild_self1_alloc(Gebs_Allocator *alloc, int argc, char ** argv, #define sb_free gebs_sb_free #define sb_finish gebs_sb_finish #define sb_append_nstr gebs_sb_append_nstr +#define sb_read_file gebs_sb_read_file #define rename1 gebs_rename #define remove1 gebs_remove