| summaryrefslogtreecommitdiff |
diff options
| author | Nathanael Sensfelder <SpamShield0@MultiAgentSystems.org> | 2016-05-05 14:59:28 +0200 |
|---|---|---|
| committer | Nathanael Sensfelder <SpamShield0@MultiAgentSystems.org> | 2016-05-05 14:59:28 +0200 |
| commit | 3405b0c1635843cbb81f042364bfcf238d7dc930 (patch) | |
| tree | 39501fec9ec72863c929a45dbc297412bbf90688 /src/io/data_input.c | |
| parent | c28bb6d31a122ec983e1e0a0dd1a8bd198098c58 (diff) | |
Adds the current code.
It's been running for close to a month on one of the IRC channels I
frequent and seems to be working fine.
One should be aware that, among other missing features, this version
does not store permanently what the bot learns. Indeed, I am currently
using a file with 431848 lines as its initial knowledge bank, making
this particular feature not a high priority one.
Also consider the fact that Zero of One converts text to underscore
before reading it but will not change its own aliases. This could
potentially be a cause for surprises when using uppercase letters in the
latter.
Diffstat (limited to 'src/io/data_input.c')
| -rw-r--r-- | src/io/data_input.c | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/src/io/data_input.c b/src/io/data_input.c new file mode 100644 index 0000000..e31d33b --- /dev/null +++ b/src/io/data_input.c @@ -0,0 +1,98 @@ +#define _POSIX_C_SOURCE 200809L +#include <stdlib.h> +#include <string.h> +#include <stdint.h> /* 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)); +} |


