-
Notifications
You must be signed in to change notification settings - Fork 5
/
CsvParserOptions.cpp
428 lines (403 loc) · 14.4 KB
/
CsvParserOptions.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
// Copyright 2009, University of Freiburg
// Chair of Algorithms and Data Structures.
// Authors: Jens Hoffmann <hoffmaje>, Hannah Bast <bast>.
#include <getopt.h>
#include <errno.h>
#include <sstream>
#include <utility>
#include <string>
#include <vector>
#include "parser/ParserBase.h"
#include "parser/CsvParserOptions.h"
// NEW 04Jan13 (baumgari): _showList is a static member of CsvField, which is
// declared here to avoid creating a new cpp.
vector<pair<FormatEnum, vector<bool> > > CsvField::_showList;
// ____________________________________________________________________________
CsvParserOptions::CsvParserOptions()
{
_columnSeparator = TAB;
_fieldSeparator = 0;
_noShowPrefix = 0;
_oldWordsFormat = false;
CsvField::resetStaticShowList();
}
// ____________________________________________________________________________
void CsvParserOptions::printUsage() const
{
cout << "Usage: ./CsvParserMain [with options as follows]"
<< endl
<< endl
<< "--field-separator : a single character that separates"
<< " fields (default = TAB)."
<< endl
<< "--within-field-separator : a single character that separates"
<< " multiple instances of a fields; can"
<< " be the empty string (default = empty)"
<< endl
<< "--full-text : fields to be indexed for full text"
<< " search."
<< endl
<< "--show : fields to be returned in result."
<< " The output format of a show list can"
<< " be specified by appending :xml or"
<< " :json."
<< " Multiple show lists can be separated"
<< " by ; (take care of escaping)."
<< endl
<< "--excerpts : fields for which excerpts will be"
<< " available."
<< endl
<< "--phrase : fields for which completion is to"
<< " whole field."
<< endl
<< "--filter : fields for which to write words like"
<< " :filter:<field>:<word> to the index."
<< endl
<< "--filter-plus : like --filter, but writing words like"
<< " :filter:<fld>:<word>:<fld-content>"
<< endl
<< "--facets : fields to add as categories / facets."
<< endl
<< "--facetids : outputs the same words as --facets,"
<< " but with prefix facetid instead of"
<< " facet; one can then use two different"
<< " block divisions (in the HYB index)"
<< " for the two types of words."
<< endl
<< "--no-show-prefix : a single character in front of field"
<< " items, which should not be added as"
<< " facet or show item."
<< endl
<< "--ordering : how to order given fields."
<< endl
<< "--score : the score for the given fields."
<< endl
<< "--old-words-format : use ct: and cn: instead of :facet:"
<< endl
<< "--field-format : format of the given field in the csv"
<< ", supported formats: xml, json"
<< " (default: xml)."
<< endl
<< "--allow-multiple-items : fields, which may have more than one"
<< " item. A typical case is \"author\"."
<< endl << endl;
ParserBase::printUsage();
cout << endl;
}
// ____________________________________________________________________________
void CsvParserOptions::getFieldName(unsigned int column, string* result) const
{
assert(result != NULL);
if (column < result->size())
{
result->clear();
*result = _fieldOptions[column].getName();
}
}
// ____________________________________________________________________________
int CsvParserOptions::getColumnNumber(const string& fieldName)
{
int result = -1;
for (unsigned int i = 0; i < _fieldOptions.size(); i++)
{
if (fieldName == _fieldOptions[i].getName())
{
result = i;
break;
}
}
return result;
}
// ____________________________________________________________________________
void CsvParserOptions::parseListOfOptArgs_show(const string& showList)
{
// The comma is the delimiter for separating fields.
// NEW 04.01.13 (baumgari): In case of multiple show lists detect them by
// searching for the delimiter ;.
// ; is the delimiter to separate show lists
// NEW 06Mar14 (baumgari): By appending :xml or :json it is possible to
// specify the output of a show list.
const char showFieldDelim = ',';
const char showListDelim = ';';
const char outputDelim = ':';
string fieldName;
unsigned int showListIndex = 0;
bool isOutputFormatField = false;
size_t start = 0;
int column = -1;
for (size_t i = 0; i <= showList.size(); i++)
{
const char ch = (i == showList.size() ? ';' : showList[i]);
if (ch == showFieldDelim || ch == showListDelim || ch == outputDelim)
{
fieldName = showList.substr(start, i - start);
if (!fieldName.empty() && !isOutputFormatField)
{
column = getColumnNumber(fieldName);
if (column == -1)
{
cerr << "CsvParserOptions:125: Field does not exist: " << fieldName
<< endl;
exit(1);
}
// Call setter function to enable the option for field column in list i.
CsvField::setStaticShowList(showListIndex, column,
_fieldOptions.size());
fieldName.clear();
column = -1;
}
else if (!fieldName.empty() && isOutputFormatField)
{
CsvField::setStaticShowListOutputFormat(showListIndex, fieldName);
fieldName.clear();
}
isOutputFormatField = false;
if (ch == outputDelim) isOutputFormatField = true;
if (ch == showListDelim) showListIndex++;
start = i + 1;
}
}
}
// ____________________________________________________________________________
void CsvParserOptions::parseListOfOptArgs_Typ0(const string& optarg,
CsvFieldSetter setterFn)
{
int column = -1;
size_t start = 0;
size_t end = 0;
string fieldName;
while (end != string::npos)
{
end = optarg.find(',', start);
fieldName = optarg.substr(start, end - start);
column = getColumnNumber(fieldName);
if (column == -1)
{
cerr << "CsvParserOptions:106: Field does not exist: " << fieldName
<< endl;
exit(1);
}
// Call setter function to enable the option.
(_fieldOptions[column].*(setterFn))("true");
start = end + 1;
}
}
// ____________________________________________________________________________
void CsvParserOptions::parseListOfOptArgs_Typ1(const string& optarg,
CsvFieldSetter setterFn)
{
int column = -1;
size_t start = 0;
size_t end = 0;
string fieldName;
string option;
while (end != string::npos)
{
// Get the name of the column.
end = optarg.find(':', start);
fieldName = optarg.substr(start, end - start);
column = getColumnNumber(fieldName);
if (column == -1)
{
cerr << "CsvParserOptions.cpp: 116: The given field name '"
<< fieldName << "' does not exist." << endl;
exit(1);
}
start = end + 1;
// Get the prepended option.
end = optarg.find(',', start);
option = optarg.substr(start, end - start);
// cout << "CsvParserOptions::line 124: option =" << option << endl;
// Call setter function to enable option.
(_fieldOptions[column].*(setterFn))(option);
start = end + 1;
}
}
// ____________________________________________________________________________
void CsvParserOptions::parseFromCommandLine(int argc, char** argv,
const string& csvFileName)
{
#ifdef DEBUG_CSV_PARSER_OPTIONS
cout << "CsvParserOptions::parseFromCommandLine ... " << flush;
#endif
string fulltext;
string excerpts;
string show;
string score;
string phraseCompletion;
string filter;
string filterPlus;
string fieldFormat;
string outputFormat;
string facets;
string facetids;
string ordering;
string multipleItemsFields;
FILE *csvFile = NULL;
// Read remaining command-line options. (Those for ParserBase have already
// been read at this point.)
opterr = 0;
optind = 1;
while (true)
{
// Define command-line options.
static struct option longOptions[] =
{
{"help", 0, NULL, 'h'},
{"full-text", 1, NULL, 'f'},
{"show", 1, NULL, 's'},
{"excerpts", 1, NULL, 'e'},
{"field-separator", 1, NULL, 'c'},
{"within-field-separator", 1, NULL, 'C'},
{"score", 1, NULL, 'S'},
{"phrase", 1, NULL, 'p'},
{"filter", 1, NULL, 'F'},
{"filter-plus", 1, NULL, 'P'},
{"facets", 1, NULL, 'a'},
{"facetids", 1, NULL, 'i'},
{"no-show-prefix", 1, NULL, 'x'},
{"ordering", 1, NULL, 'o'},
{"old-words-format", 0, NULL, 'w'},
{"field-format", 1, NULL, 't'},
{"allow-multiple-items" , 1, NULL, 'M'},
{ NULL, 0, NULL, 0 }
};
int c = getopt_long(argc, argv, "hn:f:s:C:e:c:S:p:F:P:a:i:x:o:m:t:wM:",
longOptions, NULL);
// cout << "CsvParserOptions::parseCommendLineOptions ["
// << c << "|" << (char)(c) << "]" << endl;
if (c == -1) break;
switch (c)
{
case 0:
break;
case 'h':
printUsage();
break;
case 'f':
fulltext = string(optarg);
break;
case 's':
show = string(optarg);
break;
case 'e':
excerpts = string(optarg);
break;
case 'c':
if ( strcmp(optarg, "TAB") == 0 ) _columnSeparator = TAB;
else if ( strcmp( optarg, "SPACE") == 0) _columnSeparator = SPACE;
else
{
#ifdef DEBUG_CSV_PARSER_OPTIONS
cout << "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
<< endl
<< "CsvParserOptions.cpp:211: column separator: "
<< static_cast<int>(optarg[0])
<< endl
<< "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
<< endl << endl;
#endif
_columnSeparator = optarg[0];
}
break;
case 'C':
_fieldSeparator = optarg[0];
break;
case 'S':
score = string(optarg);
break;
case 'p':
phraseCompletion = string(optarg);
break;
case 'F':
filter = string(optarg);
break;
case 'P':
filterPlus = string(optarg);
break;
case 'a':
facets = string(optarg);
break;
case 'i':
facetids = string(optarg);
break;
case 'x':
_noShowPrefix = optarg[0];
break;
case 'o':
ordering = string(optarg);
break;
case 'w':
_oldWordsFormat = true;
break;
case 't':
fieldFormat = string(optarg);
break;
case 'M':
multipleItemsFields = string(optarg);
break;
}
}
// Okay. Finished parsing command line options. Now read first line in
// <basename>.csv to determine the field name.
// TODO(hoffmaje:) Log fopen error properly.
char* buffer = new char[CSV_MAX_LINE_LENGTH];
csvFile = fopen(csvFileName.c_str(), "r");
if (csvFile == NULL)
{
snprintf(buffer, CSV_MAX_LINE_LENGTH, "fopen failed: %s",
csvFileName.c_str());
perror(buffer);
exit(1);
}
// Read first line in csv-file to determine the field-names.
string word;
int column = 0;
char delimiters[3] = { _columnSeparator, EOL, '\0' };
CsvField csvField;
size_t start = 0;
size_t end = 0;
// Parse the first line of the .csv file.
char* ret = fgets(buffer, CSV_MAX_LINE_LENGTH, csvFile);
if (ret == NULL)
{
perror("Can't read from file");
exit(errno);
}
string record = string(buffer);
SimpleTextParser simpletextparser;
simpletextparser.setSeparators(delimiters);
while (end < record.size() - 1)
{
simpletextparser.parseText(record, &start, &end);
word = record.substr(start, end - start);
csvField.setName(word);
csvField.setColumn(column);
_fieldOptions.push_back(csvField);
column++;
}
// Field names are determined. Go on parsing all those arguments that
// are lists, containing some (or no) fieldnames. They need some index value.
if (fulltext != "") parseListOfOptArgs_Typ0(fulltext, &CsvField::setFulltext);
if (excerpts != "") parseListOfOptArgs_Typ0(excerpts, &CsvField::setExcerpt);
if (show != "") parseListOfOptArgs_show(show);
if (score != "") parseListOfOptArgs_Typ1(score, &CsvField::setScore);
if (fieldFormat != "") parseListOfOptArgs_Typ1(fieldFormat,
&CsvField::setFormat);
if (phraseCompletion != "") parseListOfOptArgs_Typ0(phraseCompletion,
&CsvField::setPhraseCompletion);
if (filter != "") parseListOfOptArgs_Typ0(filter, &CsvField::setFilter);
if (filterPlus != "") parseListOfOptArgs_Typ0(filterPlus,
&CsvField::setFilterPlus);
if (facets != "") parseListOfOptArgs_Typ0(facets, &CsvField::setFacet);
if (facetids != "") parseListOfOptArgs_Typ0(facetids, &CsvField::setFacetId);
if (ordering != "") parseListOfOptArgs_Typ1(ordering, &CsvField::setOrdering);
if (multipleItemsFields != "")
parseListOfOptArgs_Typ0(multipleItemsFields, &CsvField::setMultipleItems);
}
void CsvParserOptions::readFromFile(const string& fileName)
{
// TODO(hoffmaje): Implement it!
cout << "Method CsvParserOptions::readFromFile not yet implemented." << endl;
exit(1);
}