From 11900ca5ccb3f06cabb0eb38d2c76a7aea349373 Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Fri, 21 Oct 2016 18:42:37 -0300 Subject: [PATCH] added comments and cleaned up trailing spaces --- src/Makefile.am | 2 +- src/encoding.h | 7 +++++++ src/main.c | 6 +++--- src/main.h | 37 +++++++++++++++++++++++++++++++++++++ src/makexml.c | 12 +++++++----- src/makexml.h | 5 ++++- src/readconfig.c | 10 +++++----- src/readconfig.h | 4 ++++ src/readfile.c | 9 +++++++++ src/readfile.h | 9 +++++++++ 10 files changed, 86 insertions(+), 15 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index fdb2043..d7f16be 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -13,4 +13,4 @@ generarxml_SOURCES = main.c \ encoding.h generarxml_CPPFLAGS = $(XML_CPPFLAGS) generarxml_LDFLAGS= $(XML_LIBS) -generarxml_LDADD = ${libxml2_LIBS} +generarxml_LDADD = ${libxml2_LIBS} diff --git a/src/encoding.h b/src/encoding.h index 96a1193..a0accc8 100644 --- a/src/encoding.h +++ b/src/encoding.h @@ -1,2 +1,9 @@ +/* + * This function converts the latin1 ISO_8859-1 encoding into utf-8. + */ char *latin2utf8(char *input); + +/* + * This function converts the utf-8 encoding into latin1 ISO_8859-1. + */ char *utf82latin(char *input); diff --git a/src/main.c b/src/main.c index 0ad9155..29ee7e4 100644 --- a/src/main.c +++ b/src/main.c @@ -26,7 +26,7 @@ int main(int argc, char **argv) { printusage(0); return 1; } - + /* Read the command line arguments */ #ifdef DEBUG printf("Arguments:\n"); @@ -147,7 +147,7 @@ void cleanup() { /* Cleanup on aisle 3 */ int i; int j; - + if (config) { if (config->file) { free(config->file); @@ -168,7 +168,7 @@ void cleanup() { free(config); } - if (book) { + if (book) { for (i = 0; i < book->chapters; i++) { for (j = 0; j < book->chapter[i]->verses; j++) { free(book->chapter[i]->verse[j]); diff --git a/src/main.h b/src/main.h index 021d48b..28c6bd5 100644 --- a/src/main.h +++ b/src/main.h @@ -1,5 +1,14 @@ #define MEANING 30 + 12 +/* + * This struct houses all the information from the .conf file. + * + * file is the output xml file + * bible is the bible we are searching for + * book is the book we are searching for + * chapter contains the name of the chapter + * chapter_numbers contains the specific chapter numbers to search for + */ struct configuration { char *file; char *bible; @@ -8,6 +17,14 @@ struct configuration { char *chapter_numbers; } typedef CONFIG; +/* + * This struct houses all the data for a specific chapter. + * + * chapter is the chapter number of the struct + * current is used to track iteration in the struct + * verses is the number of verses contained in the verse variable + * verse contains the verses + */ struct chapterdata { int chapter; int current; @@ -15,14 +32,34 @@ struct chapterdata { char **verse; } typedef CHAPTER; +/* + * This struct contains the data for the book and it's chapters. + * current is used for iteration in the struct + * chapters is the ammount of chapters that are contained in the chapter variable + * chapter contains all of the chapters + */ struct bookdata { int current; int chapters; CHAPTER **chapter; } typedef BOOK; +/* + * Global variables. + * + * config contains the configuration that has been loaded from the .conf file + * book contains all the information read from Biblia.txt + */ CONFIG *config; BOOK *book; +/* + * This function is called on exit to clean up the memory usage. + */ void cleanup(); + +/* + * Print information on the program's usage. If the argument is 1, the user + * put something incorrect as an argument. + */ void printusage(int error); diff --git a/src/makexml.c b/src/makexml.c index 81d43e5..ef81474 100644 --- a/src/makexml.c +++ b/src/makexml.c @@ -5,8 +5,10 @@ #include #include "main.h" - -int makexml() { +/* + * This function generates the xml and saves it to the output file. + */ +void makexml() { int i = 0; int j = 0; int length; @@ -19,7 +21,7 @@ int makexml() { xmlNodePtr versetag = NULL; xmlNodePtr node = NULL; xmlNodePtr text = NULL; - + LIBXML_TEST_VERSION; doc = xmlNewDoc(BAD_CAST "1.0"); @@ -40,6 +42,7 @@ int makexml() { xmlAddChild(node, text); xmlAddChild(booktag, node); + /* add the chapters */ for (i = 0; i < book->chapters; i++) { chaptertag = xmlNewNode(NULL, BAD_CAST "Capitulo"); xmlAddChild(booktag, chaptertag); @@ -55,6 +58,7 @@ int makexml() { free(temp); + /* add the verses */ chapter = book->chapter[i]; chapter->current = 0; for (chapter->current = 0; chapter->current < chapter->verses; chapter->current++) { @@ -90,6 +94,4 @@ int makexml() { xmlFreeDoc(doc); xmlCleanupParser(); xmlMemoryDump(); - - return 0; } diff --git a/src/makexml.h b/src/makexml.h index e0769ac..f9e9132 100644 --- a/src/makexml.h +++ b/src/makexml.h @@ -1 +1,4 @@ -int makexml(); +/* + * This function generates the xml and saves it to the output file. + */ +void makexml(); diff --git a/src/readconfig.c b/src/readconfig.c index 4e00449..128df07 100644 --- a/src/readconfig.c +++ b/src/readconfig.c @@ -15,9 +15,9 @@ int readconfig(char *config_file) { xmlNodePtr node = NULL; xmlNodePtr subnode = NULL; - /* Initilize the library */ + /* initilize the library */ LIBXML_TEST_VERSION - + context = xmlNewParserCtxt(); if (context == NULL) { fprintf(stderr, "No pudo alocar contexto de analizador!\n"); @@ -105,10 +105,10 @@ int readconfig(char *config_file) { subnode = subnode->next; } } - + node = node->next; } - + xmlFreeDoc(config_xml); } @@ -118,7 +118,7 @@ int readconfig(char *config_file) { xmlFreeParserCtxt(context); return 1; } - + xmlFreeParserCtxt(context); return 0; diff --git a/src/readconfig.h b/src/readconfig.h index f3eb377..ae143bb 100644 --- a/src/readconfig.h +++ b/src/readconfig.h @@ -1 +1,5 @@ +/* + * This function reads the designated config file and save into int the + * config struct. + */ int readconfig(char *config_file); diff --git a/src/readfile.c b/src/readfile.c index e6cfa80..f4f8a46 100644 --- a/src/readfile.c +++ b/src/readfile.c @@ -8,6 +8,10 @@ #include "readfile.h" #include "encoding.h" +/* + * This function reads Biblia.txt, searches for the information in config, + * and stores it into an array of strings to be used later to create the xml. + */ int readfile() { FILE *file = NULL; CHAPTER *chapter = NULL; @@ -126,6 +130,7 @@ int readfile() { } if (line != NULL) { if (strcmp(line, config->bible) == 0) { + /* found the bible */ matches[0] = true; matches[1] = false; matches[2] = false; @@ -134,6 +139,7 @@ int readfile() { #endif } if (strcmp(line, config->book) == 0) { + /* found the book */ matches[1] = true; matches[2] = false; #ifdef DEBUG @@ -145,6 +151,7 @@ int readfile() { temp = (char *) malloc((length + 2) * sizeof(char)); snprintf(temp, length + 2, "%s %d", config->chapter, i); if (strcmp(line, temp) == 0) { + /* found a chapter */ matches[2] = true; book->current++; book->chapter[book->current] = (CHAPTER *) malloc(sizeof(CHAPTER)); @@ -160,6 +167,7 @@ int readfile() { free(temp); } if (matches[0] == true && matches[1] == true && matches[2] == true) { + /* 3 matches, start getting the verses */ 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); @@ -215,6 +223,7 @@ int readfile() { } } if (matches[0] == true && matches[1] == true && matches[2] == true && strcmp(line, "------------------------------------------------------------------------") == 0) { + /* end of a section and we already have 3 matches, no reason to continue */ #ifdef DEBUG printf("Bible end match: %lu -> %s\n", (long) j + 1, line); #endif diff --git a/src/readfile.h b/src/readfile.h index a96fb89..3f1f80c 100644 --- a/src/readfile.h +++ b/src/readfile.h @@ -1,2 +1,11 @@ +/* + * This variable controls how much memory is used to start with, + * mesured in bytes. + */ #define MAX_LINES 100 + +/* + * This function reads Biblia.txt, searches for the information in config, + * and stores it into an array of strings to be used later to create the xml. + */ int readfile();