write with io
This commit is contained in:
parent
af9a1f1040
commit
7dc58b60a6
2 changed files with 29 additions and 0 deletions
|
|
@ -17,6 +17,14 @@ extern "C" {
|
||||||
*/
|
*/
|
||||||
void ARC_IO_FileToStr(ARC_String *path, ARC_String **data);
|
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
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
21
src/std/io.c
21
src/std/io.c
|
|
@ -9,6 +9,7 @@ void ARC_IO_FileToStr(ARC_String *path, ARC_String **data){
|
||||||
FILE *file = fopen(path->data, "rb");
|
FILE *file = fopen(path->data, "rb");
|
||||||
if(!file){
|
if(!file){
|
||||||
arc_errno = ARC_ERRNO_NULL;
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -20,6 +21,7 @@ void ARC_IO_FileToStr(ARC_String *path, ARC_String **data){
|
||||||
if(fileData == NULL){
|
if(fileData == NULL){
|
||||||
fclose(file);
|
fclose(file);
|
||||||
arc_errno = ARC_ERRNO_NULL;
|
arc_errno = ARC_ERRNO_NULL;
|
||||||
|
ARC_DEBUG_ERR("ARC_IO_FileToStr(ARC_String *path, ARC_String **data), file data is NULL");
|
||||||
*data = NULL;
|
*data = NULL;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -27,6 +29,7 @@ void ARC_IO_FileToStr(ARC_String *path, ARC_String **data){
|
||||||
if(1 != fread(fileData, length, 1, file)){
|
if(1 != fread(fileData, length, 1, file)){
|
||||||
fclose(file);
|
fclose(file);
|
||||||
arc_errno = ARC_ERRNO_COPY;
|
arc_errno = ARC_ERRNO_COPY;
|
||||||
|
ARC_DEBUG_ERR("ARC_IO_FileToStr(ARC_String *path, ARC_String **data), could not copy file data");
|
||||||
*data = NULL;
|
*data = NULL;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -35,3 +38,21 @@ void ARC_IO_FileToStr(ARC_String *path, ARC_String **data){
|
||||||
ARC_String_Create(data, fileData, length);
|
ARC_String_Create(data, fileData, length);
|
||||||
free(fileData);
|
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);
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue