From 7dc58b60a67b0f0109b106302218d5912b843642 Mon Sep 17 00:00:00 2001 From: herbglitch Date: Tue, 23 Jan 2024 21:16:43 -0700 Subject: [PATCH] write with io --- include/arc/std/io.h | 8 ++++++++ src/std/io.c | 21 +++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/include/arc/std/io.h b/include/arc/std/io.h index cc5a1ed..59bb217 100644 --- a/include/arc/std/io.h +++ b/include/arc/std/io.h @@ -17,6 +17,14 @@ extern "C" { */ void ARC_IO_FileToStr(ARC_String *path, ARC_String **data); +/** + * @brief write string to file + * + * @param path a string to path of target file + * @param data data to be written +*/ +void ARC_IO_WriteStrToFile(ARC_String *path, ARC_String *data); + #ifdef __cplusplus } #endif diff --git a/src/std/io.c b/src/std/io.c index 6d219a2..c400f3f 100644 --- a/src/std/io.c +++ b/src/std/io.c @@ -9,6 +9,7 @@ void ARC_IO_FileToStr(ARC_String *path, ARC_String **data){ FILE *file = fopen(path->data, "rb"); if(!file){ arc_errno = ARC_ERRNO_NULL; + ARC_DEBUG_LOG(arc_errno, "ARC_IO_FileToStr(ARC_String *path, ARC_String **data), could not open file \"%s\"", path->data); return; } @@ -20,6 +21,7 @@ void ARC_IO_FileToStr(ARC_String *path, ARC_String **data){ if(fileData == NULL){ fclose(file); arc_errno = ARC_ERRNO_NULL; + ARC_DEBUG_ERR("ARC_IO_FileToStr(ARC_String *path, ARC_String **data), file data is NULL"); *data = NULL; return; } @@ -27,6 +29,7 @@ void ARC_IO_FileToStr(ARC_String *path, ARC_String **data){ if(1 != fread(fileData, length, 1, file)){ fclose(file); arc_errno = ARC_ERRNO_COPY; + ARC_DEBUG_ERR("ARC_IO_FileToStr(ARC_String *path, ARC_String **data), could not copy file data"); *data = NULL; return; } @@ -35,3 +38,21 @@ void ARC_IO_FileToStr(ARC_String *path, ARC_String **data){ ARC_String_Create(data, fileData, length); free(fileData); } + +void ARC_IO_WriteStrToFile(ARC_String *path, ARC_String *data){ + FILE *file = fopen(path->data, "wb"); + if(!file){ + arc_errno = ARC_ERRNO_NULL; + ARC_DEBUG_LOG(arc_errno, "ARC_IO_WriteStrToFile(ARC_String *path, ARC_String *data), could not open file \"%s\"", path->data); + return; + } + + if(1 != fwrite(data->data, data->length, 1, file)){ + fclose(file); + arc_errno = ARC_ERRNO_COPY; + ARC_DEBUG_ERR("ARC_IO_WriteStrToFile(ARC_String *path, ARC_String **data), could not write file data"); + return; + } + + fclose(file); +} \ No newline at end of file