fixed memory leaks

This commit is contained in:
Chris Cromer 2016-10-13 19:55:33 -03:00
parent 86d772597e
commit 4d6c51e37b
4 changed files with 57 additions and 32 deletions

View File

@ -1,9 +1,9 @@
CC=gcc
BINDIR=out
DEBUG_FLAGS=-Wall -Werror -ggdb
DEBUG_FLAGS=-Wall -Werror
CFLAGS=$(DEBUG_FLAGS) $(shell pkg-config libxml-2.0 --cflags) -ansi -I ./headers
CFLAGS=$(DEBUG_FLAGS) -ggdb $(shell pkg-config libxml-2.0 --cflags) -ansi -I ./headers
LDFLAGS=$(shell pkg-config libxml-2.0 --libs)
BINARY=generarxml

View File

@ -1,9 +1,9 @@
struct configuration {
xmlChar *file;
xmlChar *bible;
xmlChar *book;
xmlChar *chapter;
xmlChar *chapter_numbers;
char *file;
char *bible;
char *book;
char *chapter;
char *chapter_numbers;
} typedef CONFIG;
CONFIG *config;

32
main.c
View File

@ -10,10 +10,10 @@
/*
* 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);
char *config_file = "";
char *config_file = NULL;
if (argc == 1) {
/* No arguments were passed */
@ -24,13 +24,13 @@ int main(int argc, char *argv[]) {
/* Read the command line arguments */
int i;
for (i = 1; i < argc; i++) {
if ((strcmp(argv[i], "-c") == 0 || strcmp(argv[i], "--config") == 0) && i != argc - 1 && strlen(config_file) == 0) {
config_file = (char *) malloc(strlen(argv[i + 1]) * sizeof(char));
config_file = argv[i + 1];
if ((strcmp(argv[1], "-c") == 0 || strcmp(argv[1], "--config") == 0) && config_file == NULL) {
i++;
config_file = (char *) malloc((strlen(argv[2]) + 1) * sizeof(char *));
strcpy(config_file, argv[2]);
}
else {
/* Incorrect usage or used the same option more than once */
/* Incorrect usage */
printusage(1);
return 1;
}
@ -39,6 +39,8 @@ int main(int argc, char *argv[]) {
config = (CONFIG *) malloc(sizeof(CONFIG));
int status = readconfig(config_file, config);
free(config_file);
config_file = NULL;
if (status != 0) {
return 1;
}
@ -50,6 +52,14 @@ int main(int argc, char *argv[]) {
printf("\tNombre de capitulo: %s\n", config->chapter);
printf("\tNumeros de capitulo: %s\n", config->chapter_numbers);
free(config->file);
free(config->bible);
free(config->book);
free(config->chapter);
free(config->chapter_numbers);
free(config);
config = NULL;
return 0;
}
@ -73,19 +83,19 @@ void cleanup() {
/* Cleanup on aisle 3 */
if (config != NULL) {
if (config->file != NULL) {
xmlFree(config->file);
free(config->file);
}
if (config->bible != NULL) {
xmlFree(config->bible);
free(config->bible);
}
if (config->book != NULL) {
xmlFree(config->book);
free(config->book);
}
if (config->chapter != NULL) {
xmlFree(config->chapter);
free(config->chapter);
}
if (config->chapter_numbers != NULL) {
xmlFree(config->chapter_numbers);
free(config->chapter_numbers);
}
free(config);

View File

@ -14,11 +14,11 @@ int readconfig(char *config_file, CONFIG *config) {
/* Initilize the library */
LIBXML_TEST_VERSION
xmlParserCtxt *context;
xmlDoc *config_xml = NULL;
xmlNode *root = NULL;
xmlNode *node = NULL;
xmlNode *subnode = NULL;
xmlParserCtxtPtr context;
xmlDocPtr config_xml = NULL;
xmlNodePtr root = NULL;
xmlNodePtr node = NULL;
xmlNodePtr subnode = NULL;
context = xmlNewParserCtxt();
if (context == NULL) {
@ -45,22 +45,37 @@ int readconfig(char *config_file, CONFIG *config) {
node = root->xmlChildrenNode;
while (node != NULL) {
if ((!xmlStrcmp(node->name, (const xmlChar *) "output"))){
config->file = xmlNodeListGetString(config_xml, node->xmlChildrenNode, 1);
xmlChar *file = xmlNodeListGetString(config_xml, node->xmlChildrenNode, 1);
config->file = (char *) malloc(strlen((char *) file) + 1 * sizeof(char));
strcpy(config->file, (char *) file);
xmlFree(file);
}
else if ((!xmlStrcmp(node->name, (const xmlChar *) "bible"))){
config->bible = xmlNodeListGetString(config_xml, node->xmlChildrenNode, 1);
xmlChar *bible = xmlNodeListGetString(config_xml, node->xmlChildrenNode, 1);
config->bible = (char *) malloc(strlen((char *) bible) + 1 * sizeof(char));
strcpy(config->bible, (char *) bible);
xmlFree(bible);
}
else if ((!xmlStrcmp(node->name, (const xmlChar *) "book"))){
config->book = xmlNodeListGetString(config_xml, node->xmlChildrenNode, 1);
xmlChar *book = xmlNodeListGetString(config_xml, node->xmlChildrenNode, 1);
config->book = (char *) malloc(strlen((char *) book) + 1 * sizeof(char));
strcpy(config->book, (char *) book);
xmlFree(book);
}
else if ((!xmlStrcmp(node->name, (const xmlChar *) "chapter"))){
subnode = node->xmlChildrenNode;
while (subnode != NULL) {
if ((!xmlStrcmp(subnode->name, (const xmlChar *) "name"))){
config->chapter = xmlNodeListGetString(config_xml, subnode->xmlChildrenNode, 1);
xmlChar *chapter = xmlNodeListGetString(config_xml, subnode->xmlChildrenNode, 1);
config->chapter = (char *) malloc(strlen((char *) chapter) + 1 * sizeof(char));
strcpy(config->chapter, (char *) chapter);
xmlFree(chapter);
}
if ((!xmlStrcmp(subnode->name, (const xmlChar *) "number"))){
config->chapter_numbers = xmlNodeListGetString(config_xml, subnode->xmlChildrenNode, 1);
xmlChar *chapter_numbers = xmlNodeListGetString(config_xml, subnode->xmlChildrenNode, 1);
config->chapter_numbers = (char *) malloc(strlen((char *) chapter_numbers) + 1 * sizeof(char));
strcpy(config->chapter_numbers, (char *) chapter_numbers);
xmlFree(chapter_numbers);
}
subnode = subnode->next;
@ -75,7 +90,7 @@ int readconfig(char *config_file, CONFIG *config) {
/* 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!");
printf("El archivo de configuración es invalido!\n");
xmlFreeParserCtxt(context);
return 1;
}