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.8KB

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