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.
 
 
 
 

117 lines
2.6 KiB

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