-
Notifications
You must be signed in to change notification settings - Fork 1
/
InputFlags.cpp
184 lines (156 loc) · 6.08 KB
/
InputFlags.cpp
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
#include <iomanip>
#include <vector>
#include <iostream>
#include "InputFlags.h"
/*
Copyright © 2014 Advanced Micro Devices, Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following are met:
You must reproduce the above copyright notice.
Neither the name of the copyright holder nor the names of its contributors may
be used to endorse or promote products derived from this software without
specific, prior, written permission from at least the copyright holder.
You must include the following terms in your license and/or other materials
provided with the software.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY, NON-INFRINGEMENT, AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Without limiting the foregoing, the software may implement third party
technologies for which you must obtain licenses from parties other than AMD.
You agree that AMD has not obtained or conveyed to you, and that you shall be
responsible for obtaining the rights to use and/or distribute the applicable
underlying intellectual property rights related to the third party
technologies. These third party technologies are not licensed hereunder.
If you use the software (in whole or in part), you shall adhere to all
applicable U.S., European, and other export laws, including but not limited to
the U.S. Export Administration Regulations (EAR) (15 C.F.R Sections
730-774), and E.U. Council Regulation (EC) No 428/2009 of 5 May 2009. Further,
pursuant to Section 740.6 of the EAR, you hereby certify that, except pursuant
to a license granted by the United States Department of Commerce Bureau of
Industry and Security or as otherwise permitted pursuant to a License Exception
under the U.S. Export Administration Regulations ("EAR"), you will not (1)
export, re-export or release to a national of a country in Country Groups D:1,
E:1 or E:2 any restricted technology, software, or source code you receive
hereunder, or (2) export to Country Groups D:1, E:1 or E:2 the direct product
of such technology or software, if such foreign produced direct product is
subject to national security controls as identified on the Commerce Control
List (currently found in Supplement 1 to Part 774 of EAR). For the most
current Country Group listings, or for additional information about the EAR
or your obligations under those regulations, please refer to the U.S. Bureau
of Industry and Securitys website at http://www.bis.doc.gov/.
*/
InputFlags::InputFlags()
{
AddInputFlag("help", 'h', "", "Print Help Message", "string");
}
void InputFlags::AddInputFlag(const std::string &_long_name,
char _short_name,
const std::string &_value,
const std::string &_help_text,
const std::string &_type)
{
Input in;
in.long_name = _long_name;
in.short_name = _short_name;
in.value = _value;
in.help_text = _help_text;
in.type = _type;
if(MapInputs.count(_short_name) > 0)
printf("Input flag: %s (%c) already exists !", _long_name.c_str(), _short_name);
else
MapInputs[_short_name] = in;
}
void InputFlags::Print()
{
printf("Input Flags: \n\n");
// for(auto &content : MapInputs)
//std::cout<<std::setw(8)<<"--"<<content.second.long_name<<std::setw(20 - content.second.long_name.length())<<"-"<<content.first<<std::setw(8)<<" "<<content.second.help_text<<"\n";
for(auto it = MapInputs.begin(); it != MapInputs.end(); it++)
std::cout<<std::setw(8)<<"--"<<it->second.long_name<<std::setw(20 - it->second.long_name.length())<<"-"<<it->first<<std::setw(8)<<" "<<it->second.help_text<<"\n";
exit(0);
}
char InputFlags::FindShortName(const std::string &long_name)
{
char short_name = '\0';
//for(auto &content : MapInputs)
//{
// if(content.second.long_name == long_name)
// short_name = content.first;
//}
for(auto it = MapInputs.begin(); it != MapInputs.end(); it++) {
if(it->second.long_name == long_name)
short_name = it->first;
}
if(short_name == '\0')
{
std::cout<<"Long Name: "<<long_name<<" Not Found !";
exit(0);
}
return short_name;
}
void InputFlags::Parse(int argc, char *argv[])
{
std::vector<std::string> args;
for(int i = 1; i < argc; i++)
args.push_back(argv[i]);
for(int i = 0; i < args.size(); i++)
{
std::string temp = args[i];
if(temp[0] != '-')
{
printf("Illegal input flag\n");
Print();
}
else if(temp[0] == '-' && temp[1] == '-') // Long Name Input
{
std::string long_name = temp.substr(2);
if(long_name == "help")
Print();
char short_name = FindShortName(long_name);
MapInputs[short_name].value = args[i+1];
i++;
}
else if (temp[0] == '-' && temp[1] == '?') // Help Input
Print();
else // Short Name Input
{
char short_name = temp[1];
if(MapInputs.find(short_name) == MapInputs.end())
{
std::cout<<"Input Flag: "<<short_name<<" Not Found !";
exit(0);
}
if(short_name == 'h')
Print();
MapInputs[short_name].value = args[i+1];
i++;
}
}
}
std::string InputFlags::GetValueStr(const std::string &long_name)
{
char short_name = FindShortName(long_name);
std::string value = MapInputs[short_name].value;
return value;
}
int InputFlags::GetValueInt(const std::string &long_name)
{
char short_name = FindShortName(long_name);
int value = atoi(MapInputs[short_name].value.c_str());
return value;
}
uint64_t InputFlags::GetValueUint64(const std::string &long_name)
{
char short_name = FindShortName(long_name);
uint64_t value = strtoull(MapInputs[short_name].value.c_str(), NULL, 10);
return value;
}