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.

readconfig.c 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <libxml/parser.h>
  4. #include <libxml/tree.h>
  5. #include <libxml/xmlIO.h>
  6. #include <libxml/xinclude.h>
  7. #include "main.h"
  8. /*
  9. * This function reads the designated config file and save into int the
  10. * config struct.
  11. */
  12. int readconfig(char *config_file) {
  13. xmlParserCtxtPtr context;
  14. xmlDocPtr config_xml = NULL;
  15. xmlNodePtr root = NULL;
  16. xmlNodePtr node = NULL;
  17. xmlNodePtr subnode = NULL;
  18. /* Initilize the library */
  19. LIBXML_TEST_VERSION
  20. context = xmlNewParserCtxt();
  21. if (context == NULL) {
  22. fprintf(stderr, "No pudo alocar contexto de analizador!\n");
  23. return 1;
  24. }
  25. config_xml = xmlCtxtReadFile(context, config_file, "UTF-8", XML_PARSE_DTDVALID);
  26. if (config_xml == NULL) {
  27. fprintf(stderr, "Falló analizar %s\n", config_file);
  28. xmlFreeParserCtxt(context);
  29. return 1;
  30. }
  31. else {
  32. if (context->valid == 0) {
  33. fprintf(stderr, "Falló validar %s\n", config_file);
  34. xmlFreeParserCtxt(context);
  35. return 1;
  36. }
  37. root = xmlDocGetRootElement(config_xml);
  38. if (root == NULL) {
  39. fprintf(stderr,"El archivo %s no contiene xml\n", config_file);
  40. xmlFreeDoc(config_xml);
  41. return 1;
  42. }
  43. /* Run through the nodes to find the config information. */
  44. node = root->xmlChildrenNode;
  45. while (node != NULL) {
  46. if ((!xmlStrcmp(node->name, (const xmlChar *) "output"))){
  47. xmlChar *file = xmlNodeListGetString(config_xml, node->xmlChildrenNode, 1);
  48. config->file = (char *) malloc(strlen((char *) file) + 1 * sizeof(char));
  49. if (!config->file) {
  50. fprintf(stderr, "Allocación de memoria falló.");
  51. return 1;
  52. }
  53. strcpy(config->file, (char *) file);
  54. xmlFree(file);
  55. }
  56. else if ((!xmlStrcmp(node->name, (const xmlChar *) "bible"))){
  57. xmlChar *bible = xmlNodeListGetRawString(config_xml, node->xmlChildrenNode, 1);
  58. config->bible = (char *) malloc(strlen((char *) bible) + 1 * sizeof(char));
  59. if (!config->bible) {
  60. fprintf(stderr, "Allocación de memoria falló.");
  61. return 1;
  62. }
  63. strcpy(config->bible, (char *) bible);
  64. xmlFree(bible);
  65. }
  66. else if ((!xmlStrcmp(node->name, (const xmlChar *) "book"))){
  67. xmlChar *book = xmlNodeListGetString(config_xml, node->xmlChildrenNode, 1);
  68. config->book = (char *) malloc(strlen((char *) book) + 1 * sizeof(char));
  69. if (!config->book) {
  70. fprintf(stderr, "Allocación de memoria falló.");
  71. return 1;
  72. }
  73. strcpy(config->book, (char *) book);
  74. xmlFree(book);
  75. }
  76. else if ((!xmlStrcmp(node->name, (const xmlChar *) "chapter"))){
  77. subnode = node->xmlChildrenNode;
  78. while (subnode != NULL) {
  79. if ((!xmlStrcmp(subnode->name, (const xmlChar *) "name"))){
  80. xmlChar *chapter = xmlNodeListGetString(config_xml, subnode->xmlChildrenNode, 1);
  81. config->chapter = (char *) malloc(strlen((char *) chapter) + 1 * sizeof(char));
  82. if (!config->chapter) {
  83. fprintf(stderr, "Allocación de memoria falló.");
  84. return 1;
  85. }
  86. strcpy(config->chapter, (char *) chapter);
  87. xmlFree(chapter);
  88. }
  89. if ((!xmlStrcmp(subnode->name, (const xmlChar *) "number"))){
  90. xmlChar *chapter_numbers = xmlNodeListGetString(config_xml, subnode->xmlChildrenNode, 1);
  91. config->chapter_numbers = (char *) malloc(strlen((char *) chapter_numbers) + 1 * sizeof(char));
  92. if (!config->chapter_numbers) {
  93. fprintf(stderr, "Allocación de memoria falló.");
  94. return 1;
  95. }
  96. strcpy(config->chapter_numbers, (char *) chapter_numbers);
  97. xmlFree(chapter_numbers);
  98. }
  99. subnode = subnode->next;
  100. }
  101. }
  102. node = node->next;
  103. }
  104. xmlFreeDoc(config_xml);
  105. }
  106. /* If any config info is missing, abort */
  107. if (config->file == NULL || config->bible == NULL || config->book == NULL || config->chapter == NULL || config->chapter_numbers == NULL) {
  108. printf("El archivo de configuración es invalido!\n");
  109. xmlFreeParserCtxt(context);
  110. return 1;
  111. }
  112. xmlFreeParserCtxt(context);
  113. return 0;
  114. }