From e82bf941ce3e7394026ba98c484794c1fb86c2ed Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Fri, 30 Mar 2012 21:20:48 -0500 Subject: [PATCH] Update: add file write support The write functions are little more than wrappers to fwrite(). --- level_1/fl_file/c/file.c | 40 ++++++++++++++++++++++++++++++++++++++++ level_1/fl_file/c/file.h | 8 ++++++++ 2 files changed, 48 insertions(+) diff --git a/level_1/fl_file/c/file.c b/level_1/fl_file/c/file.c index c821654..222802c 100644 --- a/level_1/fl_file/c/file.c +++ b/level_1/fl_file/c/file.c @@ -107,6 +107,46 @@ extern "C"{ } #endif // _di_fl_file_read_fifo_ +#ifndef _di_fl_file_write_ + f_return_status fl_file_write(f_file file, const f_dynamic_string buffer) { + if (file.file == 0) return f_file_not_open; + + f_status status = f_status_initialize; + f_size_t size = 0; + + size = fwrite(buffer.string, file.byte_size, buffer.used, file.file); + + if (size <= buffer.used * file.byte_size) { + return f_file_write_error; + } + + return f_none; + } +#endif // _di_fl_file_write_ + +#ifndef _di_fl_file_write_partial_ + f_return_status fl_file_write_partial(f_file file, const f_dynamic_string buffer, const f_string_location position) { + #ifndef _di_level_1_parameter_checking_ + if (position.start < position.stop) return f_invalid_parameter; + #endif // _di_level_1_parameter_checking_ + + if (file.file == 0) return f_file_not_open; + + f_status status = f_status_initialize; + f_size_t size = 0; + + f_string_length total = buffer.used - (position.stop - position.start + 1); + + size = fwrite(buffer.string + position.start, file.byte_size, total, file.file); + + if (size <= total * file.byte_size) { + return f_file_write_error; + } + + return f_none; + } +#endif // _di_fl_file_write_partial_ + #ifdef __cplusplus } // extern "C" #endif diff --git a/level_1/fl_file/c/file.h b/level_1/fl_file/c/file.h index c06e0b9..6f5042d 100644 --- a/level_1/fl_file/c/file.h +++ b/level_1/fl_file/c/file.h @@ -26,6 +26,14 @@ extern "C"{ extern f_return_status fl_file_read_fifo(f_file file, f_dynamic_string *buffer); #endif // _di_fl_file_read_fifo_ +#ifndef _di_fl_file_write_ + extern f_return_status fl_file_write(f_file file, const f_dynamic_string buffer); +#endif // _di_fl_file_write_ + +#ifndef _di_fl_file_write_partial_ + extern f_return_status fl_file_write_partial(f_file file, const f_dynamic_string buffer, const f_string_location position); +#endif // _di_fl_file_write_partial_ + #ifdef __cplusplus } // extern "C" #endif -- 1.8.3.1