fixed memory leaks
This commit is contained in:
parent
86d772597e
commit
4d6c51e37b
4
Makefile
4
Makefile
@ -1,9 +1,9 @@
|
|||||||
CC=gcc
|
CC=gcc
|
||||||
BINDIR=out
|
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)
|
LDFLAGS=$(shell pkg-config libxml-2.0 --libs)
|
||||||
|
|
||||||
BINARY=generarxml
|
BINARY=generarxml
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
struct configuration {
|
struct configuration {
|
||||||
xmlChar *file;
|
char *file;
|
||||||
xmlChar *bible;
|
char *bible;
|
||||||
xmlChar *book;
|
char *book;
|
||||||
xmlChar *chapter;
|
char *chapter;
|
||||||
xmlChar *chapter_numbers;
|
char *chapter_numbers;
|
||||||
} typedef CONFIG;
|
} typedef CONFIG;
|
||||||
|
|
||||||
CONFIG *config;
|
CONFIG *config;
|
||||||
|
34
main.c
34
main.c
@ -10,10 +10,10 @@
|
|||||||
/*
|
/*
|
||||||
* This program is designed to take a text file and convert part of it into xml.
|
* 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);
|
||||||
|
|
||||||
char *config_file = "";
|
char *config_file = NULL;
|
||||||
|
|
||||||
if (argc == 1) {
|
if (argc == 1) {
|
||||||
/* No arguments were passed */
|
/* No arguments were passed */
|
||||||
@ -24,21 +24,23 @@ int main(int argc, char *argv[]) {
|
|||||||
/* Read the command line arguments */
|
/* Read the command line arguments */
|
||||||
int i;
|
int i;
|
||||||
for (i = 1; i < argc; 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) {
|
if ((strcmp(argv[1], "-c") == 0 || strcmp(argv[1], "--config") == 0) && config_file == NULL) {
|
||||||
config_file = (char *) malloc(strlen(argv[i + 1]) * sizeof(char));
|
|
||||||
config_file = argv[i + 1];
|
|
||||||
i++;
|
i++;
|
||||||
|
config_file = (char *) malloc((strlen(argv[2]) + 1) * sizeof(char *));
|
||||||
|
strcpy(config_file, argv[2]);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
/* Incorrect usage or used the same option more than once */
|
/* Incorrect usage */
|
||||||
printusage(1);
|
printusage(1);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
config = (CONFIG *) malloc (sizeof(CONFIG));
|
config = (CONFIG *) malloc(sizeof(CONFIG));
|
||||||
|
|
||||||
int status = readconfig(config_file, config);
|
int status = readconfig(config_file, config);
|
||||||
|
free(config_file);
|
||||||
|
config_file = NULL;
|
||||||
if (status != 0) {
|
if (status != 0) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@ -50,6 +52,14 @@ int main(int argc, char *argv[]) {
|
|||||||
printf("\tNombre de capitulo: %s\n", config->chapter);
|
printf("\tNombre de capitulo: %s\n", config->chapter);
|
||||||
printf("\tNumeros de capitulo: %s\n", config->chapter_numbers);
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -73,19 +83,19 @@ void cleanup() {
|
|||||||
/* Cleanup on aisle 3 */
|
/* Cleanup on aisle 3 */
|
||||||
if (config != NULL) {
|
if (config != NULL) {
|
||||||
if (config->file != NULL) {
|
if (config->file != NULL) {
|
||||||
xmlFree(config->file);
|
free(config->file);
|
||||||
}
|
}
|
||||||
if (config->bible != NULL) {
|
if (config->bible != NULL) {
|
||||||
xmlFree(config->bible);
|
free(config->bible);
|
||||||
}
|
}
|
||||||
if (config->book != NULL) {
|
if (config->book != NULL) {
|
||||||
xmlFree(config->book);
|
free(config->book);
|
||||||
}
|
}
|
||||||
if (config->chapter != NULL) {
|
if (config->chapter != NULL) {
|
||||||
xmlFree(config->chapter);
|
free(config->chapter);
|
||||||
}
|
}
|
||||||
if (config->chapter_numbers != NULL) {
|
if (config->chapter_numbers != NULL) {
|
||||||
xmlFree(config->chapter_numbers);
|
free(config->chapter_numbers);
|
||||||
}
|
}
|
||||||
|
|
||||||
free(config);
|
free(config);
|
||||||
|
37
readconfig.c
37
readconfig.c
@ -14,11 +14,11 @@ int readconfig(char *config_file, CONFIG *config) {
|
|||||||
/* Initilize the library */
|
/* Initilize the library */
|
||||||
LIBXML_TEST_VERSION
|
LIBXML_TEST_VERSION
|
||||||
|
|
||||||
xmlParserCtxt *context;
|
xmlParserCtxtPtr context;
|
||||||
xmlDoc *config_xml = NULL;
|
xmlDocPtr config_xml = NULL;
|
||||||
xmlNode *root = NULL;
|
xmlNodePtr root = NULL;
|
||||||
xmlNode *node = NULL;
|
xmlNodePtr node = NULL;
|
||||||
xmlNode *subnode = NULL;
|
xmlNodePtr subnode = NULL;
|
||||||
|
|
||||||
context = xmlNewParserCtxt();
|
context = xmlNewParserCtxt();
|
||||||
if (context == NULL) {
|
if (context == NULL) {
|
||||||
@ -45,22 +45,37 @@ int readconfig(char *config_file, CONFIG *config) {
|
|||||||
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"))){
|
||||||
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"))){
|
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"))){
|
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"))){
|
else if ((!xmlStrcmp(node->name, (const xmlChar *) "chapter"))){
|
||||||
subnode = node->xmlChildrenNode;
|
subnode = node->xmlChildrenNode;
|
||||||
while (subnode != NULL) {
|
while (subnode != NULL) {
|
||||||
if ((!xmlStrcmp(subnode->name, (const xmlChar *) "name"))){
|
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"))){
|
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;
|
subnode = subnode->next;
|
||||||
@ -75,7 +90,7 @@ int readconfig(char *config_file, CONFIG *config) {
|
|||||||
|
|
||||||
/* If any config info is missing, abort */
|
/* 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!\n");
|
||||||
xmlFreeParserCtxt(context);
|
xmlFreeParserCtxt(context);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user