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.
 
 
 
 

108 lines
4.0 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. 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. strcpy(config->file, (char *) file);
  50. xmlFree(file);
  51. }
  52. else if ((!xmlStrcmp(node->name, (const xmlChar *) "bible"))){
  53. xmlChar *bible = xmlNodeListGetString(config_xml, node->xmlChildrenNode, 1);
  54. config->bible = (char *) malloc(strlen((char *) bible) + 1 * sizeof(char));
  55. strcpy(config->bible, (char *) bible);
  56. xmlFree(bible);
  57. }
  58. else if ((!xmlStrcmp(node->name, (const xmlChar *) "book"))){
  59. xmlChar *book = xmlNodeListGetString(config_xml, node->xmlChildrenNode, 1);
  60. config->book = (char *) malloc(strlen((char *) book) + 1 * sizeof(char));
  61. strcpy(config->book, (char *) book);
  62. xmlFree(book);
  63. }
  64. else if ((!xmlStrcmp(node->name, (const xmlChar *) "chapter"))){
  65. subnode = node->xmlChildrenNode;
  66. while (subnode != NULL) {
  67. if ((!xmlStrcmp(subnode->name, (const xmlChar *) "name"))){
  68. xmlChar *chapter = xmlNodeListGetString(config_xml, subnode->xmlChildrenNode, 1);
  69. config->chapter = (char *) malloc(strlen((char *) chapter) + 1 * sizeof(char));
  70. strcpy(config->chapter, (char *) chapter);
  71. xmlFree(chapter);
  72. }
  73. if ((!xmlStrcmp(subnode->name, (const xmlChar *) "number"))){
  74. xmlChar *chapter_numbers = xmlNodeListGetString(config_xml, subnode->xmlChildrenNode, 1);
  75. config->chapter_numbers = (char *) malloc(strlen((char *) chapter_numbers) + 1 * sizeof(char));
  76. strcpy(config->chapter_numbers, (char *) chapter_numbers);
  77. xmlFree(chapter_numbers);
  78. }
  79. subnode = subnode->next;
  80. }
  81. }
  82. node = node->next;
  83. }
  84. xmlFreeDoc(config_xml);
  85. }
  86. /* If any config info is missing, abort */
  87. if (config->file == NULL || config->bible == NULL || config->book == NULL || config->chapter == NULL || config->chapter_numbers == NULL) {
  88. printf("El archivo de configuración es invalido!\n");
  89. xmlFreeParserCtxt(context);
  90. return 1;
  91. }
  92. xmlFreeParserCtxt(context);
  93. return 0;
  94. }