forked from frkd-dev/yuvit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getopt_pp.cpp
247 lines (210 loc) · 6.57 KB
/
getopt_pp.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
GetOpt_pp: Yet another C++ version of getopt.
Copyright (C) 2007, 2008, 2009, 2010 Daniel Gutson, FuDePAN
This file is part of GetOpt_pp.
GetOpt_pp is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
GetOpt_pp is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//#if __APPLE__
//extern char** environ;
//#elif _WIN32
//#include <Stdio.h>
//#define environ _environ
//#else
//#include <unistd.h>
//#endif
#include "getopt_pp.h"
namespace GetOpt
{
GETOPT_INLINE Token* GetOpt_pp::_add_token(const std::string& value, Token::Type type)
{
Token* const ret = new Token(value, type);
if (_first_token == NULL)
_first_token = ret;
else
_last_token->link_to(ret);
_last_token = ret;
return ret;
}
GETOPT_INLINE void GetOpt_pp::_init_flags()
{
std::stringstream ss;
_flags = ss.flags();
}
GETOPT_INLINE void GetOpt_pp::_parse(int argc, const char* const* const argv)
{
_app_name = argv[0];
bool any_option_processed = false;
// parse arguments by their '-' or '--':
// (this will be a state machine soon)
for (int i = 1; i < argc; i++)
{
const char current = argv[i][0];
const char next = argv[i][1];
if (current == '-' && (isalnum(next) || next == '-'))
{
// see what's next, differentiate whether it's short or long:
if (next == '-' && argv[i][2] != 0)
{
// long option
_longOps[&argv[i][2]].token = _add_token(&argv[i][2], Token::LongOption);
any_option_processed = true;
}
else
{
// short option
// iterate over all of them, keeping the last one in currentData
// (so the intermediates will generate 'existent' arguments, as of '-abc')
size_t j = 1;
do
{
_shortOps[argv[i][j]].token = _add_token(std::string(&argv[i][j], 1), Token::ShortOption);
j++;
}
while (argv[i][j] != 0);
any_option_processed = true;
}
}
else
{
_add_token(argv[i], any_option_processed ? Token::UnknownYet : Token::GlobalArgument);
}
}
_last = _Option::OK; // TODO: IMPROVE!!
}
GETOPT_INLINE void GetOpt_pp::_parse_env()
{
#if 0 // We don't need such functionality
// this will be optimized in version 3
std::string var_name;
std::string var_value;
size_t var = 0;
std::string::size_type pos;
OptionData* data;
while (environ[var] != NULL)
{
var_name = environ[var];
pos = var_name.find('=');
if (pos != std::string::npos)
{
var_value = var_name.substr(pos + 1);
var_name = var_name.substr(0, pos);
if (_longOps.find(var_name) == _longOps.end())
{
data = &_longOps[var_name];
data->token = _add_token(var_name, Token::LongOption);
data->flags = OptionData::Envir;
_add_token(var_value, Token::OptionArgument);
}
}
else
(data = &_longOps[var_name])->flags = OptionData::Envir;
var++;
}
#endif
}
GETOPT_INLINE GetOpt_pp::GetOpt_pp(int argc, const char* const* const argv)
: _exc(std::ios_base::goodbit), _first_token(NULL), _last_token(NULL)
{
_init_flags();
_parse(argc, argv);
}
GETOPT_INLINE GetOpt_pp::GetOpt_pp(int argc, const char* const* const argv, _EnvTag)
: _first_token(NULL), _last_token(NULL)
{
_init_flags();
_parse(argc, argv);
_parse_env();
}
GETOPT_INLINE GetOpt_pp::~GetOpt_pp()
{
Token* next;
Token* current(_first_token);
while (current != NULL)
{
next = current->next;
delete current;
current = next;
}
}
GETOPT_INLINE GetOpt_pp& GetOpt_pp::operator >> (const _Option& opt) throw(GetOptEx)
{
if (_last != _Option::ParsingError)
{
_last = opt(_shortOps, _longOps, _first_token, _flags);
switch (_last)
{
case _Option::OK:
break;
case _Option::OptionNotFound:
if (_exc & std::ios_base::eofbit)
throw OptionNotFoundEx();
break;
case _Option::BadType:
if (_exc & std::ios_base::failbit)
throw InvalidFormatEx();
break;
case _Option::NoArgs:
if (_exc & std::ios_base::eofbit)
throw ArgumentNotFoundEx();
break;
case _Option::TooManyArgs:
if (_exc & std::ios_base::failbit)
throw TooManyArgumentsEx();
break;
case _Option::OptionNotFound_NoEx:
break; // Ok, it will be read by casting to bool
case _Option::ParsingError:
break; // just to disable warning
}
}
else if (_exc & std::ios_base::failbit)
throw ParsingErrorEx();
return *this;
}
GETOPT_INLINE GetOpt_pp& GetOpt_pp::operator >> (std::ios_base & (*iomanip)(std::ios_base&))
{
std::stringstream ss;
ss.flags(_flags);
_flags = (ss << iomanip).flags();
return *this;
}
GETOPT_INLINE bool GetOpt_pp::options_remain() const
{
bool remain = false;
ShortOptions::const_iterator it = _shortOps.begin();
while (it != _shortOps.end() && !remain)
{
remain = (it->second.flags == OptionData::CmdLine_NotExtracted);
++it;
}
if (!remain)
{
LongOptions::const_iterator it = _longOps.begin();
while (it != _longOps.end() && !remain)
{
remain = (it->second.flags == OptionData::CmdLine_NotExtracted);
++it;
}
}
if (!remain)
{
// check for the global arguments:
Token* token = _first_token;
while (!remain && token != NULL)
{
remain = (token->type == Token::GlobalArgument || token->type == Token::UnknownYet);
token = token->next;
}
}
return remain;
}
}