forked from coolwanglu/pdf2htmlEX
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArgParser.cc
More file actions
173 lines (150 loc) · 4.36 KB
/
Copy pathArgParser.cc
File metadata and controls
173 lines (150 loc) · 4.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
/*
* A wrapper of getopt
*
* by WangLu
* 2012.09.10
*/
#include <getopt.h>
#include <iostream>
#include <vector>
#include <unordered_map>
#include <cassert>
#include "ArgParser.h"
namespace pdf2htmlEX {
using std::ostream;
using std::cerr;
using std::endl;
using std::string;
using std::vector;
using std::unordered_map;
using std::make_pair;
using std::ostringstream;
bool read_value(const char * arg, char * location)
{
*location = arg[0];
return (arg[1] == 0);
}
bool read_value(const char * arg, std::string * location)
{
*location = std::string(arg);
return true;
}
void dump_value(std::ostream & out, const std::string & v)
{
out << '"' << v << '"';
}
ArgParser::~ArgParser(void)
{
for(auto iter = arg_entries.begin(); iter != arg_entries.end(); ++iter)
delete (*iter);
for(auto iter = optional_arg_entries.begin(); iter != optional_arg_entries.end(); ++iter)
delete (*iter);
}
ArgParser & ArgParser::add(const char * optname, const char * description, ArgParserCallBack callback)
{
return add<char>(optname, nullptr, 0, description, callback, true);
}
void ArgParser::parse(int argc, char ** argv) const
{
//prepare optstring and longopts
vector<char> optstring;
optstring.reserve(2*arg_entries.size() + 1);
vector<struct option> longopts;
longopts.reserve(arg_entries.size() + 1);
unordered_map<int, const ArgEntryBase*> opt_map;
for(auto iter = arg_entries.begin(); iter != arg_entries.end(); ++iter)
{
const ArgEntryBase * p = *iter;
if(p->shortname != 0)
{
optstring.push_back(p->shortname);
if(p->need_arg)
optstring.push_back(':');
int v = p->shortname;
if(!(opt_map.insert(make_pair(v, p)).second))
{
cerr << "Warning: duplicated shortname: " << v << endl;
}
}
if(p->name != "")
{
int v = (256 + (iter - arg_entries.begin()));
longopts.resize(longopts.size() + 1);
{
auto & cur = longopts.back();
cur.name = p->name.c_str();
cur.has_arg = ((p->need_arg) ? required_argument : no_argument);
cur.flag = nullptr;
cur.val = v;
}
if(!(opt_map.insert(make_pair(v, p)).second))
{
cerr << "Warning: duplicated long name: " << (p->name) << endl;
}
}
}
optstring.push_back(0);
longopts.resize(longopts.size() + 1);
{
auto & cur = longopts.back();
cur.name = 0;
cur.has_arg = 0;
cur.flag = 0;
cur.val = 0;
}
{
opterr = 1;
int r;
int idx;
while(true)
{
r = getopt_long(argc, argv, &optstring.front(), &longopts.front(), &idx);
if(r == -1)
break;
assert(r != ':');
if(r == '?')
{
throw "";
}
auto iter = opt_map.find(r);
assert(iter != opt_map.end());
iter->second->parse(optarg);
}
}
{
auto iter = optional_arg_entries.begin();
while((optind < argc) && (iter != optional_arg_entries.end()))
{
(*(iter++))->parse(argv[optind++]);
}
}
}
void ArgParser::show_usage(ostream & out) const
{
for(auto iter = arg_entries.begin(); iter != arg_entries.end(); ++iter)
{
(*iter)->show_usage(out);
}
}
template<> const char * ArgParser::get_type_name<int> (void) { return "int"; }
template<> const char * ArgParser::get_type_name<double> (void) { return "fp"; }
template<> const char * ArgParser::get_type_name<string> (void) { return "string"; }
ArgParser::ArgEntryBase::ArgEntryBase(const char * name, const char * description, bool need_arg)
: shortname(0), name(name), description(description), need_arg(need_arg)
{
size_t idx = this->name.rfind(',');
if(idx != string::npos)
{
if(idx+2 == this->name.size())
{
shortname = this->name[this->name.size()-1];
this->name = this->name.substr(0, idx);
}
else
{
cerr << "Warning: argument '" << this->name << "' cannnot be parsed as a short option" << endl;
}
}
}
const int ArgParser::arg_col_width = 31;
} // namespace pdf2htmlEX