generarxml/readconfig.c

102 líneas
3.9 KiB
C
Original Vista normal Histórico

2016-10-13 15:08:01 -03:00
#include <stdio.h>
#include <string.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xmlIO.h>
#include <libxml/xinclude.h>
2016-10-13 17:19:59 -03:00
#include "main.h"
2016-10-13 15:08:01 -03:00
/*
* This function reads the designated config file and save into int the
* config struct.
*/
2016-10-13 17:19:59 -03:00
int readconfig(char *config_file, CONFIG *config) {
2016-10-13 15:08:01 -03:00
/* Initilize the library */
LIBXML_TEST_VERSION
2016-10-13 19:55:33 -03:00
xmlParserCtxtPtr context;
xmlDocPtr config_xml = NULL;
xmlNodePtr root = NULL;
xmlNodePtr node = NULL;
xmlNodePtr subnode = NULL;
2016-10-13 15:08:01 -03:00
context = xmlNewParserCtxt();
if (context == NULL) {
fprintf(stderr, "No pudo alocar contexto de analizador!\n");
return 1;
}
2016-10-13 17:19:59 -03:00
config_xml = xmlCtxtReadFile(context, config_file, NULL, XML_PARSE_DTDVALID);
if (config_xml == NULL) {
2016-10-13 15:08:01 -03:00
fprintf(stderr, "Falló analizar %s\n", config_file);
xmlFreeParserCtxt(context);
return 1;
2016-10-13 15:08:01 -03:00
}
else {
if (context->valid == 0) {
fprintf(stderr, "Falló validar %s\n", config_file);
xmlFreeParserCtxt(context);
return 1;
2016-10-13 15:08:01 -03:00
}
2016-10-13 17:19:59 -03:00
root = xmlDocGetRootElement(config_xml);
/* Run through the nodes to find the config information. */
2016-10-13 17:19:59 -03:00
node = root->xmlChildrenNode;
while (node != NULL) {
if ((!xmlStrcmp(node->name, (const xmlChar *) "output"))){
2016-10-13 19:55:33 -03:00
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);
2016-10-13 17:19:59 -03:00
}
else if ((!xmlStrcmp(node->name, (const xmlChar *) "bible"))){
2016-10-13 19:55:33 -03:00
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);
2016-10-13 17:19:59 -03:00
}
else if ((!xmlStrcmp(node->name, (const xmlChar *) "book"))){
2016-10-13 19:55:33 -03:00
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);
2016-10-13 17:19:59 -03:00
}
else if ((!xmlStrcmp(node->name, (const xmlChar *) "chapter"))){
subnode = node->xmlChildrenNode;
while (subnode != NULL) {
if ((!xmlStrcmp(subnode->name, (const xmlChar *) "name"))){
2016-10-13 19:55:33 -03:00
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);
2016-10-13 17:19:59 -03:00
}
if ((!xmlStrcmp(subnode->name, (const xmlChar *) "number"))){
2016-10-13 19:55:33 -03:00
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);
2016-10-13 17:19:59 -03:00
}
subnode = subnode->next;
}
}
node = node->next;
}
2016-10-13 19:55:33 -03:00
2016-10-13 17:19:59 -03:00
xmlFreeDoc(config_xml);
}
/* If any config info is missing, abort */
2016-10-13 17:19:59 -03:00
if (config->file == NULL || config->bible == NULL || config->book == NULL || config->chapter == NULL || config->chapter_numbers == NULL) {
2016-10-13 19:55:33 -03:00
printf("El archivo de configuración es invalido!\n");
xmlFreeParserCtxt(context);
2016-10-13 17:19:59 -03:00
return 1;
2016-10-13 15:08:01 -03:00
}
2016-10-13 17:19:59 -03:00
2016-10-13 15:08:01 -03:00
xmlFreeParserCtxt(context);
return 0;
}