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.
 
 
 
 

182 lines
4.4 KiB

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