From ad5afe8c01d96f831822d0217a577bcb1c7e0c97 Mon Sep 17 00:00:00 2001 From: Hippolyte Chauvin Date: Mon, 4 Dec 2023 11:39:25 +0100 Subject: [PATCH] Modularisation --- src/calibration.c | 43 ++----------------------------------------- src/calibration.h | 2 ++ src/io.c | 27 +++++++++++++++++++++++++++ src/io.h | 8 ++++++++ src/string.c | 18 ++++++++++++++++++ src/string.h | 6 ++++++ 6 files changed, 63 insertions(+), 41 deletions(-) create mode 100644 src/io.c create mode 100644 src/io.h create mode 100644 src/string.c create mode 100644 src/string.h diff --git a/src/calibration.c b/src/calibration.c index 12570d6..2afd76c 100644 --- a/src/calibration.c +++ b/src/calibration.c @@ -1,23 +1,9 @@ #include #include #include -#include -static char *strrev(char *to_reverse) { - size_t length = strlen(to_reverse); - int position = length - 1; - char *reversed = malloc((1 + length) * sizeof(char)); - - while (position >= 0) { - *reversed = to_reverse[position]; - reversed++; - position--; - } - *reversed = '\0'; - reversed = reversed - length; - - return reversed; -} +#include "string.h" +#include "io.h" static char first_digit(char *line) { while (*line != '\0' && !isdigit(*line)) { @@ -31,31 +17,6 @@ static char first_digit(char *line) { } } -static char *read_line(FILE *input_file) { - unsigned int max_length = 16; - unsigned int current_length = 0; - char *line = malloc(max_length * sizeof(char)); - int current_char = fgetc(input_file); - - while (current_char != EOF && current_char != '\n') { - if (current_length == max_length) { - max_length *= 2; - line = realloc(line, max_length * sizeof(char)); - } - - line[current_length] = current_char; - current_length++; - current_char = fgetc(input_file); - } - - if (current_length == max_length) { - line = realloc(line, (max_length + 1) * sizeof(char)); - } - line[current_length] = '\0'; - - return line; -} - unsigned int get_calibration_sum(FILE *input_file) { unsigned int sum = 0; char *current_line, *reversed_line; diff --git a/src/calibration.h b/src/calibration.h index 8df3756..6fceaa4 100644 --- a/src/calibration.h +++ b/src/calibration.h @@ -1,6 +1,8 @@ #ifndef CALIBRATION_H #define CALIBRATION_H +#include + unsigned int get_calibration_sum(FILE *input_file); #endif /* CALIBRATION_H */ diff --git a/src/io.c b/src/io.c new file mode 100644 index 0000000..3002ae5 --- /dev/null +++ b/src/io.c @@ -0,0 +1,27 @@ +#include +#include + +char *read_line(FILE *input_file) { + unsigned int max_length = 16; + unsigned int current_length = 0; + char *line = malloc(max_length * sizeof(char)); + int current_char = fgetc(input_file); + + while (current_char != EOF && current_char != '\n') { + if (current_length == max_length) { + max_length *= 2; + line = realloc(line, max_length * sizeof(char)); + } + + line[current_length] = current_char; + current_length++; + current_char = fgetc(input_file); + } + + if (current_length == max_length) { + line = realloc(line, (max_length + 1) * sizeof(char)); + } + line[current_length] = '\0'; + + return line; +} diff --git a/src/io.h b/src/io.h new file mode 100644 index 0000000..eb7be37 --- /dev/null +++ b/src/io.h @@ -0,0 +1,8 @@ +#ifndef IO_H +#define IO_H + +#include + +char *read_line(FILE *input_file); + +#endif /* IO_H */ diff --git a/src/string.c b/src/string.c new file mode 100644 index 0000000..7a928fb --- /dev/null +++ b/src/string.c @@ -0,0 +1,18 @@ +#include +#include + +char *strrev(char *to_reverse) { + size_t length = strlen(to_reverse); + int position = length - 1; + char *reversed = malloc((1 + length) * sizeof(char)); + + while (position >= 0) { + *reversed = to_reverse[position]; + reversed++; + position--; + } + *reversed = '\0'; + reversed = reversed - length; + + return reversed; +} diff --git a/src/string.h b/src/string.h new file mode 100644 index 0000000..e1b79a3 --- /dev/null +++ b/src/string.h @@ -0,0 +1,6 @@ +#ifndef STRING_H +#define STRING_H + +char *strrev(char *to_reverse); + +#endif /* STRING_H */