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.

87 lines
2.9KB

  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. xmlParserCtxt *context;
  16. xmlDoc *config_xml = NULL;
  17. xmlNode *root = NULL;
  18. xmlNode *node = NULL;
  19. xmlNode *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. config->file = xmlNodeListGetString(config_xml, node->xmlChildrenNode, 1);
  43. }
  44. else if ((!xmlStrcmp(node->name, (const xmlChar *) "bible"))){
  45. config->bible = xmlNodeListGetString(config_xml, node->xmlChildrenNode, 1);
  46. }
  47. else if ((!xmlStrcmp(node->name, (const xmlChar *) "book"))){
  48. config->book = xmlNodeListGetString(config_xml, node->xmlChildrenNode, 1);
  49. }
  50. else if ((!xmlStrcmp(node->name, (const xmlChar *) "chapter"))){
  51. subnode = node->xmlChildrenNode;
  52. while (subnode != NULL) {
  53. if ((!xmlStrcmp(subnode->name, (const xmlChar *) "name"))){
  54. config->chapter = xmlNodeListGetString(config_xml, subnode->xmlChildrenNode, 1);
  55. }
  56. if ((!xmlStrcmp(subnode->name, (const xmlChar *) "number"))){
  57. config->chapter_numbers = xmlNodeListGetString(config_xml, subnode->xmlChildrenNode, 1);
  58. }
  59. subnode = subnode->next;
  60. }
  61. }
  62. node = node->next;
  63. }
  64. xmlFreeDoc(config_xml);
  65. }
  66. /* If any config info is missing, abort */
  67. if (config->file == NULL || config->bible == NULL || config->book == NULL || config->chapter == NULL || config->chapter_numbers == NULL) {
  68. printf("El archivo de configuración es invalido!");
  69. xmlFreeParserCtxt(context);
  70. return 1;
  71. }
  72. xmlFreeParserCtxt(context);
  73. return 0;
  74. }