generarxml/readconfig.c

76 lines
2.5 KiB
C
Raw Normal View History

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
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 17:19:59 -03:00
xmlParserCtxt *context;
xmlDoc *config_xml = NULL;
xmlNode *root = NULL;
xmlNode *node = NULL;
xmlNode *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);
}
else {
if (context->valid == 0) {
fprintf(stderr, "Falló validar %s\n", config_file);
}
2016-10-13 17:19:59 -03:00
root = xmlDocGetRootElement(config_xml);
node = root->xmlChildrenNode;
while (node != NULL) {
if ((!xmlStrcmp(node->name, (const xmlChar *) "output"))){
config->file = xmlNodeListGetString(config_xml, node->xmlChildrenNode, 1);
}
else if ((!xmlStrcmp(node->name, (const xmlChar *) "bible"))){
config->bible = xmlNodeListGetString(config_xml, node->xmlChildrenNode, 1);
}
else if ((!xmlStrcmp(node->name, (const xmlChar *) "book"))){
config->book = xmlNodeListGetString(config_xml, node->xmlChildrenNode, 1);
}
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);
}
if ((!xmlStrcmp(subnode->name, (const xmlChar *) "number"))){
config->chapter_numbers = xmlNodeListGetString(config_xml, subnode->xmlChildrenNode, 1);
}
subnode = subnode->next;
}
}
node = node->next;
}
xmlFreeDoc(config_xml);
}
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!");
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;
}