Program to generate an xml file based on a provided text file
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.c 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <libxml/tree.h>
  5. #include <stdbool.h>
  6. #include "main.h"
  7. #include "readconfig.h"
  8. #include "readfile.h"
  9. #include "makexml.h"
  10. #if defined(LIBXML_TREE_ENABLED) && defined(LIBXML_OUTPUT_ENABLED)
  11. /*
  12. * This program is designed to take a text file and convert part of it into xml.
  13. */
  14. int main(int argc, char **argv) {
  15. char *config_file = NULL;
  16. int status;
  17. int i;
  18. bool meaning = false;
  19. atexit(cleanup);
  20. if (argc == 1) {
  21. /* No arguments were passed */
  22. print_usage(0);
  23. return 1;
  24. }
  25. /* Read the command line arguments */
  26. #ifdef DEBUG
  27. printf("Arguments:\n");
  28. #endif
  29. for (i = 1; i < argc; i++) {
  30. #ifdef DEBUG
  31. printf("\targ %d: %s\n", i, argv[i]);
  32. #endif
  33. if ((strcmp(argv[i], "-c") == 0 || strcmp(argv[i], "--config") == 0) && config_file == NULL) {
  34. if (argc > i + 1) {
  35. i++;
  36. config_file = (char *) malloc((strlen(argv[i]) + 1) * sizeof(char *));
  37. strcpy(config_file, argv[i]);
  38. #ifdef DEBUG
  39. printf("\targ %d: %s\n", i, argv[i]);
  40. #endif
  41. }
  42. else {
  43. if (config_file != NULL) {
  44. free(config_file);
  45. }
  46. print_usage(1);
  47. return 1;
  48. }
  49. }
  50. else if ((strcmp(argv[i], "-s") == 0 || strcmp(argv[i], "--significado") == 0) && meaning == false) {
  51. meaning = true;
  52. }
  53. else {
  54. /* Incorrect usage */
  55. if (config_file != NULL) {
  56. free(config_file);
  57. }
  58. print_usage(1);
  59. return 1;
  60. }
  61. }
  62. if (meaning == true) {
  63. if (config_file != NULL) {
  64. free(config_file);
  65. config_file = NULL;
  66. }
  67. printf("El significado de la vida es: %d\n", MEANING);
  68. return 0;
  69. }
  70. config = (CONFIG *) malloc(sizeof(CONFIG));
  71. config->file = NULL;
  72. config->bible = NULL;
  73. config->book = NULL;
  74. config->chapter = NULL;
  75. config->chapter_numbers = NULL;
  76. status = read_config(config_file);
  77. if (status != 0) {
  78. free(config_file);
  79. return 1;
  80. }
  81. if (strcmp(config->file, argv[0]) == 0 ||
  82. strcmp(config->file, config_file) == 0 ||
  83. strcmp(config->file, "Biblia.txt") == 0 ||
  84. strcmp(config->file, "config.dtd") == 0 ||
  85. strcmp(config->file, "generarxml.dtd") == 0) {
  86. free(config_file);
  87. config_file = NULL;
  88. printf("Nombre de archivo de salida invalido!\n");
  89. return 1;
  90. }
  91. free(config_file);
  92. config_file = NULL;
  93. printf("Configuración:\n");
  94. printf("\tArchivo: %s\n", config->file);
  95. printf("\tBiblia: %s\n", config->bible);
  96. printf("\tLibro: %s\n", config->book);
  97. printf("\tNombre de capitulo: %s\n", config->chapter);
  98. printf("\tNumeros de capitulo: %s\n", config->chapter_numbers);
  99. book = (BOOK *) malloc(sizeof(BOOK));
  100. status = read_file();
  101. if (status != 0) {
  102. printf("Falló leer Biblia.txt!\n");
  103. return 1;
  104. }
  105. make_xml();
  106. printf("El achivo %s ha sido generado.\n", config->file);
  107. free(config->file);
  108. free(config->bible);
  109. free(config->book);
  110. free(config->chapter);
  111. free(config->chapter_numbers);
  112. free(config);
  113. config = NULL;
  114. return 0;
  115. }
  116. #else
  117. /*
  118. * Alternate main designed to prevent problems if the host system does not have
  119. * tree support enabled during compile.
  120. */
  121. int main(int argc, char *argv[]) {
  122. fprintf(stderr, "libxml2 no está configurado correctamente\n");
  123. return 1;
  124. }
  125. #endif
  126. /*
  127. * This function is called on exit to clean up the memory usage.
  128. */
  129. void cleanup() {
  130. /* cleanup on aisle 3 */
  131. int i;
  132. int j;
  133. if (config) {
  134. if (config->file) {
  135. free(config->file);
  136. }
  137. if (config->bible) {
  138. free(config->bible);
  139. }
  140. if (config->book) {
  141. free(config->book);
  142. }
  143. if (config->chapter) {
  144. free(config->chapter);
  145. }
  146. if (config->chapter_numbers) {
  147. free(config->chapter_numbers);
  148. }
  149. free(config);
  150. }
  151. if (book) {
  152. for (i = 0; i < book->chapters; i++) {
  153. for (j = 0; j < book->chapter[i]->verses; j++) {
  154. free(book->chapter[i]->verse[j]);
  155. }
  156. free(book->chapter[i]->verse);
  157. free(book->chapter[i]);
  158. }
  159. free(book->chapter);
  160. free(book);
  161. }
  162. }
  163. /*
  164. * Print information on the program's usage. If the argument is 1, the user
  165. * put something incorrect as an argument.
  166. */
  167. void print_usage(int error) {
  168. if (error == 1) {
  169. printf("Opcion desconocido!\n\n");
  170. }
  171. printf("usage: generarxml [opciones] \n");
  172. printf(" -c, --config <archivo> archivo de configuración\n");
  173. /*printf(" -s, --significado imprimir el significado de la vida\n");*/
  174. }