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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. status = readfile(config);
  88. if (status != 0) {
  89. printf("Falló leer Biblia.txt!\n");
  90. return 1;
  91. }
  92. free(config->file);
  93. free(config->bible);
  94. free(config->book);
  95. free(config->chapter);
  96. free(config->chapter_numbers);
  97. free(config);
  98. config = NULL;
  99. return 0;
  100. }
  101. #else
  102. /*
  103. * Alternate main designed to prevent problems if the host system does not have
  104. * tree support enabled during compile.
  105. */
  106. int main(int argc, char *argv[]) {
  107. fprintf(stderr, "libxml2 no tiene tree support compilado\n");
  108. return 1;
  109. }
  110. #endif
  111. /*
  112. * This function is called on exit to clean up the memory usage.
  113. */
  114. void cleanup() {
  115. /* Cleanup on aisle 3 */
  116. if (config) {
  117. if (config->file) {
  118. free(config->file);
  119. }
  120. if (config->bible) {
  121. free(config->bible);
  122. }
  123. if (config->book) {
  124. free(config->book);
  125. }
  126. if (config->chapter) {
  127. free(config->chapter);
  128. }
  129. if (config->chapter_numbers) {
  130. free(config->chapter_numbers);
  131. }
  132. free(config);
  133. }
  134. }
  135. /*
  136. * Print information on the program's usage. If the argument is 1, the user
  137. * put something incorrect as an argument.
  138. */
  139. void printusage(int error) {
  140. if (error == 1) {
  141. printf("Opcion desconocido!\n\n");
  142. }
  143. printf("usage: generarxml [opciones] \n");
  144. printf(" -c, --config <archivo> archivo de configuración\n");
  145. /*printf(" -s, --significado imprimir el significado de la vida\n");*/
  146. }