From: Kevin Day Date: Mon, 4 May 2020 02:43:46 +0000 (-0500) Subject: Update: make fl_file functions more consistent and update documentation X-Git-Tag: 0.5.0~294 X-Git-Url: https://git.kevux.org/?a=commitdiff_plain;h=28841f350077ee7389418eacade97a3f42b22b19;p=fll Update: make fl_file functions more consistent and update documentation Refactor the fl_file functions. Rewrite memory management in f_file and fl_file functions. Fix bug with size of bytes, renaming it to byte_chunk to avoid confusion. Add block size property to f_file structure to allow custom block size handling (as opposed to the previous behavior of using a hard-coded default). Make sure f_none_on_eof is actually returned. --- diff --git a/build/level_1/settings b/build/level_1/settings index 8ab7eb5..e32bfcd 100644 --- a/build/level_1/settings +++ b/build/level_1/settings @@ -12,9 +12,9 @@ build_linker ar build_libraries -lc build_libraries_fll -lfll_0 build_libraries_fll-level -lfll_0 -build_sources_library color.c console.c directory.c file.c fss.c fss_basic.c fss_basic_list.c fss_extended.c fss_extended_list.c print.c serialized.c private-serialized.c socket.c status.c string.c utf.c private-string.c private-utf.c +build_sources_library color.c console.c directory.c file.c fss.c fss_basic.c fss_basic_list.c fss_extended.c fss_extended_list.c print.c serialized.c private-serialized.c socket.c status.c string.c private-string.c utf.c private-utf.c utf_file.c build_sources_program -build_sources_headers color.h console.h directory.h file.h fss.h fss_basic.h fss_basic_list.h fss_status.h fss_extended.h fss_extended_list.h fss_macro.h print.h serialized.h socket.h status.h string.h utf.h +build_sources_headers color.h console.h directory.h file.h fss.h fss_basic.h fss_basic_list.h fss_status.h fss_extended.h fss_extended_list.h fss_macro.h print.h serialized.h socket.h status.h string.h utf.h utf_file.h build_shared yes build_static yes diff --git a/build/monolithic/settings b/build/monolithic/settings index dec2e3c..c63cf4e 100644 --- a/build/monolithic/settings +++ b/build/monolithic/settings @@ -11,9 +11,9 @@ build_compiler gcc build_linker ar build_libraries -lc build_libraries_fll -build_sources_library level_0/console.c level_0/conversion.c level_0/file.c level_0/memory.c level_0/pipe.c level_0/print.c level_0/utf.c level_0/private-utf.c level_1/color.c level_1/console.c level_1/directory.c level_1/file.c level_1/fss.c level_1/fss_basic.c level_1/fss_basic_list.c level_1/fss_extended.c level_1/fss_extended_list.c level_1/serialized.c level_1/private-serialized.c level_1/socket.c level_1/status.c level_1/string.c level_1/utf.c level_1/print.c level_1/private-string.c level_1/private-utf.c level_2/execute.c level_2/file.c level_2/fss_basic.c level_2/fss_basic_list.c level_2/fss_extended.c level_2/fss_extended_list.c level_2/fss_status.c level_2/program.c level_2/status.c +build_sources_library level_0/console.c level_0/conversion.c level_0/file.c level_0/memory.c level_0/pipe.c level_0/print.c level_0/utf.c level_0/private-utf.c level_1/color.c level_1/console.c level_1/directory.c level_1/file.c level_1/fss.c level_1/fss_basic.c level_1/fss_basic_list.c level_1/fss_extended.c level_1/fss_extended_list.c level_1/print.c level_1/serialized.c level_1/private-serialized.c level_1/socket.c level_1/status.c level_1/string.c level_1/private-string.c level_1/utf.c level_1/private-utf.c level_1/utf_file.c level_2/execute.c level_2/file.c level_2/fss_basic.c level_2/fss_basic_list.c level_2/fss_extended.c level_2/fss_extended_list.c level_2/fss_status.c level_2/program.c level_2/status.c build_sources_program -build_sources_headers level_0/color.h level_0/console.h level_0/conversion.h level_0/file.h level_0/fss.h level_0/memory.h level_0/path_fll.h level_0/path_filesystem.h level_0/pipe.h level_0/print.h level_0/serialized.h level_0/socket.h level_0/status.h level_0/string.h level_0/type.h level_0/type_array.h level_0/utf.h level_1/color.h level_1/console.h level_1/directory.h level_1/file.h level_1/fss.h level_1/fss_basic.h level_1/fss_basic_list.h level_1/fss_status.h level_1/fss_extended.h level_1/fss_extended_list.h level_1/fss_macro.h level_1/serialized.h level_1/socket.h level_1/status.h level_1/string.h level_1/utf.h level_1/print.h level_2/execute.h level_2/file.h level_2/fss_basic.h level_2/fss_basic_list.h level_2/fss_extended.h level_2/fss_extended_list.h level_2/fss_status.h level_2/program.h level_2/status.h +build_sources_headers level_0/color.h level_0/console.h level_0/conversion.h level_0/file.h level_0/fss.h level_0/memory.h level_0/path_fll.h level_0/path_filesystem.h level_0/pipe.h level_0/print.h level_0/serialized.h level_0/socket.h level_0/status.h level_0/string.h level_0/type.h level_0/type_array.h level_0/utf.h level_1/color.h level_1/console.h level_1/directory.h level_1/file.h level_1/fss.h level_1/fss_basic.h level_1/fss_basic_list.h level_1/fss_status.h level_1/fss_extended.h level_1/fss_extended_list.h level_1/fss_macro.h level_1/print.h level_1/serialized.h level_1/socket.h level_1/status.h level_1/string.h level_1/utf.h level_1/utf_file.h level_2/execute.h level_2/file.h level_2/fss_basic.h level_2/fss_basic_list.h level_2/fss_extended.h level_2/fss_extended_list.h level_2/fss_status.h level_2/program.h level_2/status.h build_sources_bash build_sources_settings build_shared yes diff --git a/level_0/f_file/c/file.c b/level_0/f_file/c/file.c index 3c55f0f..ca2f86e 100644 --- a/level_0/f_file/c/file.c +++ b/level_0/f_file/c/file.c @@ -158,17 +158,24 @@ extern "C" { f_return_status f_file_read(f_file *file, f_string_dynamic *buffer) { #ifndef _di_level_0_parameter_checking_ if (file == 0) return f_status_set_error(f_invalid_parameter); + if (file->size_chunk == 0) return f_status_set_error(f_invalid_parameter); + if (file->size_block == 0) return f_status_set_error(f_invalid_parameter); if (buffer->used >= buffer->size) return f_status_set_error(f_invalid_parameter); #endif // _di_level_0_parameter_checking_ if (file->address == 0) return f_status_set_error(f_file_not_open); - int result = fread(buffer->string + buffer->used, file->byte_size, buffer->size - buffer->used - 1, file->address); + int result = fread(buffer->string + buffer->used, file->size_chunk, file->size_block, file->address); if (file->address == 0) return f_status_set_error(f_file_error_read); if (ferror(file->address) != 0) return f_status_set_error(f_file_error_read); - buffer->used += (result / file->byte_size); + if (file->size_chunk > 1) { + buffer->used += result * file->size_chunk; + } + else { + buffer->used += result; + } if (feof(file->address)) { return f_none_on_eof; @@ -182,14 +189,15 @@ extern "C" { f_return_status f_file_read_range(f_file *file, f_string_dynamic *buffer, const f_string_length buffer_start, const f_string_length total_elements) { #ifndef _di_level_0_parameter_checking_ if (file == 0) return f_status_set_error(f_invalid_parameter); + if (file->size_chunk == 0) return f_status_set_error(f_invalid_parameter); + if (file->size_block == 0) return f_status_set_error(f_invalid_parameter); if (buffer->used >= buffer->size) return f_status_set_error(f_invalid_parameter); - if (buffer_start < 0) return f_status_set_error(f_invalid_parameter); - if (total_elements < 0) return f_status_set_error(f_invalid_parameter); + if (total_elements == 0) return f_status_set_error(f_invalid_parameter); - // when the available buffer size is smaller than the total elements, then there is not enough allocated memory available to read the file + // when the available buffer size is smaller than the total elements, then there is not enough allocated memory available to read the file. if (total_elements > 0) { - if (buffer->size - buffer_start < total_elements) return f_status_set_error(f_invalid_parameter); + if (buffer_start + total_elements > buffer->size) return f_status_set_error(f_invalid_parameter); } #endif // _di_level_0_parameter_checking_ @@ -198,21 +206,30 @@ extern "C" { int result = 0; if (total_elements == 0) { - result = fread(buffer->string + buffer_start, file->byte_size, buffer->size - buffer->used - 1, file->address); + result = fread(buffer->string + buffer_start, file->size_chunk, file->size_block, file->address); } else { - result = fread(buffer->string + buffer_start, file->byte_size, total_elements, file->address); + result = fread(buffer->string + buffer_start, file->size_chunk, total_elements, file->address); } if (file->address == 0) return f_status_set_error(f_file_error_read); if (ferror(file->address) != 0) return f_status_set_error(f_file_error_read); + f_string_length bytes_total; + + if (file->size_chunk > 1) { + bytes_total = result * file->size_chunk; + } + else { + bytes_total = result; + } + // Save how much of our allocated buffer is actually used. - if (buffer_start + result > buffer->used) { - buffer->used = buffer_start + (result / file->byte_size); + if (buffer_start + bytes_total > buffer->used) { + buffer->used = buffer_start + bytes_total; } - // Append an EOS only when the total elements were set to 0 + // Append an EOS only when the total elements were set to 0. if (total_elements == 0) { buffer->string[buffer->used] = f_string_eos; } @@ -230,21 +247,22 @@ extern "C" { f_return_status f_file_read_at(f_file *file, f_string_dynamic *buffer, const f_file_position position) { #ifndef _di_level_0_parameter_checking_ if (file == 0) return f_status_set_error(f_invalid_parameter); + if (file->size_chunk == 0) return f_status_set_error(f_invalid_parameter); + if (file->size_block == 0) return f_status_set_error(f_invalid_parameter); if (buffer->used >= buffer->size) return f_status_set_error(f_invalid_parameter); - if (position.buffer_start < 0) return f_status_set_error(f_invalid_parameter); if (position.file_start < 0) return f_status_set_error(f_invalid_parameter); if (position.total_elements < 0) return f_status_set_error(f_invalid_parameter); - // when the available buffer size is smaller than the total elements, then there is not enough allocated memory available to read the file + // when the available buffer size is smaller than the total elements, then there is not enough allocated memory available to read the file. if (position.total_elements > 0) { - if (buffer->size - position.buffer_start < position.total_elements) return f_status_set_error(f_invalid_parameter); + if (position.buffer_start + position.total_elements > buffer->size) return f_status_set_error(f_invalid_parameter); } #endif // _di_level_0_parameter_checking_ if (file->address == 0) return f_status_set_error(f_file_not_open); - // first seek to 'where' we need to begin the read + // first seek to 'where' we need to begin the read. unsigned long current_file_position = ftell(file->address); if (current_file_position == (unsigned long) -1) return f_status_set_error(f_file_error_seek); @@ -252,32 +270,41 @@ extern "C" { int result = 0; if (current_file_position > position.file_start) { - result = f_macro_file_seek_to(file->address, file->byte_size * (0 - (current_file_position - position.file_start))); + result = f_macro_file_seek_to(file->address, file->size_chunk * (0 - (current_file_position - position.file_start))); } else if (current_file_position < position.file_start) { - result = f_macro_file_seek_to(file->address, file->byte_size * (position.file_start - current_file_position)); + result = f_macro_file_seek_to(file->address, file->size_chunk * (position.file_start - current_file_position)); } if (result != 0) return f_status_set_error(f_file_error_seek); // now do the actual read if (position.total_elements == 0) { - result = fread(buffer->string + position.buffer_start, file->byte_size, buffer->size - buffer->used - 1, file->address); + result = fread(buffer->string + position.buffer_start, file->size_chunk, file->size_block, file->address); } else { - result = fread(buffer->string + position.buffer_start, file->byte_size, position.total_elements, file->address); + result = fread(buffer->string + position.buffer_start, file->size_chunk, position.total_elements, file->address); } if (file->address == 0) return f_status_set_error(f_file_error_read); if (ferror(file->address) != 0) return f_status_set_error(f_file_error_read); - // now save how much of our allocated buffer is actually used - // also make sure that we aren't making used space vanish - if (position.buffer_start + result > buffer->used) { - buffer->used = position.buffer_start + (result / file->byte_size); + f_number_unsigned bytes_total; + + if (file->size_chunk > 1) { + bytes_total = result * file->size_chunk; + } + else { + bytes_total = result; + } + + // now save how much of our allocated buffer is actually used. + // also make sure that we aren't making used space vanish. + if (position.buffer_start + bytes_total > buffer->used) { + buffer->used = position.buffer_start + bytes_total; } - // append an EOS only when the total elements were set to 0 + // append an EOS only when the total elements were set to 0. if (position.total_elements == 0) { buffer->string[buffer->used] = f_string_eos; } diff --git a/level_0/f_file/c/file.h b/level_0/f_file/c/file.h index bd345fb..355f9f6 100644 --- a/level_0/f_file/c/file.h +++ b/level_0/f_file/c/file.h @@ -38,7 +38,7 @@ extern "C" { typedef f_string f_file_mode; typedef mode_t f_file_mask; - #define f_file_default_read_size 4096 // default to 4k read sizes. + #define f_file_default_read_size 8192 // default to 8k read sizes. #define f_file_max_path_length 1024 #endif // _di_f_file_types_ @@ -73,19 +73,21 @@ extern "C" { * Commonly used file related properties. * * id: File descriptor. - * byte_size: How many bytes to use on each read/write (for normal string handling this should be sizeof(f_string). * address: A pointer to a file (generally opened). * mode: How the file is to be accessed (or is being accessed). + * size_chunk: Number of bytes to consider a character, a value of 1 means 1-byte (aka: uint8_t) (for normal string handling this should be sizeof(f_string)). + * size_block: The default number of chunks to read at a time (use (size_chunk * size_block) to determine total number of bytes). */ #ifndef _di_f_file_ typedef struct { - f_file_id id; - size_t byte_size; - FILE * address; - f_file_mode mode; + f_file_id id; + FILE * address; + f_file_mode mode; + size_t size_chunk; + f_number_unsigned size_block; } f_file; - #define f_file_initialize { 0, 1, 0, (f_file_mode) f_file_read_only } + #define f_file_initialize { 0, 0, (f_file_mode) f_file_read_only, 1, f_file_default_read_size } #endif // _di_f_file_ /** @@ -95,8 +97,7 @@ extern "C" { * * buffer_start: Designate where to start writing to the buffer. * file_start: The positions where to begin reading the file. - * total_elements: The total number of elements to read from the file into the buffer. - * if total_elements: If set to 0, then this means to buffer the entire file no matter how big it is (crazy?). + * total_elements: The total number of elements to read from the file into the buffer (set to 0 to read entire file). */ #ifndef _di_f_file_position_ typedef struct { @@ -349,7 +350,9 @@ extern "C" { #endif // _di_f_file_flush_ /** - * Read a until the entire buffer is filled or EOF is reached. + * Read until a single block is filled or EOF is reached. + * + * This does not allocate space to the buffer, so be sure enough space exists (file->size_chunk * file->size_block). * * @param file * The file to read. @@ -368,7 +371,9 @@ extern "C" { #endif // _di_f_file_read_ /** - * Read a given amount of data from the buffer, specified by the given range. + * Read until a single block is filled or EOF is reached, storing it into a specific range within the buffer. + * + * This does not allocate space to the buffer, so be sure enough space exists (file->size_chunk * file->size_block). * * @param file * The file to read. @@ -392,7 +397,9 @@ extern "C" { #endif // _di_f_file_read_range_ /** - * Read a given amount of data from the buffer, specified by the given range. + * Read until a single block is filled or EOF is reached, specified by the given range within the file, storing it in the buffer. + * + * This does not allocate space to the buffer, so be sure enough space exists (file->size_chunk * file->size_block). * * Will auto-seek file position to position.file_start. * diff --git a/level_1/fl_file/c/file.c b/level_1/fl_file/c/file.c index 911f776..b70dd02 100644 --- a/level_1/fl_file/c/file.c +++ b/level_1/fl_file/c/file.c @@ -5,144 +5,125 @@ extern "C" { #endif #ifndef _di_fl_file_read_ - f_return_status fl_file_read(f_file file, const f_file_position position, f_string_dynamic *buffer) { + f_return_status fl_file_read(f_file *file, f_string_dynamic *buffer) { #ifndef _di_level_1_parameter_checking_ + if (file == 0) return f_status_set_error(f_invalid_parameter); if (buffer == 0) return f_status_set_error(f_invalid_parameter); - - if (position.buffer_start < 0) return f_status_set_error(f_invalid_parameter); - if (position.file_start < 0) return f_status_set_error(f_invalid_parameter); - if (position.total_elements < 0) return f_status_set_error(f_invalid_parameter); #endif // _di_level_1_parameter_checking_ - if (file.address == 0) return f_status_set_warning(f_file_not_open); + if (file->address == 0) return f_status_set_error(f_file_not_open); + + const f_number_unsigned bytes_total = file->size_block * file->size_chunk; f_status status = f_none; f_string_length size = 0; - bool infinite = f_false; - - // when total_elements is 0, this means the file read will until EOF is reached - if (position.total_elements == 0) { - infinite = f_true; - size = f_file_default_read_size; - } - else { - size = position.total_elements; - } - // populate the buffer - do { - if (buffer->size <= size) { - f_macro_string_dynamic_resize(status, (*buffer), size); + size = file->size_block; - if (f_status_is_error(status)) { - return status; - } - } + for (;;) { + if (buffer->used + bytes_total > buffer->size) { + if (buffer->used + bytes_total > f_string_max_size) return f_status_set_error(f_string_too_large); - status = f_file_read_at(&file, buffer, position); + f_macro_string_dynamic_resize(status, (*buffer), buffer->used + bytes_total); - if (status == f_none_on_eof) { - break; - } - else if (f_status_is_error(status)) { - return status; + if (f_status_is_error(status)) return status; } - if (infinite) { - if (size + f_file_default_read_size > f_string_max_size) { - return f_status_set_error(f_number_overflow); - } + status = f_file_read(file, buffer); - size += f_file_default_read_size; - } - } while (infinite); + if (f_status_is_error(status)) return status; + if (status == f_none_on_eof) break; - return f_none; + size += file->size_block; + } // for + + return status; } #endif // _di_fl_file_read_ -#ifndef _di_fl_file_read_fifo_ - f_return_status fl_file_read_fifo(f_file file, f_string_dynamic *buffer) { +#ifndef _di_fl_file_read_position + f_return_status fl_file_read_position(f_file *file, f_string_dynamic *buffer, const f_file_position position) { #ifndef _di_level_1_parameter_checking_ + if (file == 0) return f_status_set_error(f_invalid_parameter); if (buffer == 0) return f_status_set_error(f_invalid_parameter); + if (position.buffer_start < 0) return f_status_set_error(f_invalid_parameter); + if (position.file_start < 0) return f_status_set_error(f_invalid_parameter); + if (position.total_elements < 0) return f_status_set_error(f_invalid_parameter); #endif // _di_level_1_parameter_checking_ - if (file.address == 0) return f_status_set_warning(f_file_not_open); + if (file->address == 0) return f_status_set_error(f_file_not_open); f_status status = f_none; - f_string_length size = 0; + bool infinite = f_false; - size = f_file_default_read_size; + f_number_unsigned bytes_total; - // populate the buffer - do { - if (buffer->size <= size) { - f_macro_string_dynamic_resize(status, (*buffer), size); + // when total_elements is 0, this means the file read will until EOF is reached. + if (position.total_elements == 0) { + infinite = f_true; + bytes_total = file->size_block * file->size_chunk; + } + else { + bytes_total = position.total_elements * file->size_chunk; + } - if (f_status_is_error(status)) { - return status; - } - } + do { + if (buffer->used + bytes_total > buffer->size) { + if (buffer->used + bytes_total > f_string_max_size) return f_status_set_error(f_string_too_large); - status = f_file_read(&file, buffer); + f_macro_string_dynamic_resize(status, (*buffer), buffer->used + bytes_total); - if (status == f_none_on_eof) { - break; - } - else if (f_status_is_error(status)) { - return status; + if (f_status_is_error(status)) return status; } - if (size + f_file_default_read_size > f_string_max_size) { - return f_status_set_error(f_number_overflow); - } + status = f_file_read_at(file, buffer, position); - size += f_file_default_read_size; - } while (f_true); + if (f_status_is_error(status)) return status; + if (status == f_none_on_eof) break; + } while (infinite); - return f_none; + return status; } -#endif // _di_fl_file_read_fifo_ +#endif // _di_fl_file_read_position #ifndef _di_fl_file_write_ - f_return_status fl_file_write(f_file file, const f_string_dynamic buffer) { - if (file.address == 0) return f_status_set_error(f_file_not_open); + f_return_status fl_file_write(f_file *file, const f_string_dynamic buffer) { + #ifndef _di_level_1_parameter_checking_ + if (file == 0) return f_status_set_error(f_invalid_parameter); + #endif // _di_level_1_parameter_checking_ + + if (file->address == 0) return f_status_set_error(f_file_not_open); - f_status status = f_none; size_t size = 0; - size = fwrite(buffer.string, file.byte_size, buffer.used, file.address); + size = fwrite(buffer.string, file->size_chunk, buffer.used, file->address); - if (size < buffer.used * file.byte_size) { - return f_status_set_error(f_file_error_write); - } + if (size < buffer.used * file->size_chunk) return f_status_set_error(f_file_error_write); 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_string_dynamic buffer, const f_string_location position) { +#ifndef _di_fl_file_write_position_ + f_return_status fl_file_write_position(f_file *file, const f_string_dynamic buffer, const f_string_location position) { #ifndef _di_level_1_parameter_checking_ + if (file == 0) return f_status_set_error(f_invalid_parameter); if (position.start < position.stop) return f_status_set_error(f_invalid_parameter); #endif // _di_level_1_parameter_checking_ - if (file.address == 0) return f_file_not_open; + if (file->address == 0) return f_file_not_open; - f_status status = f_none; 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.address); + size = fwrite(buffer.string + position.start, file->size_chunk, total, file->address); - if (size < total * file.byte_size) { - return f_status_set_error(f_file_error_write); - } + if (size < total * file->size_chunk) return f_status_set_error(f_file_error_write); return f_none; } -#endif // _di_fl_file_write_partial_ +#endif // _di_fl_file_write_position_ #ifdef __cplusplus } // extern "C" diff --git a/level_1/fl_file/c/file.h b/level_1/fl_file/c/file.h index f0d5777..65aded3 100644 --- a/level_1/fl_file/c/file.h +++ b/level_1/fl_file/c/file.h @@ -19,33 +19,89 @@ extern "C" { #endif +/** + * Load entire file into buffer. + * + * @param file + * The file to read from. + * @param buffer + * The buffer to load the file into. + * + * @return + * f_none on success. + * f_none_on_eof on success and EOF was reached. + * f_file_not_open (with error bit) if file is not open. + * f_file_error_seek (with error bit) if file seek failed. + * f_file_error_read (with error bit) if file read failed. + * f_invalid_parameter (with error bit) if a parameter is invalid. + * f_error_reallocation (with error bit) on memory reallocation error. + * f_string_too_large (with error bit) if string is too large to fit into buffer. + */ #ifndef _di_fl_file_read_ - /** - * read file, specific positions. - */ - extern f_return_status fl_file_read(f_file file, const f_file_position position, f_string_dynamic *buffer); + extern f_return_status fl_file_read(f_file *file, f_string_dynamic *buffer); #endif // _di_fl_file_read_ -#ifndef _di_fl_file_read_fifo_ - /** - * read file, entire file. - */ - extern f_return_status fl_file_read_fifo(f_file file, f_string_dynamic *buffer); -#endif // _di_fl_file_read_fifo_ +/** + * Load file into buffer, based on specified positions. + * + * @param file + * The file to read from. + * @param buffer + * The buffer to save the file. + * @param position + * The file position to base reading off of. + * + * @return + * f_none on success. + * f_none_on_eof on success and EOF was reached. + * f_file_not_open (with error bit) if file is not open. + * f_file_error_seek (with error bit) if file seek failed. + * f_file_error_read (with error bit) if file read failed. + * f_invalid_parameter (with error bit) if a parameter is invalid. + * f_error_reallocation (with error bit) on memory reallocation error. + * f_string_too_large (with error bit) if string is too large to fit into buffer. + */ +#ifndef _di_fl_file_read_position_ + extern f_return_status fl_file_read_position(f_file *file, f_string_dynamic *buffer, const f_file_position position); +#endif // _di_fl_file_read_position +/** + * Save entire buffer into file. + * + * @param file + * The file to save to. + * @param buffer + * The buffer to save to the file. + * + * @return + * f_none on success. + * f_file_not_open (with error bit) if file is not open. + * f_file_error_write (with error bit) if write failed. + * f_invalid_parameter (with error bit) if a parameter is invalid. + */ #ifndef _di_fl_file_write_ - /** - * write file, entire file. - */ - extern f_return_status fl_file_write(f_file file, const f_string_dynamic buffer); + extern f_return_status fl_file_write(f_file *file, const f_string_dynamic buffer); #endif // _di_fl_file_write_ -#ifndef _di_fl_file_write_partial_ - /** - * write file, specific positions. - */ - extern f_return_status fl_file_write_partial(f_file file, const f_string_dynamic buffer, const f_string_location position); -#endif // _di_fl_file_write_partial_ +/** + * Save entire buffer into file, based on specified positions. + * + * @param file + * The file to save to. + * @param buffer + * The buffer to save to the file. + * @param position + * The file position to base writing off of. + * + * @return + * f_none on success. + * f_file_not_open (with error bit) if file is not open. + * f_file_error_write (with error bit) if write failed. + * f_invalid_parameter (with error bit) if a parameter is invalid. + */ +#ifndef _di_fl_file_write_position_ + extern f_return_status fl_file_write_position(f_file *file, const f_string_dynamic buffer, const f_string_location position); +#endif // _di_fl_file_write_position_ #ifdef __cplusplus } // extern "C" diff --git a/level_3/firewall/c/private-firewall.c b/level_3/firewall/c/private-firewall.c index 5d1df25..e96939a 100644 --- a/level_3/firewall/c/private-firewall.c +++ b/level_3/firewall/c/private-firewall.c @@ -619,7 +619,7 @@ f_return_status firewall_perform_commands(const firewall_local_data local, const fseek(file.address, 0, SEEK_SET); } - status = fl_file_read(file, file_position, &local_buffer); + status = fl_file_read_position(&file, &local_buffer, file_position); f_file_close(&file); @@ -627,7 +627,7 @@ f_return_status firewall_perform_commands(const firewall_local_data local, const status = f_status_set_fine(status); if (status == f_invalid_parameter) { - fl_color_print_line(f_standard_error, data.context.error, data.context.reset, "INTERNAL ERROR: Invalid parameter when calling fl_file_read()"); + fl_color_print_line(f_standard_error, data.context.error, data.context.reset, "INTERNAL ERROR: Invalid parameter when calling fl_file_read_position()"); } else if (status == f_number_overflow) { fl_color_print_line(f_standard_error, data.context.error, data.context.reset, "ERROR: Integer overflow while trying to buffer the file '%.*s'", file_path.used, file_path.string); @@ -645,7 +645,7 @@ f_return_status firewall_perform_commands(const firewall_local_data local, const fl_color_print_line(f_standard_error, data.context.error, data.context.reset, "CRITICAL ERROR: Unable to allocate memory."); } else { - fl_color_print_line(f_standard_error, data.context.error, data.context.reset, "INTERNAL ERROR: An unhandled error (%u) has occurred while calling fl_file_read()", status); + fl_color_print_line(f_standard_error, data.context.error, data.context.reset, "INTERNAL ERROR: An unhandled error (%u) has occurred while calling fl_file_read_position()", status); } status = f_status_set_error(status); @@ -1377,7 +1377,7 @@ f_return_status firewall_buffer_rules(const f_string filename, const bool option f_macro_file_reset_position(local->file_position, file) fflush(stdout); - status = fl_file_read(file, local->file_position, &local->buffer); + status = fl_file_read_position(&file, &local->buffer, local->file_position); f_file_close(&file); @@ -1385,7 +1385,7 @@ f_return_status firewall_buffer_rules(const f_string filename, const bool option status = f_status_set_fine(status); if (status == f_invalid_parameter) { - fl_color_print_line(f_standard_error, data->context.error, data->context.reset, "INTERNAL ERROR: Invalid parameter when calling fl_file_read()."); + fl_color_print_line(f_standard_error, data->context.error, data->context.reset, "INTERNAL ERROR: Invalid parameter when calling fl_file_read_position()."); } else if (status == f_number_overflow) { fl_color_print_line(f_standard_error, data->context.error, data->context.reset, "ERROR: Integer overflow while trying to buffer the file '%s'.", filename); @@ -1403,7 +1403,7 @@ f_return_status firewall_buffer_rules(const f_string filename, const bool option fl_color_print_line(f_standard_error, data->context.error, data->context.reset, "CRITICAL ERROR: Unable to allocate memory."); } else { - fl_color_print_line(f_standard_error, data->context.error, data->context.reset, "INTERNAL ERROR: An unhandled error (%u) has occurred while calling fl_file_read().", status); + fl_color_print_line(f_standard_error, data->context.error, data->context.reset, "INTERNAL ERROR: An unhandled error (%u) has occurred while calling fl_file_read_position().", status); } return status; diff --git a/level_3/fss_basic_list_read/c/fss_basic_list_read.c b/level_3/fss_basic_list_read/c/fss_basic_list_read.c index 4636609..93d8366 100644 --- a/level_3/fss_basic_list_read/c/fss_basic_list_read.c +++ b/level_3/fss_basic_list_read/c/fss_basic_list_read.c @@ -271,10 +271,10 @@ extern "C" { file.address = f_pipe; - status = fl_file_read_fifo(file, &data->buffer); + status = fl_file_read(&file, &data->buffer); if (f_status_is_error(status)) { - fss_basic_list_read_print_file_error(data->context, "fl_file_read_fifo", "-", f_status_set_fine(status)); + fss_basic_list_read_print_file_error(data->context, "fl_file_read", "-", f_status_set_fine(status)); macro_fss_basic_list_read_depths_delete_simple(depths); fss_basic_list_read_delete_data(data); @@ -324,12 +324,12 @@ extern "C" { fseek(file.address, 0, SEEK_SET); } - status = fl_file_read(file, data->file_position, &data->buffer); + status = fl_file_read_position(&file, &data->buffer, data->file_position); f_file_close(&file); if (f_status_is_error(status)) { - fss_basic_list_read_print_file_error(data->context, "fl_file_read", arguments.argv[data->remaining.array[counter]], f_status_set_fine(status)); + fss_basic_list_read_print_file_error(data->context, "fl_file_read_position", arguments.argv[data->remaining.array[counter]], f_status_set_fine(status)); macro_fss_basic_list_read_depths_delete_simple(depths); fss_basic_list_read_delete_data(data); return status; diff --git a/level_3/fss_basic_list_write/c/fss_basic_list_write.c b/level_3/fss_basic_list_write/c/fss_basic_list_write.c index c81311c..74f59a4 100644 --- a/level_3/fss_basic_list_write/c/fss_basic_list_write.c +++ b/level_3/fss_basic_list_write/c/fss_basic_list_write.c @@ -64,7 +64,7 @@ extern "C" { file.address = f_pipe; - status = fl_file_read_fifo(file, &input); + status = fl_file_read(&file, &input); if (f_status_is_error(status)) { status = f_status_set_fine(status); diff --git a/level_3/fss_basic_read/c/fss_basic_read.c b/level_3/fss_basic_read/c/fss_basic_read.c index 06d7728..8f37d38 100644 --- a/level_3/fss_basic_read/c/fss_basic_read.c +++ b/level_3/fss_basic_read/c/fss_basic_read.c @@ -271,10 +271,10 @@ extern "C" { file.address = f_pipe; - status = fl_file_read_fifo(file, &data->buffer); + status = fl_file_read(&file, &data->buffer); if (f_status_is_error(status)) { - fss_basic_read_print_file_error(data->context, "fl_file_read_fifo", "-", f_status_set_fine(status)); + fss_basic_read_print_file_error(data->context, "fl_file_read", "-", f_status_set_fine(status)); macro_fss_basic_read_depths_delete_simple(depths); fss_basic_read_delete_data(data); @@ -325,12 +325,12 @@ extern "C" { fseek(file.address, 0, SEEK_SET); } - status = fl_file_read(file, data->file_position, &data->buffer); + status = fl_file_read_position(&file, &data->buffer, data->file_position); f_file_close(&file); if (f_status_is_error(status)) { - fss_basic_read_print_file_error(data->context, "fl_file_read", arguments.argv[data->remaining.array[counter]], f_status_set_fine(status)); + fss_basic_read_print_file_error(data->context, "fl_file_read_position", arguments.argv[data->remaining.array[counter]], f_status_set_fine(status)); macro_fss_basic_read_depths_delete_simple(depths); fss_basic_read_delete_data(data); diff --git a/level_3/fss_basic_write/c/fss_basic_write.c b/level_3/fss_basic_write/c/fss_basic_write.c index 743bbfa..dc93fde 100644 --- a/level_3/fss_basic_write/c/fss_basic_write.c +++ b/level_3/fss_basic_write/c/fss_basic_write.c @@ -64,7 +64,7 @@ extern "C" { file.address = f_pipe; - status = fl_file_read_fifo(file, &input); + status = fl_file_read(&file, &input); if (f_status_is_error(status)) { status = f_status_set_fine(status); diff --git a/level_3/fss_extended_list_read/c/fss_extended_list_read.c b/level_3/fss_extended_list_read/c/fss_extended_list_read.c index dcf8e81..b7fc399 100644 --- a/level_3/fss_extended_list_read/c/fss_extended_list_read.c +++ b/level_3/fss_extended_list_read/c/fss_extended_list_read.c @@ -256,10 +256,10 @@ extern "C" { file.address = f_pipe; - status = fl_file_read_fifo(file, &data->buffer); + status = fl_file_read(&file, &data->buffer); if (f_status_is_error(status)) { - fss_extended_list_read_print_file_error(data->context, "fl_file_read_fifo", "-", f_status_set_fine(status)); + fss_extended_list_read_print_file_error(data->context, "fl_file_read", "-", f_status_set_fine(status)); macro_fss_extended_list_read_depths_delete_simple(depths); fss_extended_list_read_delete_data(data); @@ -309,12 +309,12 @@ extern "C" { fseek(file.address, 0, SEEK_SET); } - status = fl_file_read(file, data->file_position, &data->buffer); + status = fl_file_read_position(&file, &data->buffer, data->file_position); f_file_close(&file); if (f_status_is_error(status)) { - fss_extended_list_read_print_file_error(data->context, "fl_file_read", arguments.argv[data->remaining.array[counter]], f_status_set_fine(status)); + fss_extended_list_read_print_file_error(data->context, "fl_file_read_position", arguments.argv[data->remaining.array[counter]], f_status_set_fine(status)); macro_fss_extended_list_read_depths_delete_simple(depths); fss_extended_list_read_delete_data(data); diff --git a/level_3/fss_extended_read/c/fss_extended_read.c b/level_3/fss_extended_read/c/fss_extended_read.c index 7d9c8dc..2cd3dbb 100644 --- a/level_3/fss_extended_read/c/fss_extended_read.c +++ b/level_3/fss_extended_read/c/fss_extended_read.c @@ -271,10 +271,10 @@ extern "C" { file.address = f_pipe; - status = fl_file_read_fifo(file, &data->buffer); + status = fl_file_read(&file, &data->buffer); if (f_status_is_error(status)) { - fss_extended_read_print_file_error(data->context, "fl_file_read_fifo", "-", f_status_set_fine(status)); + fss_extended_read_print_file_error(data->context, "fl_file_read", "-", f_status_set_fine(status)); macro_fss_extended_read_depths_delete_simple(depths); fss_extended_read_delete_data(data); @@ -325,12 +325,12 @@ extern "C" { fseek(file.address, 0, SEEK_SET); } - status = fl_file_read(file, data->file_position, &data->buffer); + status = fl_file_read_position(&file, &data->buffer, data->file_position); f_file_close(&file); if (f_status_is_error(status)) { - fss_extended_read_print_file_error(data->context, "fl_file_read", arguments.argv[data->remaining.array[counter]], f_status_set_fine(status)); + fss_extended_read_print_file_error(data->context, "fl_file_read_position", arguments.argv[data->remaining.array[counter]], f_status_set_fine(status)); macro_fss_extended_read_depths_delete_simple(depths); fss_extended_read_delete_data(data); diff --git a/level_3/fss_extended_write/c/fss_extended_write.c b/level_3/fss_extended_write/c/fss_extended_write.c index 812d41a..f71b3ca 100644 --- a/level_3/fss_extended_write/c/fss_extended_write.c +++ b/level_3/fss_extended_write/c/fss_extended_write.c @@ -91,7 +91,7 @@ extern "C" { file.address = f_pipe; - status = fl_file_read_fifo(file, &input); + status = fl_file_read(&file, &input); if (f_status_is_error(status)) { status = f_status_set_fine(status); diff --git a/level_3/init/c/private-init.c b/level_3/init/c/private-init.c index fc05fda..deaaa92 100644 --- a/level_3/init/c/private-init.c +++ b/level_3/init/c/private-init.c @@ -41,7 +41,7 @@ f_macro_file_reset_position(file_position, file) fflush(stdout); - status = fl_file_read(file, file_position, buffer); + status = fl_file_read_position(&file, buffer, file_position); f_file_close(&file); @@ -49,7 +49,7 @@ status = f_status_set_fine(status); if (status == f_invalid_parameter) { - fl_color_print_line(f_standard_error, data.context.error, data.context.reset, "INTERNAL ERROR: Invalid parameter when calling fl_file_read()."); + fl_color_print_line(f_standard_error, data.context.error, data.context.reset, "INTERNAL ERROR: Invalid parameter when calling fl_file_read_position()."); } else if (status == f_number_overflow) { fl_color_print_line(f_standard_error, data.context.error, data.context.reset, "ERROR: Integer overflow while trying to buffer the file '%s'.", filename); } else if (status == f_file_not_open) { @@ -61,7 +61,7 @@ } else if (status == f_error_allocation || status == f_error_reallocation) { fl_color_print_line(f_standard_error, data.context.error, data.context.reset, "CRITICAL ERROR: Unable to allocate memory."); } else { - fl_color_print_line(f_standard_error, data.context.error, data.context.reset, "INTERNAL ERROR: An unhandled error (%u) has occurred while calling fl_file_read().", status); + fl_color_print_line(f_standard_error, data.context.error, data.context.reset, "INTERNAL ERROR: An unhandled error (%u) has occurred while calling fl_file_read_position().", status); } return f_status_set_error(status);