added documentation and fixed memory leak

This commit is contained in:
Chris Cromer 2016-10-13 17:46:05 -03:00
parent e37024b041
commit f2d697f555
2 changed files with 26 additions and 4 deletions

19
main.c
View File

@ -7,6 +7,9 @@
#ifdef LIBXML_TREE_ENABLED #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[]) { int main(int argc, char *argv[]) {
atexit(cleanup); atexit(cleanup);
@ -52,6 +55,10 @@ int main(int argc, char *argv[]) {
#else #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[]) { int main(int argc, char *argv[]) {
fprintf(stderr, "libxml2 no tiene tree support compilado\n"); fprintf(stderr, "libxml2 no tiene tree support compilado\n");
return 1; return 1;
@ -59,9 +66,11 @@ int main(int argc, char *argv[]) {
#endif #endif
/* Cleanup on aisle 3 /*
* */ * This function is called on exit to clean up the memory usage.
*/
void cleanup() { void cleanup() {
/* Cleanup on aisle 3 */
if (config != NULL) { if (config != NULL) {
if (config->file != NULL) { if (config->file != NULL) {
xmlFree(config->file); 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) { void printusage(int error) {
if (error == 1) { if (error == 1) {
printf("Opcion desconocido!\n\n"); printf("Opcion desconocido!\n\n");

View File

@ -6,6 +6,10 @@
#include <libxml/xinclude.h> #include <libxml/xinclude.h>
#include "main.h" #include "main.h"
/*
* This function reads the designated config file and save into int the
* config struct.
*/
int readconfig(char *config_file, CONFIG *config) { int readconfig(char *config_file, CONFIG *config) {
/* Initilize the library */ /* Initilize the library */
LIBXML_TEST_VERSION LIBXML_TEST_VERSION
@ -25,14 +29,19 @@ int readconfig(char *config_file, CONFIG *config) {
config_xml = xmlCtxtReadFile(context, config_file, NULL, XML_PARSE_DTDVALID); config_xml = xmlCtxtReadFile(context, config_file, NULL, XML_PARSE_DTDVALID);
if (config_xml == NULL) { if (config_xml == NULL) {
fprintf(stderr, "Falló analizar %s\n", config_file); fprintf(stderr, "Falló analizar %s\n", config_file);
xmlFreeParserCtxt(context);
return 1;
} }
else { else {
if (context->valid == 0) { if (context->valid == 0) {
fprintf(stderr, "Falló validar %s\n", config_file); fprintf(stderr, "Falló validar %s\n", config_file);
xmlFreeParserCtxt(context);
return 1;
} }
root = xmlDocGetRootElement(config_xml); root = xmlDocGetRootElement(config_xml);
/* Run through the nodes to find the config information. */
node = root->xmlChildrenNode; node = root->xmlChildrenNode;
while (node != NULL) { while (node != NULL) {
if ((!xmlStrcmp(node->name, (const xmlChar *) "output"))){ if ((!xmlStrcmp(node->name, (const xmlChar *) "output"))){
@ -64,8 +73,10 @@ int readconfig(char *config_file, CONFIG *config) {
xmlFreeDoc(config_xml); 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) { 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!"); printf("El archivo de configuración es invalido!");
xmlFreeParserCtxt(context);
return 1; return 1;
} }