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.

76 lines
2.5KB

  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. int readconfig(char *config_file, CONFIG *config) {
  9. /* Initilize the library */
  10. LIBXML_TEST_VERSION
  11. xmlParserCtxt *context;
  12. xmlDoc *config_xml = NULL;
  13. xmlNode *root = NULL;
  14. xmlNode *node = NULL;
  15. xmlNode *subnode = NULL;
  16. context = xmlNewParserCtxt();
  17. if (context == NULL) {
  18. fprintf(stderr, "No pudo alocar contexto de analizador!\n");
  19. return 1;
  20. }
  21. config_xml = xmlCtxtReadFile(context, config_file, NULL, XML_PARSE_DTDVALID);
  22. if (config_xml == NULL) {
  23. fprintf(stderr, "Falló analizar %s\n", config_file);
  24. }
  25. else {
  26. if (context->valid == 0) {
  27. fprintf(stderr, "Falló validar %s\n", config_file);
  28. }
  29. root = xmlDocGetRootElement(config_xml);
  30. node = root->xmlChildrenNode;
  31. while (node != NULL) {
  32. if ((!xmlStrcmp(node->name, (const xmlChar *) "output"))){
  33. config->file = xmlNodeListGetString(config_xml, node->xmlChildrenNode, 1);
  34. }
  35. else if ((!xmlStrcmp(node->name, (const xmlChar *) "bible"))){
  36. config->bible = xmlNodeListGetString(config_xml, node->xmlChildrenNode, 1);
  37. }
  38. else if ((!xmlStrcmp(node->name, (const xmlChar *) "book"))){
  39. config->book = xmlNodeListGetString(config_xml, node->xmlChildrenNode, 1);
  40. }
  41. else if ((!xmlStrcmp(node->name, (const xmlChar *) "chapter"))){
  42. subnode = node->xmlChildrenNode;
  43. while (subnode != NULL) {
  44. if ((!xmlStrcmp(subnode->name, (const xmlChar *) "name"))){
  45. config->chapter = xmlNodeListGetString(config_xml, subnode->xmlChildrenNode, 1);
  46. }
  47. if ((!xmlStrcmp(subnode->name, (const xmlChar *) "number"))){
  48. config->chapter_numbers = xmlNodeListGetString(config_xml, subnode->xmlChildrenNode, 1);
  49. }
  50. subnode = subnode->next;
  51. }
  52. }
  53. node = node->next;
  54. }
  55. xmlFreeDoc(config_xml);
  56. }
  57. if (config->file == NULL || config->bible == NULL || config->book == NULL || config->chapter == NULL || config->chapter_numbers == NULL) {
  58. printf("El archivo de configuración es invalido!");
  59. return 1;
  60. }
  61. xmlFreeParserCtxt(context);
  62. return 0;
  63. }