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.

133 lines
3.0KB

  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. for (i = 1; i < argc; i++) {
  24. if ((strcmp(argv[1], "-c") == 0 || strcmp(argv[1], "--config") == 0) && config_file == NULL) {
  25. i++;
  26. config_file = (char *) malloc((strlen(argv[2]) + 1) * sizeof(char *));
  27. strcpy(config_file, argv[2]);
  28. }
  29. else {
  30. /* Incorrect usage */
  31. if (config_file != NULL) {
  32. free(config_file);
  33. }
  34. printusage(1);
  35. return 1;
  36. }
  37. }
  38. config = (CONFIG *) malloc(sizeof(CONFIG));
  39. config->file = NULL;
  40. config->bible = NULL;
  41. config->book = NULL;
  42. config->chapter = NULL;
  43. config->chapter_numbers = NULL;
  44. status = readconfig(config_file, config);
  45. free(config_file);
  46. config_file = NULL;
  47. if (status != 0) {
  48. return 1;
  49. }
  50. printf("Configuración:\n");
  51. printf("\tArchivo: %s\n", config->file);
  52. printf("\tBiblia: %s\n", config->bible);
  53. printf("\tLibro: %s\n", config->book);
  54. printf("\tNombre de capitulo: %s\n", config->chapter);
  55. printf("\tNumeros de capitulo: %s\n", config->chapter_numbers);
  56. status = readfile(config);
  57. if (status != 0) {
  58. printf("Falló leer Biblia.txt!\n");
  59. return 1;
  60. }
  61. free(config->file);
  62. free(config->bible);
  63. free(config->book);
  64. free(config->chapter);
  65. free(config->chapter_numbers);
  66. free(config);
  67. config = NULL;
  68. return 0;
  69. }
  70. #else
  71. /*
  72. * Alternate main designed to prevent problems if the host system does not have
  73. * tree support enabled during compile.
  74. */
  75. int main(int argc, char *argv[]) {
  76. fprintf(stderr, "libxml2 no tiene tree support compilado\n");
  77. return 1;
  78. }
  79. #endif
  80. /*
  81. * This function is called on exit to clean up the memory usage.
  82. */
  83. void cleanup() {
  84. /* Cleanup on aisle 3 */
  85. if (config != NULL) {
  86. if (config->file != NULL) {
  87. free(config->file);
  88. }
  89. if (config->bible != NULL) {
  90. free(config->bible);
  91. }
  92. if (config->book != NULL) {
  93. free(config->book);
  94. }
  95. if (config->chapter != NULL) {
  96. free(config->chapter);
  97. }
  98. if (config->chapter_numbers != NULL) {
  99. free(config->chapter_numbers);
  100. }
  101. free(config);
  102. }
  103. }
  104. /*
  105. * Print information on the program's usage. If the argument is 1, the user
  106. * put something incorrect as an argument.
  107. */
  108. void printusage(int error) {
  109. if (error == 1) {
  110. printf("Opcion desconocido!\n\n");
  111. }
  112. printf("usage: generarxml [opciones] \n");
  113. printf(" -c, --config <archivo> archivo de configuración\n");
  114. }