From 0d49fb74eadcf933f696420cd182077927680d26 Mon Sep 17 00:00:00 2001 From: Nathanael Sensfelder Date: Wed, 18 Jan 2017 19:09:16 +0100 Subject: Done with 'core', starting to work on 'knowledge'. --- src/file/data_input.c | 98 +++++++++++++++++++++++++++++++++++++++++++++ src/file/data_input.h | 21 ++++++++++ src/file/data_input_types.h | 16 ++++++++ src/file/data_output.c | 66 ++++++++++++++++++++++++++++++ src/file/data_output.h | 11 +++++ 5 files changed, 212 insertions(+) create mode 100644 src/file/data_input.c create mode 100644 src/file/data_input.h create mode 100644 src/file/data_input_types.h create mode 100644 src/file/data_output.c create mode 100644 src/file/data_output.h (limited to 'src/file') diff --git a/src/file/data_input.c b/src/file/data_input.c new file mode 100644 index 0000000..e31d33b --- /dev/null +++ b/src/file/data_input.c @@ -0,0 +1,98 @@ +#define _POSIX_C_SOURCE 200809L +#include +#include +#include /* defines SIZE_MAX */ + +#include "error.h" + +#include "data_input.h" + +int ZoO_data_input_open +( + struct ZoO_data_input di [const static 1], + const char filename [const restrict static 1] +) +{ + /* prevents di [restrict] */ + ZoO_strings_initialize(&(di->string)); + + di->file = fopen(filename, "r"); + + if (di->file == (FILE *) NULL) + { + ZoO_ERROR + ( + "Could not open file '%s' in readonly mode.", + filename + ); + + return -1; + } + + return 0; +} + +int ZoO_data_input_read_line +( + struct ZoO_data_input di [const static 1], + ZoO_index const punctuations_count, + const ZoO_char punctuations [const restrict static punctuations_count] +) +{ + size_t line_size, i, w_start; + ZoO_char * line; + + /* prevents di [restrict] */ + ZoO_strings_finalize(&(di->string)); + + line = (ZoO_char *) NULL; + line_size = 0; + + /* XXX: assumed compatible with ZoO_char */ + + if (getline(&line, &line_size, di->file) < 1) + { + free((void *) line); + + return -1; + } + + line_size = strlen(line); + line[line_size - 1] = '\0'; + + --line_size; /* removed '\n' */ + + if + ( + ZoO_strings_parse + ( + &(di->string), + line_size, + line, + punctuations_count, + punctuations + ) < 0 + ) + { + free((void *) line); + + return -1; + } + + free((void *) line); + + return 0; +} + +void ZoO_data_input_close (struct ZoO_data_input di [const static 1]) +{ + if (di->file != (FILE *) NULL) + { + fclose(di->file); + + di->file = (FILE *) NULL; + } + + /* prevents di [restrict] */ + ZoO_strings_finalize(&(di->string)); +} diff --git a/src/file/data_input.h b/src/file/data_input.h new file mode 100644 index 0000000..a2f004b --- /dev/null +++ b/src/file/data_input.h @@ -0,0 +1,21 @@ +#ifndef _ZoO_IO_DATA_INPUT_H_ +#define _ZoO_IO_DATA_INPUT_H_ + +#include "data_input_types.h" + +int ZoO_data_input_open +( + struct ZoO_data_input di [const static 1], + const char filename [const restrict static 1] +); + +int ZoO_data_input_read_line +( + struct ZoO_data_input di [const static 1], + ZoO_index const punctuations_count, + const ZoO_char punctuations [const restrict static punctuations_count] +); + +void ZoO_data_input_close (struct ZoO_data_input di [const static 1]); + +#endif diff --git a/src/file/data_input_types.h b/src/file/data_input_types.h new file mode 100644 index 0000000..bd2709b --- /dev/null +++ b/src/file/data_input_types.h @@ -0,0 +1,16 @@ +#ifndef _ZoO_IO_DATA_INPUT_TYPES_H_ +#define _ZoO_IO_DATA_INPUT_TYPES_H_ + +#include + +#include "../pervasive.h" + +#include "../tool/strings.h" + +struct ZoO_data_input +{ + FILE * restrict file; + struct ZoO_strings string; +}; + +#endif diff --git a/src/file/data_output.c b/src/file/data_output.c new file mode 100644 index 0000000..04e3964 --- /dev/null +++ b/src/file/data_output.c @@ -0,0 +1,66 @@ +#define _POSIX_C_SOURCE 200809L + +#include +#include +#include +#include /* defines SIZE_MAX */ +#include + +#include "error.h" + +#include "data_output.h" + +int ZoO_data_output_write_line +( + const char filename [const restrict static 1], + char line [const restrict static 1], + size_t const line_size +) +{ + const int old_errno = errno; + FILE * file; + + file = fopen(filename, "a"); + + if (file == (FILE *) NULL) + { + ZoO_ERROR + ( + "Could not open file '%s' in appending mode.", + filename + ); + + return -1; + } + + line[line_size - 1] = '\n'; + + if + ( + fwrite + ( + (const void *) line, + sizeof(char), + line_size, + file + ) < line_size + ) + { + line[line_size - 1] = '\0'; + + ZoO_ERROR + ( + "Could not store line '%s' in %s.", + line, + filename + ); + + fclose(file); + + return -1; + } + + fclose(file); + + return 0; +} diff --git a/src/file/data_output.h b/src/file/data_output.h new file mode 100644 index 0000000..ef963a0 --- /dev/null +++ b/src/file/data_output.h @@ -0,0 +1,11 @@ +#ifndef _ZoO_IO_DATA_OUTPUT_H_ +#define _ZoO_IO_DATA_OUTPUT_H_ + +int ZoO_data_output_write_line +( + const char filename [const restrict static 1], + char line [const restrict static 1], + size_t const line_size +); + +#endif -- cgit v1.2.3-70-g09d2