added file_readWholeText
This commit is contained in:
parent
6a7f0a8715
commit
83091ac707
@ -133,10 +133,14 @@ static inline Result(void) file_readBytesArrayExactly(FILE* f, Array(u8) dst){
|
|||||||
return file_readStructsExactly(f, dst.data, 1, dst.len);
|
return file_readStructsExactly(f, dst.data, 1, dst.len);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @brief allocates array of size equal `file_getSize()` and reads whole file
|
/// @brief allocates buffer of size `file_getSize(f)` and reads whole file
|
||||||
/// @param out_buf output array allocated on heap
|
/// @param out_buf output array allocated on heap
|
||||||
Result(void) file_readWhole(FILE* f, Array(u8)* out_buf);
|
Result(void) file_readWhole(FILE* f, Array(u8)* out_buf);
|
||||||
|
|
||||||
|
/// @brief allocates buffer of size `file_getSize(f) + 1` and reads whole file
|
||||||
|
/// @param out_str output str allocated on heap, null-terminated
|
||||||
|
Result(void) file_readWholeText(FILE* f, str* out_str);
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
// DIRECTORY //
|
// DIRECTORY //
|
||||||
//////////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
|||||||
@ -142,10 +142,28 @@ Result(void) file_readWhole(FILE* f, Array(u8)* out_buf){
|
|||||||
|
|
||||||
try(i64 f_size, i, file_getSize(f));
|
try(i64 f_size, i, file_getSize(f));
|
||||||
Array(u8) buf = Array_u8_alloc(f_size);
|
Array(u8) buf = Array_u8_alloc(f_size);
|
||||||
Defer(if(!success) free(buf.data));
|
Defer(if(!success) Array_u8_destroy(&buf));
|
||||||
try_void(file_readBytesArray(f, buf));
|
|
||||||
|
try_void(file_readBytesArrayExactly(f, buf));
|
||||||
|
|
||||||
*out_buf = buf;
|
*out_buf = buf;
|
||||||
success = true;
|
success = true;
|
||||||
Return RESULT_VOID;
|
Return RESULT_VOID;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Result(void) file_readWholeText(FILE* f, str* out_str){
|
||||||
|
Deferral(1);
|
||||||
|
bool success = false;
|
||||||
|
|
||||||
|
try(i64 f_size, i, file_getSize(f));
|
||||||
|
Array(u8) buf = Array_u8_alloc(f_size + 1);
|
||||||
|
Defer(if(!success) Array_u8_destroy(&buf));
|
||||||
|
buf.len--;
|
||||||
|
|
||||||
|
try_void(file_readBytesArrayExactly(f, buf));
|
||||||
|
buf.data[buf.len] = '\0';
|
||||||
|
|
||||||
|
*out_str = Array_u8_castTo_str(buf, true);
|
||||||
|
success = true;
|
||||||
|
Return RESULT_VOID;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user