diff options
author | FreeArtMan <dos21h@gmail.com> | 2018-06-03 12:33:36 +0100 |
---|---|---|
committer | FreeArtMan <dos21h@gmail.com> | 2018-06-03 12:33:36 +0100 |
commit | bdf536e69839edd86fbd3a4be31a5f1c0debe82e (patch) | |
tree | 61dc81bad31792e464ea0f880b115dfc8035f927 | |
parent | 587dd24b5970aa3605cc2bc2826f7e35344b4317 (diff) | |
download | libarg++-bdf536e69839edd86fbd3a4be31a5f1c0debe82e.tar.gz libarg++-bdf536e69839edd86fbd3a4be31a5f1c0debe82e.zip |
Flag type works
-rw-r--r-- | arg.c | 129 | ||||
-rw-r--r-- | arg.h | 16 | ||||
-rw-r--r-- | test/test1.c | 20 |
3 files changed, 157 insertions, 8 deletions
@@ -827,6 +827,18 @@ int CmdArgFlag::get() return this->flag; } +void CmdArgFlag::setUsed() { + this->used = 1; +} + +int CmdArgFlag::getValue() { + return this->flag; +} + +void CmdArgFlag::setFlag() { + this->flag = 1; +} + CmdArgFlagConf::CmdArgFlagConf() { this->used = 0; @@ -846,6 +858,10 @@ int CmdArgFlagConf::isUsed() return this->used; } +void CmdArgFlagConf::setUsed() { + this->used=1; +} + void CmdArgFlagConf::link(CmdArgFlag *flagArg) { this->result = flagArg; @@ -875,7 +891,7 @@ CmdArgDefault::CmdArgDefault(char *param, uint32_t type, void *def, char *help, CmdArgDefault::CmdArgDefault(char *param, CmdArgFlagConf *def, char *help, CmdArgFlag *result) { - printf("Construct CmdArgDefault::CmdArgDefault(char *param, CmdArgFlagConf *def, char *help, CmdArgFlag *result)\n"); + //printf("Construct CmdArgDefault::CmdArgDefault(char *param, CmdArgFlagConf *def, char *help, CmdArgFlag *result)\n"); this->param = param; this->type = ARGT_FLAG; this->def = (void *)def; @@ -890,11 +906,20 @@ void CmdArgDefault::setParam(char *param) this->param = param; } +char* CmdArgDefault::getParam() { + return this->param; +} + void CmdArgDefault::setType(uint32_t type) { this->type = type; } +uint32_t CmdArgDefault::getType() +{ + return this->type; +} + void CmdArgDefault::setDefValue(void *def) { this->def = def; @@ -905,6 +930,10 @@ int CmdArgDefault::isUsed() return this->used; } +void CmdArgDefault::setUsed() { + this->used = 1; +} + void CmdArgDefault::setHelp(char *help) { this->help = help; @@ -916,17 +945,113 @@ void CmdArgDefault::link(void *result) this->result = result; } +void *CmdArgDefault::getResult() { + return this->result; +} + +void *CmdArgDefault::getDefault() { + return this->def; +} + + CmdArg::CmdArg() { - + this->num = 0; + this->defaultArgs = NULL; } void CmdArg::load(int argc, char **argv) { + int i; + int pos; + + i = 1; + while (i < argc) { + + pos = this->searchArg(argv[i]); + + if (pos==-1) { + printf("Couldnt find any arguments\n"); + break; + } + CmdArgDefault *param = (CmdArgDefault *)this->defaultArgs[pos]; + switch (param->getType()) { + case ARGT_IP: { + break; + } + case ARGT_RANGE: { + break; + } + + case ARGT_FLOAT: { + break; + } + + case ARGT_LIST: { + break; + } + + case ARGT_FILE: { + break; + } + + case ARGT_VAL: { + break; + } + + case ARGT_FLAG: { + + CmdArgFlag *flag=(CmdArgFlag *)param->getResult(); + CmdArgFlagConf *conf=(CmdArgFlagConf *)param->getDefault(); + + param->setUsed(); + flag->setUsed(); + flag->setFlag(); + conf->setUsed(); + + break; + } + default: { + printf("Unknown type\n"); + } + } + + i++; + } } void CmdArg::add(CmdArgDefault *param) { + if (param == NULL) { + return; + } + + this->num += 1; + void *new_ptr=NULL; + new_ptr = realloc(this->defaultArgs, sizeof(void *)*this->num); + if (new_ptr != NULL) + { + this->defaultArgs = (void **)new_ptr; + new_ptr = NULL; + } else { + this->num-=1; + printf("Couldnt allocate memory\n"); + return; + } + CmdArgDefault *p = (CmdArgDefault *)param; + printf("Added type %d\n", p->getType()); + + this->defaultArgs[this->num-1] = param; +} +int CmdArg::searchArg(char *in) { + int i; + for (i=0;i<this->num;i++) { + CmdArgDefault *param = (CmdArgDefault *)this->defaultArgs[i]; + if (strncmp(param->getParam(), in, strlen(in)) == 0) { + return i; + } + } + return -1; }
\ No newline at end of file @@ -346,6 +346,9 @@ public: CmdArgFlag(); int isUsed(); int get(); + void setUsed(); + void setFlag(); + int getValue(); }; class CmdArgFlagConf { @@ -356,6 +359,7 @@ public: CmdArgFlagConf(); CmdArgFlagConf(int flag); int isUsed(); + void setUsed(); void link(CmdArgFlag *flagArg); }; @@ -381,24 +385,34 @@ class CmdArgDefault void *def; //define default values, NULL if no default values int32_t used; char *help; //command description - void *result; //result + void *result; //result where to save resulting value public: CmdArgDefault(); //CmdArgDefault(char *param, uint32_t type, void *def, char *help, void *result); CmdArgDefault(char *param, CmdArgFlagConf *def, char *help, CmdArgFlag *result); + //CmdArgDefault(char *param, CmdArgFlagConf *def, char *help, CmdArgFlag *result); void setParam(char *param); + char *getParam(); void setType(uint32_t type); + uint32_t getType(); void setDefValue(void *def); int isUsed(); + void setUsed(); void setHelp(char *help); void link(void *result); + void *getResult(); + void *getDefault(); }; class CmdArg { + int num; + void **defaultArgs; + int searchArg(char *in); public: CmdArg(); void load(int argc, char **argv); void add(CmdArgDefault *param); + }; /* diff --git a/test/test1.c b/test/test1.c index d87443f..b340db1 100644 --- a/test/test1.c +++ b/test/test1.c @@ -102,7 +102,7 @@ void* operator new[](long unsigned int len) __attribute__((alias("emulate_cc_new void operator delete(void* p, unsigned long len) __attribute__((alias("emulate_cc_delete"))); void operator delete[](void* p, unsigned long len) __attribute__((alias("emulate_cc_delete"))); void* __cxa_pure_virtual = 0; -__gxx_personality_v0 +void *__gxx_personality_v0=0; #include "../arg.h" @@ -157,15 +157,25 @@ exit_error: CmdArgFlag *enable = new CmdArgFlag(); CmdArgFlagConf enableConf = CmdArgFlagConf(); - //CmdArgDefault enableDefault = CmdArgDefault((char *)"-a", &enableConf, (char *)"enable A", &enable); + CmdArgDefault enableDefault = CmdArgDefault((char *)"-a", &enableConf, (char *)"enable A", enable); - CmdArgFlag enableText = CmdArgFlag(); + CmdArgFlag *enableText = new CmdArgFlag(); CmdArgFlagConf enableTextConf = CmdArgFlagConf(); - CmdArgDefault enableTextDefault = CmdArgDefault((char *)"-t", &enableTextConf, (char *)"enable text", &enableText); + CmdArgDefault enableTextDefault = CmdArgDefault((char *)"-t", &enableTextConf, (char *)"enable text", enableText); CmdArg arguments = CmdArg(); + arguments.add(&enableTextDefault); + arguments.add(&enableDefault); - //enableConf(); + arguments.load(argc, argv); + + if (enable->getValue()) { + printf("enable is set\n"); + } + + if (enableText->getValue()) { + printf("enable text is set\n"); + } } |