From d706fb8904be882df38dbfed838779e25d805bcc Mon Sep 17 00:00:00 2001 From: Gwendolyn Date: Wed, 8 Feb 2023 01:15:56 +0100 Subject: [PATCH] added clay-xml with xmllib2 that reads in an xml file with test.xml as input it produces the same output as clay-demo (except for different order of properties in the debug print) --- CMakeLists.txt | 12 ++++++++ src/clay-xml/main.c | 71 +++++++++++++++++++++++++++++++++++++++++++++ test.xml | 12 ++++++++ 3 files changed, 95 insertions(+) create mode 100644 src/clay-xml/main.c create mode 100644 test.xml diff --git a/CMakeLists.txt b/CMakeLists.txt index 22552de..3d2b745 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,6 +11,12 @@ FetchContent_Declare( GIT_TAG c3b672634f0635af1ad0ffa8c15b34fc7c1035cf # 1.17.8 ) +FetchContent_Declare( + libxml2 + GIT_REPOSITORY https://gitlab.gnome.org/GNOME/libxml2.git + GIT_TAG f507d167f1755b7eaea09fb1a44d29aab828b6d1 +) + #FetchContent_MakeAvailable(cairo) @@ -35,4 +41,10 @@ target_compile_options(clay PRIVATE -Wall -Werror) if (PROJECT_IS_TOP_LEVEL) add_executable(clay-demo src/clay-demo/demo.c) target_link_libraries(clay-demo PRIVATE clay) + + FetchContent_MakeAvailable(libxml2) + add_executable(clay-xml src/clay-xml/main.c) + target_link_libraries(clay-xml PRIVATE clay) + target_link_libraries(clay-xml PRIVATE LibXml2) + endif () \ No newline at end of file diff --git a/src/clay-xml/main.c b/src/clay-xml/main.c new file mode 100644 index 0000000..d21d760 --- /dev/null +++ b/src/clay-xml/main.c @@ -0,0 +1,71 @@ +#include +#include +#include +#include "clay.h" + + +clay make_clay_from_xml(clay_ctx, xmlNodePtr); + +int main(int argc, char **argv) { + assert(argc == 2); + const char * input_file_name = argv[1]; + + xmlDocPtr doc; + xmlNodePtr cur; + doc = xmlParseFile(input_file_name); + assert(doc != NULL); + + xmlNodePtr rootNode = xmlDocGetRootElement(doc); + assert(rootNode != NULL); + assert(xmlStrcmp(rootNode->name, (const xmlChar *) "clay") == 0); + + cur = rootNode->children; + xmlNodePtr documentNode = NULL; + while(cur != NULL) { + if (cur->type == XML_ELEMENT_NODE) { + assert(documentNode == NULL); + assert(xmlStrcmp(cur->name, (const xmlChar *) "document") == 0); + documentNode = cur; + } + cur = cur->next; + } + assert(documentNode != NULL); + + clay_ctx ctx = clay_create_context(); + + clay document_element = make_clay_from_xml(ctx, documentNode); + + xmlFreeDoc(doc); + + clay_print_layout_tree(document_element); + + clay_destroy_context(ctx); +} + + +clay make_clay_from_xml(clay_ctx ctx, xmlNodePtr xml_node) { + clay layout = clay_create(ctx, (const char *)xml_node->name); + xmlAttrPtr attribute = xml_node->properties; + while (attribute != NULL) { + const char * key = (const char *) attribute->name; + char * value = (char *) xmlNodeListGetString(xml_node->doc, attribute->children, 1); + clay_set(layout, key, value); + attribute = attribute->next; + } + + xmlNodePtr child = xml_node->children; + bool has_element_children = false; + while(child != NULL) { + if (child->type == XML_ELEMENT_NODE) { + clay child_layout = make_clay_from_xml(ctx, child); + clay_append_child(layout, child_layout); + has_element_children = true; + } + child = child->next; + } + if (!has_element_children) { + clay_set(layout, "text", (char*) xml_node->children->content); + } + + return layout; +} \ No newline at end of file diff --git a/test.xml b/test.xml new file mode 100644 index 0000000..1771118 --- /dev/null +++ b/test.xml @@ -0,0 +1,12 @@ + + + + + ITEM 1 + ITEM 2 + ITEM 3 + ITEM 4 + ITEM 5 + + + \ No newline at end of file