From f2d697f555f3b3ba4bea6ac815135388df0784bf Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Thu, 13 Oct 2016 17:46:05 -0300 Subject: [PATCH] added documentation and fixed memory leak --- main.c | 19 +++++++++++++++---- readconfig.c | 11 +++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/main.c b/main.c index 883ab9a..0990d86 100644 --- a/main.c +++ b/main.c @@ -7,6 +7,9 @@ #ifdef LIBXML_TREE_ENABLED +/* + * This program is designed to take a text file and convert part of it into xml. + */ int main(int argc, char *argv[]) { atexit(cleanup); @@ -52,6 +55,10 @@ int main(int argc, char *argv[]) { #else +/* + * Alternate main designed to prevent problems if the host system does not have + * tree support enabled during compile. + */ int main(int argc, char *argv[]) { fprintf(stderr, "libxml2 no tiene tree support compilado\n"); return 1; @@ -59,9 +66,11 @@ int main(int argc, char *argv[]) { #endif -/* Cleanup on aisle 3 - * */ +/* + * This function is called on exit to clean up the memory usage. + */ void cleanup() { + /* Cleanup on aisle 3 */ if (config != NULL) { if (config->file != NULL) { xmlFree(config->file); @@ -83,8 +92,10 @@ void cleanup() { } } -/* Print how to use the program - * */ +/* + * Print information on the program's usage. If the argument is 1, the user + * put something incorrect as an argument. + */ void printusage(int error) { if (error == 1) { printf("Opcion desconocido!\n\n"); diff --git a/readconfig.c b/readconfig.c index fc80a03..9d66762 100644 --- a/readconfig.c +++ b/readconfig.c @@ -6,6 +6,10 @@ #include #include "main.h" +/* + * This function reads the designated config file and save into int the + * config struct. + */ int readconfig(char *config_file, CONFIG *config) { /* Initilize the library */ LIBXML_TEST_VERSION @@ -25,14 +29,19 @@ int readconfig(char *config_file, CONFIG *config) { config_xml = xmlCtxtReadFile(context, config_file, NULL, XML_PARSE_DTDVALID); if (config_xml == NULL) { fprintf(stderr, "Falló analizar %s\n", config_file); + xmlFreeParserCtxt(context); + return 1; } else { if (context->valid == 0) { fprintf(stderr, "Falló validar %s\n", config_file); + xmlFreeParserCtxt(context); + return 1; } root = xmlDocGetRootElement(config_xml); + /* Run through the nodes to find the config information. */ node = root->xmlChildrenNode; while (node != NULL) { if ((!xmlStrcmp(node->name, (const xmlChar *) "output"))){ @@ -64,8 +73,10 @@ int readconfig(char *config_file, CONFIG *config) { xmlFreeDoc(config_xml); } + /* If any config info is missing, abort */ if (config->file == NULL || config->bible == NULL || config->book == NULL || config->chapter == NULL || config->chapter_numbers == NULL) { printf("El archivo de configuración es invalido!"); + xmlFreeParserCtxt(context); return 1; }