From 2e3e00836df7b04af6ead324a95704f654da1076 Mon Sep 17 00:00:00 2001 From: "Marcel M. Otte" Date: Thu, 26 Dec 2013 15:00:58 +0000 Subject: [PATCH] working on the functions... --- c/cmdlineoptions.c | 88 ++++++++++++++++++++++++++++++++++------------ c/cmdlineoptions.h | 1 + 2 files changed, 66 insertions(+), 23 deletions(-) diff --git a/c/cmdlineoptions.c b/c/cmdlineoptions.c index 6ce4a7a..5afe87b 100644 --- a/c/cmdlineoptions.c +++ b/c/cmdlineoptions.c @@ -16,22 +16,34 @@ void CmdOptions_Init() { } } -int CmdOptions_Create(char* name) { - if (CmdOptions_NodeGet(name) == 0) { - CONode* newnode = 0; - newnode = malloc(sizeof(CONode)); +CONode* CmdOptions_Create(char* name) { + CONode* node = CmdOptions_NodeGet(name); + if (node == 0) { + node = malloc(sizeof(CONode)); if (cmdoptions.options == 0) { - cmdoptions.options = newnode; + cmdoptions.options = node; } else { CONode* last = cmdoptions.options; while (last->next != 0) last = last->next; - last->next = newnode; + last->next = node; } - newnode->next = 0; - newnode->option = malloc(sizeof(Option)); - newnode->option->name = name; + node->next = 0; + node->option = malloc(sizeof(Option)); + node->option->name = name; + // init the option parts + node->option->defaultparametercount = 0; + node->option->defaultparameters = 0; + node->option->description = 0; + node->option->options = 0; + node->option->optionscount = 0; + node->option->possibleparametercount = 0; + node->option->possibleparameters = 0; + node->option->set = 0; + node->option->values = 0; + node->option->valuecount = 0; } + return node; } CONode* CmdOptions_NodeGet(char* name) { @@ -51,42 +63,72 @@ int CmdOptions_Parse(int argc, char** argv) { } -int CmdOptions_Add(char* name, char* option) { - +void CmdOptions_Add(char* name, char* option) { + CONode* node = CmdOptions_Create(name); + CmdOptions_AddElement(node->option->options, &(node->option->optionscount), + option); } -int CmdOptions_AddDefaultParameter(char* name, char* defaultparameter) { - +void CmdOptions_AddDefaultParameter(char* name, char* defaultparameter) { + CONode* node = CmdOptions_Create(name); + CmdOptions_AddElement(node->option->defaultparameters, + &(node->option->defaultparametercount), defaultparameter); } -int CmdOptions_AddPossibleParameter(char* name, char* possibleParameter) { - +void CmdOptions_AddPossibleParameter(char* name, char* possibleParameter) { + CONode* node = CmdOptions_Create(name); + CmdOptions_AddElement(node->option->possibleparameters, + &(node->option->possibleparametercount), possibleParameter); } -int CmdOptions_AddDescription(char* name, char* description) { - +void CmdOptions_AddDescription(char* name, char* description) { + CONode* node = CmdOptions_Create(name); + node->option->description = description; } int CmdOptions_IsSet(char* name) { - + CONode* node = CmdOptions_Create(name); + return node->option->set; } char* CmdOptions_Get(char* name) { - + CONode* node = CmdOptions_Create(name); + if (CmdOptions_IsSet(name) && node->option->valuecount > 0) + return node->option->values[0]; + return 0; } int CmdOptions_GetInt(char* name) { - + return atoi(CmdOptions_Get(name)); } long CmdOptions_GetLong(char* name) { - + return atol(CmdOptions_Get(name)); } double CmdOptions_GetDouble(char* name) { - + return atod(CmdOptions_Get(name)); } int CmdOptions_GetAll(char* name, char** values, unsigned int* count) { - + CONode* node = CmdOptions_Create(name); + if(node == 0) + return 1; + values = node->option->values; + *count = node->option->valuecount; + return 0; +} + +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*)); + if (old != 0) + memcpy(target, old, (cnt) * sizeof(char*)); + target[cnt] = element; + if (old != 0) + free(old); + *counter = cnt + 1; } diff --git a/c/cmdlineoptions.h b/c/cmdlineoptions.h index b47f8d7..0295499 100644 --- a/c/cmdlineoptions.h +++ b/c/cmdlineoptions.h @@ -88,5 +88,6 @@ int CmdOptions_GetAll(char* name, char** values, unsigned int* count); int CmdOptions_Create(char* name); CONode* CmdOptions_NodeGet(char* name); +void CmdOptions_AddElement(char** target, char* element); #endif