From: Kevin Day Date: Sun, 27 Sep 2020 01:41:40 +0000 (-0500) Subject: Progress: add additional f_file stream functions, improve existing f_file functions... X-Git-Tag: 0.5.1~54 X-Git-Url: https://git.kevux.org/?a=commitdiff_plain;h=fa6e05074085b99e3bb4e209b2a2c14aa39e8527;p=fll Progress: add additional f_file stream functions, improve existing f_file functions, and continue print changes. The stream read and write functions should also be implemented. This is just an off the top of my mind implementation and may need to be reviewed at a later date. Change the existing f_file functions to re-use the existing string structure to avoid the more wasteful memset and memcopy from the static array. This design now, instead, has a cost of potentially allocating more memory than needed at the size specified by file.size_read. Continue the print changes. There is going to need to be work done in regards to how I am reading opening and reading streams. I started to do this in private-fake.c but then noticed some things that needed review, such as the path.current and path.top. --- diff --git a/level_0/f_file/c/file.c b/level_0/f_file/c/file.c index b72d0f0..6795fdd 100644 --- a/level_0/f_file/c/file.c +++ b/level_0/f_file/c/file.c @@ -1454,42 +1454,41 @@ extern "C" { f_status_t status = F_none; ssize_t size_read = 0; - char buffer_read[file.size_read]; + for (f_string_t buffer_read = 0; ;) { - memset(&buffer_read, 0, sizeof(file.size_read)); - - while ((size_read = read(file.id, buffer_read, file.size_read)) > 0) { - - if (buffer->used + size_read > buffer->size) { - if (buffer->size + size_read > f_string_length_t_size) { + if (buffer->used + file.size_read > buffer->size) { + if (buffer->size + file.size_read > f_string_length_t_size) { return F_status_set_error(F_string_too_large); } - f_macro_string_dynamic_t_resize(status, (*buffer), buffer->size + size_read); + f_macro_string_dynamic_t_resize(status, (*buffer), buffer->size + file.size_read); if (F_status_is_error(status)) return status; + + memset(buffer->string + buffer->used, 0, sizeof(file.size_read)); } - memcpy(buffer->string + buffer->used, buffer_read, size_read); - buffer->used += size_read; - } // while + buffer_read = buffer->string + buffer->used; - if (!size_read) { - return F_none_eof; - } + size_read = read(file.id, buffer_read, file.size_read); - if (size_read < 0) { - if (errno == EAGAIN || errno == EWOULDBLOCK) return F_status_set_error(F_block); - if (errno == EBADF) return F_status_set_error(F_file_descriptor); - if (errno == EFAULT) return F_status_set_error(F_buffer); - if (errno == EINTR) return F_status_set_error(F_interrupted); - if (errno == EINVAL) return F_status_set_error(F_parameter); - if (errno == EIO) return F_status_set_error(F_input_output); - if (errno == EISDIR) return F_status_set_error(F_file_type_directory); + if (size_read < 0) { + if (errno == EAGAIN || errno == EWOULDBLOCK) return F_status_set_error(F_block); + if (errno == EBADF) return F_status_set_error(F_file_descriptor); + if (errno == EFAULT) return F_status_set_error(F_buffer); + if (errno == EINTR) return F_status_set_error(F_interrupted); + if (errno == EINVAL) return F_status_set_error(F_parameter); + if (errno == EIO) return F_status_set_error(F_input_output); + if (errno == EISDIR) return F_status_set_error(F_file_type_directory); - return F_status_set_error(F_failure); - } + return F_status_set_error(F_failure); + } - return F_none; + buffer->used += size_read; + + if (size_read < file.size_read) break; + } // for + + return F_none_eof; } #endif // _di_f_file_read_ @@ -1505,26 +1504,25 @@ extern "C" { f_status_t status = F_none; ssize_t size_read = 0; - char buffer_read[file.size_read]; + f_string_t buffer_read = 0; - memset(&buffer_read, 0, sizeof(file.size_read)); - - if ((size_read = read(file.id, buffer_read, file.size_read)) > 0) { - if (buffer->used + size_read > buffer->size) { - if (buffer->size + size_read > f_string_length_t_size) { - return F_status_set_error(F_string_too_large); - } - - f_macro_string_dynamic_t_resize(status, (*buffer), buffer->size + size_read); - if (F_status_is_error(status)) return status; + if (buffer->used + size_read > buffer->size) { + if (buffer->size + size_read > f_string_length_t_size) { + return F_status_set_error(F_string_too_large); } - memcpy(buffer->string + buffer->used, buffer_read, size_read); - buffer->used += size_read; + f_macro_string_dynamic_t_resize(status, (*buffer), buffer->size + size_read); + if (F_status_is_error(status)) return status; + + memset(buffer->string + buffer->used, 0, sizeof(file.size_read)); } - if (!size_read) { - return F_none_eof; + buffer_read = buffer->string + buffer->used; + + size_read = read(file.id, buffer_read, file.size_read); + + if (size_read > 0) { + buffer->used += size_read; } if (size_read < 0) { @@ -1539,6 +1537,10 @@ extern "C" { return F_status_set_error(F_failure); } + if (size_read < file.size_read) { + return F_none_eof; + } + return F_none; } #endif // _di_f_file_read_block_ @@ -1552,51 +1554,57 @@ extern "C" { if (file.id == -1) return F_status_set_error(F_file_closed); - f_status_t status = F_none; - ssize_t size_read = 0; - f_string_length_t buffer_size = file.size_read; f_string_length_t buffer_count = 0; - if (total < buffer_size) { - buffer_size = total; - } - - char buffer_read[buffer_size]; + f_status_t status = F_none; + ssize_t size_read = 0; - memset(&buffer_read, 0, sizeof(buffer_size)); + for (f_string_t buffer_read = 0; ;) { - while (buffer_count < total && (size_read = read(file.id, buffer_read, buffer_size)) > 0) { + if (buffer_count + buffer_size > total) { + buffer_size = total - buffer_count; + } - if (buffer->used + size_read > buffer->size) { - if (buffer->size + size_read > f_string_length_t_size) { + if (buffer->used + buffer_size > buffer->size) { + if (buffer->size + buffer_size > f_string_length_t_size) { return F_status_set_error(F_string_too_large); } - f_macro_string_dynamic_t_resize(status, (*buffer), buffer->size + size_read); + f_macro_string_dynamic_t_resize(status, (*buffer), buffer->size + buffer_size); if (F_status_is_error(status)) return status; + + memset(buffer->string + buffer->used, 0, sizeof(buffer_size)); + } + + buffer_read = buffer->string + buffer->used; + + size_read = read(file.id, buffer_read, buffer_size); + + if (size_read < 0) { + if (errno == EAGAIN || errno == EWOULDBLOCK) return F_status_set_error(F_block); + if (errno == EBADF) return F_status_set_error(F_file_descriptor); + if (errno == EFAULT) return F_status_set_error(F_buffer); + if (errno == EINTR) return F_status_set_error(F_interrupted); + if (errno == EINVAL) return F_status_set_error(F_parameter); + if (errno == EIO) return F_status_set_error(F_input_output); + if (errno == EISDIR) return F_status_set_error(F_file_type_directory); + + return F_status_set_error(F_failure); } - memcpy(buffer->string + buffer->used, buffer_read, size_read); buffer->used += size_read; - buffer_count += size_read; - } // while - if (!size_read) { - return F_none_eof; - } + if (size_read < buffer_size) { + return F_none_eof; + } - if (size_read < 0) { - if (errno == EAGAIN || errno == EWOULDBLOCK) return F_status_set_error(F_block); - if (errno == EBADF) return F_status_set_error(F_file_descriptor); - if (errno == EFAULT) return F_status_set_error(F_buffer); - if (errno == EINTR) return F_status_set_error(F_interrupted); - if (errno == EINVAL) return F_status_set_error(F_parameter); - if (errno == EIO) return F_status_set_error(F_input_output); - if (errno == EISDIR) return F_status_set_error(F_file_type_directory); + buffer_count += size_read; - return F_status_set_error(F_failure); - } + if (buffer_count == total) { + return F_none_stop; + } + } // for return F_none; } @@ -1902,16 +1910,20 @@ extern "C" { #endif // _di_f_file_stream_close_ #ifndef _di_f_file_stream_descriptor_ - f_return_status f_file_stream_descriptor(const int id, const f_string_t mode, FILE *stream) { + f_return_status f_file_stream_descriptor(const f_string_t mode, f_file_t *file) { #ifndef _di_level_0_parameter_checking_ - if (id == -1) return F_status_set_error(F_parameter); - if (!mode) return F_status_set_error(F_parameter); - if (!stream) return F_status_set_error(F_parameter); + if (!file) return F_status_set_error(F_parameter); + if (file->id == -1) return F_status_set_error(F_parameter); #endif // _di_level_0_parameter_checking_ - stream = fdopen(id, mode); + if (mode) { + file->stream = fdopen(file->id, mode); + } + else { + file->stream = fdopen(file->id, private_f_file_stream_open_mode_determine(file->flag)); + } - if (!stream) { + if (!file->stream) { if (errno == EACCES) return F_status_set_error(F_access_denied); if (errno == EAGAIN) return F_status_set_error(F_prohibited); if (errno == EBADF) return F_status_set_error(F_file_descriptor); @@ -1936,11 +1948,15 @@ extern "C" { f_return_status f_file_stream_open(const f_string_t path, const f_string_t mode, f_file_t *file) { #ifndef _di_level_0_parameter_checking_ if (!path) return F_status_set_error(F_parameter); - if (!mode) return F_status_set_error(F_parameter); if (!file) return F_status_set_error(F_parameter); #endif // _di_level_0_parameter_checking_ - file->stream = fopen(path, mode); + if (mode) { + file->stream = fopen(path, mode); + } + else { + file->stream = fopen(path, private_f_file_stream_open_mode_determine(file->flag)); + } if (!file->stream) { if (errno == EACCES) return F_status_set_error(F_access_denied); @@ -1976,15 +1992,184 @@ extern "C" { } #endif // _di_f_file_stream_open_ +#ifndef _di_f_file_stream_read_ + f_return_status f_file_stream_read(const f_file_t file, const f_string_length_t amount, f_string_dynamic_t *buffer) { + #ifndef _di_level_0_parameter_checking_ + if (!file.size_read) return F_status_set_error(F_parameter); + if (amount < 1) return F_status_set_error(F_parameter); + if (buffer->used > buffer->size) return F_status_set_error(F_parameter); + #endif // _di_level_0_parameter_checking_ + + if (!file.stream) return F_status_set_error(F_file_closed); + + f_status_t status = F_none; + ssize_t size_read = 0; + + for (;;) { + + if (buffer->used + file.size_read > buffer->size) { + if (buffer->size + file.size_read > f_string_length_t_size) { + return F_status_set_error(F_string_too_large); + } + + f_macro_string_dynamic_t_resize(status, (*buffer), buffer->size + file.size_read); + if (F_status_is_error(status)) return status; + + memset(buffer->string + buffer->used, 0, sizeof(file.size_read)); + } + + size_read = fread(buffer->string + buffer->used, amount, file.size_read, file.stream); + + if (ferror(file.stream)) { + if (errno == EAGAIN || errno == EWOULDBLOCK) return F_status_set_error(F_block); + if (errno == EBADF) return F_status_set_error(F_file_descriptor); + if (errno == EFAULT) return F_status_set_error(F_buffer); + if (errno == EINTR) return F_status_set_error(F_interrupted); + if (errno == EINVAL) return F_status_set_error(F_parameter); + if (errno == EIO) return F_status_set_error(F_input_output); + if (errno == EISDIR) return F_status_set_error(F_file_type_directory); + + return F_status_set_error(F_failure); + } + + buffer->used += size_read * amount; + + if (feof(file.stream)) break; + } // for + + return F_none_eof; + } +#endif // _di_f_file_stream_read_ + +#ifndef _di_f_file_stream_read_block_ + f_return_status f_file_stream_read_block(const f_file_t file, const f_string_length_t amount, f_string_dynamic_t *buffer) { + #ifndef _di_level_0_parameter_checking_ + if (!file.size_read) return F_status_set_error(F_parameter); + if (amount < 1) return F_status_set_error(F_parameter); + if (buffer->used > buffer->size) return F_status_set_error(F_parameter); + #endif // _di_level_0_parameter_checking_ + + if (!file.stream) return F_status_set_error(F_file_closed); + + const f_string_length_t buffer_size = file.size_read * amount; + + f_status_t status = F_none; + ssize_t size_read = 0; + + if (buffer->used + buffer_size > buffer->size) { + if (buffer->size + buffer_size > f_string_length_t_size) { + return F_status_set_error(F_string_too_large); + } + + f_macro_string_dynamic_t_resize(status, (*buffer), buffer->size + buffer_size); + if (F_status_is_error(status)) return status; + } + + memset(buffer->string + buffer->used, 0, sizeof(buffer_size)); + + size_read = fread(buffer->string + buffer->used, amount, file.size_read, file.stream); + + if (ferror(file.stream)) { + if (errno == EAGAIN || errno == EWOULDBLOCK) return F_status_set_error(F_block); + if (errno == EBADF) return F_status_set_error(F_file_descriptor); + if (errno == EFAULT) return F_status_set_error(F_buffer); + if (errno == EINTR) return F_status_set_error(F_interrupted); + if (errno == EINVAL) return F_status_set_error(F_parameter); + if (errno == EIO) return F_status_set_error(F_input_output); + if (errno == EISDIR) return F_status_set_error(F_file_type_directory); + + return F_status_set_error(F_failure); + } + + buffer->used += size_read * amount; + + if (feof(file.stream)) { + return F_none_eof; + } + + return F_none; + } +#endif // _di_f_file_stream_read_block_ + +#ifndef _di_f_file_stream_read_until_ + f_return_status f_file_stream_read_until(const f_file_t file, const f_string_length_t amount, const f_string_length_t total, f_string_dynamic_t *buffer) { + #ifndef _di_level_0_parameter_checking_ + if (!file.size_read) return F_status_set_error(F_parameter); + if (amount < 1) return F_status_set_error(F_parameter); + if (buffer->used > buffer->size) return F_status_set_error(F_parameter); + #endif // _di_level_0_parameter_checking_ + + if (!file.stream) return F_status_set_error(F_file_closed); + + f_string_length_t buffer_size = file.size_read; + f_string_length_t buffer_count = 0; + + f_status_t status = F_none; + ssize_t size_read = 0; + + for (;;) { + + if (buffer_count + buffer_size > total) { + buffer_size = total - buffer_count; + } + + if (buffer->used + buffer_size > buffer->size) { + if (buffer->size + buffer_size > f_string_length_t_size) { + return F_status_set_error(F_string_too_large); + } + + f_macro_string_dynamic_t_resize(status, (*buffer), buffer->size + buffer_size); + if (F_status_is_error(status)) return status; + + memset(buffer->string + buffer->used, 0, sizeof(buffer_size)); + } + + size_read = fread(buffer->string + buffer->used, amount, file.size_read, file.stream); + + if (ferror(file.stream)) { + if (errno == EAGAIN || errno == EWOULDBLOCK) return F_status_set_error(F_block); + if (errno == EBADF) return F_status_set_error(F_file_descriptor); + if (errno == EFAULT) return F_status_set_error(F_buffer); + if (errno == EINTR) return F_status_set_error(F_interrupted); + if (errno == EINVAL) return F_status_set_error(F_parameter); + if (errno == EIO) return F_status_set_error(F_input_output); + if (errno == EISDIR) return F_status_set_error(F_file_type_directory); + + return F_status_set_error(F_failure); + } + + buffer->used += size_read * amount; + + if (feof(file.stream)) { + return F_none_eof; + } + + buffer_count += size_read * amount; + + if (buffer_count == total) break; + } // for + + return F_none_stop; + } +#endif // _di_f_file_stream_read_until_ + #ifndef _di_f_file_stream_reopen_ f_return_status f_file_stream_reopen(const f_string_t path, const f_string_t mode, f_file_t *file) { #ifndef _di_level_0_parameter_checking_ if (!path) return F_status_set_error(F_parameter); - if (!mode) return F_status_set_error(F_parameter); if (!file) return F_status_set_error(F_parameter); #endif // _di_level_0_parameter_checking_ - if (!freopen(path, mode, file->stream)) { + FILE *result = 0; + + if (mode) { + result = freopen(path, mode, file->stream); + } + else { + result = freopen(path, private_f_file_stream_open_mode_determine(file->flag), file->stream); + } + + if (!result) { if (errno == EACCES) return F_status_set_error(F_access_denied); if (errno == EAGAIN) return F_status_set_error(F_prohibited); if (errno == EBADF) return F_status_set_error(F_file_descriptor); @@ -2010,6 +2195,184 @@ extern "C" { } #endif // _di_f_file_stream_reopen_ +#ifndef _di_f_file_stream_write_ + f_return_status f_file_stream_write(const f_file_t file, const f_string_static_t buffer, const f_string_length_t amount, f_string_length_t *written) { + #ifndef _di_level_0_parameter_checking_ + if (!file.size_write) return F_status_set_error(F_parameter); + if (buffer.used > buffer.size) return F_status_set_error(F_parameter); + if (amount < 1) return F_status_set_error(F_parameter); + #endif // _di_level_0_parameter_checking_ + + if (!file.stream) return F_status_set_error(F_file_closed); + + if (!buffer.used) { + if (written) *written = 0; + return F_data_not; + } + + f_status_t status = F_none; + + if (written) { + private_f_file_stream_write_until(file, buffer.string, amount, buffer.used, written); + + if (status == F_none && *written == buffer.used) return F_none_eos; + } + else { + f_string_length_t written_local = 0; + + private_f_file_stream_write_until(file, buffer.string, amount, buffer.used, &written_local); + + if (status == F_none && written_local == buffer.used) return F_none_eos; + } + + if (F_status_is_error(status)) return F_status_set_error(status); + + return status; + } +#endif // _di_f_file_stream_write_ + +#ifndef _di_f_file_stream_write_block_ + f_return_status f_file_stream_write_block(const f_file_t file, const f_string_static_t buffer, const f_string_length_t amount, f_string_length_t *written) { + #ifndef _di_level_0_parameter_checking_ + if (!file.size_write) return F_status_set_error(F_parameter); + if (buffer.used > buffer.size) return F_status_set_error(F_parameter); + if (amount < 1) return F_status_set_error(F_parameter); + #endif // _di_level_0_parameter_checking_ + + if (!file.stream) return F_status_set_error(F_file_closed); + + if (!buffer.used) { + if (written) *written = 0; + return F_data_not; + } + + f_string_length_t write_max = file.size_write; + + if (write_max > buffer.used) { + write_max = buffer.used; + } + + f_status_t status = F_none; + + if (written) { + private_f_file_stream_write_until(file, buffer.string, amount, write_max, written); + + if (status == F_none) { + if (*written == buffer.used) return F_none_eos; + if (*written == write_max) return F_none_stop; + } + } + else { + f_string_length_t written_local = 0; + + private_f_file_stream_write_until(file, buffer.string, amount, write_max, &written_local); + + if (status == F_none) { + if (written_local == buffer.used) return F_none_eos; + if (written_local == write_max) return F_none_stop; + } + } + + return status; + } +#endif // _di_f_file_stream_write_block_ + +#ifndef _di_f_file_stream_write_until_ + f_return_status f_file_stream_write_until(const f_file_t file, const f_string_static_t buffer, const f_string_length_t amount, const f_string_length_t total, f_string_length_t *written) { + #ifndef _di_level_0_parameter_checking_ + if (!file.size_write) return F_status_set_error(F_parameter); + if (buffer.used > buffer.size) return F_status_set_error(F_parameter); + if (amount < 1) return F_status_set_error(F_parameter); + if (!total) return F_status_set_error(F_parameter); + #endif // _di_level_0_parameter_checking_ + + if (!file.stream) return F_status_set_error(F_file_closed); + + if (!buffer.used || !total) { + if (written) *written = 0; + return F_data_not; + } + + f_string_length_t write_max = total; + + if (write_max > buffer.used) { + write_max = buffer.used; + } + + f_status_t status = F_none; + + if (written) { + private_f_file_stream_write_until(file, buffer.string, amount, write_max, written); + + if (status == F_none) { + if (*written == buffer.used) return F_none_eos; + if (*written == write_max) return F_none_stop; + } + } + else { + f_string_length_t written_local = 0; + + private_f_file_stream_write_until(file, buffer.string, amount, buffer.used, &written_local); + + if (status == F_none) { + if (written_local == buffer.used) return F_none_eos; + if (written_local == write_max) return F_none_stop; + } + } + + return status; + } +#endif // _di_f_file_stream_write_until_ + +#ifndef _di_f_file_stream_write_range_ + f_return_status f_file_stream_write_range(const f_file_t file, const f_string_static_t buffer, const f_string_length_t amount, const f_string_range_t range, f_string_length_t *written) { + #ifndef _di_level_0_parameter_checking_ + if (!file.size_write) return F_status_set_error(F_parameter); + if (buffer.used > buffer.size) return F_status_set_error(F_parameter); + if (amount < 1) return F_status_set_error(F_parameter); + if (range.stop < range.start) return F_status_set_error(F_parameter); + if (range.start >= buffer.used) return F_status_set_error(F_parameter); + #endif // _di_level_0_parameter_checking_ + + if (!file.stream) return F_status_set_error(F_file_closed); + + if (!buffer.used) { + if (written) *written = 0; + return F_data_not; + } + + const f_string_length_t total = (range.stop - range.start) + 1; + f_string_length_t write_max = total; + + if (write_max > buffer.used) { + write_max = buffer.used; + } + + f_status_t status = F_none; + + if (written) { + private_f_file_stream_write_until(file, buffer.string + range.start, amount, write_max, written); + + if (status == F_none) { + if (range.start + *written == buffer.used) return F_none_stop; + if (range.start + *written == total) return F_none_eos; + } + } + else { + f_string_length_t written_local = 0; + + private_f_file_stream_write_until(file, buffer.string + range.start, amount, write_max, &written_local); + + if (status == F_none) { + if (range.start + written_local == buffer.used) return F_none_eos; + if (range.start + written_local == total) return F_none_stop; + } + } + + return status; + } +#endif // _di_f_file_stream_write_range_ + #ifndef _di_f_file_touch_ f_return_status f_file_touch(const f_string_t path, const mode_t mode, const bool dereference) { #ifndef _di_level_0_parameter_checking_ @@ -2236,6 +2599,7 @@ extern "C" { #ifndef _di_level_0_parameter_checking_ if (!file.size_write) return F_status_set_error(F_parameter); if (buffer.used > buffer.size) return F_status_set_error(F_parameter); + if (!total) return F_status_set_error(F_parameter); #endif // _di_level_0_parameter_checking_ if (file.id == -1) return F_status_set_error(F_file_closed); @@ -2245,7 +2609,7 @@ extern "C" { return F_data_not; } - f_string_length_t write_max = file.size_write; + f_string_length_t write_max = total; if (write_max > buffer.used) { write_max = buffer.used; diff --git a/level_0/f_file/c/file.h b/level_0/f_file/c/file.h index 19f7771..09e04d5 100644 --- a/level_0/f_file/c/file.h +++ b/level_0/f_file/c/file.h @@ -127,6 +127,8 @@ extern "C" { * * Will flush before closing. * + * @todo consider making this consistent and acceptinf f_file_t instead of just the id. + * * @param id * The file descriptor. * @@ -1290,7 +1292,7 @@ extern "C" { /** * Read until EOF is reached. * - * To check how much was read into the buffer, record buffer->used before execution and compare to buffer->used after execution. + * To determine how much was read into the buffer, record buffer->used before execution and compare to buffer->used after execution. * * @param file * The file to read. @@ -1300,7 +1302,6 @@ extern "C" { * The contents of the file is appended into this buffer. * * @return - * F_none on success. * F_none_eof on success and EOF was reached. * F_block (with error bit) if file descriptor is set to non-block and the read would result in a blocking operation. * F_buffer (with error bit) if the buffer is invalid. @@ -1350,7 +1351,12 @@ extern "C" { /** * Read until a given number or EOF is reached, storing it in the buffer. * - * To check how much was read into the buffer, record buffer->used before execution and compare to buffer->used after execution. + * To determine how much was read into the buffer, record buffer->used before execution and compare to buffer->used after execution. + * + * This is different from simply using the file.size_read. + * The file.size_read represents the amount to process for a given buffer size. + * The total represents the maximum number of bytes to process. + * For example, if file.size_read is 16 and total is 128, then this function would need to be called 8 times until total is reached. * * @param file * The file to read. @@ -1361,8 +1367,8 @@ extern "C" { * The buffer the file is being read into. * * @return - * F_none on success. * F_none_eof on success and EOF was reached. + * F_none_stop on success and total was reached. * F_block (with error bit) if file descriptor is set to non-block and the read would result in a blocking operation. * F_buffer (with error bit) if the buffer is invalid. * F_file_closed (with error bit) if file is not open. @@ -1843,14 +1849,15 @@ extern "C" { /** * Open a file stream from a file descriptor. * - * @param id - * The file descriptor. * @param mode * The file modes do use when opening. + * The file modes do use when opening. + * Set to 0 to determine mode from file.flags (falling back to read only as a failsafe). + * If neither truncate nor append are not specified in write only mode, then the failsafe is to append. * This should match the modes used to open the file descriptor as it relates to the stream modes. - * @param stream - * The file stream. - * Updated on success, but may be set to NULL on error. + * @param file + * The file with a valid file descriptor (file.id). + * THe file stream (file.stream) is updated on success, but may be set to NULL on error. * * @return * F_none is returned on success. @@ -1868,7 +1875,7 @@ extern "C" { * @see fdopen() */ #ifndef _di_f_file_stream_descriptor_ - extern f_return_status f_file_stream_descriptor(const int id, const f_string_t mode, FILE *stream); + extern f_return_status f_file_stream_descriptor(const f_string_t mode, f_file_t *file); #endif // _di_f_file_stream_descriptor_ /** @@ -1880,6 +1887,8 @@ extern "C" { * The file path * @param mode * The file modes do use when opening. + * Set to 0 to determine mode from file.flags (falling back to read only as a failsafe). + * If neither truncate nor append are not specified in write only mode, then the failsafe is to append. * @param file * The file information. * The file.stream is updated if necessary. @@ -1914,6 +1923,110 @@ extern "C" { #endif // _di_f_file_stream_open_ /** + * Read until EOF is reached. + * + * To check how much was read into the buffer, record buffer->used before execution and compare to buffer->used after execution. + * + * @param file + * The file to read. + * The file must already be open. + * @param amount + * The total amount of file.size_read to process. + * This amount is multiplied against file.size_read and must be greater than 0. + * @param buffer + * The buffer the file is being read into. + * The contents of the file is appended into this buffer. + * + * @return + * F_none_eof on success and EOF was reached. + * F_block (with error bit) if file descriptor is set to non-block and the read would result in a blocking operation. + * F_buffer (with error bit) if the buffer is invalid. + * F_file_closed (with error bit) if file is not open. + * F_file_descriptor (with error bit) if the file descriptor is invalid. + * F_file_type_directory (with error bit) if file descriptor represents a directory. + * F_input_output (with error bit) on I/O error. + * F_interrupted (with error bit) if interrupt was received. + * F_parameter (with error bit) if a parameter is invalid. + * + * @see fread() + */ +#ifndef _di_f_file_stream_read_ + extern f_return_status f_file_stream_read(const f_file_t file, const f_string_length_t amount, f_string_dynamic_t *buffer); +#endif // _di_f_file_stream_read_ + +/** + * Read until a single block is filled or EOF is reached. + * + * To determine how much was read into the buffer, record buffer->used before execution and compare to buffer->used after execution. + * + * @param file + * The file to read. + * The file must already be open. + * @param amount + * The total amount of file.size_read to process. + * This amount is multiplied against file.size_read and must be greater than 0. + * @param buffer + * The buffer the file is being read into. + * The contents of the file is appended into this buffer. + * + * @return + * F_none on success. + * F_none_eof on success and EOF was reached. + * F_block (with error bit) if file descriptor is set to non-block and the read would result in a blocking operation. + * F_buffer (with error bit) if the buffer is invalid. + * F_file_closed (with error bit) if file is not open. + * F_file_descriptor (with error bit) if the file descriptor is invalid. + * F_file_type_directory (with error bit) if file descriptor represents a directory. + * F_input_output (with error bit) on I/O error. + * F_interrupted (with error bit) if interrupt was received. + * F_parameter (with error bit) if a parameter is invalid. + * + * @see fread() + */ +#ifndef _di_f_file_stream_read_block_ + extern f_return_status f_file_stream_read_block(const f_file_t file, const f_string_length_t amount, f_string_dynamic_t *buffer); +#endif // _di_f_file_stream_read_block_ + +/** + * Read until a given number or EOF is reached, storing it in the buffer. + * + * To check how much was read into the buffer, record buffer->used before execution and compare to buffer->used after execution. + * + * This is different from simply using the file.size_read. + * The file.size_read represents the amount to process for a given buffer size. + * The total represents the maximum number of "size" to process. + * For example, if file.size_read is 16 and total is 128, then this function would need to be called 8 times until total is reached. + * + * @param file + * The file to read. + * The file must already be open. + * @param amount + * The total amount of file.size_read to process. + * This amount is multiplied against file.size_read and must be greater than 0. + * @param total + * The total bytes to read, unless EOF is reached first. + * @param buffer + * The buffer the file is being read into. + * + * @return + * F_none_eof on success and EOF was reached. + * F_none_stop on success and total was reached. + * F_block (with error bit) if file descriptor is set to non-block and the read would result in a blocking operation. + * F_buffer (with error bit) if the buffer is invalid. + * F_file_closed (with error bit) if file is not open. + * F_file_descriptor (with error bit) if the file descriptor is invalid. + * F_file_type_directory (with error bit) if file descriptor represents a directory. + * F_input_output (with error bit) on I/O error. + * F_interrupted (with error bit) if interrupt was received. + * F_parameter (with error bit) if a parameter is invalid. + * + * @see fread() + */ +#ifndef _di_f_file_stream_read_until_ + extern f_return_status f_file_stream_read_until(const f_file_t file, const f_string_length_t amount, const f_string_length_t total, f_string_dynamic_t *buffer); +#endif // _di_f_file_stream_read_until_ + +/** * Re-open a file stream. * * This allows for re-using an existing file stream and possibly file-descriptor. @@ -1924,6 +2037,8 @@ extern "C" { * The file path * @param mode * The file modes do use when opening. + * Set to 0 to determine mode from file.flags (falling back to read only as a failsafe). + * If neither truncate nor append are not specified in write only mode, then the failsafe is to append. * @param file * The file information. * The file.stream is updated, if necessary. @@ -1958,6 +2073,146 @@ extern "C" { #endif // _di_f_file_stream_reopen_ /** + * Write until entire buffer is written. + * + * @param file + * The file to write to. + * The file must already be open. + * @param buffer + * The buffer to write to the file. + * @param amount + * The total amount of file.size_write to process. + * This amount is multiplied against file.size_write and must be greater than 0. + * @param written + * The total bytes written. + * Set pointer to 0 to not use. + * + * @return + * F_none on success. + * F_none_stop on success but no data was written (written == 0) (not an error and often happens if file type is not a regular file). + * F_block (with error bit) if file descriptor is set to non-block and the write would result in a blocking operation. + * F_buffer (with error bit) if the buffer is invalid. + * F_file_closed (with error bit) if file is not open. + * F_file_descriptor (with error bit) if the file descriptor is invalid. + * F_file_type_directory (with error bit) if file descriptor represents a directory. + * F_input_output (with error bit) on I/O error. + * F_interrupted (with error bit) if interrupt was received. + * F_parameter (with error bit) if a parameter is invalid. + * + * @see fwrite() + */ +#ifndef _di_f_file_stream_write_ + extern f_return_status f_file_stream_write(const f_file_t file, const f_string_static_t buffer, const f_string_length_t amount, f_string_length_t *written); +#endif // _di_f_file_stream_write_ + +/** + * Write until a single block is filled or entire buffer is written. + * + * To check how much was write into the buffer, record buffer->used before execution and compare to buffer->used after execution. + * + * @param file + * The file to write to. + * The file must already be open. + * @param buffer + * The buffer to write to the file. + * @param amount + * The total amount of file.size_write to process. + * This amount is multiplied against file.size_write and must be greater than 0. + * @param written + * The total bytes written. + * Set pointer to 0 to not use. + * + * @return + * F_none on success. + * F_none_stop on success but no data was written (written == 0) (not an error and often happens if file type is not a regular file). + * F_block (with error bit) if file descriptor is set to non-block and the write would result in a blocking operation. + * F_buffer (with error bit) if the buffer is invalid. + * F_file_closed (with error bit) if file is not open. + * F_file_descriptor (with error bit) if the file descriptor is invalid. + * F_file_type_directory (with error bit) if file descriptor represents a directory. + * F_input_output (with error bit) on I/O error. + * F_interrupted (with error bit) if interrupt was received. + * F_parameter (with error bit) if a parameter is invalid. + * + * @see fwrite() + */ +#ifndef _di_f_file_stream_write_block_ + extern f_return_status f_file_stream_write_block(const f_file_t file, const f_string_static_t buffer, const f_string_length_t amount, f_string_length_t *written); +#endif // _di_f_file_stream_write_block_ + +/** + * Write until a given number or entire buffer is written. + * + * @param file + * The file to write to. + * The file must already be open. + * @param buffer + * The buffer to write to the file. + * @param amount + * The total amount of file.size_write to process. + * This amount is multiplied against file.size_write and must be greater than 0. + * @param total + * The total bytes to write, unless end of buffer is reached first. + * @param written + * The total bytes written. + * Set pointer to 0 to not use. + * + * @return + * F_none on success. + * F_none_stop on success but no data was written (written == 0) (not an error and often happens if file type is not a regular file). + * F_none_eos on success but range.stop exceeded buffer.used (only wrote up to buffer.used). + * F_block (with error bit) if file descriptor is set to non-block and the write would result in a blocking operation. + * F_buffer (with error bit) if the buffer is invalid. + * F_file_closed (with error bit) if file is not open. + * F_file_descriptor (with error bit) if the file descriptor is invalid. + * F_file_type_directory (with error bit) if file descriptor represents a directory. + * F_input_output (with error bit) on I/O error. + * F_interrupted (with error bit) if interrupt was received. + * F_parameter (with error bit) if a parameter is invalid. + * + * @see fwrite() + */ +#ifndef _di_f_file_stream_write_until_ + extern f_return_status f_file_stream_write_until(const f_file_t file, const f_string_static_t buffer, const f_string_length_t amount, const f_string_length_t total, f_string_length_t *written); +#endif // _di_f_file_stream_write_until_ + +/** + * Write a given range within the buffer. + * + * @param file + * The file to write to. + * The file must already be open. + * @param buffer + * The buffer to write to the file. + * @param amount + * The total amount of file.size_write to process. + * This amount is multiplied against file.size_write and must be greater than 0. + * @param range + * An inclusive start an stop range within the buffer to read. + * @param written + * The total bytes written. + * Set pointer to 0 to not use. + * + * @return + * F_none on success. + * F_none_stop on success but no data was written (written == 0) (not an error and often happens if file type is not a regular file). + * F_none_eos on success but range.stop exceeded buffer.used (only wrote up to buffer.used). + * F_block (with error bit) if file descriptor is set to non-block and the write would result in a blocking operation. + * F_buffer (with error bit) if the buffer is invalid. + * F_file_descriptor (with error bit) if the file descriptor is invalid. + * F_file_type_directory (with error bit) if file descriptor represents a directory. + * F_input_output (with error bit) on I/O error. + * F_interrupted (with error bit) if interrupt was received. + * F_parameter (with error bit) if a parameter is invalid. + * + * @see fwrite() + */ +#ifndef _di_f_file_stream_write_range_ + extern f_return_status f_file_stream_write_range(const f_file_t file, const f_string_static_t buffer, const f_string_length_t amount, const f_string_range_t range, f_string_length_t *written); +#endif // _di_f_file_stream_write_range_ + + +/** * Update the files access and modification timestamp, creating the file if it does not already exist. * * When the file is created, it is created as a regular file. diff --git a/level_0/f_file/c/private-file.c b/level_0/f_file/c/private-file.c index 860708b..80af353 100644 --- a/level_0/f_file/c/private-file.c +++ b/level_0/f_file/c/private-file.c @@ -722,7 +722,84 @@ extern "C" { } #endif // !defined(_di_f_file_stat_by_id_) || !defined(_di_f_file_size_by_id_) -#if !defined(f_file_write) || !defined(f_file_write_until) || !defined(f_file_write_range) +#if !defined(_di_f_file_stream_descriptor_) || !defined(_di_f_file_stream_open_) || !defined(_di_f_file_stream_reopen_) + const char *private_f_file_stream_open_mode_determine(const int flag) { + + if (flag & f_file_flag_read_write) { + if (flag & f_file_flag_truncate) { + return "w+"; + } + else if (flag & f_file_flag_append) { + return "a+"; + } + + // failsafe to read write prepend. + return "r+"; + } + else if (flag & f_file_flag_write_only) { + if (flag & f_file_flag_truncate) { + return "w"; + } + + // failsafe to append. + return "a"; + } + + // failsafe to read only. + return "r"; + } +#endif // !defined(_di_f_file_stream_descriptor_) || !defined(_di_f_file_stream_open_) || !defined(_di_f_file_stream_reopen_) + +#if !defined(f_file_stream_write) || !defined(_di_f_file_stream_write_block_) || !defined(f_file_stream_write_until) || !defined(f_file_stream_write_range) + f_return_status private_f_file_stream_write_until(const f_file_t file, const f_string_t string, const f_string_length_t amount, const f_string_length_t total, f_string_length_t *written) { + *written = 0; + + f_status_t status = F_none; + f_string_length_t write_amount = amount; + f_string_length_t write_size = file.size_write; + f_string_length_t write_max = total; + + ssize_t size_write = 0; + + if (write_max < file.size_write) { + write_amount = 1; + write_size = write_max; + } + else if (amount * file.size_write > total) { + write_amount = total / file.size_write; + + if (total % file.size_write) { + write_amount++; + } + } + + while (*written < write_max) { + size_write = fwrite(string + *written, write_amount, write_size, file.stream); + + if (size_write < 0) { + if (errno == EAGAIN || errno == EWOULDBLOCK) return F_status_set_error(F_block); + if (errno == EBADF) return F_status_set_error(F_file_descriptor); + if (errno == EFAULT) return F_status_set_error(F_buffer); + if (errno == EINTR) return F_status_set_error(F_interrupted); + if (errno == EINVAL) return F_status_set_error(F_parameter); + if (errno == EIO) return F_status_set_error(F_input_output); + if (errno == EISDIR) return F_status_set_error(F_file_type_directory); + + return F_status_set_error(F_failure); + } + + *written += size_write * write_amount; + + if (!size_write) { + return F_none_stop; + } + } // while + + return F_none; + } +#endif // !defined(f_file_stream_write) || !defined(_di_f_file_stream_write_block_) || !defined(f_file_stream_write_until) || !defined(f_file_stream_write_range) + +#if !defined(f_file_write) || !defined(_di_f_file_write_block_) || !defined(f_file_write_until) || !defined(f_file_write_range) f_return_status private_f_file_write_until(const f_file_t file, const f_string_t string, const f_string_length_t total, f_string_length_t *written) { *written = 0; @@ -732,7 +809,7 @@ extern "C" { ssize_t size_write = 0; - if (write_max < write_size) { + if (write_max < file.size_write) { write_size = write_max; } @@ -762,7 +839,7 @@ extern "C" { return F_none; } -#endif // !defined(f_file_write) || !defined(f_file_write_until) || !defined(f_file_write_range) +#endif // !defined(f_file_write) || !defined(_di_f_file_write_block_) || !defined(f_file_write_until) || !defined(f_file_write_range) #ifdef __cplusplus } // extern "C" diff --git a/level_0/f_file/c/private-file.h b/level_0/f_file/c/private-file.h index 215e982..5330d02 100644 --- a/level_0/f_file/c/private-file.h +++ b/level_0/f_file/c/private-file.h @@ -860,6 +860,65 @@ extern "C" { #endif // !defined(_di_f_file_stat_by_id_) || !defined(_di_f_file_size_by_id_) /** + * Special function for use in the f_file_stream_open() and related functions. + * + * Intended to be shared to each of the different implementation variations. + * + * @param flag + * The file flag. + * + * @return + * A string representing the file mode for use in fopen(), fdopen(), or freopen(). + * + * @see f_file_stream_descriptor() + * @see f_file_stream_open() + * @see f_file_stream_reopen() + */ +#if !defined(_di_f_file_stream_descriptor_) || !defined(_di_f_file_stream_open_) || !defined(_di_f_file_stream_reopen_) + extern const char *private_f_file_stream_open_mode_determine(const int flag) f_gcc_attribute_visibility_internal; +#endif // !defined(_di_f_file_stream_descriptor_) || !defined(_di_f_file_stream_open_) || !defined(_di_f_file_stream_reopen_) + +/** + * Private implementation of f_file_stream_write_until(). + * + * Intended to be shared to each of the different implementation variations. + * + * @param file + * The file to write to. + * The file must already be open. + * @param string + * The string to write to the file. + * @param amount + * The total amount of file.size_write to process. + * This amount is multiplied against file.size_write and must be greater than 0. + * @param total + * The total bytes to write. + * @param written + * The total bytes to written. + * + * @return + * F_none on success. + * F_none_stop on success but no data was written (written == 0) (not an error and often happens if file type is not a regular file). + * F_block (with error bit) if file descriptor is set to non-block and the write would result in a blocking operation. + * F_buffer (with error bit) if the buffer is invalid. + * F_file (with error bit) if file descriptor is in an error state. + * F_file_closed (with error bit) if file is not open. + * F_file_descriptor (with error bit) if the file descriptor is invalid. + * F_file_type_directory (with error bit) if file descriptor represents a directory. + * F_input_output (with error bit) on I/O error. + * F_interrupted (with error bit) if interrupt was received. + * F_parameter (with error bit) if a parameter is invalid. + * + * @see f_file_stream_write() + * @see f_file_stream_write_block() + * @see f_file_stream_write_range() + * @see f_file_stream_write_until() + */ +#if !defined(f_file_stream_write) || !defined(_di_f_file_stream_write_block_) || !defined(f_file_stream_write_until) || !defined(f_file_stream_write_range) + extern f_return_status private_f_file_stream_write_until(const f_file_t file, const f_string_t string, const f_string_length_t amount, const f_string_length_t total, f_string_length_t *written) f_gcc_attribute_visibility_internal; +#endif // !defined(f_file_stream_write) || !defined(_di_f_file_stream_write_block_) || !defined(f_file_stream_write_until) || !defined(f_file_stream_write_range) + +/** * Private implementation of f_file_write_until(). * * Intended to be shared to each of the different implementation variations. @@ -888,12 +947,13 @@ extern "C" { * F_parameter (with error bit) if a parameter is invalid. * * @see f_file_write() + * @see f_file_write_block() * @see f_file_write_range() * @see f_file_write_until() */ -#if !defined(f_file_write) || !defined(f_file_write_until) || !defined(f_file_write_range) +#if !defined(f_file_write) || !defined(_di_f_file_write_block_) || !defined(f_file_write_until) || !defined(f_file_write_range) extern f_return_status private_f_file_write_until(const f_file_t file, const f_string_t string, const f_string_length_t total, f_string_length_t *written) f_gcc_attribute_visibility_internal; -#endif // !defined(f_file_write) || !defined(f_file_write_until) || !defined(f_file_write_range) +#endif // !defined(f_file_write) || !defined(_di_f_file_write_block_) || !defined(f_file_write_until) || !defined(f_file_write_range) #ifdef __cplusplus } // extern "C" diff --git a/level_3/byte_dump/c/byte_dump.c b/level_3/byte_dump/c/byte_dump.c index c813e4d..16d53d8 100644 --- a/level_3/byte_dump/c/byte_dump.c +++ b/level_3/byte_dump/c/byte_dump.c @@ -367,7 +367,7 @@ extern "C" { missing_files = status; } - fll_error_file_print(data.error, F_status_set_fine(status), "f_file_exists", F_true, arguments.argv[data->remaining.array[counter]], "find", fll_error_file_type_file); + fll_error_file_print(data->error, F_status_set_fine(status), "f_file_exists", F_true, arguments.argv[data->remaining.array[counter]], "find", fll_error_file_type_file); } } // for @@ -382,10 +382,11 @@ extern "C" { f_file_t file = f_file_t_initialize; for (f_array_length_t counter = 0; counter < data->remaining.used; counter++) { + status = f_file_open(arguments.argv[data->remaining.array[counter]], 0, &file); if (F_status_is_error(status)) { - fll_error_file_print(data.error, F_status_set_fine(status), "f_file_open", F_true, arguments.argv[data->remaining.array[counter]], "open", fll_error_file_type_file); + fll_error_file_print(data->error, F_status_set_fine(status), "f_file_open", F_true, arguments.argv[data->remaining.array[counter]], "open", fll_error_file_type_file); byte_dump_delete_data(data); return status; @@ -419,7 +420,7 @@ extern "C" { f_file_close(&file.id); if (F_status_is_error(status)) { - fll_error_file_print(data.error, F_status_set_fine(status), "byte_dump_file", F_true, arguments.argv[data->remaining.array[counter]], "open", fll_error_file_type_file); + fll_error_file_print(data->error, F_status_set_fine(status), "byte_dump_file", F_true, arguments.argv[data->remaining.array[counter]], "process", fll_error_file_type_file); byte_dump_delete_data(data); return status; diff --git a/level_3/byte_dump/c/private-byte_dump.c b/level_3/byte_dump/c/private-byte_dump.c index 885d891..9494c5a 100644 --- a/level_3/byte_dump/c/private-byte_dump.c +++ b/level_3/byte_dump/c/private-byte_dump.c @@ -205,7 +205,7 @@ extern "C" { fprintf(data.output.stream, "%c", f_string_eol[0]); // make sure to flush standard out to help prevent standard error from causing poblems. - fflush(f_type_output); + fflush(data.output.stream); if (found_invalid_utf) { fl_color_print(data.error.to.stream, data.context.set.error, "Invalid UTF-8 codes were detected for file '"); @@ -224,7 +224,7 @@ extern "C" { status = F_status_set_error(F_failure); } - fflush(f_type_error); + fflush(data.error.to.stream); return status; } diff --git a/level_3/fake/c/private-fake.c b/level_3/fake/c/private-fake.c index ec581d9..8f4ed28 100644 --- a/level_3/fake/c/private-fake.c +++ b/level_3/fake/c/private-fake.c @@ -24,7 +24,7 @@ extern "C" { fprintf(data.output.stream, "%c", f_string_eol[0]); // flush to stdout before executing command. - fflush(f_type_output); + fflush(data.output.stream); } if (fake_signal_received(data)) { @@ -113,11 +113,11 @@ extern "C" { } name_function = "f_file_open"; - status = f_file_open(path_file, 0, &file); + status = f_file_stream_open(path_file, 0, &file); if (fake_signal_received(data)) { if (file.id) { - f_file_close(&file.id); + f_file_stream_close(F_true, &file); } return F_status_set_error(F_signal); @@ -127,7 +127,7 @@ extern "C" { name_function = "f_file_read"; status = f_file_read(file, buffer); - f_file_close(&file.id); + f_file_stream_close(F_true, &file); } } else if (status == F_false) { diff --git a/level_3/fake/c/private-make.c b/level_3/fake/c/private-make.c index 41423eb..781a019 100644 --- a/level_3/fake/c/private-make.c +++ b/level_3/fake/c/private-make.c @@ -1055,7 +1055,7 @@ extern "C" { return status; } - status = f_directory_open(data_make.path.stack.array[0].string, F_false, &data_make.path.top); + status = f_directory_open(data_make.path.stack.array[0].string, F_false, &data_make.path.top.id); if (F_status_is_error(status)) { fll_error_print(data.error, F_status_set_fine(status), "f_directory_open", F_true); @@ -1104,12 +1104,12 @@ extern "C" { fake_make_operate_section(data, data_make.main, &data_make, §ion_stack, &status); - if (data_make.path.current > 0) { - f_file_close(&data_make.path.current); + if (data_make.path.current.stream) { + f_file_stream_close(F_true, &data_make.path.current); } { - f_status_t status_path = f_path_change_at(data_make.path.top); + f_status_t status_path = f_path_change_at(data_make.path.top.id); if (F_status_is_error(status_path) && data.error.verbosity == f_console_verbosity_verbose) { fprintf(data.output.stream, "%c", f_string_eol[0]); @@ -1121,7 +1121,7 @@ extern "C" { } } - f_file_close(&data_make.path.top); + f_file_stream_close(F_true, &data_make.path.top); f_macro_string_lengths_t_delete_simple(section_stack); fake_macro_make_data_t_delete_simple(data_make); @@ -3646,7 +3646,7 @@ extern "C" { if (operation == fake_make_operation_type_top) { - *status = f_path_change_at(data_make->path.top); + *status = f_path_change_at(data_make->path.top.id); if (F_status_is_error(*status)) { fake_print_message_section_operation_path_stack_max(data, data_make->error, F_status_set_fine(*status), "f_path_change", arguments.array[0].string); @@ -3784,7 +3784,7 @@ extern "C" { fprintf(data.output.stream, "%c", f_string_eol[0]); // flush to stdout before executing command. - fflush(f_type_output); + fflush(data.output.stream); } int return_code = 0; diff --git a/level_3/fake/c/private-make.h b/level_3/fake/c/private-make.h index b1ad2a2..1a5956d 100644 --- a/level_3/fake/c/private-make.h +++ b/level_3/fake/c/private-make.h @@ -332,15 +332,15 @@ extern "C" { #ifndef _di_fake_make_path_t_ typedef struct { - int top; - int current; + f_file_t top; + f_file_t current; f_string_dynamics_t stack; } fake_make_path_t; #define fake_make_path_t_initialize { \ - 0, \ - 0, \ + f_file_t_initialize, \ + f_file_t_initialize, \ f_string_dynamics_t_initialize, \ } @@ -356,7 +356,7 @@ extern "C" { fake_environment_t environment; fake_make_parameter_t parameter; - fake_make_path_t path; + fake_make_path_t path; // @todo review this, check if path.current is used anymore. fll_error_print_t error; diff --git a/level_3/fake/c/private-skeleton.c b/level_3/fake/c/private-skeleton.c index 6da8a45..a9e541c 100644 --- a/level_3/fake/c/private-skeleton.c +++ b/level_3/fake/c/private-skeleton.c @@ -294,7 +294,7 @@ extern "C" { if (F_status_is_error(status)) { fll_error_file_print(data.error, F_status_set_fine(status), "f_file_write", F_true, path.string, "pre-populate", fll_error_file_type_file); - f_file_close(&file.id); + f_file_stream_close(F_true, &file); return status; } @@ -302,7 +302,7 @@ extern "C" { fprintf(data.output.stream, "File '%s' pre-populated.%c", path.string, f_string_eol[0]); } - f_file_close(&file.id); + f_file_stream_close(F_true, &file); } } else if (F_status_is_error(status)) { diff --git a/level_3/firewall/c/main.c b/level_3/firewall/c/main.c index ff0f484..1949f03 100644 --- a/level_3/firewall/c/main.c +++ b/level_3/firewall/c/main.c @@ -8,9 +8,7 @@ int main(const int argc, const f_string_t *argv) { data.process_pipe = F_true; } - f_status_t status = firewall_main(arguments, &data); - - if (F_status_is_error(status)) { + if (F_status_is_error(firewall_main(arguments, &data))) { return 1; } diff --git a/level_3/firewall/c/private-firewall.c b/level_3/firewall/c/private-firewall.c index 33c5797..687b069 100644 --- a/level_3/firewall/c/private-firewall.c +++ b/level_3/firewall/c/private-firewall.c @@ -611,12 +611,12 @@ f_return_status firewall_perform_commands(const firewall_local_data_t local, con status = F_status_set_error(status); } - f_file_close(&file.id); + f_file_stream_close(F_true, &file); } else { status = f_file_read(file, &local_buffer); - f_file_close(&file.id); + f_file_stream_close(F_true, &file); if (F_status_is_error(status)) { status = F_status_set_fine(status); @@ -1353,7 +1353,7 @@ f_return_status firewall_buffer_rules(const f_string_t filename, const bool opti status = f_file_read(file, &local->buffer); - f_file_close(&file.id); + f_file_stream_close(F_true, &file); if (F_status_is_error(status)) { status = F_status_set_fine(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 5ea2413..c067b83 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 @@ -363,7 +363,7 @@ extern "C" { if (F_status_is_error(status)) { fll_error_file_print(data->error, F_status_set_fine(status), "f_file_size_by_id", F_true, arguments.argv[data->remaining.array[counter]], "read", fll_error_file_type_file); - f_file_close(&file.id); + f_file_stream_close(F_true, &file); macro_fss_basic_list_read_depths_t_delete_simple(depths); fss_basic_list_read_delete_data(data); @@ -372,14 +372,14 @@ extern "C" { // Skip past empty files. if (!data->quantity.total) { - f_file_close(&file.id); + f_file_stream_close(F_true, &file); continue; } } status = f_file_read_until(file, data->quantity.total, &data->buffer); - f_file_close(&file.id); + f_file_stream_close(F_true, &file); if (F_status_is_error(status)) { fll_error_file_print(data->error, F_status_set_fine(status), "f_file_read_until", F_true, arguments.argv[data->remaining.array[counter]], "read", fll_error_file_type_file); diff --git a/level_3/fss_basic_list_read/c/main.c b/level_3/fss_basic_list_read/c/main.c index 1097eba..8bc7bb7 100644 --- a/level_3/fss_basic_list_read/c/main.c +++ b/level_3/fss_basic_list_read/c/main.c @@ -8,9 +8,7 @@ int main(const unsigned long argc, const f_string_t *argv) { data.process_pipe = F_true; } - f_status_t status = fss_basic_list_read_main(arguments, &data); - - if (F_status_is_error(status)) { + if (F_status_is_error(fss_basic_list_read_main(arguments, &data))) { return 1; } 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 9359dbc..3be7ab7 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 @@ -21,9 +21,9 @@ extern "C" { printf("%c", f_string_eol[0]); + fll_program_print_help_option(file, context, fss_basic_list_write_short_file, fss_basic_list_write_long_file, f_console_symbol_short_enable, f_console_symbol_long_enable, " Specify a file to send output to."); fll_program_print_help_option(file, context, fss_basic_list_write_short_content, fss_basic_list_write_long_content, f_console_symbol_short_enable, f_console_symbol_long_enable, " The content to output."); fll_program_print_help_option(file, context, fss_basic_list_write_short_double, fss_basic_list_write_long_double, f_console_symbol_short_enable, f_console_symbol_long_enable, " Use double quotes (default)."); - fll_program_print_help_option(file, context, fss_basic_list_write_short_file, fss_basic_list_write_long_file, f_console_symbol_short_enable, f_console_symbol_long_enable, " Specify a file to send output to."); fll_program_print_help_option(file, context, fss_basic_list_write_short_object, fss_basic_list_write_long_object, f_console_symbol_short_enable, f_console_symbol_long_enable, " The object to output."); fll_program_print_help_option(file, context, fss_basic_list_write_short_partial, fss_basic_list_write_long_partial, f_console_symbol_short_enable, f_console_symbol_long_enable, "Do not output end of object/content character."); fll_program_print_help_option(file, context, fss_basic_list_write_short_single, fss_basic_list_write_long_single, f_console_symbol_short_enable, f_console_symbol_long_enable, " Use single quotes."); @@ -226,50 +226,22 @@ extern "C" { status = f_file_open(arguments.argv[data->parameters[fss_basic_list_write_parameter_file].additional.array[0]], 0, &output); if (F_status_is_error(status)) { - status = F_status_set_fine(status); - - f_file_close(&output.id); - - if (status == F_parameter) { - fl_color_print(data->error.to.stream, data->context.set.error, "%sInvalid parameter when calling f_file_open()%c", fll_error_print_error, f_string_eol[0]); - } - else if (status == F_file_found_not) { - fl_color_print(data->error.to.stream, data->context.set.error, "%sUnable to find the file '%s'%c", fll_error_print_error, arguments.argv[data->parameters[fss_basic_list_write_parameter_file].additional.array[0]], f_string_eol[0]); - } - else if (status == F_file_open) { - fl_color_print(data->error.to.stream, data->context.set.error, "%sUnable to open the file '%s'%c", fll_error_print_error, arguments.argv[data->parameters[fss_basic_list_write_parameter_file].additional.array[0]], f_string_eol[0]); - } - else if (status == F_file_descriptor) { - fl_color_print(data->error.to.stream, data->context.set.error, "%sFile descriptor error while trying to open the file '%s'%c", fll_error_print_error, arguments.argv[data->parameters[fss_basic_list_write_parameter_file].additional.array[0]], f_string_eol[0]); - } - else { - fl_color_print(data->error.to.stream, data->context.set.error, "%sAn unhandled error (%u) has occurred while calling f_file_open()%c", fll_error_print_error, status, f_string_eol[0]); - } + fll_error_file_print(data->error, F_status_set_fine(status), "f_file_flag_append_wo", F_true, file.string, "open", fll_error_file_type_file); f_macro_string_dynamic_t_delete_simple(buffer); fss_basic_list_write_delete_data(data); - return F_status_set_error(status); + return status; } status = f_file_write(output, buffer, 0); - f_file_close(&output.id); + f_file_stream_close(F_true, &output); if (F_status_is_error(status)) { - status = F_status_set_fine(status); - - if (status == F_parameter) { - fl_color_print(data->error.to.stream, data->context.set.error, "%sInvalid parameter when calling f_file_write()%c", fll_error_print_error, f_string_eol[0]); - } - else if (status == F_file_write) { - fl_color_print(data->error.to.stream, data->context.set.error, "%sUnable to write to the file '%s'%c", fll_error_print_error, arguments.argv[data->parameters[fss_basic_list_write_parameter_file].additional.array[0]], f_string_eol[0]); - } - else { - fl_color_print(data->error.to.stream, data->context.set.error, "%sAn unhandled error (%u) has occurred while calling f_file_write()%c", fll_error_print_error, status, f_string_eol[0]); - } + fll_error_file_print(data->error, F_status_set_fine(status), "f_file_close", F_true, file.string, "close", fll_error_file_type_file); f_macro_string_dynamic_t_delete_simple(buffer); fss_basic_list_write_delete_data(data); - return F_status_set_error(status); + return status; } } else { diff --git a/level_3/fss_basic_list_write/c/fss_basic_list_write.h b/level_3/fss_basic_list_write/c/fss_basic_list_write.h index f59b6e2..c5fab01 100644 --- a/level_3/fss_basic_list_write/c/fss_basic_list_write.h +++ b/level_3/fss_basic_list_write/c/fss_basic_list_write.h @@ -54,16 +54,16 @@ extern "C" { #endif // _di_fss_basic_list_write_name_ #ifndef _di_fss_basic_list_write_defines_ + #define fss_basic_list_write_short_file "f" #define fss_basic_list_write_short_content "c" #define fss_basic_list_write_short_double "d" - #define fss_basic_list_write_short_file "f" #define fss_basic_list_write_short_object "o" #define fss_basic_list_write_short_partial "p" #define fss_basic_list_write_short_single "s" + #define fss_basic_list_write_long_file "file" #define fss_basic_list_write_long_content "content" #define fss_basic_list_write_long_double "double" - #define fss_basic_list_write_long_file "file" #define fss_basic_list_write_long_object "object" #define fss_basic_list_write_long_partial "partial" #define fss_basic_list_write_long_single "single" @@ -79,9 +79,9 @@ extern "C" { fss_basic_list_write_parameter_verbosity_debug, fss_basic_list_write_parameter_version, + fss_basic_list_write_parameter_file, fss_basic_list_write_parameter_content, fss_basic_list_write_parameter_double, - fss_basic_list_write_parameter_file, fss_basic_list_write_parameter_object, fss_basic_list_write_parameter_partial, fss_basic_list_write_parameter_single, @@ -98,9 +98,9 @@ extern "C" { f_console_parameter_t_initialize(f_console_standard_short_verbose, f_console_standard_long_verbose, 0, 0, f_console_type_inverse), \ f_console_parameter_t_initialize(f_console_standard_short_debug, f_console_standard_long_debug, 0, 0, f_console_type_inverse), \ f_console_parameter_t_initialize(f_console_standard_short_version, f_console_standard_long_version, 0, F_false, f_console_type_inverse), \ + f_console_parameter_t_initialize(fss_basic_list_write_short_file, fss_basic_list_write_long_file, 0, F_true, f_console_type_normal), \ f_console_parameter_t_initialize(fss_basic_list_write_short_content, fss_basic_list_write_long_content, 0, F_true, f_console_type_normal), \ f_console_parameter_t_initialize(fss_basic_list_write_short_double, fss_basic_list_write_long_double, 0, F_true, f_console_type_normal), \ - f_console_parameter_t_initialize(fss_basic_list_write_short_file, fss_basic_list_write_long_file, 0, F_true, f_console_type_normal), \ f_console_parameter_t_initialize(fss_basic_list_write_short_object, fss_basic_list_write_long_object, 0, F_false, f_console_type_normal), \ f_console_parameter_t_initialize(fss_basic_list_write_short_partial, fss_basic_list_write_long_partial, 0, F_true, f_console_type_normal), \ f_console_parameter_t_initialize(fss_basic_list_write_short_single, fss_basic_list_write_long_single, 0, F_true, f_console_type_normal), \ diff --git a/level_3/fss_basic_list_write/c/main.c b/level_3/fss_basic_list_write/c/main.c index 6cb043d..d7b0ee0 100644 --- a/level_3/fss_basic_list_write/c/main.c +++ b/level_3/fss_basic_list_write/c/main.c @@ -8,9 +8,7 @@ int main(const unsigned long argc, const f_string_t *argv) { data.process_pipe = F_true; } - f_status_t status = fss_basic_list_write_main(arguments, &data); - - if (F_status_is_error(status)) { + if (F_status_is_error(fss_basic_list_write_main(arguments, &data))) { return 1; } 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 f9c78f2..8eb01bf 100644 --- a/level_3/fss_basic_read/c/fss_basic_read.c +++ b/level_3/fss_basic_read/c/fss_basic_read.c @@ -363,7 +363,7 @@ extern "C" { if (F_status_is_error(status)) { fll_error_file_print(data->error, F_status_set_fine(status), "f_file_size_by_id", F_true, arguments.argv[data->remaining.array[counter]], "read", fll_error_file_type_file); - f_file_close(&file.id); + f_file_stream_close(F_true, &file); macro_fss_basic_read_depths_t_delete_simple(depths); fss_basic_read_delete_data(data); @@ -372,14 +372,14 @@ extern "C" { // Skip past empty files. if (!data->quantity.total) { - f_file_close(&file.id); + f_file_stream_close(F_true, &file); continue; } } status = f_file_read_until(file, data->quantity.total, &data->buffer); - f_file_close(&file.id); + f_file_stream_close(F_true, &file); if (F_status_is_error(status)) { fll_error_file_print(data->error, F_status_set_fine(status), "f_file_read_until", F_true, arguments.argv[data->remaining.array[counter]], "read", fll_error_file_type_file); diff --git a/level_3/fss_basic_read/c/main.c b/level_3/fss_basic_read/c/main.c index 4fe0c7a..8c53a8c 100644 --- a/level_3/fss_basic_read/c/main.c +++ b/level_3/fss_basic_read/c/main.c @@ -8,9 +8,7 @@ int main(const unsigned long argc, const f_string_t *argv) { data.process_pipe = F_true; } - f_status_t status = fss_basic_read_main(arguments, &data); - - if (F_status_is_error(status)) { + if (F_status_is_error(fss_basic_read_main(arguments, &data))) { return 1; } 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 74cb2fb..a5ab880 100644 --- a/level_3/fss_basic_write/c/fss_basic_write.c +++ b/level_3/fss_basic_write/c/fss_basic_write.c @@ -21,9 +21,9 @@ extern "C" { printf("%c", f_string_eol[0]); + fll_program_print_help_option(file, context, fss_basic_write_short_file, fss_basic_write_long_file, f_console_symbol_short_enable, f_console_symbol_long_enable, " Specify a file to send output to."); fll_program_print_help_option(file, context, fss_basic_write_short_content, fss_basic_write_long_content, f_console_symbol_short_enable, f_console_symbol_long_enable, " The content to output."); fll_program_print_help_option(file, context, fss_basic_write_short_double, fss_basic_write_long_double, f_console_symbol_short_enable, f_console_symbol_long_enable, " Use double quotes (default)."); - fll_program_print_help_option(file, context, fss_basic_write_short_file, fss_basic_write_long_file, f_console_symbol_short_enable, f_console_symbol_long_enable, " Specify a file to send output to."); fll_program_print_help_option(file, context, fss_basic_write_short_object, fss_basic_write_long_object, f_console_symbol_short_enable, f_console_symbol_long_enable, " The object to output."); fll_program_print_help_option(file, context, fss_basic_write_short_partial, fss_basic_write_long_partial, f_console_symbol_short_enable, f_console_symbol_long_enable, "Do not output end of object/content character."); fll_program_print_help_option(file, context, fss_basic_write_short_single, fss_basic_write_long_single, f_console_symbol_short_enable, f_console_symbol_long_enable, " Use single quotes."); @@ -101,7 +101,7 @@ extern "C" { } f_array_length_t counter = 0; - bool object = (data->parameters[fss_basic_write_parameter_object].result == f_console_result_found); + bool object = data->parameters[fss_basic_write_parameter_object].result == f_console_result_found; f_string_dynamic_t buffer = f_string_dynamic_t_initialize; f_string_range_t range = f_string_range_t_initialize; @@ -115,23 +115,7 @@ extern "C" { status = f_file_read(file, &input); if (F_status_is_error(status)) { - status = F_status_set_fine(status); - - if (status == F_parameter) { - fl_color_print(data->error.to.stream, data->context.set.error, "%sInvalid parameter when calling f_file_open()%c", fll_error_print_error, f_string_eol[0]); - } - else if (status == F_file_found_not) { - fl_color_print(data->error.to.stream, data->context.set.error, "%sUnable to find the file '%s'%c", fll_error_print_error, "-", f_string_eol[0]); - } - else if (status == F_file_open) { - fl_color_print(data->error.to.stream, data->context.set.error, "%sUnable to open the file '%s'%c", fll_error_print_error, "-", f_string_eol[0]); - } - else if (status == F_file_descriptor) { - fl_color_print(data->error.to.stream, data->context.set.error, "%sFile descriptor error while trying to open the file '%s'%c", fll_error_print_error, "-", f_string_eol[0]); - } - else { - fl_color_print(data->error.to.stream, data->context.set.error, "%sAn unhandled error (%u) has occurred while calling f_file_open()%c", fll_error_print_error, status, f_string_eol[0]); - } + fll_error_file_print(data->error, F_status_set_fine(status), "f_file_read", F_true, file.string, "read", fll_error_file_type_file); f_macro_string_dynamic_t_delete_simple(input); fss_basic_write_delete_data(data); @@ -215,25 +199,7 @@ extern "C" { status = f_file_open(arguments.argv[data->parameters[fss_basic_write_parameter_file].additional.array[0]], 0, &output); if (F_status_is_error(status)) { - status = F_status_set_fine(status); - - f_file_close(&output.id); - - if (status == F_parameter) { - fl_color_print(data->error.to.stream, data->context.set.error, "%sInvalid parameter when calling f_file_open()%c", fll_error_print_error, f_string_eol[0]); - } - else if (status == F_file_found_not) { - fl_color_print(data->error.to.stream, data->context.set.error, "%sUnable to find the file '%s'%c", fll_error_print_error, arguments.argv[data->parameters[fss_basic_write_parameter_file].additional.array[0]], f_string_eol[0]); - } - else if (status == F_file_open) { - fl_color_print(data->error.to.stream, data->context.set.error, "%sUnable to open the file '%s'%c", fll_error_print_error, arguments.argv[data->parameters[fss_basic_write_parameter_file].additional.array[0]], f_string_eol[0]); - } - else if (status == F_file_descriptor) { - fl_color_print(data->error.to.stream, data->context.set.error, "%sFile descriptor error while trying to open the file '%s'%c", fll_error_print_error, arguments.argv[data->parameters[fss_basic_write_parameter_file].additional.array[0]], f_string_eol[0]); - } - else { - fl_color_print(data->error.to.stream, data->context.set.error, "%sAn unhandled error (%u) has occurred while calling f_file_open()%c", fll_error_print_error, status, f_string_eol[0]); - } + fll_error_file_print(data->error, F_status_set_fine(status), "f_file_open", F_true, file.string, "open", fll_error_file_type_file); f_macro_string_dynamic_t_delete_simple(buffer); fss_basic_write_delete_data(data); @@ -241,24 +207,14 @@ extern "C" { } status = f_file_write(output, buffer, 0); - f_file_close(&output.id); + f_file_stream_close(F_true, &output); if (F_status_is_error(status)) { - status = F_status_set_fine(status); - - if (status == F_parameter) { - fl_color_print(data->error.to.stream, data->context.set.error, "%sInvalid parameter when calling f_file_write()%c", fll_error_print_error, f_string_eol[0]); - } - else if (status == F_file_write) { - fl_color_print(data->error.to.stream, data->context.set.error, "%sUnable to write to the file '%s'%c", fll_error_print_error, arguments.argv[data->parameters[fss_basic_write_parameter_file].additional.array[0]], f_string_eol[0]); - } - else { - fl_color_print(data->error.to.stream, data->context.set.error, "%sAn unhandled error (%u) has occurred while calling f_file_write()%c", fll_error_print_error, status, f_string_eol[0]); - } + fll_error_file_print(data->error, F_status_set_fine(status), "f_file_stream_close", F_true, file.string, "close", fll_error_file_type_file); f_macro_string_dynamic_t_delete_simple(buffer); fss_basic_write_delete_data(data); - return F_status_set_error(status); + return status; } } else { diff --git a/level_3/fss_basic_write/c/fss_basic_write.h b/level_3/fss_basic_write/c/fss_basic_write.h index b7a0968..ade55a1 100644 --- a/level_3/fss_basic_write/c/fss_basic_write.h +++ b/level_3/fss_basic_write/c/fss_basic_write.h @@ -53,16 +53,16 @@ extern "C" { #endif // _di_fss_basic_write_name_ #ifndef _di_fss_basic_write_defines_ + #define fss_basic_write_short_file "f" #define fss_basic_write_short_content "c" #define fss_basic_write_short_double "d" - #define fss_basic_write_short_file "f" #define fss_basic_write_short_object "o" #define fss_basic_write_short_partial "p" #define fss_basic_write_short_single "s" + #define fss_basic_write_long_file "file" #define fss_basic_write_long_content "content" #define fss_basic_write_long_double "double" - #define fss_basic_write_long_file "file" #define fss_basic_write_long_object "object" #define fss_basic_write_long_partial "partial" #define fss_basic_write_long_single "single" @@ -78,9 +78,9 @@ extern "C" { fss_basic_write_parameter_verbosity_debug, fss_basic_write_parameter_version, + fss_basic_write_parameter_file, fss_basic_write_parameter_content, fss_basic_write_parameter_double, - fss_basic_write_parameter_file, fss_basic_write_parameter_object, fss_basic_write_parameter_partial, fss_basic_write_parameter_single, @@ -97,9 +97,9 @@ extern "C" { f_console_parameter_t_initialize(f_console_standard_short_verbose, f_console_standard_long_verbose, 0, 0, f_console_type_inverse), \ f_console_parameter_t_initialize(f_console_standard_short_debug, f_console_standard_long_debug, 0, 0, f_console_type_inverse), \ f_console_parameter_t_initialize(f_console_standard_short_version, f_console_standard_long_version, 0, F_false, f_console_type_inverse), \ + f_console_parameter_t_initialize(fss_basic_write_short_file, fss_basic_write_long_file, 0, F_true, f_console_type_normal), \ f_console_parameter_t_initialize(fss_basic_write_short_content, fss_basic_write_long_content, 0, F_true, f_console_type_normal), \ f_console_parameter_t_initialize(fss_basic_write_short_double, fss_basic_write_long_double, 0, F_true, f_console_type_normal), \ - f_console_parameter_t_initialize(fss_basic_write_short_file, fss_basic_write_long_file, 0, F_true, f_console_type_normal), \ f_console_parameter_t_initialize(fss_basic_write_short_object, fss_basic_write_long_object, 0, F_false, f_console_type_normal), \ f_console_parameter_t_initialize(fss_basic_write_short_partial, fss_basic_write_long_partial, 0, F_true, f_console_type_normal), \ f_console_parameter_t_initialize(fss_basic_write_short_single, fss_basic_write_long_single, 0, F_true, f_console_type_normal), \ diff --git a/level_3/fss_basic_write/c/main.c b/level_3/fss_basic_write/c/main.c index 2ecc391..bd67e87 100644 --- a/level_3/fss_basic_write/c/main.c +++ b/level_3/fss_basic_write/c/main.c @@ -4,9 +4,7 @@ int main(const unsigned long argc, const f_string_t *argv) { const f_console_arguments_t arguments = { argc, argv }; fss_basic_write_data data = fss_basic_write_data_initialize; - f_status_t status = fss_basic_write_main(arguments, &data); - - if (F_status_is_error(status)) { + if (F_status_is_error(fss_basic_write_main(arguments, &data))) { return 1; } 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 30d555e..ae48f28 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 @@ -181,14 +181,14 @@ extern "C" { if (data->parameters[fss_extended_list_read_parameter_help].result == f_console_result_found) { fss_extended_list_read_print_help(data->output, data->context); - fss_extended_list_write_delete_data(data); + fss_extended_list_read_delete_data(data); return F_none; } if (data->parameters[fss_extended_list_read_parameter_version].result == f_console_result_found) { fll_program_print_version(data->output, fss_extended_list_read_version); - fss_extended_list_write_delete_data(data); + fss_extended_list_read_delete_data(data); return F_none; } @@ -347,7 +347,7 @@ extern "C" { if (F_status_is_error(status)) { fll_error_file_print(data->error, F_status_set_fine(status), "f_file_size_by_id", F_true, arguments.argv[data->remaining.array[counter]], "read", fll_error_file_type_file); - f_file_close(&file.id); + f_file_stream_close(F_true, &file); macro_fss_extended_list_read_depths_t_delete_simple(depths); fss_extended_list_read_delete_data(data); @@ -356,14 +356,14 @@ extern "C" { // Skip past empty files. if (!data->quantity.total) { - f_file_close(&file.id); + f_file_stream_close(F_true, &file); continue; } } status = f_file_read_until(file, data->quantity.total, &data->buffer); - f_file_close(&file.id); + f_file_stream_close(F_true, &file); if (F_status_is_error(status)) { fll_error_file_print(data->error, F_status_set_fine(status), "f_file_read_until", F_true, arguments.argv[data->remaining.array[counter]], "read", fll_error_file_type_file); diff --git a/level_3/fss_extended_list_read/c/main.c b/level_3/fss_extended_list_read/c/main.c index 07994ba..a53a249 100644 --- a/level_3/fss_extended_list_read/c/main.c +++ b/level_3/fss_extended_list_read/c/main.c @@ -8,9 +8,7 @@ int main(const unsigned long argc, const f_string_t *argv) { data.process_pipe = F_true; } - f_status_t status = fss_extended_list_read_main(arguments, &data); - - if (F_status_is_error(status)) { + if (F_status_is_error(fss_extended_list_read_main(arguments, &data))) { return 1; } 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 1476983..5283a59 100644 --- a/level_3/fss_extended_read/c/fss_extended_read.c +++ b/level_3/fss_extended_read/c/fss_extended_read.c @@ -364,7 +364,7 @@ extern "C" { if (F_status_is_error(status)) { fll_error_file_print(data->error, F_status_set_fine(status), "f_file_size_by_id", F_true, arguments.argv[data->remaining.array[counter]], "read", fll_error_file_type_file); - f_file_close(&file.id); + f_file_stream_close(F_true, &file); macro_fss_extended_read_depths_t_delete_simple(depths); fss_extended_read_delete_data(data); @@ -373,14 +373,14 @@ extern "C" { // Skip past empty files. if (!data->quantity.total) { - f_file_close(&file.id); + f_file_stream_close(F_true, &file); continue; } } status = f_file_read_until(file, data->quantity.total, &data->buffer); - f_file_close(&file.id); + f_file_stream_close(F_true, &file); if (F_status_is_error(status)) { fll_error_file_print(data->error, F_status_set_fine(status), "f_file_read_until", F_true, arguments.argv[data->remaining.array[counter]], "read", fll_error_file_type_file); diff --git a/level_3/fss_extended_read/c/main.c b/level_3/fss_extended_read/c/main.c index 4846123..871b543 100644 --- a/level_3/fss_extended_read/c/main.c +++ b/level_3/fss_extended_read/c/main.c @@ -8,9 +8,7 @@ int main(const unsigned long argc, const f_string_t *argv) { data.process_pipe = F_true; } - f_status_t status = fss_extended_read_main(arguments, &data); - - if (F_status_is_error(status)) { + if (F_status_is_error(fss_extended_read_main(arguments, &data))) { return 1; } 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 267f103..3e351c1 100644 --- a/level_3/fss_extended_write/c/fss_extended_write.c +++ b/level_3/fss_extended_write/c/fss_extended_write.c @@ -21,9 +21,9 @@ extern "C" { printf("%c", f_string_eol[0]); + fll_program_print_help_option(file, context, fss_extended_write_short_file, fss_extended_write_long_file, f_console_symbol_short_enable, f_console_symbol_long_enable, " Specify a file to send output to."); fll_program_print_help_option(file, context, fss_extended_write_short_content, fss_extended_write_long_content, f_console_symbol_short_enable, f_console_symbol_long_enable, " The content to output."); fll_program_print_help_option(file, context, fss_extended_write_short_double, fss_extended_write_long_double, f_console_symbol_short_enable, f_console_symbol_long_enable, " Use double quotes (default)."); - fll_program_print_help_option(file, context, fss_extended_write_short_file, fss_extended_write_long_file, f_console_symbol_short_enable, f_console_symbol_long_enable, " Specify a file to send output to."); fll_program_print_help_option(file, context, fss_extended_write_short_object, fss_extended_write_long_object, f_console_symbol_short_enable, f_console_symbol_long_enable, " The object to output."); fll_program_print_help_option(file, context, fss_extended_write_short_partial, fss_extended_write_long_partial, f_console_symbol_short_enable, f_console_symbol_long_enable, "Do not output end of object/content character."); fll_program_print_help_option(file, context, fss_extended_write_short_single, fss_extended_write_long_single, f_console_symbol_short_enable, f_console_symbol_long_enable, " Use single quotes."); @@ -101,7 +101,7 @@ extern "C" { } f_array_length_t counter = 0; - bool object = (data->parameters[fss_extended_write_parameter_object].result == f_console_result_found); + bool object = data->parameters[fss_extended_write_parameter_object].result == f_console_result_found; f_string_dynamic_t buffer = f_string_dynamic_t_initialize; f_string_range_t range = f_string_range_t_initialize; @@ -115,27 +115,11 @@ extern "C" { status = f_file_read(file, &input); if (F_status_is_error(status)) { - status = F_status_set_fine(status); - - if (status == F_parameter) { - fl_color_print(data->error.to.stream, data->context.set.error, "%sInvalid parameter when calling f_file_open()%c", fll_error_print_error, f_string_eol[0]); - } - else if (status == F_file_found_not) { - fl_color_print(data->error.to.stream, data->context.set.error, "%sUnable to find the file '%s'%c", fll_error_print_error, "-", f_string_eol[0]); - } - else if (status == F_file_open) { - fl_color_print(data->error.to.stream, data->context.set.error, "%sUnable to open the file '%s'%c", fll_error_print_error, "-", f_string_eol[0]); - } - else if (status == F_file_descriptor) { - fl_color_print(data->error.to.stream, data->context.set.error, "%sFile descriptor error while trying to open the file '%s'%c", fll_error_print_error, "-", f_string_eol[0]); - } - else { - fl_color_print(data->error.to.stream, data->context.set.error, "%sAn unhandled error (%u) has occurred while calling f_file_open()%c", fll_error_print_error, status, f_string_eol[0]); - } + fll_error_file_print(data->error, F_status_set_fine(status), "f_file_read", F_true, file.string, "read", fll_error_file_type_file); f_macro_string_dynamic_t_delete_simple(input); fss_extended_write_delete_data(data); - return F_status_set_error(status); + return status; } if (input.used) { @@ -267,25 +251,7 @@ extern "C" { status = f_file_open(arguments.argv[data->parameters[fss_extended_write_parameter_file].additional.array[0]], 0, &output); if (F_status_is_error(status)) { - status = F_status_set_fine(status); - - f_file_close(&output.id); - - if (status == F_parameter) { - fl_color_print(data->error.to.stream, data->context.set.error, "%sInvalid parameter when calling f_file_open()%c", fll_error_print_error, f_string_eol[0]); - } - else if (status == F_file_found_not) { - fl_color_print(data->error.to.stream, data->context.set.error, "%sUnable to find the file '%s'%c", fll_error_print_error, arguments.argv[data->parameters[fss_extended_write_parameter_file].additional.array[0]], f_string_eol[0]); - } - else if (status == F_file_open) { - fl_color_print(data->error.to.stream, data->context.set.error, "%sUnable to open the file '%s'%c", fll_error_print_error, arguments.argv[data->parameters[fss_extended_write_parameter_file].additional.array[0]], f_string_eol[0]); - } - else if (status == F_file_descriptor) { - fl_color_print(data->error.to.stream, data->context.set.error, "%sFile descriptor error while trying to open the file '%s'%c", fll_error_print_error, arguments.argv[data->parameters[fss_extended_write_parameter_file].additional.array[0]], f_string_eol[0]); - } - else { - fl_color_print(data->error.to.stream, data->context.set.error, "%sAn unhandled error (%u) has occurred while calling f_file_open()%c", fll_error_print_error, status, f_string_eol[0]); - } + fll_error_file_print(data->error, F_status_set_fine(status), "f_file_open", F_true, file.string, "open", fll_error_file_type_file); f_macro_string_dynamic_t_delete_simple(buffer); fss_extended_write_delete_data(data); @@ -293,24 +259,14 @@ extern "C" { } status = f_file_write(output, buffer, 0); - f_file_close(&output.id); + f_file_stream_close(F_true, &output); if (F_status_is_error(status)) { - status = F_status_set_fine(status); - - if (status == F_parameter) { - fl_color_print(data->error.to.stream, data->context.set.error, "%sInvalid parameter when calling f_file_write()%c", fll_error_print_error, f_string_eol[0]); - } - else if (status == F_file_write) { - fl_color_print(data->error.to.stream, data->context.set.error, "%sUnable to write to the file '%s'%c", fll_error_print_error, arguments.argv[data->parameters[fss_extended_write_parameter_file].additional.array[0]], f_string_eol[0]); - } - else { - fl_color_print(data->error.to.stream, data->context.set.error, "%sAn unhandled error (%u) has occurred while calling f_file_write()%c", fll_error_print_error, status, f_string_eol[0]); - } + fll_error_file_print(data->error, F_status_set_fine(status), "f_file_close", F_true, file.string, "close", fll_error_file_type_file); f_macro_string_dynamic_t_delete_simple(buffer); fss_extended_write_delete_data(data); - return F_status_set_error(status); + return status; } } else { diff --git a/level_3/fss_extended_write/c/fss_extended_write.h b/level_3/fss_extended_write/c/fss_extended_write.h index 92486e6..523b478 100644 --- a/level_3/fss_extended_write/c/fss_extended_write.h +++ b/level_3/fss_extended_write/c/fss_extended_write.h @@ -53,16 +53,16 @@ extern "C" { #endif // _di_fss_extended_write_name_ #ifndef _di_fss_extended_write_defines_ + #define fss_extended_write_short_file "f" #define fss_extended_write_short_content "c" #define fss_extended_write_short_double "d" - #define fss_extended_write_short_file "f" #define fss_extended_write_short_object "o" #define fss_extended_write_short_partial "p" #define fss_extended_write_short_single "s" + #define fss_extended_write_long_file "file" #define fss_extended_write_long_content "content" #define fss_extended_write_long_double "double" - #define fss_extended_write_long_file "file" #define fss_extended_write_long_object "object" #define fss_extended_write_long_partial "partial" #define fss_extended_write_long_single "single" @@ -78,9 +78,9 @@ extern "C" { fss_extended_write_parameter_verbosity_debug, fss_extended_write_parameter_version, + fss_extended_write_parameter_file, fss_extended_write_parameter_content, fss_extended_write_parameter_double, - fss_extended_write_parameter_file, fss_extended_write_parameter_object, fss_extended_write_parameter_partial, fss_extended_write_parameter_single, @@ -97,9 +97,9 @@ extern "C" { f_console_parameter_t_initialize(f_console_standard_short_verbose, f_console_standard_long_verbose, 0, 0, f_console_type_inverse), \ f_console_parameter_t_initialize(f_console_standard_short_debug, f_console_standard_long_debug, 0, 0, f_console_type_inverse), \ f_console_parameter_t_initialize(f_console_standard_short_version, f_console_standard_long_version, 0, F_false, f_console_type_inverse), \ + f_console_parameter_t_initialize(fss_extended_write_short_file, fss_extended_write_long_file, 0, F_true, f_console_type_normal), \ f_console_parameter_t_initialize(fss_extended_write_short_content, fss_extended_write_long_content, 0, F_true, f_console_type_normal), \ f_console_parameter_t_initialize(fss_extended_write_short_double, fss_extended_write_long_double, 0, F_true, f_console_type_normal), \ - f_console_parameter_t_initialize(fss_extended_write_short_file, fss_extended_write_long_file, 0, F_true, f_console_type_normal), \ f_console_parameter_t_initialize(fss_extended_write_short_object, fss_extended_write_long_object, 0, F_false, f_console_type_normal), \ f_console_parameter_t_initialize(fss_extended_write_short_partial, fss_extended_write_long_partial, 0, F_false, f_console_type_normal), \ f_console_parameter_t_initialize(fss_extended_write_short_single, fss_extended_write_long_single, 0, F_true, f_console_type_normal), \ diff --git a/level_3/fss_extended_write/c/main.c b/level_3/fss_extended_write/c/main.c index 24246a0..5f78046 100644 --- a/level_3/fss_extended_write/c/main.c +++ b/level_3/fss_extended_write/c/main.c @@ -8,9 +8,7 @@ int main(const unsigned long argc, const f_string_t *argv) { data.process_pipe = F_true; } - f_status_t status = fss_extended_write_main(arguments, &data); - - if (F_status_is_error(status)) { + if (F_status_is_error(fss_extended_write_main(arguments, &data))) { return 1; } diff --git a/level_3/fss_status_code/c/main.c b/level_3/fss_status_code/c/main.c index 1ebd618..765f96f 100644 --- a/level_3/fss_status_code/c/main.c +++ b/level_3/fss_status_code/c/main.c @@ -8,7 +8,7 @@ int main(const unsigned long argc, const f_string_t *argv) { data.process_pipe = F_true; } - f_status_t status = fss_status_code_main(arguments, &data); + const f_status_t status = fss_status_code_main(arguments, &data); if (F_status_is_error(status) || status == F_false) { return 1; diff --git a/level_3/iki_read/c/iki_read.c b/level_3/iki_read/c/iki_read.c index 5c4d74c..15747a7 100644 --- a/level_3/iki_read/c/iki_read.c +++ b/level_3/iki_read/c/iki_read.c @@ -409,19 +409,19 @@ extern "C" { if (F_status_is_error(status)) { fll_error_file_print(data->error, F_status_set_fine(status), "f_file_size_by_id", F_true, arguments.argv[data->remaining.array[i]], "process", fll_error_file_type_file); - f_file_close(&file.id); + f_file_stream_close(F_true, &file); break; } // Skip past empty files. if (!total) { - f_file_close(&file.id); + f_file_stream_close(F_true, &file); continue; } status = f_file_read_until(file, total, &data->buffer); - f_file_close(&file.id); + f_file_stream_close(F_true, &file); if (F_status_is_error(status)) { fll_error_file_print(data->error, F_status_set_fine(status), "f_file_read_until", F_true, arguments.argv[data->remaining.array[i]], "process", fll_error_file_type_file); diff --git a/level_3/iki_read/c/main.c b/level_3/iki_read/c/main.c index 35d373b..d0c4093 100644 --- a/level_3/iki_read/c/main.c +++ b/level_3/iki_read/c/main.c @@ -8,9 +8,7 @@ int main(const unsigned long argc, const f_string_t *argv) { data.process_pipe = F_true; } - f_status_t status = iki_read_main(arguments, &data); - - if (F_status_is_error(status)) { + if (F_status_is_error(iki_read_main(arguments, &data))) { return 1; } diff --git a/level_3/iki_write/c/iki_write.c b/level_3/iki_write/c/iki_write.c index 30e3808..a81ed74 100644 --- a/level_3/iki_write/c/iki_write.c +++ b/level_3/iki_write/c/iki_write.c @@ -28,7 +28,7 @@ extern "C" { fll_program_print_help_option(file, context, iki_write_short_object, iki_write_long_object, f_console_symbol_short_enable, f_console_symbol_long_enable, " The object to output."); fll_program_print_help_option(file, context, iki_write_short_single, iki_write_long_single, f_console_symbol_short_enable, f_console_symbol_long_enable, " Use single quotes."); - fll_program_print_help_usage(file, context, iki_write_name, "filename(s)"); + fll_program_print_help_usage(file, context, iki_write_name, ""); fl_color_print(f_type_output, context.set.important, " Notes:"); @@ -228,10 +228,10 @@ extern "C" { f_string_dynamic_t escaped = f_string_dynamic_t_initialize; if (data->process_pipe) { - f_file_t file = f_file_t_initialize; + f_file_t input = f_file_t_initialize; - file.id = f_type_descriptor_input; - file.size_read = 1; + input.id = f_type_descriptor_input; + input.size_read = 1; f_string_dynamic_t buffer = f_string_dynamic_t_initialize; f_string_dynamic_t object = f_string_dynamic_t_initialize; @@ -247,7 +247,7 @@ extern "C" { for (f_status_t status_pipe = F_none; ; ) { if (status_pipe != F_none_eof) { - status_pipe = f_file_read(file, &buffer); + status_pipe = f_file_read(input, &buffer); if (F_status_is_error(status_pipe)) { fll_error_file_print(data->error, F_status_set_fine(status), "f_file_read_to", F_true, "-", "read", fll_error_file_type_pipe); @@ -307,10 +307,10 @@ extern "C" { } } - status = iki_write_process(*data, object, content, quote, output.id, &escaped); + status = iki_write_process(*data, data->output, object, content, quote, &escaped); if (F_status_is_error(status)) break; - dprintf(output.id, "%c", f_string_eol[0]); + fprintf(output.stream, "%c", f_string_eol[0]); object_ended = F_false; } @@ -367,10 +367,10 @@ extern "C" { content.used = strnlen(content.string, f_console_length_size); content.size = content.used; - status = iki_write_process(*data, object, content, quote, output.id, &escaped); + status = iki_write_process(*data, data->output, object, content, quote, &escaped); if (F_status_is_error(status)) break; - dprintf(output.id, "%c", f_string_eol[0]); + fprintf(output.stream, "%c", f_string_eol[0]); } // for // ensure there is always a newline at the end, unless in quiet mode. @@ -384,7 +384,7 @@ extern "C" { if (data->parameters[iki_write_parameter_file].result == f_console_result_additional) { if (output.id != -1) { - f_file_close(&output.id); + f_file_stream_close(F_true, &output); } } diff --git a/level_3/iki_write/c/main.c b/level_3/iki_write/c/main.c index 1ef60bc..f86f945 100644 --- a/level_3/iki_write/c/main.c +++ b/level_3/iki_write/c/main.c @@ -8,9 +8,7 @@ int main(const unsigned long argc, const f_string_t *argv) { data.process_pipe = F_true; } - f_status_t status = iki_write_main(arguments, &data); - - if (F_status_is_error(status)) { + if (F_status_is_error(iki_write_main(arguments, &data))) { return 1; } diff --git a/level_3/iki_write/c/private-iki_write.c b/level_3/iki_write/c/private-iki_write.c index 6dcd094..1983f90 100644 --- a/level_3/iki_write/c/private-iki_write.c +++ b/level_3/iki_write/c/private-iki_write.c @@ -6,7 +6,7 @@ extern "C" { #endif #ifndef _di_iki_write_process_ - f_return_status iki_write_process(const iki_write_data_t data, const f_string_static_t object, const f_string_static_t content, const uint8_t quote, const int output, f_string_dynamic_t *escaped) { + f_return_status iki_write_process(const iki_write_data_t data, const f_file_t output, const f_string_static_t object, const f_string_static_t content, const uint8_t quote, f_string_dynamic_t *escaped) { if (!object.used) { if (data.error.verbosity != f_console_verbosity_quiet) { @@ -51,7 +51,7 @@ extern "C" { return F_status_set_error(F_failure); } - dprintf(output, "%s%c%c%s%c", object.string, f_iki_syntax_separator, quote, escaped->string, quote); + fprintf(output.stream, "%s%c%c%s%c", object.string, f_iki_syntax_separator, quote, escaped->string, quote); return F_none; } diff --git a/level_3/iki_write/c/private-iki_write.h b/level_3/iki_write/c/private-iki_write.h index 4adf626..ba70026 100644 --- a/level_3/iki_write/c/private-iki_write.h +++ b/level_3/iki_write/c/private-iki_write.h @@ -17,6 +17,8 @@ extern "C" { * * @param data * The program data. + * @param output + * The file to output to. * @param object * The object to validate and print. * @param content @@ -24,8 +26,6 @@ extern "C" { * @param quote * The quote character to use. * This is either f_iki_syntax_quote_single or f_iki_syntax_quote_double. - * @param output - * The output file descriptor to print processed iki text to. * @param escaped * A string buffer used as a string cache to save the string into while escaping. * @@ -34,7 +34,7 @@ extern "C" { * F_failure (with error bit) for any othe failure. */ #ifndef _di_iki_write_process_ - extern f_return_status iki_write_process(const iki_write_data_t data, const f_string_static_t object, const f_string_static_t content, const uint8_t quote, const int output, f_string_dynamic_t *escaped) f_gcc_attribute_visibility_internal; + extern f_return_status iki_write_process(const iki_write_data_t data, const f_file_t output, const f_string_static_t object, const f_string_static_t content, const uint8_t quote, f_string_dynamic_t *escaped) f_gcc_attribute_visibility_internal; #endif // _di_iki_write_process_ #ifdef __cplusplus diff --git a/level_3/init/c/main.c b/level_3/init/c/main.c index fc927cb..a603896 100644 --- a/level_3/init/c/main.c +++ b/level_3/init/c/main.c @@ -8,9 +8,7 @@ int main(const unsigned long argc, const f_string_t *argv) { data.process_pipe = F_true; } - f_status_t status = init_main(arguments, &data); - - if (F_status_is_error(status)) { + if (F_status_is_error(init_main(arguments, &data))) { return 1; } diff --git a/level_3/init/c/private-init.c b/level_3/init/c/private-init.c index 750e946..eea8c53 100644 --- a/level_3/init/c/private-init.c +++ b/level_3/init/c/private-init.c @@ -40,10 +40,10 @@ f_macro_file_t_reset_position(quantity, file) - fflush(stdout); + fflush(data.output.stream); status = f_file_read_until(file, quantity, buffer); - f_file_close(&file.id); + f_file_stream_close(F_true, &file); if (F_status_is_error(status)) { status = F_status_set_fine(status); diff --git a/level_3/status_code/c/main.c b/level_3/status_code/c/main.c index 8973b92..19775d1 100644 --- a/level_3/status_code/c/main.c +++ b/level_3/status_code/c/main.c @@ -8,7 +8,7 @@ int main(const unsigned long argc, const f_string_t *argv) { data.process_pipe = F_true; } - f_status_t status = status_code_main(arguments, &data); + const f_status_t status = status_code_main(arguments, &data); if (F_status_is_error(status) || status == F_false) { return 1;