base
This commit is contained in:
parent
b9e2a82f36
commit
809f8835ce
25
Makefile
Normal file
25
Makefile
Normal file
@ -0,0 +1,25 @@
|
||||
CC=gcc
|
||||
BINDIR=out
|
||||
|
||||
DEBUG_FLAGS=-Wall -Werror -ggdb
|
||||
|
||||
CFLAGS=$(DEBUG_FLAGS) $(shell pkg-config libxml-2.0 --cflags) -ansi -I ./headers
|
||||
LDFLAGS=$(shell pkg-config libxml-2.0 --libs)
|
||||
|
||||
BINARY=generarxml
|
||||
|
||||
OBJS = \
|
||||
main.o \
|
||||
readconfig.o
|
||||
|
||||
all: bin
|
||||
|
||||
bin: $(OBJS)
|
||||
$(CC) $(xml2-config --cflags --libs) $(CFLAGS) $(OBJS) $(LDFLAGS) \
|
||||
-o $(BINDIR)/$(BINARY)
|
||||
|
||||
clean:
|
||||
rm -fR $(BINDIR)/$(BINARY) *.o
|
||||
|
||||
%.o: %.c
|
||||
$(CC) $(xml2-config --cflags --libs) $(CFLAGS) -c $< -o $@ $(LDFLAGS)
|
2
headers/main.h
Normal file
2
headers/main.h
Normal file
@ -0,0 +1,2 @@
|
||||
void printusage(int error);
|
||||
char *output_file = "";
|
1
headers/readconfig.h
Normal file
1
headers/readconfig.h
Normal file
@ -0,0 +1 @@
|
||||
int readconfig(char *config_file);
|
53
main.c
Normal file
53
main.c
Normal file
@ -0,0 +1,53 @@
|
||||
/* Christopher Cromer
|
||||
* Ingeniería Civil en Informática
|
||||
* */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "main.h"
|
||||
#include "readconfig.h"
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
char *config_file = "";
|
||||
|
||||
if (argc == 1) {
|
||||
/* No arguments were passed */
|
||||
printusage(0);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* 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];
|
||||
/* Use config file */
|
||||
break;
|
||||
}
|
||||
else {
|
||||
/* Incorrect usage or used the same option more than once */
|
||||
printusage(1);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
int status = readconfig(config_file);
|
||||
if (status != 0) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Print how to use the program
|
||||
* */
|
||||
void printusage(int error) {
|
||||
if (error == 1) {
|
||||
printf("Opcion desconocido!\n\n");
|
||||
}
|
||||
|
||||
printf("usage: generarxml [opciones] \n");
|
||||
printf(" -s, --config <archivo> archivo de configuración\n");
|
||||
}
|
7714
out/Biblia.txt
Normal file
7714
out/Biblia.txt
Normal file
File diff suppressed because it is too large
Load Diff
11
out/chris.conf
Normal file
11
out/chris.conf
Normal file
@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE config SYSTEM "config.dtd">
|
||||
<config>
|
||||
<output>chris.xml</output>
|
||||
<bible>El Santo Evangelio Según</bible>
|
||||
<book>SAN MATEO</book>
|
||||
<chapter>
|
||||
<name>MATEO</name>
|
||||
<number>16-20</number>
|
||||
</chapter>
|
||||
</config>
|
22
out/config.dtd
Normal file
22
out/config.dtd
Normal file
@ -0,0 +1,22 @@
|
||||
<?xml encoding="UTF-8"?>
|
||||
|
||||
<!ELEMENT config (output,bible,book,chapter)>
|
||||
<!ATTLIST config xmlns CDATA #FIXED ''>
|
||||
|
||||
<!ELEMENT output (#PCDATA)>
|
||||
<!ATTLIST output xmlns CDATA #FIXED ''>
|
||||
|
||||
<!ELEMENT bible (#PCDATA)>
|
||||
<!ATTLIST bible xmlns CDATA #FIXED ''>
|
||||
|
||||
<!ELEMENT book (#PCDATA)>
|
||||
<!ATTLIST book xmlns CDATA #FIXED ''>
|
||||
|
||||
<!ELEMENT chapter (name,number)>
|
||||
<!ATTLIST chapter xmlns CDATA #FIXED ''>
|
||||
|
||||
<!ELEMENT name (#PCDATA)>
|
||||
<!ATTLIST name xmlns CDATA #FIXED ''>
|
||||
|
||||
<!ELEMENT number (#PCDATA)>
|
||||
<!ATTLIST number xmlns CDATA #FIXED ''>
|
64
readconfig.c
Normal file
64
readconfig.c
Normal file
@ -0,0 +1,64 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <libxml/parser.h>
|
||||
#include <libxml/tree.h>
|
||||
#include <libxml/xmlIO.h>
|
||||
#include <libxml/xinclude.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) {
|
||||
/* Initilize the library */
|
||||
LIBXML_TEST_VERSION
|
||||
|
||||
xmlParserCtxtPtr context;
|
||||
xmlDocPtr config = NULL;
|
||||
xmlNodePtr root_element = NULL;
|
||||
|
||||
context = xmlNewParserCtxt();
|
||||
if (context == NULL) {
|
||||
fprintf(stderr, "No pudo alocar contexto de analizador!\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
config = xmlCtxtReadFile(context, config_file, NULL, XML_PARSE_DTDVALID);
|
||||
if (config == NULL) {
|
||||
fprintf(stderr, "Falló analizar %s\n", config_file);
|
||||
}
|
||||
else {
|
||||
if (context->valid == 0) {
|
||||
fprintf(stderr, "Falló validar %s\n", config_file);
|
||||
}
|
||||
|
||||
root_element = xmlDocGetRootElement(config);
|
||||
print_element_names(root_element);
|
||||
|
||||
xmlFreeDoc(config);
|
||||
}
|
||||
xmlFreeParserCtxt(context);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
int readconfig(char *config_file) {
|
||||
fprintf(stderr, "libxml2 no tiene tree support compilado\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user