From 70670e129ef09acfb28be013d06f2773b5a7d04d Mon Sep 17 00:00:00 2001 From: Xavier Del Campo Romero Date: Fri, 15 Sep 2023 14:58:37 +0200 Subject: Add HTML serializer example --- .gitignore | 1 + examples/CMakeLists.txt | 1 + examples/html/CMakeLists.txt | 4 +++ examples/html/Makefile | 26 +++++++++++++++++++ examples/html/README.md | 25 +++++++++++++++++++ examples/html/main.c | 59 ++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 116 insertions(+) create mode 100644 examples/html/CMakeLists.txt create mode 100644 examples/html/Makefile create mode 100644 examples/html/README.md create mode 100644 examples/html/main.c diff --git a/.gitignore b/.gitignore index 65771fe..97946af 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ *.a *.d examples/hello/hello +examples/html/html diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 1c51e14..d9bf4cc 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,2 +1,3 @@ cmake_minimum_required(VERSION 3.13) add_subdirectory(hello) +add_subdirectory(html) diff --git a/examples/html/CMakeLists.txt b/examples/html/CMakeLists.txt new file mode 100644 index 0000000..bc4202d --- /dev/null +++ b/examples/html/CMakeLists.txt @@ -0,0 +1,4 @@ +cmake_minimum_required(VERSION 3.13) +project(html C) +add_executable(html main.c) +target_link_libraries(${PROJECT_NAME} PRIVATE slweb dynstr) diff --git a/examples/html/Makefile b/examples/html/Makefile new file mode 100644 index 0000000..77d94f8 --- /dev/null +++ b/examples/html/Makefile @@ -0,0 +1,26 @@ +.POSIX: + +PROJECT = html +DEPS = \ + main.o +SLWEB = ../../libslweb.a +DYNSTR = ../../dynstr/libdynstr.a +CFLAGS = -I ../../include -I ../../dynstr/include +SLWEB_FLAGS = -L ../../ -l slweb +DYNSTR_FLAGS = -L ../../dynstr -l dynstr + +all: $(PROJECT) + +clean: + rm -f $(DEPS) + +FORCE: + +$(PROJECT): $(DEPS) $(SLWEB) $(DYNSTR) + $(CC) $(LDFLAGS) $(DEPS) $(SLWEB_FLAGS) $(DYNSTR_FLAGS) -o $@ + +$(SLWEB): FORCE + +cd ../../ && $(MAKE) + +$(DYNSTR): FORCE + +cd ../../dynstr && $(MAKE) diff --git a/examples/html/README.md b/examples/html/README.md new file mode 100644 index 0000000..ac89287 --- /dev/null +++ b/examples/html/README.md @@ -0,0 +1,25 @@ +# HTML serializer example + +This example shows a minimal application that serializes a HTML file +and writes it to standard output. + +## How to build + +If using `make(1)`, just run `make` from this directory. + +If using CMake, examples are built by default when configuring the project +from [the top-level `CMakeLists.txt`](../../CMakeLists.txt). + +## How to run + +Run the executable without any command line arguments. The following text +should be written to the standard output: + +``` + + + + + testing slweb + +``` diff --git a/examples/html/main.c b/examples/html/main.c new file mode 100644 index 0000000..c29e360 --- /dev/null +++ b/examples/html/main.c @@ -0,0 +1,59 @@ +#include +#include +#include +#include + +int main() +{ + int ret = EXIT_FAILURE; + struct dynstr d; + struct html_node *const html = html_node_alloc("html"), *head, + *meta, *body; + static const char text[] = "testing slweb"; + + dynstr_init(&d); + + if (!html) + { + fprintf(stderr, "html_node_alloc_failed\n"); + goto end; + } + else if (!(head = html_node_add_child(html, "head"))) + { + fprintf(stderr, "html_node_add_child head failed\n"); + goto end; + } + else if (!(meta = html_node_add_child(head, "meta"))) + { + fprintf(stderr, "html_node_add_child meta failed\n"); + goto end; + } + else if (html_node_add_attr(meta, "charset", "UTF-8")) + { + fprintf(stderr, "%s: html_node_add_attr charset failed\n", __func__); + goto end; + } + else if (!(body = html_node_add_child(html, "body"))) + { + fprintf(stderr, "html_node_add_child failed\n"); + goto end; + } + else if (html_node_set_value(body, text)) + { + fprintf(stderr, "html_node_set_value failed\n"); + goto end; + } + else if (html_serialize(html, &d)) + { + fprintf(stderr, "html_serialize failed\n"); + goto end; + } + + printf("%s", d.str); + ret = EXIT_SUCCESS; + +end: + dynstr_free(&d); + html_node_free(html); + return ret; +} -- cgit v1.2.3