working on cmdlineoptions in C

This commit is contained in:
Marcel M. Otte 2014-02-05 16:53:37 +01:00
parent c42ce26c88
commit 522d396dba
2 changed files with 23 additions and 16 deletions

View File

@ -5,8 +5,8 @@
CmdOptions cmdoptions; CmdOptions cmdoptions;
void CmdOptions_Init(char addhelp){ void CmdOptions_Init(char addhelp) {
CmdOptions_InitCmd(addhelp,0); CmdOptions_InitCmd(addhelp, 0);
} }
void CmdOptions_InitCmd(char addhelp, char cmdchar) { void CmdOptions_InitCmd(char addhelp, char cmdchar) {
@ -71,13 +71,20 @@ CONode* CmdOptions_NodeGet(char* name) {
} }
CONode* CmdOptions_SearchNode(char* cmdlineargument) { CONode* CmdOptions_SearchNode(char* cmdlineargument) {
printf("searching\n");
if (cmdoptions.options != 0) { if (cmdoptions.options != 0) {
printf("searching further\n");
CONode* node = cmdoptions.options; CONode* node = cmdoptions.options;
while (node != 0) { while (node != 0) {
int i = 0; if (node->option->optionscount > 0) {
for (; i < node->option->optionscount; ++i) { printf("searching more! %d \n",node->option->optionscount);
if (strcmp(node->option->options[i], cmdlineargument) == 0) { int i = 0;
return node; 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; node = node->next;
@ -104,14 +111,14 @@ int CmdOptions_Parse(int argc, char** argv) {
} }
if (cnode != 0) { if (cnode != 0) {
if (cnode->option->possibleparametercount == 0) { if (cnode->option->possibleparametercount == 0) {
CmdOptions_AddElement(cnode->option->values, CmdOptions_AddElement(&cnode->option->values,
&(cnode->option->valuecount), argv[i]); &(cnode->option->valuecount), argv[i]);
} else { } else {
int j = 0; int j = 0;
for (; j < cnode->option->possibleparametercount; ++j) { for (; j < cnode->option->possibleparametercount; ++j) {
if (strcmp(argv[i], if (strcmp(argv[i],
cnode->option->possibleparameters[j]) == 0) { cnode->option->possibleparameters[j]) == 0) {
CmdOptions_AddElement(cnode->option->values, CmdOptions_AddElement(&cnode->option->values,
&(cnode->option->valuecount), argv[i]); &(cnode->option->valuecount), argv[i]);
break; break;
} }
@ -135,19 +142,19 @@ int CmdOptions_Parse(int argc, char** argv) {
void CmdOptions_Add(char* name, char* option) { void CmdOptions_Add(char* name, char* option) {
CONode* node = CmdOptions_Create(name); CONode* node = CmdOptions_Create(name);
CmdOptions_AddElement(node->option->options, &(node->option->optionscount), CmdOptions_AddElement(&node->option->options, &(node->option->optionscount),
option); option);
} }
void CmdOptions_AddDefaultParameter(char* name, char* defaultparameter) { void CmdOptions_AddDefaultParameter(char* name, char* defaultparameter) {
CONode* node = CmdOptions_Create(name); CONode* node = CmdOptions_Create(name);
CmdOptions_AddElement(node->option->defaultparameters, CmdOptions_AddElement(&node->option->defaultparameters,
&(node->option->defaultparametercount), defaultparameter); &(node->option->defaultparametercount), defaultparameter);
} }
void CmdOptions_AddPossibleParameter(char* name, char* possibleParameter) { void CmdOptions_AddPossibleParameter(char* name, char* possibleParameter) {
CONode* node = CmdOptions_Create(name); CONode* node = CmdOptions_Create(name);
CmdOptions_AddElement(node->option->possibleparameters, CmdOptions_AddElement(&node->option->possibleparameters,
&(node->option->possibleparametercount), possibleParameter); &(node->option->possibleparametercount), possibleParameter);
} }
@ -189,15 +196,15 @@ int CmdOptions_GetAll(char* name, char** values, unsigned int* count) {
return 0; return 0;
} }
void CmdOptions_AddElement(char** target, int* counter, char* element) { void CmdOptions_AddElement(char*** target, int* counter, char* element) {
int cnt = *counter; int cnt = *counter;
char** old = 0; char** old = 0;
if (target != 0) if (target != 0)
old = target; old = *target;
target = malloc((cnt + 1) * sizeof(char*)); *target = malloc((cnt + 1) * sizeof(char*));
if (old != 0) if (old != 0)
memcpy(target, old, (cnt) * sizeof(char*)); memcpy(target, old, (cnt) * sizeof(char*));
target[cnt] = element; (*target)[cnt] = element;
if (old != 0) if (old != 0)
free(old); free(old);
*counter = cnt + 1; *counter = cnt + 1;

View File

@ -91,6 +91,6 @@ int CmdOptions_GetAll(char* name, char** values, unsigned int* count);
CONode* CmdOptions_Create(char* name); CONode* CmdOptions_Create(char* name);
CONode* CmdOptions_NodeGet(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 #endif