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

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