From a0f5f7509bb9040752fa61fe0fdb447608e22b1c Mon Sep 17 00:00:00 2001 From: Xavier Del Campo Romero Date: Tue, 30 Sep 2025 23:50:33 +0200 Subject: Implement form interface This new interface allows library users to parse application/x-www-form-urlencoded data conveniently. --- examples/form/main.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 examples/form/main.c (limited to 'examples/form/main.c') diff --git a/examples/form/main.c b/examples/form/main.c new file mode 100644 index 0000000..afc1954 --- /dev/null +++ b/examples/form/main.c @@ -0,0 +1,48 @@ +#include +#include +#include + +static int print(const char *const key, const char *const value, + void *const user) +{ + unsigned *const cnt = user; + + printf("key=%s, value=%s, cnt=%u\n", key, value, ++(*cnt)); + return 0; +} + +int main(int argc, char *argv[]) +{ + int ret = EXIT_FAILURE; + const char *payload; + struct form *f = NULL; + unsigned cnt = 0; + int n; + + if (argc != 2) + { + fprintf(stderr, "Usage: %s \n", *argv); + goto end; + } + else if ((n = form_alloc(payload = argv[1], &f)) < 0) + { + fprintf(stderr, "%s: form_alloc failed\n", __func__); + goto end; + } + else if (n) + { + fprintf(stderr, "%s: invalid user input: %s\n", __func__, payload); + goto end; + } + else if (form_foreach(f, print, &cnt)) + { + fprintf(stderr, "%s: form_foreach failed\n", __func__); + goto end; + } + + ret = EXIT_SUCCESS; + +end: + form_free(f); + return ret; +} -- cgit v1.2.3