2016-10-13 15:08:01 -03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2016-10-13 17:19:59 -03:00
|
|
|
#include <libxml/tree.h>
|
2016-10-17 11:31:42 -03:00
|
|
|
#include <stdbool.h>
|
2016-10-13 15:08:01 -03:00
|
|
|
#include "main.h"
|
|
|
|
#include "readconfig.h"
|
2016-10-16 14:16:03 -03:00
|
|
|
#include "readfile.h"
|
2016-10-13 15:08:01 -03:00
|
|
|
|
2016-10-13 17:31:50 -03:00
|
|
|
#ifdef LIBXML_TREE_ENABLED
|
|
|
|
|
2016-10-13 17:46:05 -03:00
|
|
|
/*
|
|
|
|
* This program is designed to take a text file and convert part of it into xml.
|
|
|
|
*/
|
2016-10-13 19:55:33 -03:00
|
|
|
int main(int argc, char **argv) {
|
|
|
|
char *config_file = NULL;
|
2016-10-16 14:16:03 -03:00
|
|
|
int status;
|
|
|
|
int i;
|
2016-10-17 11:31:42 -03:00
|
|
|
bool meaning = false;
|
2016-10-16 14:16:03 -03:00
|
|
|
|
|
|
|
atexit(cleanup);
|
2016-10-13 15:08:01 -03:00
|
|
|
|
|
|
|
if (argc == 1) {
|
|
|
|
/* No arguments were passed */
|
|
|
|
printusage(0);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the command line arguments */
|
2016-10-16 17:49:48 -03:00
|
|
|
#ifdef DEBUG
|
|
|
|
printf("Arguments:\n");
|
|
|
|
#endif
|
2016-10-13 15:08:01 -03:00
|
|
|
for (i = 1; i < argc; i++) {
|
2016-10-16 17:49:48 -03:00
|
|
|
#ifdef DEBUG
|
|
|
|
printf("\targ %d: %s\n", i, argv[i]);
|
|
|
|
#endif
|
2016-10-17 11:31:42 -03:00
|
|
|
if ((strcmp(argv[i], "-c") == 0 || strcmp(argv[i], "--config") == 0) && config_file == NULL) {
|
2016-10-16 17:49:48 -03:00
|
|
|
if (argc > i + 1) {
|
|
|
|
i++;
|
2016-10-17 11:31:42 -03:00
|
|
|
config_file = (char *) malloc((strlen(argv[i]) + 1) * sizeof(char *));
|
|
|
|
strcpy(config_file, argv[i]);
|
2016-10-16 17:49:48 -03:00
|
|
|
#ifdef DEBUG
|
|
|
|
printf("\targ %d: %s\n", i, argv[i]);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (config_file != NULL) {
|
|
|
|
free(config_file);
|
|
|
|
}
|
|
|
|
printusage(1);
|
|
|
|
return 1;
|
|
|
|
}
|
2016-10-17 11:31:42 -03:00
|
|
|
}
|
|
|
|
else if ((strcmp(argv[i], "-s") == 0 || strcmp(argv[i], "--significado") == 0) && meaning == false) {
|
|
|
|
meaning = true;
|
2016-10-13 15:08:01 -03:00
|
|
|
}
|
|
|
|
else {
|
2016-10-13 19:55:33 -03:00
|
|
|
/* Incorrect usage */
|
2016-10-13 21:07:35 -03:00
|
|
|
if (config_file != NULL) {
|
|
|
|
free(config_file);
|
|
|
|
}
|
2016-10-13 15:08:01 -03:00
|
|
|
printusage(1);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-17 11:31:42 -03:00
|
|
|
if (meaning == true) {
|
|
|
|
if (config_file != NULL) {
|
|
|
|
free(config_file);
|
|
|
|
config_file = NULL;
|
|
|
|
}
|
|
|
|
printf("El significado de la vida es: %d\n", MEANING);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-10-13 19:55:33 -03:00
|
|
|
config = (CONFIG *) malloc(sizeof(CONFIG));
|
2016-10-13 21:07:35 -03:00
|
|
|
config->file = NULL;
|
|
|
|
config->bible = NULL;
|
|
|
|
config->book = NULL;
|
|
|
|
config->chapter = NULL;
|
|
|
|
config->chapter_numbers = NULL;
|
2016-10-13 17:19:59 -03:00
|
|
|
|
2016-10-16 14:16:03 -03:00
|
|
|
status = readconfig(config_file, config);
|
2016-10-13 19:55:33 -03:00
|
|
|
free(config_file);
|
|
|
|
config_file = NULL;
|
2016-10-13 15:08:01 -03:00
|
|
|
if (status != 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-10-13 17:31:50 -03:00
|
|
|
printf("ConfiguraciĆ³n:\n");
|
2016-10-13 17:50:55 -03:00
|
|
|
printf("\tArchivo: %s\n", config->file);
|
|
|
|
printf("\tBiblia: %s\n", config->bible);
|
|
|
|
printf("\tLibro: %s\n", config->book);
|
|
|
|
printf("\tNombre de capitulo: %s\n", config->chapter);
|
|
|
|
printf("\tNumeros de capitulo: %s\n", config->chapter_numbers);
|
2016-10-13 17:19:59 -03:00
|
|
|
|
2016-10-16 14:16:03 -03:00
|
|
|
status = readfile(config);
|
|
|
|
if (status != 0) {
|
|
|
|
printf("FallĆ³ leer Biblia.txt!\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-10-13 19:55:33 -03:00
|
|
|
free(config->file);
|
|
|
|
free(config->bible);
|
|
|
|
free(config->book);
|
|
|
|
free(config->chapter);
|
|
|
|
free(config->chapter_numbers);
|
|
|
|
free(config);
|
|
|
|
config = NULL;
|
|
|
|
|
2016-10-13 15:08:01 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-10-13 17:31:50 -03:00
|
|
|
#else
|
|
|
|
|
2016-10-13 17:46:05 -03:00
|
|
|
/*
|
|
|
|
* Alternate main designed to prevent problems if the host system does not have
|
|
|
|
* tree support enabled during compile.
|
|
|
|
*/
|
2016-10-13 17:31:50 -03:00
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
fprintf(stderr, "libxml2 no tiene tree support compilado\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2016-10-13 17:46:05 -03:00
|
|
|
/*
|
|
|
|
* This function is called on exit to clean up the memory usage.
|
|
|
|
*/
|
2016-10-13 17:19:59 -03:00
|
|
|
void cleanup() {
|
2016-10-13 17:46:05 -03:00
|
|
|
/* Cleanup on aisle 3 */
|
2016-10-16 17:49:48 -03:00
|
|
|
if (config) {
|
|
|
|
if (config->file) {
|
2016-10-13 19:55:33 -03:00
|
|
|
free(config->file);
|
2016-10-13 17:19:59 -03:00
|
|
|
}
|
2016-10-16 17:49:48 -03:00
|
|
|
if (config->bible) {
|
2016-10-13 19:55:33 -03:00
|
|
|
free(config->bible);
|
2016-10-13 17:19:59 -03:00
|
|
|
}
|
2016-10-16 17:49:48 -03:00
|
|
|
if (config->book) {
|
2016-10-13 19:55:33 -03:00
|
|
|
free(config->book);
|
2016-10-13 17:19:59 -03:00
|
|
|
}
|
2016-10-16 17:49:48 -03:00
|
|
|
if (config->chapter) {
|
2016-10-13 19:55:33 -03:00
|
|
|
free(config->chapter);
|
2016-10-13 17:19:59 -03:00
|
|
|
}
|
2016-10-16 17:49:48 -03:00
|
|
|
if (config->chapter_numbers) {
|
2016-10-13 19:55:33 -03:00
|
|
|
free(config->chapter_numbers);
|
2016-10-13 17:19:59 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
free(config);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-13 17:46:05 -03:00
|
|
|
/*
|
|
|
|
* Print information on the program's usage. If the argument is 1, the user
|
|
|
|
* put something incorrect as an argument.
|
|
|
|
*/
|
2016-10-13 15:08:01 -03:00
|
|
|
void printusage(int error) {
|
|
|
|
if (error == 1) {
|
|
|
|
printf("Opcion desconocido!\n\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("usage: generarxml [opciones] \n");
|
2016-10-16 14:16:03 -03:00
|
|
|
printf(" -c, --config <archivo> archivo de configuraciĆ³n\n");
|
2016-10-17 11:31:42 -03:00
|
|
|
printf(" -s, --significado imprimir el significado de la vida\n");
|
2016-10-13 15:08:01 -03:00
|
|
|
}
|