diff options
| author | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2023-09-15 14:58:37 +0200 |
|---|---|---|
| committer | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2023-09-15 14:58:37 +0200 |
| commit | 70670e129ef09acfb28be013d06f2773b5a7d04d (patch) | |
| tree | 7114a0986c71c4d298799796c0056943c920795c /examples/html | |
| parent | 07c59a83d1255e9772afd13299955bf710bbe988 (diff) | |
Add HTML serializer example
Diffstat (limited to 'examples/html')
| -rw-r--r-- | examples/html/CMakeLists.txt | 4 | ||||
| -rw-r--r-- | examples/html/Makefile | 26 | ||||
| -rw-r--r-- | examples/html/README.md | 25 | ||||
| -rw-r--r-- | examples/html/main.c | 59 |
4 files changed, 114 insertions, 0 deletions
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: + +``` +<html> + <head> + <meta charset="UTF-8"/> + </head> + <body>testing slweb</body> +</html> +``` 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 <slweb/html.h> +#include <dynstr.h> +#include <stdlib.h> +#include <stdio.h> + +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; +} |
