From 522d396dbadc709b54aff80a9fcf0070487a5351 Mon Sep 17 00:00:00 2001 From: "Marcel M. Otte" Date: Wed, 5 Feb 2014 16:53:37 +0100 Subject: [PATCH] working on cmdlineoptions in C --- c/cmdlineoptions.c | 37 ++++++++++++++++++++++--------------- c/cmdlineoptions.h | 2 +- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/c/cmdlineoptions.c b/c/cmdlineoptions.c index c6f6d67..5d7a32c 100644 --- a/c/cmdlineoptions.c +++ b/c/cmdlineoptions.c @@ -5,8 +5,8 @@ CmdOptions cmdoptions; -void CmdOptions_Init(char addhelp){ - CmdOptions_InitCmd(addhelp,0); +void CmdOptions_Init(char addhelp) { + CmdOptions_InitCmd(addhelp, 0); } void CmdOptions_InitCmd(char addhelp, char cmdchar) { @@ -71,13 +71,20 @@ CONode* CmdOptions_NodeGet(char* name) { } CONode* CmdOptions_SearchNode(char* cmdlineargument) { + printf("searching\n"); if (cmdoptions.options != 0) { + printf("searching further\n"); 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; + if (node->option->optionscount > 0) { + printf("searching more! %d \n",node->option->optionscount); + int i = 0; + for (; i < node->option->optionscount; ++i) { + printf("addresses 0x%x 0x%x\n",node->option->options, node->option->options[i]); + if (strcmp(node->option->options[i], cmdlineargument) + == 0) { + return node; + } } } node = node->next; @@ -104,14 +111,14 @@ int CmdOptions_Parse(int argc, char** argv) { } if (cnode != 0) { if (cnode->option->possibleparametercount == 0) { - CmdOptions_AddElement(cnode->option->values, + 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, + CmdOptions_AddElement(&cnode->option->values, &(cnode->option->valuecount), argv[i]); break; } @@ -135,19 +142,19 @@ int CmdOptions_Parse(int argc, char** argv) { void CmdOptions_Add(char* name, char* option) { CONode* node = CmdOptions_Create(name); - CmdOptions_AddElement(node->option->options, &(node->option->optionscount), + CmdOptions_AddElement(&node->option->options, &(node->option->optionscount), option); } void CmdOptions_AddDefaultParameter(char* name, char* defaultparameter) { CONode* node = CmdOptions_Create(name); - CmdOptions_AddElement(node->option->defaultparameters, + CmdOptions_AddElement(&node->option->defaultparameters, &(node->option->defaultparametercount), defaultparameter); } void CmdOptions_AddPossibleParameter(char* name, char* possibleParameter) { CONode* node = CmdOptions_Create(name); - CmdOptions_AddElement(node->option->possibleparameters, + CmdOptions_AddElement(&node->option->possibleparameters, &(node->option->possibleparametercount), possibleParameter); } @@ -189,15 +196,15 @@ int CmdOptions_GetAll(char* name, char** values, unsigned int* count) { return 0; } -void CmdOptions_AddElement(char** target, int* counter, char* element) { +void CmdOptions_AddElement(char*** target, int* counter, char* element) { int cnt = *counter; char** old = 0; if (target != 0) - old = target; - target = malloc((cnt + 1) * sizeof(char*)); + old = *target; + *target = malloc((cnt + 1) * sizeof(char*)); if (old != 0) memcpy(target, old, (cnt) * sizeof(char*)); - target[cnt] = element; + (*target)[cnt] = element; if (old != 0) free(old); *counter = cnt + 1; diff --git a/c/cmdlineoptions.h b/c/cmdlineoptions.h index 5e8fc6a..88d7fcf 100644 --- a/c/cmdlineoptions.h +++ b/c/cmdlineoptions.h @@ -91,6 +91,6 @@ int CmdOptions_GetAll(char* name, char** values, unsigned int* count); CONode* CmdOptions_Create(char* name); CONode* CmdOptions_NodeGet(char* name); -void CmdOptions_AddElement(char** target,int* counter, char* element); +void CmdOptions_AddElement(char*** target,int* counter, char* element); #endif