From 659e34f07c05e00baf10417503111048cbc634e3 Mon Sep 17 00:00:00 2001 From: Soikk Date: Wed, 5 Nov 2025 20:52:52 +0100 Subject: Added string to file functions --- str/str.c | 7 +++++++ str/str.h | 6 +++++- 2 files changed, 12 insertions(+), 1 deletion(-) (limited to 'str') diff --git a/str/str.c b/str/str.c index 145aca9..7767e99 100755 --- a/str/str.c +++ b/str/str.c @@ -356,6 +356,12 @@ str file_to_nstr(char *filename, u32 len){ return s; } +void str_to_file(str s, char *filename){ + FILE *fp = fopen(filename, "r"); + str_to_fp(s, fp); + fclose(fp) +} + str map_file(char *filename){ int fd = open(filename, 0); if(fd == -1){ @@ -397,6 +403,7 @@ void unmap_file(str *s){ s->cap = 0; } +// TODO: check if this function is redundant str fread_str(int fd, int max){ str s = dnstr(max); s.len = read(fd, s.ptr, s.cap); diff --git a/str/str.h b/str/str.h index 9c68595..7a386c4 100755 --- a/str/str.h +++ b/str/str.h @@ -112,8 +112,12 @@ str fp_to_str(FILE *fp); str fp_to_nstr(FILE *fp, u32 len); str file_to_str(char *filename); str file_to_nstr(char *filename, u32 len); +#define str_to_fd(s, fd) write(fd, s.ptr, s.len) +#define str_to_fp(s, fp) fwrite(s.ptr, sizeof(str.ptr[0]), s.len, fp) +void str_to_file(str s, char *filename); + str map_file(char *filename); -// consider adding 'at' to other file-to-str functions +// TODO: consider adding 'at' to other file-to-str functions str map_file_at(char *filename, int len, int at); void unmap_file(str *s); -- cgit v1.2.3