From 7b23dfbcd848ef7bddd1150f98c19e912e35409e Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Sat, 1 Jan 2022 22:03:24 -0600 Subject: [PATCH] Update: f_file project. Now that I am moving towards using pointer constants (sch as "int * const"), I can move many of the parameters to the left side. I believe this sufficiently follows the pattern of having constants on the left and editables on the right even though the data pointed to can be edited. I feel this allows me to relax the compromise I originally made when following this design paradigm. --- level_0/f_file/c/file-common.h | 24 ++++++++++++++---------- level_0/f_file/c/file.c | 18 +++++++++++++----- level_0/f_file/c/file.h | 4 ++-- level_0/f_file/c/private-file.c | 30 ++++++++++++++++-------------- level_0/f_file/c/private-file.h | 7 ++++--- 5 files changed, 49 insertions(+), 34 deletions(-) diff --git a/level_0/f_file/c/file-common.h b/level_0/f_file/c/file-common.h index e98d0b5..0cc7ef4 100644 --- a/level_0/f_file/c/file-common.h +++ b/level_0/f_file/c/file-common.h @@ -17,12 +17,16 @@ extern "C" { #endif /** - * Provide common file-typ specific data types. + * Provide file defaults. + * + * F_file_default_*: + * - read_size: Default read size in bytes. + * - write_size: Default write size in bytes. */ -#ifndef _di_f_file_types_ - #define F_file_default_read_size_d 8192 // default to 8k read sizes. // @fixme rename and move into _di_f_file_type_ - #define F_file_default_write_size_d 8192 // default to 8k write sizes. // @fixme rename and move into _di_f_file_type_ -#endif // _di_f_file_types_ +#ifndef _di_f_file_defaults_ + #define F_file_default_read_size_d 8192 + #define F_file_default_write_size_d 8192 +#endif // _di_f_file_defaults_ /** * Provide macros for file-seek operations. @@ -30,11 +34,11 @@ extern "C" { * The fseek() function parameters can be confusing, so provide a hopefully more readibly code via these macros. * * macro_f_file_seek_*: - * - begin: sets the file pointer from this many bytes from the beginning of the file. - * - data: sets the file pointer from this many bytes from the end of the file, relative to the next data. - * - end: sets the file pointer from this many bytes from the end of the file. - * - hole: sets the file pointer from this many bytes from the end of the file, relative to the next hole. - * - to: sets the file pointer from this many bytes relative to the current position. + * - begin: sets the file pointer from this many bytes from the beginning of the file. + * - data: sets the file pointer from this many bytes from the end of the file, relative to the next data. + * - end: sets the file pointer from this many bytes from the end of the file. + * - hole: sets the file pointer from this many bytes from the end of the file, relative to the next hole. + * - to: sets the file pointer from this many bytes relative to the current position. */ #ifndef _di_f_file_seeks_ #define macro_f_file_seek_begin(file, bytes) fseek(file, bytes, SEEK_SET) diff --git a/level_0/f_file/c/file.c b/level_0/f_file/c/file.c index 23853c1..eec25d1 100644 --- a/level_0/f_file/c/file.c +++ b/level_0/f_file/c/file.c @@ -84,14 +84,22 @@ extern "C" { #endif // _di_f_file_clone_ #ifndef _di_f_file_close_ - f_status_t f_file_close(int *id) { - return private_f_file_close(F_false, id); + f_status_t f_file_close(int * const id) { + #ifndef _di_level_0_parameter_checking_ + if (!id) return F_status_set_error(F_parameter); + #endif // _di_level_0_parameter_checking_ + + return private_f_file_close(id, F_false); } #endif // _di_f_file_close_ #ifndef _di_f_file_close_flush_ - f_status_t f_file_close_flush(int *id) { - return private_f_file_close(F_true, id); + f_status_t f_file_close_flush(int * const id) { + #ifndef _di_level_0_parameter_checking_ + if (!id) return F_status_set_error(F_parameter); + #endif // _di_level_0_parameter_checking_ + + return private_f_file_close(id, F_true); } #endif // _di_f_file_close_flush_ @@ -1893,7 +1901,7 @@ extern "C" { } if (complete) { - return private_f_file_close(F_true, &file->id); + return private_f_file_close(&file->id, F_true); } return F_none; diff --git a/level_0/f_file/c/file.h b/level_0/f_file/c/file.h index 27c5f2b..e0885bd 100644 --- a/level_0/f_file/c/file.h +++ b/level_0/f_file/c/file.h @@ -145,7 +145,7 @@ extern "C" { * @see fclose() */ #ifndef _di_f_file_close_ - extern f_status_t f_file_close(int *id); + extern f_status_t f_file_close(int * const id); #endif // _di_f_file_close_ /** @@ -170,7 +170,7 @@ extern "C" { * @see fclose() */ #ifndef _di_f_file_close_flush_ - extern f_status_t f_file_close_flush(int *id); + extern f_status_t f_file_close_flush(int * const id); #endif // _di_f_file_close_flush_ /** diff --git a/level_0/f_file/c/private-file.c b/level_0/f_file/c/private-file.c index 0d9d858..2353a47 100644 --- a/level_0/f_file/c/private-file.c +++ b/level_0/f_file/c/private-file.c @@ -6,9 +6,9 @@ extern "C" { #endif #if !defined(_di_f_file_close_) || !defined(_di_f_file_copy_) || !defined(_di_f_file_stream_close_) - f_status_t private_f_file_close(const bool flush, int *id) { + f_status_t private_f_file_close(int * const id, const bool flush) { - if (id == 0 || *id == -1) { + if (*id == -1) { return F_none; } @@ -53,7 +53,8 @@ extern "C" { status = private_f_file_open(destination, 0, &file_destination); if (F_status_is_error(status)) { - private_f_file_close(F_true, &file_source.id); + private_f_file_close(&file_source.id, F_true); + return status; } @@ -68,15 +69,15 @@ extern "C" { size_write = write(file_destination.id, buffer, size_read); if (size_write < 0 || size_write != size_read) { - private_f_file_close(F_true, &file_destination.id); - private_f_file_close(F_true, &file_source.id); + private_f_file_close(&file_destination.id, F_true); + private_f_file_close(&file_source.id, F_true); return F_status_set_error(F_file_write); } } // while - private_f_file_close(F_true, &file_destination.id); - private_f_file_close(F_true, &file_source.id); + private_f_file_close(&file_destination.id, F_true); + private_f_file_close(&file_source.id, F_true); if (size_read < 0) { return F_status_set_error(F_file_read); @@ -101,7 +102,8 @@ extern "C" { status = private_f_file_open_at(at_id, destination, 0, &file_destination); if (F_status_is_error(status)) { - private_f_file_close(F_true, &file_source.id); + private_f_file_close(&file_source.id, F_true); + return status; } @@ -116,15 +118,15 @@ extern "C" { size_write = write(file_destination.id, buffer, size_read); if (size_write < 0 || size_write != size_read) { - private_f_file_close(F_true, &file_destination.id); - private_f_file_close(F_true, &file_source.id); + private_f_file_close(&file_destination.id, F_true); + private_f_file_close(&file_source.id, F_true); return F_status_set_error(F_file_write); } } // while - private_f_file_close(F_true, &file_destination.id); - private_f_file_close(F_true, &file_source.id); + private_f_file_close(&file_destination.id, F_true); + private_f_file_close(&file_source.id, F_true); if (size_read < 0) { return F_status_set_error(F_file_read); @@ -148,7 +150,7 @@ extern "C" { const f_status_t status = private_f_file_open(path, mode, &file); if (file.id != -1) { - return private_f_file_close(F_true, &file.id); + return private_f_file_close(&file.id, F_true); } return status; @@ -169,7 +171,7 @@ extern "C" { const f_status_t status = private_f_file_open_at(at_id, path, mode, &file); if (file.id != -1) { - return private_f_file_close(F_true, &file.id); + return private_f_file_close(&file.id, F_true); } return status; diff --git a/level_0/f_file/c/private-file.h b/level_0/f_file/c/private-file.h index b45e359..45cfa13 100644 --- a/level_0/f_file/c/private-file.h +++ b/level_0/f_file/c/private-file.h @@ -20,11 +20,12 @@ extern "C" { * * Intended to be shared to each of the different implementation variations. * + * @param id + * The file descriptor. + * The value gets set to -1. * @param flush * If TRUE, then perform flush before closing. * If FALSE, then do not perform flush before closing. - * @param id - * The file descriptor. * * @return * F_none on success. @@ -43,7 +44,7 @@ extern "C" { * @see f_file_stream_close() */ #if !defined(_di_f_file_close_) || !defined(_di_f_file_copy_) || !defined(_di_f_file_stream_close_) - extern f_status_t private_f_file_close(const bool flush, int *id) F_attribute_visibility_internal_d; + extern f_status_t private_f_file_close(int * const id, const bool flush) F_attribute_visibility_internal_d; #endif // !defined(_di_f_file_close_) || !defined(_di_f_file_copy_) || !defined(_di_f_file_stream_close_) /** -- 1.8.3.1