From 7d2791db88851ccffb4747186832d270c5954cc4 Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Thu, 20 Oct 2016 20:45:07 -0300 Subject: [PATCH] save the information to book struct --- src/main.c | 18 ++++++++- src/main.h | 15 +++++++- src/readfile.c | 101 ++++++++++++++++++++++++++++++++++++++++++++++--- src/readfile.h | 2 +- 4 files changed, 128 insertions(+), 8 deletions(-) diff --git a/src/main.c b/src/main.c index e916015..8a69056 100644 --- a/src/main.c +++ b/src/main.c @@ -94,7 +94,8 @@ int main(int argc, char **argv) { printf("\tNombre de capitulo: %s\n", config->chapter); printf("\tNumeros de capitulo: %s\n", config->chapter_numbers); - status = readfile(config); + book = (BOOK *) malloc(sizeof(BOOK)); + status = readfile(config, book); if (status != 0) { printf("Falló leer Biblia.txt!\n"); return 1; @@ -129,6 +130,9 @@ int main(int argc, char *argv[]) { */ void cleanup() { /* Cleanup on aisle 3 */ + int i; + int j; + if (config) { if (config->file) { free(config->file); @@ -148,6 +152,18 @@ void cleanup() { free(config); } + + if (book) { + for (i = 0; i < book->chapters; i++) { + for (j = 0; j < book->chapter[i]->verses; j++) { + free(book->chapter[i]->verse[j]); + } + free(book->chapter[i]->verse); + free(book->chapter[i]); + } + free(book->chapter); + free(book); + } } /* diff --git a/src/main.h b/src/main.h index f3f5e98..0a05dd8 100644 --- a/src/main.h +++ b/src/main.h @@ -1,4 +1,4 @@ -#define MEANING 42 +#define MEANING 30 + 12 struct configuration { char *file; @@ -8,7 +8,20 @@ struct configuration { char *chapter_numbers; } typedef CONFIG; +struct chapterdata { + int current; + int verses; + char **verse; +} typedef CHAPTER; + +struct bookdata { + int current; + int chapters; + CHAPTER **chapter; +} typedef BOOK; + CONFIG *config; +BOOK *book; void cleanup(); void printusage(int error); diff --git a/src/readfile.c b/src/readfile.c index df3874b..f52acec 100644 --- a/src/readfile.c +++ b/src/readfile.c @@ -2,17 +2,23 @@ #include #include #include +#include #include #include "main.h" #include "readfile.h" #include "encoding.h" -int readfile(CONFIG *config) { +int readfile(CONFIG *config, BOOK *book) { FILE *file = NULL; + CHAPTER *chapter = NULL; int start = 0; int end = 0; + int chapters = 1; + int verse = 0; int length; int i = 0; + int k = 0; + int l = 0; char *line = NULL; char *temp = NULL; char **array = NULL; @@ -41,11 +47,24 @@ int readfile(CONFIG *config) { printf("Start chapter: %d\nEnd chapter: %d\n", start, end); #endif - if (end != 0 && start > end) { - printf("Archivo de configuración invalido!"); - return 1; + if (end != 0) { + if (start > end) { + printf("Archivo de configuración invalido!"); + return 1; + } + else { + chapters = (end - start) + 1; + } } + #ifdef DEBUG + printf("Chapters: %d\n", chapters); + #endif + + book->chapters = chapters; + book->current = -1; + book->chapter = (CHAPTER **) malloc((chapters + 1) * sizeof(CHAPTER *)); + i = 0; file = fopen("Biblia.txt", "r"); @@ -83,7 +102,7 @@ int readfile(CONFIG *config) { array = tmp; } } - + /* free the extra unused memory */ if (new_max > lines) { char **tmp = realloc(array, lines * sizeof(*array)); @@ -128,12 +147,84 @@ int readfile(CONFIG *config) { snprintf(temp, length + 2, "%s %d", config->chapter, i); if (strcmp(line, temp) == 0) { matches[2] = true; + book->current++; + book->chapter[book->current] = (CHAPTER *) malloc(sizeof(CHAPTER)); + chapter = book->chapter[book->current]; + chapter->current = -1; + chapter->verses = 0; + chapter->verse = (char **) malloc(sizeof(char *)); #ifdef DEBUG printf("Chapter match: %lu -> %s\n", (long) j + 1, line); #endif } free(temp); } + if (matches[0] == true && matches[1] == true && matches[2] == true) { + length = snprintf(NULL, 0, "%d", end + 1) + strlen(config->chapter); + temp = (char *) malloc((length + 2) * sizeof(char)); + snprintf(temp, length + 2, "%s %d", config->chapter, end + 1); + if (strcmp(line, temp) == 0) { + free(line); + free(temp); + line = NULL; + break; + } + if (temp) { + free(temp); + } + + temp = (char *) malloc((strlen(line) + 1) * sizeof(char)); + /* If it's a verse, match */ + for (i = 0; i <= 2; i++) { + if (line[i] == ' ') { + temp[i] = '\0'; + + verse = atoi(temp); + if (temp) { + free(temp); + } + + l = 0; + temp = (char *) malloc((strlen(line) + 1) * sizeof(char)); + for (k = i + 1; k < strlen(line) - 1; (k++)) { + /*printf("i: %d k: %d chars: %d\n", i, k, strlen(line));*/ + temp[l] = line[k]; + l++; + } + temp[l] = '\0'; + + chapter->current++; + chapter->verses++; + chapter->verse = (char **) realloc(chapter->verse, chapter->verses * sizeof(char *)); + chapter->verse[chapter->current] = (char *) malloc((strlen(temp) + 1) * sizeof(char)); + memcpy(chapter->verse[chapter->current], temp, strlen(temp) + 1); + + /*printf("%d - %s\n", verse, chapter->verses[chapter->current]);*/ + + if (temp) { + free(temp); + } + break; + } + if (!isdigit(line[i])) { + if (temp) { + free(temp); + } + break; + } + else { + temp[i] = line[i]; + } + } + } + if (matches[0] == true && matches[1] == true && matches[2] == true && strcmp(line, "------------------------------------------------------------------------") == 0) { + #ifdef DEBUG + printf("Bible end match: %lu -> %s\n", (long) j + 1, line); + #endif + free(line); + line = NULL; + break; + } } free(line); line = NULL; diff --git a/src/readfile.h b/src/readfile.h index d0e2b73..67b611b 100644 --- a/src/readfile.h +++ b/src/readfile.h @@ -1,2 +1,2 @@ #define MAX_LINES 100 -int readfile(CONFIG *config); +int readfile(CONFIG *config, BOOK *book);