From 9e9c1b21569faeabd33716e4153a881e2eed7134 Mon Sep 17 00:00:00 2001 From: Filip Wandzio Date: Sun, 1 Mar 2026 17:45:00 +0100 Subject: Separate quiz logic from main function fo dedicated module Signed-off-by: Filip Wandzio --- src/questions.c | 160 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 83 insertions(+), 77 deletions(-) (limited to 'src/questions.c') diff --git a/src/questions.c b/src/questions.c index 88671a7..491c106 100644 --- a/src/questions.c +++ b/src/questions.c @@ -5,88 +5,94 @@ #define MAX_LINE 512 -static int add_question(char ***arr, size_t *count, const char *text) { - char **tmp = realloc(*arr, (*count + 1) * sizeof(char *)); - if (!tmp) - return EXIT_FAILURE; - *arr = tmp; - - (*arr)[*count] = malloc(strlen(text) + 1); - if (!(*arr)[*count]) - return EXIT_FAILURE; - - strcpy((*arr)[*count], text); - (*count)++; - return EXIT_SUCCESS; +static int add_question(char*** arr, size_t* count, const char* text) +{ + char** tmp = realloc(*arr, (*count + 1) * sizeof(char*)); + if (!tmp) + return EXIT_FAILURE; + *arr = tmp; + + (*arr)[*count] = malloc(strlen(text) + 1); + if (!(*arr)[*count]) + return EXIT_FAILURE; + + strcpy((*arr)[*count], text); + (*count)++; + return EXIT_SUCCESS; } -int load_questions(const char *filename, QuestionSet *qs) { - if (!filename || !qs) - return EXIT_FAILURE; - - FILE *f = fopen(filename, "r"); - if (!f) - return EXIT_FAILURE; - - qs->general = NULL; - qs->major = NULL; - qs->general_count = 0; - qs->major_count = 0; - - char line[MAX_LINE]; - enum { NONE, GENERAL, MAJOR } mode = NONE; - - while (fgets(line, sizeof(line), f)) { - line[strcspn(line, "\n")] = '\0'; - if (line[0] == '\0') - continue; - - if (strcmp(line, "#GENERAL") == 0) { - mode = GENERAL; - continue; - } - if (strcmp(line, "#MAJOR") == 0) { - mode = MAJOR; - continue; - } - - int res = EXIT_FAILURE; - switch (mode) { - case GENERAL: - res = add_question(&qs->general, &qs->general_count, line); - break; - case MAJOR: - res = add_question(&qs->major, &qs->major_count, line); - break; - default: - continue; // ignore lines before section - } - - if (res != EXIT_SUCCESS) { - fclose(f); - free_questions(qs); - return EXIT_FAILURE; - } - } - - fclose(f); - return (qs->general_count && qs->major_count) ? EXIT_SUCCESS : EXIT_FAILURE; +int load_questions(const char* filename, QuestionSet* qs) +{ + if (!filename || !qs) + return EXIT_FAILURE; + + FILE* f = fopen(filename, "r"); + if (!f) + return EXIT_FAILURE; + + qs->general = NULL; + qs->major = NULL; + qs->general_count = 0; + qs->major_count = 0; + + char line[MAX_LINE]; + enum { NONE, GENERAL, MAJOR } mode = NONE; + + while (fgets(line, sizeof(line), f)) { + line[strcspn(line, "\n")] = '\0'; + if (line[0] == '\0') + continue; + + if (strcmp(line, "#GENERAL") == 0) { + mode = GENERAL; + continue; + } + if (strcmp(line, "#MAJOR") == 0) { + mode = MAJOR; + continue; + } + + int res = EXIT_FAILURE; + switch (mode) { + case GENERAL: + res = add_question( + &qs->general, &qs->general_count, line); + break; + case MAJOR: + res = add_question( + &qs->major, &qs->major_count, line); + break; + default: + continue; + } + + if (res != EXIT_SUCCESS) { + fclose(f); + free_questions(qs); + return EXIT_FAILURE; + } + } + + fclose(f); + return (qs->general_count && qs->major_count) ? EXIT_SUCCESS + : EXIT_FAILURE; } -void free_questions(QuestionSet *qs) { - if (!qs) - return; +void free_questions(QuestionSet* qs) +{ + if (!qs) + return; - for (size_t i = 0; i < qs->general_count; i++) - free(qs->general[i]); - for (size_t i = 0; i < qs->major_count; i++) - free(qs->major[i]); + for (size_t i = 0; i < qs->general_count; i++) + free(qs->general[i]); + for (size_t i = 0; i < qs->major_count; i++) + free(qs->major[i]); - free(qs->general); - free(qs->major); + free(qs->general); + free(qs->major); - qs->general = NULL; - qs->major = NULL; - qs->general_count = 0; - qs->major_count = 0; + qs->general = NULL; + qs->major = NULL; + qs->general_count = 0; + qs->major_count = 0; } -- cgit v1.2.3