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.

197 lines
4.8KB

  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. printusage(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. printusage(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. printusage(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 = readconfig(config_file);
  77. if (status != 0) {
  78. return 1;
  79. }
  80. if (strcmp(config->file, argv[0]) == 0 ||
  81. strcmp(config->file, config_file) == 0 ||
  82. strcmp(config->file, "Biblia.txt") == 0 ||
  83. strcmp(config->file, "config.dtd") == 0 ||
  84. strcmp(config->file, "generarxml.dtd") == 0) {
  85. free(config_file);
  86. config_file = NULL;
  87. printf("Nombre de archivo de salida invalido!\n");
  88. return 1;
  89. }
  90. free(config_file);
  91. config_file = NULL;
  92. printf("Configuración:\n");
  93. printf("\tArchivo: %s\n", config->file);
  94. printf("\tBiblia: %s\n", config->bible);
  95. printf("\tLibro: %s\n", config->book);
  96. printf("\tNombre de capitulo: %s\n", config->chapter);
  97. printf("\tNumeros de capitulo: %s\n", config->chapter_numbers);
  98. book = (BOOK *) malloc(sizeof(BOOK));
  99. status = readfile();
  100. if (status != 0) {
  101. printf("Falló leer Biblia.txt!\n");
  102. return 1;
  103. }
  104. makexml();
  105. free(config->file);
  106. free(config->bible);
  107. free(config->book);
  108. free(config->chapter);
  109. free(config->chapter_numbers);
  110. free(config);
  111. config = NULL;
  112. return 0;
  113. }
  114. #else
  115. /*
  116. * Alternate main designed to prevent problems if the host system does not have
  117. * tree support enabled during compile.
  118. */
  119. int main(int argc, char *argv[]) {
  120. fprintf(stderr, "libxml2 no está configurado correctamente\n");
  121. return 1;
  122. }
  123. #endif
  124. /*
  125. * This function is called on exit to clean up the memory usage.
  126. */
  127. void cleanup() {
  128. /* Cleanup on aisle 3 */
  129. int i;
  130. int j;
  131. if (config) {
  132. if (config->file) {
  133. free(config->file);
  134. }
  135. if (config->bible) {
  136. free(config->bible);
  137. }
  138. if (config->book) {
  139. free(config->book);
  140. }
  141. if (config->chapter) {
  142. free(config->chapter);
  143. }
  144. if (config->chapter_numbers) {
  145. free(config->chapter_numbers);
  146. }
  147. free(config);
  148. }
  149. if (book) {
  150. for (i = 0; i < book->chapters; i++) {
  151. for (j = 0; j < book->chapter[i]->verses; j++) {
  152. free(book->chapter[i]->verse[j]);
  153. }
  154. free(book->chapter[i]->verse);
  155. free(book->chapter[i]);
  156. }
  157. free(book->chapter);
  158. free(book);
  159. }
  160. }
  161. /*
  162. * Print information on the program's usage. If the argument is 1, the user
  163. * put something incorrect as an argument.
  164. */
  165. void printusage(int error) {
  166. if (error == 1) {
  167. printf("Opcion desconocido!\n\n");
  168. }
  169. printf("usage: generarxml [opciones] \n");
  170. printf(" -c, --config <archivo> archivo de configuración\n");
  171. /*printf(" -s, --significado imprimir el significado de la vida\n");*/
  172. }