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.
 
 
 
 

102 lines
3.9 KiB

  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, CONFIG *config) {
  13. /* Initilize the library */
  14. LIBXML_TEST_VERSION
  15. xmlParserCtxtPtr context;
  16. xmlDocPtr config_xml = NULL;
  17. xmlNodePtr root = NULL;
  18. xmlNodePtr node = NULL;
  19. xmlNodePtr subnode = NULL;
  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, NULL, 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. /* Run through the nodes to find the config information. */
  39. node = root->xmlChildrenNode;
  40. while (node != NULL) {
  41. if ((!xmlStrcmp(node->name, (const xmlChar *) "output"))){
  42. xmlChar *file = xmlNodeListGetString(config_xml, node->xmlChildrenNode, 1);
  43. config->file = (char *) malloc(strlen((char *) file) + 1 * sizeof(char));
  44. strcpy(config->file, (char *) file);
  45. xmlFree(file);
  46. }
  47. else if ((!xmlStrcmp(node->name, (const xmlChar *) "bible"))){
  48. xmlChar *bible = xmlNodeListGetString(config_xml, node->xmlChildrenNode, 1);
  49. config->bible = (char *) malloc(strlen((char *) bible) + 1 * sizeof(char));
  50. strcpy(config->bible, (char *) bible);
  51. xmlFree(bible);
  52. }
  53. else if ((!xmlStrcmp(node->name, (const xmlChar *) "book"))){
  54. xmlChar *book = xmlNodeListGetString(config_xml, node->xmlChildrenNode, 1);
  55. config->book = (char *) malloc(strlen((char *) book) + 1 * sizeof(char));
  56. strcpy(config->book, (char *) book);
  57. xmlFree(book);
  58. }
  59. else if ((!xmlStrcmp(node->name, (const xmlChar *) "chapter"))){
  60. subnode = node->xmlChildrenNode;
  61. while (subnode != NULL) {
  62. if ((!xmlStrcmp(subnode->name, (const xmlChar *) "name"))){
  63. xmlChar *chapter = xmlNodeListGetString(config_xml, subnode->xmlChildrenNode, 1);
  64. config->chapter = (char *) malloc(strlen((char *) chapter) + 1 * sizeof(char));
  65. strcpy(config->chapter, (char *) chapter);
  66. xmlFree(chapter);
  67. }
  68. if ((!xmlStrcmp(subnode->name, (const xmlChar *) "number"))){
  69. xmlChar *chapter_numbers = xmlNodeListGetString(config_xml, subnode->xmlChildrenNode, 1);
  70. config->chapter_numbers = (char *) malloc(strlen((char *) chapter_numbers) + 1 * sizeof(char));
  71. strcpy(config->chapter_numbers, (char *) chapter_numbers);
  72. xmlFree(chapter_numbers);
  73. }
  74. subnode = subnode->next;
  75. }
  76. }
  77. node = node->next;
  78. }
  79. xmlFreeDoc(config_xml);
  80. }
  81. /* If any config info is missing, abort */
  82. if (config->file == NULL || config->bible == NULL || config->book == NULL || config->chapter == NULL || config->chapter_numbers == NULL) {
  83. printf("El archivo de configuración es invalido!\n");
  84. xmlFreeParserCtxt(context);
  85. return 1;
  86. }
  87. xmlFreeParserCtxt(context);
  88. return 0;
  89. }