From 0119e8dfb0fb39d93b526c23c6fc2a4363ce37f8 Mon Sep 17 00:00:00 2001 From: "Marcel M. Otte" Date: Fri, 27 Dec 2013 18:59:33 +0000 Subject: [PATCH] working on parser and added example --- c/Makefile | 8 +++-- c/cmdlineoptions.c | 86 +++++++++++++++++++++++++++++++++++++++++----- c/cmdlineoptions.h | 9 +++-- c/example.c | 17 +++++++++ 4 files changed, 107 insertions(+), 13 deletions(-) create mode 100644 c/example.c diff --git a/c/Makefile b/c/Makefile index b0e5ae5..94d7ad6 100644 --- a/c/Makefile +++ b/c/Makefile @@ -1,6 +1,6 @@ -all: build static-lib dynamic-lib +all: build static-lib dynamic-lib example build: gcc -c cmdlineoptions.c @@ -15,4 +15,8 @@ dynamic-lib: clean: - rm -f *.o *.a *.so \ No newline at end of file + rm -f *.o *.a *.so example + + +example: + gcc -ggdb example.c -o example libcmdlineoptions.a \ No newline at end of file diff --git a/c/cmdlineoptions.c b/c/cmdlineoptions.c index 5afe87b..c6f6d67 100644 --- a/c/cmdlineoptions.c +++ b/c/cmdlineoptions.c @@ -1,18 +1,29 @@ #include "cmdlineoptions.h" #include #include +#include CmdOptions cmdoptions; -void CmdOptions_Init() { +void CmdOptions_Init(char addhelp){ + CmdOptions_InitCmd(addhelp,0); +} + +void CmdOptions_InitCmd(char addhelp, char cmdchar) { if (cmdoptions.init == 0) { cmdoptions.init = 1; + if (cmdchar != 0) + cmdoptions.cmdchar = cmdchar; + else + cmdoptions.cmdchar = '-'; // do we have to init something? // add the help option - CmdOptions_Add("help", "--help"); - CmdOptions_Add("help", "-h"); - CmdOptions_Add("help", "-?"); - CmdOptions_AddDescription("help", "Display the help text."); + if (addhelp != 0) { + CmdOptions_Add("help", "--help"); + CmdOptions_Add("help", "-h"); + CmdOptions_Add("help", "-?"); + CmdOptions_AddDescription("help", "Display the help text."); + } } } @@ -59,8 +70,67 @@ CONode* CmdOptions_NodeGet(char* name) { return 0; } -int CmdOptions_Parse(int argc, char** argv) { +CONode* CmdOptions_SearchNode(char* cmdlineargument) { + if (cmdoptions.options != 0) { + CONode* node = cmdoptions.options; + while (node != 0) { + int i = 0; + for (; i < node->option->optionscount; ++i) { + if (strcmp(node->option->options[i], cmdlineargument) == 0) { + return node; + } + } + node = node->next; + } + } + return 0; +} +int CmdOptions_Parse(int argc, char** argv) { + if (argc > 1) { + int i = 1; + CONode* cnode = 0; + char* carg = 0; + ; + for (; i < argc; ++i) { + if (argv[i][0] == cmdoptions.cmdchar) { + if ((cnode = CmdOptions_SearchNode(argv[i])) != 0) { + cnode->option->set = 1; + } else { + carg = argv[i]; + fprintf(stderr, "CmdLineOptions: Unrecognized option '%s'.", + carg); + } + } + if (cnode != 0) { + if (cnode->option->possibleparametercount == 0) { + CmdOptions_AddElement(cnode->option->values, + &(cnode->option->valuecount), argv[i]); + } else { + int j = 0; + for (; j < cnode->option->possibleparametercount; ++j) { + if (strcmp(argv[i], + cnode->option->possibleparameters[j]) == 0) { + CmdOptions_AddElement(cnode->option->values, + &(cnode->option->valuecount), argv[i]); + break; + } + } + if (cnode->option->possibleparametercount == j) { + fprintf(stderr, + "CmdLineOptions: Parameter '%s' is not allowed, parameter not added.", + argv[i]); + } + } + } else { + fprintf(stderr, + "CmdLineOptions: Unrecognized option parameter '%s' for unrecognized option '%s'.", + argv[i], carg); + } + } + } + // no arguments! + return 1; } void CmdOptions_Add(char* name, char* option) { @@ -107,12 +177,12 @@ long CmdOptions_GetLong(char* name) { } double CmdOptions_GetDouble(char* name) { - return atod(CmdOptions_Get(name)); + return atof(CmdOptions_Get(name)); } int CmdOptions_GetAll(char* name, char** values, unsigned int* count) { CONode* node = CmdOptions_Create(name); - if(node == 0) + if (node == 0) return 1; values = node->option->values; *count = node->option->valuecount; diff --git a/c/cmdlineoptions.h b/c/cmdlineoptions.h index 876ed72..5e8fc6a 100644 --- a/c/cmdlineoptions.h +++ b/c/cmdlineoptions.h @@ -26,21 +26,24 @@ typedef struct _CONode { typedef struct _CmdOptions { char init; CONode* options; + char cmdchar; } CmdOptions; -/* +/** * Main structure */ extern CmdOptions cmdoptions; -/** Init function dummy. +/** Init function. */ -void CmdOptions_Init(); +void CmdOptions_InitCmd(char addhelp, char cmdchar); +void CmdOptions_Init(char addhelp); /** * The heart of this whole thing, the parsing of all options... */ int CmdOptions_Parse(int argc, char** argv); +CONode* CmdOptions_SearchNode(char* cmdlineargument); /** * Add a new option with a new name, or a new option alternative to an existing one. diff --git a/c/example.c b/c/example.c new file mode 100644 index 0000000..c6f2760 --- /dev/null +++ b/c/example.c @@ -0,0 +1,17 @@ +#include "cmdlineoptions.h" + +void configureOptions() { + CmdOptions_Init(1); + CmdOptions_Add("test","--test"); + CmdOptions_Add("test","-t"); + CmdOptions_AddDescription("test","This is a test Option."); + +} + +int main(int argc, char** argv) { + configureOptions(); + CmdOptions_Parse(argc,argv); + +} + +