diff options
author | sanine <sanine.not@pm.me> | 2021-09-04 12:39:29 -0500 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2021-09-04 12:39:29 -0500 |
commit | ddc38c3e6524b27224b97faeb3abfc8663943d88 (patch) | |
tree | d228df0a5192d876703cfa6b6633e007220a7271 | |
parent | 0b55e12f7f9fc98d22b2bead44d064af8c3f92c4 (diff) |
add table processing demo
-rw-r--r-- | CMakeLists.txt | 13 | ||||
-rw-r--r-- | examples/table_processing/table_processing.c | 60 |
2 files changed, 72 insertions, 1 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index d9776cd..85f3024 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -66,8 +66,19 @@ set_target_properties(example_argument_parsing PROPERTIES CMAKE_C_FLAGS "-Wall -Wextra -Werror -Wfatal-errors -Wpedantic") target_link_libraries(example_argument_parsing ${LUA_LIBRARIES} honeysuckle) +add_executable(example_table_processing EXCLUDE_FROM_ALL + ${EX_ROOT}/table_processing/table_processing.c) +set_target_properties(example_table_processing PROPERTIES + C_STANDARD 99 + CMAKE_C_FLAGS "-Wall -Wextra -Werror -Wfatal-errors -Wpedantic") +target_link_libraries(example_table_processing ${LUA_LIBRARIES} honeysuckle) + + add_custom_target(examples) -add_dependencies(examples example_table_creation example_argument_parsing) +add_dependencies(examples + example_table_creation + example_argument_parsing + example_table_processing) include(GNUInstallDirs) diff --git a/examples/table_processing/table_processing.c b/examples/table_processing/table_processing.c new file mode 100644 index 0000000..64c1f01 --- /dev/null +++ b/examples/table_processing/table_processing.c @@ -0,0 +1,60 @@ +#include <string.h> +#include <stdlib.h> +#include <honeysuckle.h> + +struct settings +{ + bool debug; + int debug_level; + char *logfile; +} global_settings; + +void set_logfile(const char* filename, void *s) +{ + struct settings *settings = (struct settings *) s; + settings->logfile = malloc(sizeof(char) * strlen(filename)); + strcpy(settings->logfile, filename); +} + +int process(lua_State *L) +{ + int table_index; + hs_parse_args(L, hs_tbl(table_index)); + hs_process_table(L, table_index, + hs_process_bool("debug", + hs_pt_set_boolean, + &(global_settings.debug)), + hs_process_int("level", + hs_pt_set_integer, + &(global_settings.debug_level)), + hs_process_str("logfile", + set_logfile, + &global_settings)); + return 0; +} + +int main() +{ + lua_State *L = luaL_newstate(); + luaL_openlibs(L); + + lua_pushcfunction(L, process); + lua_setglobal(L, "configure"); + + global_settings.debug = false; + global_settings.debug_level = 0; + global_settings.logfile = "nil"; + + printf("settings: [ %d, %d, '%s' ]\n", + global_settings.debug, + global_settings.debug_level, + global_settings.logfile); + + luaL_dostring(L, "configure{debug=true, level=6, logfile='output.log'}"); + + printf("settings: [ %d, %d, '%s' ]\n", + global_settings.debug, + global_settings.debug_level, + global_settings.logfile); + return 0; +} |