forked from tat/mimetic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.cxx
244 lines (227 loc) · 7.33 KB
/
search.cxx
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
/***************************************************************************
copyright : (C) 2002-2008 by Stefano Barbato
email : [email protected]
$Id: search.cxx,v 1.3 2008-10-07 11:06:25 tat Exp $
***************************************************************************/
/** \example search.cc
* extract a Part based on command line parameters
* more info on:
* search -h
*/
#include <iostream>
#include <sstream>
#include <iterator>
#include <fstream>
#include <cassert>
#include <mimetic/mimetic.h>
using namespace std;
using namespace mimetic;
unsigned int g_matches = 0;
void usage()
{
cout << "search [params] [in_file...]" << endl;
cout << "\t-I filename use index (disables '-r')" << endl;
cout << "\t-r looks for matches in nested entities"<<endl;
cout << "\t-t type[/subtype] matches Content-Type" << endl;
cout << "\t-p param[=value] matches Content-Type param" << endl;
cout << "\t-f name[=value] matches an header field" << endl;
cout << "\t-l doesn't write anything but filename" << endl;
cout << "\t-q totaly quiet; exit code = num of matches" << endl;
exit(-1);
}
struct MatchParamRq
{
typedef pair<istring,istring> Param;
typedef pair<istring,istring> Field;
MatchParamRq()
: recursive(0), quiet(0)
{
}
bool operator()(const MimeEntity* pMe) const
{
// check for content type match
const Header& h = pMe->header();
if(type.length() && type != h.contentType().type())
return false;
if(subtype.length() && subtype != h.contentType().subtype())
return false;
bool matched;
// check for params matches
if(paramList.size())
{
ContentType::ParamList::const_iterator ctpbit,ctpeit;
ctpbit = h.contentType().paramList().begin();
ctpeit = h.contentType().paramList().end();
matched = 0;
for(; !matched && ctpbit != ctpeit; ++ctpbit)
{
list<Param>::const_iterator pbit, peit;
pbit = paramList.begin(), peit = paramList.end();
for(; pbit != peit; ++pbit)
{
if(ctpbit->name() != pbit->first)
break;
// case insensitive
istring value = ctpbit->value();
if(value.find(pbit->second) != string::npos)
matched++;
}
}
if(!matched)
return false;
}
// check for field matches
if(fieldList.size())
{
matched = 0;
Header::const_iterator hbit, heit;
hbit = h.begin();
heit = h.end();
for(; !matched && hbit != heit; ++hbit)
{
list<Field>::const_iterator fbit, feit;
fbit = fieldList.begin(), feit = fieldList.end();
for(; fbit != feit; ++fbit)
{
if(hbit->name() != fbit->first)
break;
// case insensitive
istring value = hbit->value();
if(value.find(fbit->second) != string::npos)
matched++;
}
}
if(!matched)
return false;
}
return true;
}
istring type, subtype;
list<Param> paramList;
list<Field> fieldList;
bool recursive, quiet;
};
void die(bool b, const string& msg)
{
if(b)
{
cerr << "Error: " << msg << endl << endl;
usage();
}
}
void printPart(const MimeEntity& me, const MatchParamRq& mpr, string& fqn)
{
while(mpr(&me))
{
++g_matches;
if(mpr.quiet)
break;
if(fqn.length() == 0)
break;
cout << fqn << endl;
fqn.clear();
break; // never loop
}
if(!mpr.recursive)
return; // just top level entity
MimeEntityList::const_iterator mbit, meit;
mbit = me.body().parts().begin(), meit = me.body().parts().end();
for(; mbit != meit; ++mbit)
printPart(**mbit, mpr, fqn);
}
// search the whole index for matches
void searchIndex(const MimeEntity& me, const MatchParamRq& mpr)
{
cout << "PARTS COUNT: " << me.body().parts().size() << endl;
MimeEntityList::const_iterator mbit, meit;
mbit = me.body().parts().begin(), meit = me.body().parts().end();
for(; mbit != meit; ++mbit)
{
MimeEntity* pMe = *mbit;
if(mpr(pMe))
cout << pMe->header().field("x-filename").value() << endl;
}
}
int main(int argc, char** argv)
{
std::ios_base::sync_with_stdio(false);
MatchParamRq mpr;
int ignoreMask = imChildParts | imBody;
string indexFqn;
bool useIndex = 0;
int p = 1;
while(p < argc)
{
string param = argv[p];
if(param == "-h")
usage();
else if (param == "-r") {
mpr.recursive = 1;
ignoreMask = ignoreMask & ~imChildParts;
} else if (param == "-q") {
mpr.quiet = 1;
} else if (param == "-t") {
die( ++p == argc, param + " requires an argument");
ContentType ct(argv[p]);
die(mpr.type.length() != 0, "just one -t allowed");
mpr.type = ct.type();
mpr.subtype = ct.subtype();
} else if (param == "-I") {
die( ++p == argc, param + " requires an argument");
indexFqn = argv[p];
useIndex = 1;
} else if (param == "-p") {
die( ++p == argc, param + " requires an argument");
string switch_param = argv[p];
StringTokenizer stok(&switch_param, "=");
pair<string,string> item;
stok.next(item.first) && stok.next(item.second);
mpr.paramList.push_back(item);
} else if (param == "-f") {
die( ++p == argc, param + " requires an argument");
string switch_param = argv[p];
StringTokenizer stok(&switch_param, "=");
pair<string,string> item;
stok.next(item.first) && stok.next(item.second);
mpr.fieldList.push_back(item);
} else if( param.length() == 2 && param[0] == '-') {
usage();
} else {
// filename list starts here
// first filename: argv[p]
break;
}
++p;
}
string fqn;
if(useIndex) {
File in(indexFqn);
die(!in, "Error opening index file");
mpr.recursive = 0;
MimeEntity idxEntity;
idxEntity.load(in.begin(), in.end());
searchIndex(idxEntity, mpr);
} else if(argc == p) {
// read from stdin
istreambuf_iterator<char> bit(cin), eit;
MimeEntity me;
me.load(bit, eit, ignoreMask);
fqn = "stdin";
printPart(me, mpr, fqn);
} else
for(int fc = p; fc < argc; ++fc)
{
fqn = argv[fc];
File in(fqn);
if(!in)
{
cerr << "ERR: unable to open file " << argv[fc]
<< endl;
continue;
}
MimeEntity me;
me.load(in.begin(), in.end(), ignoreMask);
printPart(me,mpr, fqn);
}
return g_matches;
}