Simple example showing how to access the path variables of the router feature.
#include <stdlib.h>
#include <stdio.h>
static int vars_iter_cb(__SG_UNUSED void *cls, const char *name,
const char *val) {
fprintf(stdout, " %s: %s\n", name, val);
return 0;
}
static void route_cb(
void *cls,
struct sg_route *route) {
fprintf(stdout,
"%s: %s\n",
sg_route_path(route), (
const char *) cls);
}
int main(void) {
sg_routes_add(&routes,
"/customer/(?P<name>[a-zA-Z]+)", route_cb,
"customer-data");
sg_routes_add(&routes,
"/product/(?P<id>[0-9]+)", route_cb,
"product-data");
sg_routes_add(&routes,
"/employee/(?P<id>[0-9]+)/[a|i]", route_cb,
"employee-data");
fprintf(stdout, "---\n");
fprintf(stdout, "---\n");
fprintf(stdout, "---\n");
fflush(stdout);
return EXIT_SUCCESS;
}
int sg_router_dispatch(struct sg_router *router, const char *path, void *user_data)
int sg_routes_cleanup(struct sg_route **routes)
const char * sg_route_path(struct sg_route *route)
struct sg_router * sg_router_new(struct sg_route *routes) __attribute__((malloc))
bool sg_routes_add(struct sg_route **routes, const char *pattern, sg_route_cb cb, void *cls)
void sg_router_free(struct sg_router *router)
int sg_route_vars_iter(struct sg_route *route, sg_vars_iter_cb cb, void *cls)