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.

151 lines
3.5KB

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