Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
268 lines (225 loc) · 7.41 KB
/
Copy pathmain.cpp
File metadata and controls
268 lines (225 loc) · 7.41 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
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
/*
This program 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.
This program 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/>.
Copyright 2014 dl12345@xda-developers forum
*/
#include <string>
#include <iostream>
#include <boost/program_options/options_description.hpp>
#include <boost/program_options/cmdline.hpp>
#include <boost/program_options/parsers.hpp>
#include <boost/program_options/variables_map.hpp>
#include <boost/filesystem.hpp>
#include "qcn.hpp"
using qcn::Qcn;
namespace fs = boost::filesystem;
typedef enum {interleave, sequential, count} printformat;
bool const ProcessCommandLine(
int ac,
char *av[],
Qcn::cmp& cmp,
printformat& format,
std::string& fileone,
std::string& filetwo,
std::string& filedict
)
{
namespace po = boost::program_options;
typedef std::vector<std::string> files_type;
char t, pf;
files_type f;
std::string prog(fs::path(av[0]).filename().string());
std::string usage = "Usage: " + prog + " [options] file file";
po::options_description visible;
visible.add_options()
("help,h", "show help message\n")
("type,t", po::value<char>(&t)->default_value('p'),
"show differences\n"
" p for items present in both files\n"
" m for items missing in either file\n"
" b for both present and missing items\n")
("format,f", po::value<char>(&pf)->default_value('i'),
"output format\n"
" i for interleaved output\n"
" s for sequential output\n"
" c to suppress item data and print only count\n")
("lookup,l", po::value<std::string>(&filedict)->default_value("nv.txt"),
"nv item descriptions")
;
po::options_description hidden("hidden options");
hidden.add_options()("input,i", po::value<files_type>(&f), "input file");
po::positional_options_description p;
p.add("input", -1);
po::options_description all;
all.add(visible).add(hidden);
po::variables_map vm;
po::store(
po::command_line_parser(ac, av).options(all).positional(p).run(), vm
);
po::notify(vm);
if (vm.count("help"))
{
std::cout << std::endl << usage << std::endl << std::endl;
std::cout << visible;
return false;
}
// process and set input files
if (vm.count("input") && f.size() == 2)
{
fileone = f[0];
filetwo = f[1];
if (!fs::exists(fileone))
{
std::cout << prog << ": " << fileone << " not found" << std::endl;
return false;
}
if (!fs::exists(filetwo))
{
std::cout << prog << ": " << filetwo << " not found" << std::endl;
return false;
}
}
else
{
std::cout << std::endl << usage << std::endl << std::endl;
std::cout << visible;
return false;
}
// process and set comparison type
if (vm.count("type"))
{
switch(t)
{
case 'p':
cmp = Qcn::cmp::present;
break;
case 'm':
cmp = Qcn::cmp::missing;
break;
case 'b':
cmp = Qcn::cmp::both;
break;
default:
std::cout << std::endl << usage << std::endl << std::endl;
std::cout << visible;
return false;
}
}
// process print output format
if (vm.count("format"))
{
switch(pf)
{
case 'i':
format = interleave;
break;
case 's':
format = sequential;
break;
case 'c':
format = count;
break;
default:
std::cout << std::endl << usage << std::endl << std::endl;
std::cout << visible;
return false;
}
}
return true;
}
void PrintOutput(
qcn::diff_type const& d,
std::string const& fileone,
std::string const& filetwo,
std::string const& filedict,
printformat p = interleave
)
{
std::cout << std::endl;
std::cout << "Found " << d.size() << " non matching items";
std::cout << std::endl << std::endl;
qcn::Dictionary dict(filedict);
bool printinfo = false;
if (dict.Open()) printinfo = true;
switch(p)
{
case interleave:
for (auto i = d.begin(); i != d.end(); ++i)
{
auto p = *i;
if (printinfo)
{
auto j = dict.Find(p.first.code);
if (j != dict.end())
{
auto q = *j;
p.first.description = q.description;
p.second.description = q.description;
p.first.category = q.category;
p.second.category = q.category;
}
}
std::cout << '[' << fileone << "]: ";
std::cout << p.first << std::endl;
std::cout << '[' << filetwo << "]: ";
std::cout << p.second << std::endl;
}
break;
case sequential:
std::cout << '[' << fileone << "]: " << std::endl << std::endl;
for (auto i = d.begin(); i != d.end(); ++i)
{
auto p = *i;
std::cout << p.first << std::endl;
}
std::cout << '[' << filetwo << "]: " << std::endl << std::endl;
for (auto i = d.begin(); i != d.end(); ++i)
{
auto p = *i;
std::cout << p.second << std::endl;
}
break;
default:
break;
}
}
int main(int argc, char *argv[])
{
Qcn::cmp cmp;
std::string nameone, nametwo, nameinfo;
printformat pf;
if (ProcessCommandLine(argc, argv, cmp, pf, nameone, nametwo, nameinfo))
{
qcn::Qcn fileone(nameone), filetwo(nametwo);
auto n1 = fs::path(nameone).filename().string();
auto n2 = fs::path(nametwo).filename().string();
if (fileone.Open() && filetwo.Open())
{
auto d = qcn::Compare(fileone, filetwo, cmp);
PrintOutput(d, n1, n2, nameinfo, pf);
}
else
{
if (!fileone.IsOpen())
{
std::cout << n1 << ": ";
std::cout << fileone.ErrorMessage() << std::endl;
}
if (!filetwo.IsOpen())
{
std::cout << n2 << ": ";
std::cout << filetwo.ErrorMessage() << std::endl;
}
return 1;
}
}
return 0;
}