From c19fd9f23eb52a354259ada8fa07aeaf3a7afc3d Mon Sep 17 00:00:00 2001 From: Soikk Date: Sat, 7 Jun 2025 22:31:30 +0200 Subject: Added fd/file_to_str without length specifiers --- str/str.c | 26 ++++++++++++++++++++++++-- str/str.h | 8 ++++++-- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/str/str.c b/str/str.c index 89dc053..f64c7e8 100755 --- a/str/str.c +++ b/str/str.c @@ -250,7 +250,10 @@ u32 get_line_len(char *buf){ return l; } -void fd_to_str(str *s, int fd, u32 len){ +void fd_to_str(str *s, int fd){ + // should probably check this isnt bigger than u32 max + off_t len = lseek(fd, 0, SEEK_END); + lseek(fd, 0, SEEK_SET); s->ptr = calloc(len+1, sizeof(char)); if(s->ptr == NULL) return; for(u32 l = len; l != 0; l -= read(fd, s->ptr+(len-l), l)); @@ -258,7 +261,26 @@ void fd_to_str(str *s, int fd, u32 len){ s->len = len; } -void file_to_str(str *s, FILE *fp, u32 len){ +void fd_to_nstr(str *s, int fd, u32 len){ + s->ptr = calloc(len+1, sizeof(char)); + if(s->ptr == NULL) return; + for(u32 l = len; l != 0; l -= read(fd, s->ptr+(len-l), l)); + s->cap = len; + s->len = len; +} + +void file_to_str(str *s, FILE *fp){ + fseek(fp, 0, SEEK_END); + long len = ftell(fp); + fseek(fp, 0, SEEK_SET); + s->ptr = calloc(len+1, sizeof(char)); + if(s->ptr == NULL) return; + for(u32 l = len; l != 0; l -= fread(s->ptr, sizeof(char), len, fp)); + s->cap = len; + s->len = len; +} + +void file_to_nstr(str *s, FILE *fp, u32 len){ s->ptr = calloc(len+1, sizeof(char)); if(s->ptr == NULL) return; for(u32 l = len; l != 0; l -= fread(s->ptr, sizeof(char), len, fp)); diff --git a/str/str.h b/str/str.h index 4ba897f..2484c19 100755 --- a/str/str.h +++ b/str/str.h @@ -115,9 +115,13 @@ str sread_delim_f(char *buf, bool (*func)(char), bool func_cond); u32 get_line_len(char *buf); -void fd_to_str(str *s, int fd, u32 len); +void fd_to_str(str *s, int fd); -void file_to_str(str *s, FILE *fp, u32 len); +void fd_to_nstr(str *s, int fd, u32 len); + +void file_to_str(str *s, FILE *fp); + +void file_to_nstr(str *s, FILE *fp, u32 len); void print_str(str s); -- cgit v1.2.3