-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
310 lines (286 loc) · 8.31 KB
/
main.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
*
* Usage:
* $0 < [inputfile] > [outputfile]
* or
* $0 [inputfile] [outputfile]
*
*/
#include <filesystem>
#include "rmcpp.h"
#include "../optionparser/optionparser.hpp"
class Preprocessor
{
public:
struct MacroDef
{
std::string name;
std::string value;
bool hasvalue = false;
MacroDef()
{
}
MacroDef(const std::string& nm): name(nm), value(), hasvalue(false)
{
}
template<typename... ArgsT>
MacroDef(const std::string& nm, ArgsT&&... args)
{
std::stringstream valstrm;
((valstrm << args), ...);
value = valstrm.str();
if(!value.empty())
{
hasvalue = true;
}
}
};
using MacroTable = std::map<std::string, MacroDef>;
private:
std::istream* m_infp;
std::stringstream m_datastrm;
/*
* lookup table containing the first character of all macros defined
* greatly reduces time searching
*/
std::vector<int> m_chmap;
MacroTable m_macrotbl;
private:
bool remove_comments()
{
CommentStripper cs(CommentStripper::Options(), m_infp);
return cs.run(m_datastrm);
}
bool mightbemacro(int ch)
{
for(auto it=m_macrotbl.begin(); it!=m_macrotbl.end(); it++)
{
if(it->first[0] == ch)
{
return true;
}
}
return false;
}
void make_mmchars()
{
for(auto it=m_macrotbl.begin(); it!=m_macrotbl.end(); it++)
{
if(std::find(m_chmap.begin(), m_chmap.end(), it->first[0]) == m_chmap.end())
{
m_chmap.push_back(it->first[0]);
}
}
}
bool process_macros(std::ostream& outfp)
{
int currch;
make_mmchars();
while(true)
{
currch = m_datastrm.get();
if(currch == EOF)
{
break;
}
if(std::find(m_chmap.begin(), m_chmap.end(), currch) != m_chmap.end())
{
}
}
return true;
}
public:
Preprocessor(std::istream* infp): m_infp(infp)
{
}
void define(const std::string& name)
{
m_macrotbl[name] = MacroDef(name);
}
template<typename... ArgsT>
void define(const std::string& name, ArgsT&&... args)
{
m_macrotbl[name] = MacroDef(name, args...);
}
bool run(std::ostream& outfp)
{
int rc;
rc = 0;
rc += !remove_comments();
#if 0
/* not yet implemented */
rc += !read_tokens();
#endif
rc += !process_macros(outfp);
return (rc == 0);
}
};
/*
MacroProcessor mc(&std::cin);
mc.define("__MACROPROC__", 1);
mc.define("assert", {"x"}, "if(!(x)){ fprintf(stderr, \"ASSERTION %s\n\", #x); abort(); } ");
*/
int main(int argc, char** argv)
{
bool rc;
bool have_infile;
bool have_outfile;
bool have_commentfile;
std::string outfilename;
std::istream* infp;
std::ostream* outfp;
std::ostream* commentfp;
CommentStripper::Options opts;
// default input is standard input, default output is standard output
infp = &std::cin;
outfp = &std::cout;
commentfp = nullptr;
// track user-supplied file arguments
have_infile = false;
have_outfile = false;
have_commentfile = false;
OptionParser prs;
prs.onUnknownOption([&](const std::string& v)
{
std::cerr << "unknown option '" << v << "'!" << std::endl;
return false;
});
prs.on({"-d", "--debug"}, "show debug messages written to standard error", [&]
{
opts.use_debugmessages = true;
});
prs.on({"-w", "--nowarnings"}, "disable warnings written to standard error", [&]
{
opts.use_warningmessages = false;
});
prs.on({"-o?", "--writecomments=?"}, "write removed comments to file <val>", [&](const auto& v)
{
auto file = v.str();
commentfp = new std::fstream(file, std::ios::out | std::ios::binary);
if(!commentfp->good())
{
std::cerr << "failed to open '" << file << "' for writing" << std::endl;
delete commentfp;
}
have_commentfile = true;
});
prs.on({"-s", "--strip"}, "remove empty lines (does NOT remove linefeeds preceded by whitespace) (default: keep)", [&]
{
opts.remove_emptylines = true;
});
prs.on({"-a", "--keepansi"}, "keep ansi C comments (default: remove)", [&]
{
opts.remove_ansicomments = false;
});
prs.on({"-c", "--keepcpp"}, "keep C++ comments (default: remove)", [&]
{
opts.remove_cppcomments = false;
});
prs.on({"-p", "--pascal"}, "remove pascal style comments (default: keep)", [&]
{
opts.remove_pascalcomments = true;
});
prs.on({"-l", "--hash"}, "remove #-style comments (clashes with preprocessor! dangerous)", [&]
{
opts.remove_hashcomments = true;
});
prs.on({"--convert-cpp"}, "convert C++ comments to C comments - does not remove comments!", [&]{
opts.do_convertcpp = true;
opts.remove_ansicomments = false;
opts.remove_cppcomments = false;
opts.remove_hashcomments = false;
opts.remove_pascalcomments = false;
});
/* implement me! */
#if 0
prs.on({"-x?", "--preprocessor=?"}, "remove comma-separated C-preprocessor tokens (i.e., '-xinclude,import')",
[&](const OptionParser::Value& val)
{
// -x can be specified several times!
Util::split<char>(val.str(), ",", [&](const std::string& tok)
{
opts.ppctokens.push_back(tok);
});
if(opts.ppctokens.size() > 0)
{
opts.have_ppctokens = true;
}
});
#endif
try
{
prs.parse(argc, argv);
auto pos = prs.positional();
/*
* merely assigning to a pointer ref would break RTTI:
* the stream would go out of scope, and the file would be closed.
* so despite all, the streams need to be new'd. :-b
*/
if(pos.size() > 0)
{
opts.infilename = pos[0];
infp = new std::ifstream(opts.infilename, std::ios::in | std::ios::binary);
if(!infp->good())
{
Util::error("cannot open %q for reading", opts.infilename);
return 1;
}
have_infile = true;
}
if(pos.size() > 1)
{
outfilename = pos[1];
if(std::filesystem::exists(outfilename))
{
/* ensure we do not accidently clobber the input file! */
if(std::filesystem::equivalent(opts.infilename, outfilename))
{
Util::error("outputfile %q is also inputfile!", outfilename);
return 1;
}
}
outfp = new std::ofstream(outfilename, std::ios::out | std::ios::binary);
if(!outfp->good())
{
Util::error("cannot open '%s' for writing", outfilename);
return 1;
}
have_outfile = true;
}
}
catch(std::runtime_error& ex)
{
std::cerr << "error: " << ex.what() << std::endl;
}
CommentStripper x(opts, infp);
if(have_commentfile)
{
x.onComment([&](CommentStripper::State st, char ch)
{
if(st == CommentStripper::CT_UNDEF)
{
commentfp->put('\n');
//(*commentfp) << "\n(((end of comment)))\n";
}
else
{
commentfp->put(ch);
}
return true;
});
}
rc = x.run(*outfp);
if(have_commentfile)
{
delete commentfp;
}
if(have_infile)
{
delete infp;
}
if(have_outfile)
{
delete outfp;
}
return !rc;
}