From 670e03997afca107abbfc61a61f89477a9f9ab82 Mon Sep 17 00:00:00 2001 From: FreeArtMan Date: Tue, 17 Feb 2015 20:00:33 +0900 Subject: c_conf_gen lua script that generates getopt code for fast argc,argv parsing. for lazy people --- c_conf_gen/consts.lua | 121 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 c_conf_gen/consts.lua (limited to 'c_conf_gen/consts.lua') diff --git a/c_conf_gen/consts.lua b/c_conf_gen/consts.lua new file mode 100644 index 0000000..87e183a --- /dev/null +++ b/c_conf_gen/consts.lua @@ -0,0 +1,121 @@ +local M = {} + +-- +local function header_def_start( name ) + local s = "" + local name = string.upper( name ) + s = s .. "#ifndef __" .. name .. "_H\n" + s = s .. "#define __" .. name .. "_H\n" + s = s .. "#include \n" + s = s .. "void parse_args( int argc, char **argv, cmd_config *cmd );" + return s +end +M.header_def_start = header_def_start + + +-- +local function header_def_end( ) + local s = "" + s = s .. [[ + +#endif +]] + return s +end +M.header_def_end = header_def_end + +local function implement_start( headername ) + local s = "" + s = s .. [[//auto-generated file no meaning to edit it +#include "]].. headername .. [[" +]] + return s +end +M.implement_start = implement_start + +local function implement_end() + local s = "" + return s +end +M.implement_end = implement_end + +local function config_structure( values ) + local s = "" + return s +end +M.config_structure = config_structure + +--make as separete function +--just to make it easy to generate and then embed in code +-- to not make name coalisions +local function getopt_start( sup_args ) + local s = "" + if sup_args == nil then + sup_args = "\"\"" + end + s = s .. [[ void parse_args( int argc, char **argv, cmd_config *cmd ) +{ + int c; + char *cvalue; + + while ( (c = getopt(argc, argv, "]] .. sup_args .. [[" )) != -1 ) + switch ( c ) + { +]] + return s +end +M.getopt_start = getopt_start + +local function getopt_end() + local s = "" + s = s .. [[ + default: + printf("Unknown option\n"); + } +} +]] + return s +end +M.getopt_end = getopt_end + +-- +local function getopt_int( param_config ) + local s = "" + s = s .. [[ + case ']] .. param_config.shortopt .. [[':{ + ]] .. param_config.name .. [[ = atoi( optarg ); + } +]] + return s +end +M.getopt_int = getopt_int + + +-- +local function getopt_string( param_config ) + local s = "" + s = s .. [[ + case ']] .. param_config.shortopt .. [[':{ + ]] .. param_config.name .. [[ = atoi( optarg ); + } +]] + return s +end +M.getopt_string = getopt_string + + +-- +local function getopt_file( param_config ) + local s = "" + s = s .. [[ + case ']] .. param_config.shortopt .. [[':{ + ]] .. param_config.name .. [[ = atoi( optarg ); + } +]] + return s +end +M.getopt_file = getopt_file + + +print("cconsts module loaded") +return M \ No newline at end of file -- cgit v1.2.3