From 137993ddcbca1d3f29196d2ba44628c4f73c7f6c Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Sat, 10 Mar 2012 17:44:58 -0600 Subject: [PATCH] Update: add support for reading from fifos In particular, this is used for reading from piped files, namely stdin. --- level_0/f_file/c/file.c | 27 ++++++++++++++++++++++++++ level_0/f_file/c/file.h | 5 +++++ level_1/fl_file/c/file.c | 50 +++++++++++++++++++++++++++++++++++++++++++++--- level_1/fl_file/c/file.h | 4 ++++ 4 files changed, 83 insertions(+), 3 deletions(-) diff --git a/level_0/f_file/c/file.c b/level_0/f_file/c/file.c index b2bc806..2e58db2 100644 --- a/level_0/f_file/c/file.c +++ b/level_0/f_file/c/file.c @@ -131,6 +131,33 @@ extern "C"{ } #endif // _di_f_file_read_ +#ifndef _di_f_file_read_fifo_ + f_return_status f_file_read_fifo(f_file *file_information, f_dynamic_string *buffer){ + #ifndef _di_level_0_parameter_checking_ + if (file_information == f_null) return f_invalid_parameter; + #endif // _di_level_0_parameter_checking_ + + if (file_information->file == 0) return f_file_not_open; + + f_s_int result = 0; + + // now do the actual read + result = fread(buffer->string + buffer->used, file_information->byte_size, buffer->size - 1, file_information->file); + + if (file_information->file == 0) return f_file_read_error; + if (ferror(file_information->file) != 0) return f_file_read_error; + + buffer->used += result; + + // make sure to communicate that we are done without a problem and the eof was reached + if (feof(file_information->file)){ + return f_none_on_eof; + } + + return f_none; + } +#endif // _di_f_file_read_fifo_ + #ifdef __cplusplus } // extern "C" #endif diff --git a/level_0/f_file/c/file.h b/level_0/f_file/c/file.h index bfcb2d0..62d3db0 100644 --- a/level_0/f_file/c/file.h +++ b/level_0/f_file/c/file.h @@ -206,6 +206,11 @@ extern "C"{ extern f_return_status f_file_read(f_file *file_information, f_dynamic_string *buffer, const f_file_position location); #endif // _di_f_file_read_ +#ifndef _di_f_file_read_fifo_ + // read a given amount of data from the buffer, will not auto seek + extern f_return_status f_file_read_fifo(f_file *file_information, f_dynamic_string *buffer); +#endif // _di_f_file_read_fifo_ + #ifdef __cplusplus } // extern "C" #endif diff --git a/level_1/fl_file/c/file.c b/level_1/fl_file/c/file.c index d18cc0f..1e69f74 100644 --- a/level_1/fl_file/c/file.c +++ b/level_1/fl_file/c/file.c @@ -36,10 +36,12 @@ extern "C"{ // populate the buffer do{ - f_resize_dynamic_string(status, (*buffer), size); + if (buffer->size < size){ + f_resize_dynamic_string(status, (*buffer), size); - if (f_macro_test_for_allocation_errors(status)){ - return status; + if (f_macro_test_for_allocation_errors(status)){ + return status; + } } status = f_file_read(&file, buffer, position); @@ -63,6 +65,48 @@ extern "C"{ } #endif // _di_fl_file_read_ +#ifndef _di_fl_file_read_fifo_ + f_return_status fl_file_read_fifo(f_file file, f_dynamic_string *buffer){ + #ifndef _di_level_1_parameter_checking_ + if (buffer == f_null) return f_invalid_parameter; + #endif // _di_level_1_parameter_checking_ + + if (file.file == 0) return f_file_not_open; + + f_status status = f_status_initialize; + f_string_length size = f_string_length_initialize; + + size = f_file_default_read_size; + + // populate the buffer + do { + if (buffer->size < size){ + f_resize_dynamic_string(status, (*buffer), size); + + if (f_macro_test_for_allocation_errors(status)){ + return status; + } + } + + status = f_file_read_fifo(&file, buffer); + + if (status == f_none_on_eof){ + break; + } else if (status != f_none){ + return status; + } + + if (size + f_file_default_read_size > f_string_max_size){ + return f_overflow; + } + + size += f_file_default_read_size; + } while (f_true); + + return f_none; + } +#endif // _di_fl_file_read_fifo_ + #ifdef __cplusplus } // extern "C" #endif diff --git a/level_1/fl_file/c/file.h b/level_1/fl_file/c/file.h index e3bf01c..c06e0b9 100644 --- a/level_1/fl_file/c/file.h +++ b/level_1/fl_file/c/file.h @@ -22,6 +22,10 @@ extern "C"{ extern f_return_status fl_file_read(f_file file, const f_file_position position, f_dynamic_string *buffer); #endif // _di_fl_file_read_ +#ifndef _di_fl_file_read_fifo_ + extern f_return_status fl_file_read_fifo(f_file file, f_dynamic_string *buffer); +#endif // _di_fl_file_read_fifo_ + #ifdef __cplusplus } // extern "C" #endif -- 1.8.3.1