Add sb_read_file()

This commit is contained in:
kamkow1
2025-06-01 23:34:31 +02:00
parent 0ce4a1018e
commit 5724c8ffa8
3 changed files with 59 additions and 4 deletions

17
example/sb.c Normal file
View File

@ -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;
}

10
gebs.c
View File

@ -11,15 +11,19 @@ int main(int argc, char ** argv)
} }
RULE("build/self_rebuild", "example/self_rebuild.c", "gebs.h") { 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") { 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") { 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; return 0;

36
gebs.h
View File

@ -88,6 +88,7 @@ int main(int argc, char ** argv)
#include <inttypes.h> #include <inttypes.h>
#include <stdarg.h> #include <stdarg.h>
#include <string.h> #include <string.h>
#include <limits.h>
#include <unistd.h> #include <unistd.h>
#include <sys/stat.h> #include <sys/stat.h>
@ -283,6 +284,28 @@ typedef struct {
#define gebs_sb_append_nstr(sb, nstr) \ #define gebs_sb_append_nstr(sb, nstr) \
gebs_sb_append_nstr_alloc(&gebs_default_allocator, (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 // Filesystem operations
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -382,8 +405,9 @@ void gebs_rebuild_self1_alloc(Gebs_Allocator *alloc, int argc, char ** argv,
extern Gebs_Arena gebs_scratch_arena; extern Gebs_Arena gebs_scratch_arena;
char *gebs_fmt(const char *fmt, ...);
void gebs_scratch_arena_reset(void); void gebs_scratch_arena_reset(void);
char *gebs_fmt(const char *fmt, ...);
char *gebs_cwd(void);
#ifdef GEBS_IMPLEMENTATION #ifdef GEBS_IMPLEMENTATION
@ -649,6 +673,15 @@ void gebs_scratch_arena_reset(void)
gebs_arena_reset(&gebs_scratch_arena); 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 // 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_free gebs_sb_free
#define sb_finish gebs_sb_finish #define sb_finish gebs_sb_finish
#define sb_append_nstr gebs_sb_append_nstr #define sb_append_nstr gebs_sb_append_nstr
#define sb_read_file gebs_sb_read_file
#define rename1 gebs_rename #define rename1 gebs_rename
#define remove1 gebs_remove #define remove1 gebs_remove