[go: up one dir, main page]

Menu

[592f53]: / src / clampp.cpp  Maximize  Restore  History

Download this file

146 lines (114 with data), 4.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*******************************************************************************
* clampp - Command Line Argument Manager, Plus Plus
* A Simple, Efficient and Fast CLI Argument Handler Library.
*
* See the GitHub For more information: https://github.com/ADBeta/clampp
* See example.cpp for programatic demonstation of usage/syntax.
*
* ADBeta(c)
*******************************************************************************/
#include "clampp.hpp"
#include <iterator>
#include <cstring>
#include <string>
/*** Configuration ************************************************************/
bool ClamppConfig::allow_undefined_args = false;
/*** Private Functions ********************************************************/
int ClamppClass::FindDefinedByFlag(const char *flag) {
//Go through each Defined Argument looking for a match
for(auto def = this->DefinedArgList.begin(); def != this->DefinedArgList.end(); def++) {
//See if the primary or secondary flag strings match
int pri_cmp = -1, sec_cmp = -1;
pri_cmp = strcmp(def->flag_pri, flag);
if(def->flag_sec != NULL) sec_cmp = strcmp(def->flag_sec, flag);
//If either flag string matched, return the distance from begin() to now
if(pri_cmp == 0 || sec_cmp == 0) {
return (int)std::distance(DefinedArgList.begin(), def);
}
}
//No match
return -1;
}
/*** API Functions ************************************************************/
//Add a Defined Argument with a single, primary flag string
int ClamppClass::AddDefinition(const char *pri, const bool has_substr) {
ArgDef_t arg_temp;
arg_temp.flag_pri = pri;
arg_temp.flag_sec = NULL;
arg_temp.has_substr = has_substr;
try {
DefinedArgList.push_back(arg_temp);
} catch(const std::bad_alloc &) {
return -1;
}
return (int)std::distance(DefinedArgList.begin(), DefinedArgList.end()) - 1;
}
//Add a Defined Argument with two flag strings, one primary + one alias
int ClamppClass::AddDefinition(const char *pri, const char *sec, const bool has_substr) {
ArgDef_t arg_temp;
arg_temp.flag_pri = pri;
arg_temp.flag_sec = sec;
arg_temp.has_substr = has_substr;
try {
DefinedArgList.push_back(arg_temp);
} catch(const std::bad_alloc &) {
return -1;
}
return (int)std::distance(DefinedArgList.begin(), DefinedArgList.end()) - 1;
}
ClamppError ClamppClass::ScanArgs(const int argc, const char *argv[]) {
if(argc <= 0) return CLAMPP_ENOARGS;
//Go through all of the input argument strings
for(int crnt_arg = 0; crnt_arg < argc; crnt_arg++) {
const char *arg_str = argv[crnt_arg];
int index = FindDefinedByFlag(arg_str);
//If the target was found, set its detected flag, and get a substring if
//It was set. Return if no substring was given but was required
if(index >= 0) {
ArgDef_t *found_arg = &this->DefinedArgList[(size_t)index];
found_arg->was_detected = true;
if(found_arg->has_substr == true) {
if(++crnt_arg == argc) return CLAMPP_ENOSUBSTR;
found_arg->substr = argv[crnt_arg];
}
}
else {
//If undefined strings are not allowed, return an error.
if(ClamppConfig::allow_undefined_args == false) return CLAMPP_ENOMATCH;
//If they are allowed, push the string to the end of the list.
this->UndefinedArgList.push_back(std::string(arg_str));
}
}
//If everything went well, exit with no error
return CLAMPP_ENONE;
}
int ClamppClass::GetDetectedStatus(const int index) {
if(index < 0) return -1;
if((size_t)index >= this->DefinedArgList.size()) return -2;
return DefinedArgList[(size_t)index].was_detected;
}
int ClamppClass::GetDetectedStatus(const char* flag) {
int ret;
if((ret = FindDefinedByFlag(flag)) < 0) return -1;
//Get index's detected var
return this->GetDetectedStatus(ret);
}
std::string ClamppClass::GetSubstring(const int index) {
//Return empty string is there's an error
if(index < 0 || (size_t)index >= this->DefinedArgList.size()) return std::string();
const char *substr = this->DefinedArgList[ (size_t)index ].substr;
//Guard against NULL char strings.
if(substr == NULL) return std::string();
return std::string(substr);
}
std::string ClamppClass::GetSubstring(const char *flag) {
int ret;
if((ret = FindDefinedByFlag(flag)) < 0) return std::string();
//Get index's detected var
return this->GetSubstring(ret);
}
std::string ClamppClass::GetUndefinedArg(const int index) {
//Return empty string is there's an error
if(index < 0 || (size_t)index >= this->UndefinedArgList.size()) return std::string();
return UndefinedArgList[ (size_t)index ];
}