read config files into struct

This commit is contained in:
Chris Cromer 2016-10-13 17:19:59 -03:00
parent 0261e3c863
commit 17f620f919
4 changed files with 93 additions and 34 deletions

View File

@ -1,2 +1,12 @@
struct configuration {
xmlChar *file;
xmlChar *bible;
xmlChar *book;
xmlChar *chapter;
xmlChar *chapter_numbers;
} typedef CONFIG;
CONFIG *config;
void cleanup();
void printusage(int error);
char *output_file = "";

View File

@ -1 +1 @@
int readconfig(char *config_file);
int readconfig(char *config_file, CONFIG *config);

41
main.c
View File

@ -1,14 +1,13 @@
/* Christopher Cromer
* Ingeniería Civil en Informática
* */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libxml/tree.h>
#include "main.h"
#include "readconfig.h"
int main(int argc, char *argv[]) {
atexit(cleanup);
char *config_file = "";
if (argc == 1) {
@ -23,8 +22,7 @@ int main(int argc, char *argv[]) {
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];
/* Use config file */
break;
i++;
}
else {
/* Incorrect usage or used the same option more than once */
@ -33,14 +31,43 @@ int main(int argc, char *argv[]) {
}
}
int status = readconfig(config_file);
config = (CONFIG *) malloc (sizeof(CONFIG));
int status = readconfig(config_file, config);
if (status != 0) {
return 1;
}
printf("file: %s\n", config->file);
printf("bible: %s\n", config->bible);
return 0;
}
/* Cleanup on aisle 3
* */
void cleanup() {
if (config != NULL) {
if (config->file != NULL) {
xmlFree(config->file);
}
if (config->bible != NULL) {
xmlFree(config->bible);
}
if (config->book != NULL) {
xmlFree(config->book);
}
if (config->chapter != NULL) {
xmlFree(config->chapter);
}
if (config->chapter_numbers != NULL) {
xmlFree(config->chapter_numbers);
}
free(config);
}
}
/* Print how to use the program
* */
void printusage(int error) {

View File

@ -4,30 +4,19 @@
#include <libxml/tree.h>
#include <libxml/xmlIO.h>
#include <libxml/xinclude.h>
#include "main.h"
#ifdef LIBXML_TREE_ENABLED
static void
print_element_names(xmlNode * a_node)
{
xmlNode *cur_node = NULL;
for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
if (cur_node->type == XML_ELEMENT_NODE) {
printf("node type: Element, name: %s\n", cur_node->name);
}
print_element_names(cur_node->children);
}
}
int readconfig(char *config_file) {
int readconfig(char *config_file, CONFIG *config) {
/* Initilize the library */
LIBXML_TEST_VERSION
xmlParserCtxtPtr context;
xmlDocPtr config = NULL;
xmlNodePtr root_element = NULL;
xmlParserCtxt *context;
xmlDoc *config_xml = NULL;
xmlNode *root = NULL;
xmlNode *node = NULL;
xmlNode *subnode = NULL;
context = xmlNewParserCtxt();
if (context == NULL) {
@ -35,8 +24,8 @@ int readconfig(char *config_file) {
return 1;
}
config = xmlCtxtReadFile(context, config_file, NULL, XML_PARSE_DTDVALID);
if (config == NULL) {
config_xml = xmlCtxtReadFile(context, config_file, NULL, XML_PARSE_DTDVALID);
if (config_xml == NULL) {
fprintf(stderr, "Falló analizar %s\n", config_file);
}
else {
@ -44,11 +33,44 @@ int readconfig(char *config_file) {
fprintf(stderr, "Falló validar %s\n", config_file);
}
root_element = xmlDocGetRootElement(config);
print_element_names(root_element);
xmlFreeDoc(config);
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;
}
xmlFreeParserCtxt(context);
return 0;
@ -56,7 +78,7 @@ int readconfig(char *config_file) {
#else
int readconfig(char *config_file) {
int readconfig(char *config_file, CONFIG config) {
fprintf(stderr, "libxml2 no tiene tree support compilado\n");
return 1;
}