From 360646e68070ae6b976e2943f13faab00abe546d Mon Sep 17 00:00:00 2001 From: whitglint Date: Thu, 12 Jun 2014 16:55:44 +0800 Subject: [PATCH 1/3] Add C++ example. --- configure.ac | 1 + examples/Makefile.am | 4 +- examples/simple_cpp.cpp | 97 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 examples/simple_cpp.cpp diff --git a/configure.ac b/configure.ac index 2464b96..03a2bff 100644 --- a/configure.ac +++ b/configure.ac @@ -7,6 +7,7 @@ AM_INIT_AUTOMAKE([foreign subdir-objects]) LT_INIT AC_PROG_CC AC_PROG_CC_STDC +AC_PROG_CXX AC_PROG_INSTALL # older debian diff --git a/examples/Makefile.am b/examples/Makefile.am index 4343019..906d99f 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -1,5 +1,7 @@ AM_CFLAGS=$(DEPS_CFLAGS) $(GVC_DEPS_CFLAGS) -I$(top_builddir)/include -Wall -std=c99 +AM_CXXFLAGS=$(DEPS_CXXFLAGS) $(GVC_DEPS_CXXFLAGS) -I$(top_builddir)/include -Wall AM_LDFLAGS=$(DEPS_LIBS) $(GVC_DEPS_LIBS) $(top_builddir)/libr3.la -noinst_PROGRAMS = simple +noinst_PROGRAMS = simple simple_cpp simple_SOURCES = simple.c +simple_cpp_SOURCES = simple_cpp.cpp diff --git a/examples/simple_cpp.cpp b/examples/simple_cpp.cpp new file mode 100644 index 0000000..ddade90 --- /dev/null +++ b/examples/simple_cpp.cpp @@ -0,0 +1,97 @@ +#include +#include +#include +#include + +using namespace std; + +void example_1() { + // create a router tree with 10 children capacity (this capacity can grow dynamically) + r3::Tree tree(10); + + // insert the route path into the router tree + int route_data_1 = 1; + tree.insert_path("/bar", &route_data_1); // ignore the length of path + + int route_data_2 = 2; + tree.insert_pathl("/zoo", strlen("/zoo"), &route_data_2); + int route_data_3 = 3; + tree.insert_pathl("/foo/bar", strlen("/foo/bar"), &route_data_3); + + int route_data_4 = 4; + tree.insert_pathl("/post/{id}", strlen("/post/{id}") , &route_data_4); + + int route_data_5 = 5; + tree.insert_pathl("/user/{id:\\d+}", strlen("/user/{id:\\d+}"), + &route_data_5); + + // if you want to catch error, you may call the extended path function for insertion + int data = 10; + char* errstr; + r3::Node ret = tree.insert_pathl("/foo/{name:\\d{5}", + strlen("/foo/{name:\\d{5}"), &data, &errstr); + if (ret == NULL) { + // failed insertion + cout << "error: " << errstr << endl; + free(errstr); // errstr is created from `asprintf`, so you have to free it manually. + } + + // let's compile the tree! + int err = tree.compile(&errstr); + if (err != 0) { + cout << "error: " << errstr << endl; + free(errstr); // errstr is created from `asprintf`, so you have to free it manually. + } + + // dump the compiled tree + tree.dump(0); + + // match a route + r3::Node matched_node = tree.matchl("/foo/bar", strlen("/foo/bar")); + if (matched_node) { + int ret = *static_cast(matched_node.data()); + cout << "match path ret: " << ret << endl; + } + + r3::MatchEntry entry("/foo/bar"); + matched_node = tree.match_entry(entry); + if (matched_node) { + int ret = *static_cast(matched_node.data()); + cout << "match entry ret: " << ret << endl; + } +} + +void example_2() { + // create a router tree with 10 children capacity (this capacity can grow dynamically) + r3::Tree tree(10); + + // insert the route path into the router tree + int route_data = 1; + tree.insert_routel(METHOD_GET | METHOD_POST, "/blog/post", + sizeof("/blog/post") - 1, &route_data); + + char* errstr; + int err = tree.compile(&errstr); + if (err != 0) { + cout << "errstr: " << errstr << endl; + free(errstr); // errstr is created from `asprintf`, so you have to free it manually. + } + + // in your http server handler + + // create the match entry for capturing dynamic variables. + r3::MatchEntry entry("/blog/post"); + entry.set_request_method(METHOD_GET); + + r3::Route matched_route = tree.match_route(entry); + if (matched_route) { + int ret = *static_cast(matched_route.data()); + cout << "match route ret: " << ret << endl; + } +} + +int main() { + example_1(); + example_2(); + return 0; +} From fb65694934a5f7a163ea902a117a13721154b37a Mon Sep 17 00:00:00 2001 From: whitglint Date: Thu, 12 Jun 2014 19:37:08 +0800 Subject: [PATCH 2/3] Fix Travis CI compile error. --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 18a7da2..e936218 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,7 +11,7 @@ matrix: env: CONFIGURE_OPTION='--enable-debug --enable-gcov' COVERALLS=yes VALGRIND=yes DEBUG=yes LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib/ # -D_BSD_SOURCE=1 is for asprintf - compiler: clang - env: ASAN_OPTIONS=symbolize=1 ASAN_SYMBOLIZER_PATH=/usr/local/clang-3.4/bin/llvm-symbolizer CFLAGS='-fsanitize=address -g -O1 -D_BSD_SOURCE=1' + env: ASAN_OPTIONS=symbolize=1 ASAN_SYMBOLIZER_PATH=/usr/local/clang-3.4/bin/llvm-symbolizer CFLAGS='-fsanitize=address -g -O1 -D_BSD_SOURCE=1' CXXFLAGS='-fsanitize=address -g -O1 -D_BSD_SOURCE=1' install: - sudo apt-get update -qq From 09cdbfaa60b9c00ef24718428171ddfbbf2bb01d Mon Sep 17 00:00:00 2001 From: whitglint Date: Thu, 12 Jun 2014 20:05:24 +0800 Subject: [PATCH 3/3] Fix Travis CI compile error. --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index e936218..218b39c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,7 +11,7 @@ matrix: env: CONFIGURE_OPTION='--enable-debug --enable-gcov' COVERALLS=yes VALGRIND=yes DEBUG=yes LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib/ # -D_BSD_SOURCE=1 is for asprintf - compiler: clang - env: ASAN_OPTIONS=symbolize=1 ASAN_SYMBOLIZER_PATH=/usr/local/clang-3.4/bin/llvm-symbolizer CFLAGS='-fsanitize=address -g -O1 -D_BSD_SOURCE=1' CXXFLAGS='-fsanitize=address -g -O1 -D_BSD_SOURCE=1' + env: ASAN_OPTIONS=symbolize=1 ASAN_SYMBOLIZER_PATH=/usr/local/clang-3.4/bin/llvm-symbolizer CFLAGS='-fsanitize=address -g -O1 -D_BSD_SOURCE=1' CXX=clang++ CXXFLAGS='-fsanitize=address -g -O1 -D_BSD_SOURCE=1' install: - sudo apt-get update -qq @@ -29,7 +29,7 @@ script: - make V=1 - sudo make install - if [ "x$VALGRIND" == xyes ]; then make check > /dev/null 2>&1; else make check V=1; fi - # XXX: tracing memory leak, disabled for some mystery reason for automake... + # XXX: tracing memory leak, disabled for some mystery reason for automake... # - if [ "x$VALGRIND" == xyes && "x$DEBUG" == xyes ]; then valgrind ./tests/check_* -v --trace-children=yes --show-leak-kinds=full --leak-check=full; fi after_success: