-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHandlers.cpp
More file actions
188 lines (146 loc) · 5.36 KB
/
Handlers.cpp
File metadata and controls
188 lines (146 loc) · 5.36 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <cstdlib>
#include <sstream>
#include <functional>
#include <unordered_set>
#include "CLIParser.hpp"
namespace Handlers
{
using namespace CLIParser;
const std::unordered_set<FlagType> numTypes {
FlagType::Int,
FlagType::Float
};
extern const std::unordered_set<FlagType> listType {
FlagType::IntList,
FlagType::FloatList,
FlagType::StringList
};
char** cliEntries;
int entryCount;
std::string_view prefix;
std::string_view boundPrefix;
std::unordered_map<std::string, FlagType>* flagTypes;
void Error(const std::vector<std::string>& messages, int line)
{
std::stringstream ss;
for (const auto& msg : messages)
ss << msg;
std::cerr << "[ERROR][CLIParser::Error](CLIParser/CLIParser.cpp:" << line << ") >>> " << ss.str() << '\n';
exit(1);
}
void Error(const std::string& message, int line)
{
std::cerr << "[ERROR][CLIParser::Error](CLIParser/CLIParser.cpp:" << line << ") >>> " << message << '\n';
exit(1);
}
ReturnPtr HandleIntList(int& index)
{
std::vector<int>* resultVec = new std::vector<int>{};
index++;
for (; index < entryCount; index++)
{
const std::string_view entry { cliEntries[index] };
if (entry.starts_with(prefix) || entry.starts_with(boundPrefix))
break;
try
{
int base { 10 };
if (entry.starts_with("0x"))
base = 16;
resultVec->emplace_back(std::stoi(entry.data(), 0, base));
}
catch (const std::exception& e)
{
Error({"Can't convert ", entry.data(), " to an integer."}, __LINE__);
}
}
return ReturnPtr{ resultVec };
}
ReturnPtr HandleFloatList(int& index)
{
std::vector<float>* resultVec = new std::vector<float>{};
index++;
for (; index < entryCount; index++)
{
const std::string_view entry { cliEntries[index] };
if (entry.starts_with(prefix) || entry.starts_with(boundPrefix))
break;
try { resultVec->emplace_back(std::stof(entry.data())); }
catch (const std::exception& e)
{
Error({"Can't convert ", entry.data(), " to a float"}, __LINE__);
}
}
return ReturnPtr{ resultVec };
}
ReturnPtr HandleStringList(int& index)
{
std::vector<std::string>* resultVec = new std::vector<std::string>{};
index++;
for (; index < entryCount; index++)
{
const std::string_view entry { cliEntries[index] };
if (entry.starts_with(prefix) || entry.starts_with(boundPrefix))
break;
resultVec->emplace_back(entry.data());
}
return ReturnPtr{ resultVec };
}
ReturnPtr HandleCliNumber(int& index)
{
std::string entry = cliEntries[index];
if (!numTypes.contains(flagTypes->at(entry)))
Error({"Type missmatch. Given flag ", cliEntries[index], " is not a number."}, __LINE__);
bool isFloat = false;
index++;
std::string val { cliEntries[index] };
index++; // responsibility of the function.
if (flagTypes->at(entry) == FlagType::Float)
return ReturnPtr { new float{std::stof(val)} };
if (entry.starts_with("0x"))
return ReturnPtr { new int{std::stoi(val, 0, 16)} };
return ReturnPtr { new int{std::stoi(val)} };
}
ReturnPtr HandleCliString(int& index)
{
if (flagTypes->at(cliEntries[index]) != FlagType::String)
Error({"Type missmatch. Given flag ", cliEntries[index], " is not a string."}, __LINE__);
// I'm sorry. Please don't kill m)e
return ReturnPtr { new std::string{cliEntries[(++index)++]} };
}
ReturnPtr HandleCliList(int& index)
{
std::string flagName = cliEntries[index];
//if (std::find(listType.begin(), listType.end(), flagTypes->at(flagName)) == listType.end())
if (!listType.contains(flagTypes->at(flagName)))
Error({"Type missmatch. Given flag ", flagName, " is not a list."}, __LINE__);
ReturnPtr rp;
if (flagTypes->at(flagName) == FlagType::FloatList)
rp = HandleFloatList(index);
if (flagTypes->at(flagName) == FlagType::IntList)
rp = HandleIntList(index);
if (flagTypes->at(flagName) == FlagType::StringList)
rp = HandleStringList(index);
return rp;
}
ReturnPtr HandleCliBool(int& index)
{
index++;
return ReturnPtr { new bool{true} };
}
ReturnPtr CLIParamToObject(int& index)
{
const std::string& flag { cliEntries[index] };
const FlagType type { flagTypes->at(flag) };
if (numTypes.contains(type))
return std::move(HandleCliNumber(index));
if (listType.contains(type))
return std::move(HandleCliList(index));
if (type == FlagType::String)
return HandleCliString(index);
if (type == FlagType::Bool)
return HandleCliBool(index);
Error({"Can't identify parameter type from entry ", cliEntries[index+1], " for ", cliEntries[index]}, __LINE__);
return ReturnPtr{};
}
}