write with io

This commit is contained in:
herbglitch 2024-01-23 21:16:43 -07:00
parent af9a1f1040
commit 7dc58b60a6
2 changed files with 29 additions and 0 deletions

View file

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

View file

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