2013-12-25 13:29:54 +01:00
|
|
|
#include "cmdlineoptions.h"
|
2013-12-25 17:27:41 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2013-12-27 19:59:33 +01:00
|
|
|
#include <stdio.h>
|
2013-12-25 13:29:54 +01:00
|
|
|
|
|
|
|
CmdOptions cmdoptions;
|
|
|
|
|
2014-02-05 16:53:37 +01:00
|
|
|
void CmdOptions_Init(char addhelp) {
|
|
|
|
CmdOptions_InitCmd(addhelp, 0);
|
2013-12-27 19:59:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CmdOptions_InitCmd(char addhelp, char cmdchar) {
|
2013-12-25 13:29:54 +01:00
|
|
|
if (cmdoptions.init == 0) {
|
|
|
|
cmdoptions.init = 1;
|
2013-12-27 19:59:33 +01:00
|
|
|
if (cmdchar != 0)
|
|
|
|
cmdoptions.cmdchar = cmdchar;
|
|
|
|
else
|
|
|
|
cmdoptions.cmdchar = '-';
|
2013-12-25 13:29:54 +01:00
|
|
|
// do we have to init something?
|
|
|
|
// add the help option
|
2013-12-27 19:59:33 +01:00
|
|
|
if (addhelp != 0) {
|
|
|
|
CmdOptions_Add("help", "--help");
|
|
|
|
CmdOptions_Add("help", "-h");
|
|
|
|
CmdOptions_Add("help", "-?");
|
|
|
|
CmdOptions_AddDescription("help", "Display the help text.");
|
|
|
|
}
|
2013-12-25 17:27:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-26 16:00:58 +01:00
|
|
|
CONode* CmdOptions_Create(char* name) {
|
|
|
|
CONode* node = CmdOptions_NodeGet(name);
|
|
|
|
if (node == 0) {
|
|
|
|
node = malloc(sizeof(CONode));
|
2013-12-25 17:27:41 +01:00
|
|
|
if (cmdoptions.options == 0) {
|
2013-12-26 16:00:58 +01:00
|
|
|
cmdoptions.options = node;
|
2013-12-25 17:27:41 +01:00
|
|
|
} else {
|
|
|
|
CONode* last = cmdoptions.options;
|
|
|
|
while (last->next != 0)
|
|
|
|
last = last->next;
|
2013-12-26 16:00:58 +01:00
|
|
|
last->next = node;
|
2013-12-25 17:27:41 +01:00
|
|
|
}
|
2013-12-26 16:00:58 +01:00
|
|
|
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;
|
2013-12-25 17:27:41 +01:00
|
|
|
}
|
2013-12-26 16:00:58 +01:00
|
|
|
return node;
|
2013-12-25 17:27:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CONode* CmdOptions_NodeGet(char* name) {
|
|
|
|
if (cmdoptions.options != 0) {
|
|
|
|
CONode* node = cmdoptions.options;
|
|
|
|
while (node != 0) {
|
|
|
|
if (strcmp(name, node->option->name) == 0) {
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
node = node->next;
|
|
|
|
}
|
2013-12-25 13:29:54 +01:00
|
|
|
}
|
2013-12-25 17:27:41 +01:00
|
|
|
return 0;
|
2013-12-25 13:29:54 +01:00
|
|
|
}
|
|
|
|
|
2013-12-27 19:59:33 +01:00
|
|
|
CONode* CmdOptions_SearchNode(char* cmdlineargument) {
|
2014-02-05 16:53:37 +01:00
|
|
|
printf("searching\n");
|
2013-12-27 19:59:33 +01:00
|
|
|
if (cmdoptions.options != 0) {
|
2014-02-05 16:53:37 +01:00
|
|
|
printf("searching further\n");
|
2013-12-27 19:59:33 +01:00
|
|
|
CONode* node = cmdoptions.options;
|
|
|
|
while (node != 0) {
|
2014-02-05 16:53:37 +01:00
|
|
|
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;
|
|
|
|
}
|
2013-12-27 19:59:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
node = node->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2013-12-25 13:29:54 +01:00
|
|
|
|
2013-12-27 19:59:33 +01:00
|
|
|
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) {
|
2014-02-05 16:53:37 +01:00
|
|
|
CmdOptions_AddElement(&cnode->option->values,
|
2013-12-27 19:59:33 +01:00
|
|
|
&(cnode->option->valuecount), argv[i]);
|
|
|
|
} else {
|
|
|
|
int j = 0;
|
|
|
|
for (; j < cnode->option->possibleparametercount; ++j) {
|
|
|
|
if (strcmp(argv[i],
|
|
|
|
cnode->option->possibleparameters[j]) == 0) {
|
2014-02-05 16:53:37 +01:00
|
|
|
CmdOptions_AddElement(&cnode->option->values,
|
2013-12-27 19:59:33 +01:00
|
|
|
&(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;
|
2013-12-25 13:29:54 +01:00
|
|
|
}
|
|
|
|
|
2013-12-26 16:00:58 +01:00
|
|
|
void CmdOptions_Add(char* name, char* option) {
|
|
|
|
CONode* node = CmdOptions_Create(name);
|
2014-02-05 16:53:37 +01:00
|
|
|
CmdOptions_AddElement(&node->option->options, &(node->option->optionscount),
|
2013-12-26 16:00:58 +01:00
|
|
|
option);
|
2013-12-25 13:29:54 +01:00
|
|
|
}
|
|
|
|
|
2013-12-26 16:00:58 +01:00
|
|
|
void CmdOptions_AddDefaultParameter(char* name, char* defaultparameter) {
|
|
|
|
CONode* node = CmdOptions_Create(name);
|
2014-02-05 16:53:37 +01:00
|
|
|
CmdOptions_AddElement(&node->option->defaultparameters,
|
2013-12-26 16:00:58 +01:00
|
|
|
&(node->option->defaultparametercount), defaultparameter);
|
2013-12-25 13:29:54 +01:00
|
|
|
}
|
|
|
|
|
2013-12-26 16:00:58 +01:00
|
|
|
void CmdOptions_AddPossibleParameter(char* name, char* possibleParameter) {
|
|
|
|
CONode* node = CmdOptions_Create(name);
|
2014-02-05 16:53:37 +01:00
|
|
|
CmdOptions_AddElement(&node->option->possibleparameters,
|
2013-12-26 16:00:58 +01:00
|
|
|
&(node->option->possibleparametercount), possibleParameter);
|
2013-12-25 13:29:54 +01:00
|
|
|
}
|
|
|
|
|
2013-12-26 16:00:58 +01:00
|
|
|
void CmdOptions_AddDescription(char* name, char* description) {
|
|
|
|
CONode* node = CmdOptions_Create(name);
|
|
|
|
node->option->description = description;
|
2013-12-25 13:29:54 +01:00
|
|
|
}
|
|
|
|
|
2013-12-25 17:27:41 +01:00
|
|
|
int CmdOptions_IsSet(char* name) {
|
2013-12-26 16:00:58 +01:00
|
|
|
CONode* node = CmdOptions_Create(name);
|
|
|
|
return node->option->set;
|
2013-12-25 13:29:54 +01:00
|
|
|
}
|
|
|
|
|
2013-12-25 17:27:41 +01:00
|
|
|
char* CmdOptions_Get(char* name) {
|
2013-12-26 16:00:58 +01:00
|
|
|
CONode* node = CmdOptions_Create(name);
|
|
|
|
if (CmdOptions_IsSet(name) && node->option->valuecount > 0)
|
|
|
|
return node->option->values[0];
|
|
|
|
return 0;
|
2013-12-25 13:29:54 +01:00
|
|
|
}
|
|
|
|
|
2013-12-25 17:27:41 +01:00
|
|
|
int CmdOptions_GetInt(char* name) {
|
2013-12-26 16:00:58 +01:00
|
|
|
return atoi(CmdOptions_Get(name));
|
2013-12-25 13:29:54 +01:00
|
|
|
}
|
|
|
|
|
2013-12-25 17:27:41 +01:00
|
|
|
long CmdOptions_GetLong(char* name) {
|
2013-12-26 16:00:58 +01:00
|
|
|
return atol(CmdOptions_Get(name));
|
2013-12-25 13:29:54 +01:00
|
|
|
}
|
|
|
|
|
2013-12-25 17:27:41 +01:00
|
|
|
double CmdOptions_GetDouble(char* name) {
|
2013-12-27 19:59:33 +01:00
|
|
|
return atof(CmdOptions_Get(name));
|
2013-12-25 13:29:54 +01:00
|
|
|
}
|
|
|
|
|
2013-12-25 17:27:41 +01:00
|
|
|
int CmdOptions_GetAll(char* name, char** values, unsigned int* count) {
|
2013-12-26 16:00:58 +01:00
|
|
|
CONode* node = CmdOptions_Create(name);
|
2013-12-27 19:59:33 +01:00
|
|
|
if (node == 0)
|
2013-12-26 16:00:58 +01:00
|
|
|
return 1;
|
|
|
|
values = node->option->values;
|
|
|
|
*count = node->option->valuecount;
|
|
|
|
return 0;
|
|
|
|
}
|
2013-12-25 13:29:54 +01:00
|
|
|
|
2014-02-05 16:53:37 +01:00
|
|
|
void CmdOptions_AddElement(char*** target, int* counter, char* element) {
|
2013-12-26 16:00:58 +01:00
|
|
|
int cnt = *counter;
|
|
|
|
char** old = 0;
|
|
|
|
if (target != 0)
|
2014-02-05 16:53:37 +01:00
|
|
|
old = *target;
|
|
|
|
*target = malloc((cnt + 1) * sizeof(char*));
|
2013-12-26 16:00:58 +01:00
|
|
|
if (old != 0)
|
|
|
|
memcpy(target, old, (cnt) * sizeof(char*));
|
2014-02-05 16:53:37 +01:00
|
|
|
(*target)[cnt] = element;
|
2013-12-26 16:00:58 +01:00
|
|
|
if (old != 0)
|
|
|
|
free(old);
|
|
|
|
*counter = cnt + 1;
|
2013-12-25 13:29:54 +01:00
|
|
|
}
|