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.

125 lines
2.9KB

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