-
Notifications
You must be signed in to change notification settings - Fork 1
/
sp_csv_filter.c
460 lines (377 loc) · 12.2 KB
/
sp_csv_filter.c
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/*
* This file is part of libsysperf
*
* Copyright (C) 2001, 2004-2007 by Nokia Corporation.
*
* Contact: Eero Tamminen <eero.tamminen@nokia.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*
*/
/* ========================================================================= *
* File: sp_csv_filter.c
*
* Author: Simo Piiroinen
*
* -------------------------------------------------------------------------
*
* History:
*
* 21-Sep-2006 Simo Piiroinen
* - fixed include paths
*
* 23-May-2006 Simo Piiroinen
* - actual filtering code moved to libsysperf
*
* 05-Jan-2006 Simo Piiroinen
* - added 'origin' operator -> subtracts value on first row from column
*
* 22-Jul-2005 Simo Piiroinen
* - csv API cleanup
*
* 14-Jul-2005 Simo Piiroinen
* - string values can be used in calculations
*
* 05-Jul-2005 Simo Piiroinen
* - default operation: one past last '_' in progname instead of skipping
* initial "csv_" if present
* - renamed: app_template.c -> sp_csv_filter.c
*
* 29-Jun-2005 Simo Piiroinen
* - added opearions: header, labels
* - added options: --verbose, --quiet, --silent
* - EXIT_FAILURE if load or save fails
*
* 28-Jun-2005 Simo Piiroinen
* - added opearions: calc, select, unique, usecols, order and reverse
* - added options: --no-header, --no-labels, --data-only
* - initial version
* ========================================================================= */
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <assert.h>
#include "msg.h"
#include "argvec.h"
#include "csv_table.h"
#include "str_array.h"
/* ========================================================================= *
* Configuration
* ========================================================================= */
/* ------------------------------------------------------------------------- *
* Tool Version
* ------------------------------------------------------------------------- */
#define TOOL_NAME "sp_csv_filter"
#include "release.h"
/* ------------------------------------------------------------------------- *
* Runtime Manual
* ------------------------------------------------------------------------- */
static const manual_t app_man[]=
{
MAN_ADD("NAME",
TOOL_NAME"\n"
)
MAN_ADD("SYNOPSIS",
""TOOL_NAME" [options] [operations] \n"
)
MAN_ADD("DESCRIPTION",
"This tool allows making various operations on CSV format data.\n"
)
MAN_ADD("OPTIONS", 0)
MAN_ADD("EXAMPLES",
"% "TOOL_NAME" -ifoo.csv -obar.csv --data-only \\\n"
" :select:pid>1000 :uniq:pid,app\n"
"\n"
" Selects rows with <pid> columns larger than 1000 and writes out\n"
" unique combinations of <pid> and <app> column values omitting\n"
" headers, labels and empty line separators.\n"
)
MAN_ADD("OPERATIONS",
":calc:<expr>\n"
":select:<expr>\n"
":sort:[label,...]]\n"
":uniq:[label,...]]\n"
":usecols:<label,...>\n"
":remcols:<label,...>\n"
":order:<label,...>\n"
":origin:<label,...>\n"
":reverse:\n"
":header:\n"
":labels:\n"
"\n"
"Default operation is derived from binary name, thus calling\n"
"this tool via link named 'csv_select' makes\n"
" % csv_select 'pid<10'\n"
"equal to\n"
" % "TOOL_NAME" :select:'pid<10'\n"
"\n"
"The operations are executed after the data has been read in the\n"
"same order as specified on command line\n"
"\n"
"Note that you should escape of quote chars that have special\n"
"meaning for shell.\n"
)
MAN_ADD("COPYRIGHT",
"Copyright (C) 2001, 2004-2007 Nokia Corporation.\n\n"
"This is free software. You may redistribute copies of it under the\n"
"terms of the GNU General Public License v2 included with the software.\n"
"There is NO WARRANTY, to the extent permitted by law.\n"
)
MAN_ADD("SEE ALSO",
"\n"
)
MAN_END
};
/* ------------------------------------------------------------------------- *
* Commandline Arguments
* ------------------------------------------------------------------------- */
enum
{
opt_noswitch = -1,
opt_help,
opt_vers,
opt_verbose,
opt_quiet,
opt_silent,
opt_input,
opt_output,
opt_no_header,
opt_no_labels,
opt_data_only,
};
static const option_t app_opt[] =
{
/* - - - - - - - - - - - - - - - - - - - *
* usage, version & verbosity
* - - - - - - - - - - - - - - - - - - - */
OPT_ADD(opt_help,
"h", "help", 0,
"This help text\n"),
OPT_ADD(opt_vers,
"V", "version", 0,
"Tool version\n"),
OPT_ADD(opt_verbose,
"v", "verbose", 0,
"Enable diagnostic messages\n"),
OPT_ADD(opt_quiet,
"q", "quiet", 0,
"Disable warning messages\n"),
OPT_ADD(opt_silent,
"s", "silent", 0,
"Disable all messages\n"),
/* - - - - - - - - - - - - - - - - - - - *
* application options
* - - - - - - - - - - - - - - - - - - - */
OPT_ADD(opt_input,
"f", "input", "<source path>",
"Input file to use instead of stdin.\n" ),
OPT_ADD(opt_output,
"o", "output", "<destination path>",
"Output file to use instead of stdout.\n" ),
OPT_ADD(opt_no_header,
0, "no-header", 0,
"Omit header rows from Output.\n" ),
OPT_ADD(opt_no_labels,
0, "no-labels", 0,
"Omit label row from Output.\n" ),
OPT_ADD(opt_data_only,
0, "data-only", 0,
"Output only data rows.\n" ),
OPT_END
};
/* ========================================================================= *
* sp_csv_filter_t
* ========================================================================= */
typedef struct sp_csv_filter_t sp_csv_filter_t;
struct sp_csv_filter_t
{
char *input;
char *output;
csv_t *table;
str_array_t expressions;
};
/* ------------------------------------------------------------------------- *
* sp_csv_filter_ctor
* ------------------------------------------------------------------------- */
static inline void sp_csv_filter_ctor(sp_csv_filter_t *self)
{
self->input = 0;
self->output = 0;
self->table = csv_create();
str_array_ctor(&self->expressions);
}
/* ------------------------------------------------------------------------- *
* sp_csv_filter_dtor
* ------------------------------------------------------------------------- */
static inline void sp_csv_filter_dtor(sp_csv_filter_t *self)
{
free(self->input);
free(self->output);
csv_delete(self->table);
array_dtor(&self->expressions);
}
/* ------------------------------------------------------------------------- *
* sp_csv_filter_create
* ------------------------------------------------------------------------- */
sp_csv_filter_t *sp_csv_filter_create(void)
{
sp_csv_filter_t *self = calloc(1, sizeof *self);
sp_csv_filter_ctor(self);
return self;
}
/* ------------------------------------------------------------------------- *
* sp_csv_filter_delete
* ------------------------------------------------------------------------- */
void sp_csv_filter_delete(sp_csv_filter_t *self)
{
if( self != 0 )
{
sp_csv_filter_dtor(self);
free(self);
}
}
/* ------------------------------------------------------------------------- *
* sp_csv_filter_sanity
* ------------------------------------------------------------------------- */
void sp_csv_filter_sanity(sp_csv_filter_t *self)
{
if( self->input == 0 && self->output == 0 &&
isatty(STDIN_FILENO) && isatty(STDOUT_FILENO) )
{
msg_fatal("refusing to filter stdin -> stdout interactively\n"
"(use --help for usage)\n");
}
}
/* ------------------------------------------------------------------------- *
* sp_csv_filter_load_table
* ------------------------------------------------------------------------- */
int sp_csv_filter_load_table(sp_csv_filter_t *self)
{
return csv_load(self->table, self->input);
}
/* ------------------------------------------------------------------------- *
* sp_csv_filter_save_table
* ------------------------------------------------------------------------- */
int sp_csv_filter_save_table(sp_csv_filter_t *self)
{
return csv_save(self->table, self->output);
}
/* ------------------------------------------------------------------------- *
* sp_csv_filter_add_expression
* ------------------------------------------------------------------------- */
void sp_csv_filter_add_expression(sp_csv_filter_t *self, char *expr)
{
str_array_add(&self->expressions, expr);
}
/* ------------------------------------------------------------------------- *
* sp_csv_filter_handle_expressions
* ------------------------------------------------------------------------- */
void sp_csv_filter_handle_expressions(sp_csv_filter_t *self)
{
/* - - - - - - - - - - - - - - - - - - - *
* default operation derived from
* executable name
* - - - - - - - - - - - - - - - - - - - */
const char *prog = msg_getprogname();
const char *base = strrchr(prog, '_');
const char *oper = base ? (base + 1) : prog;
/* - - - - - - - - - - - - - - - - - - - *
* execute filters in original order
* - - - - - - - - - - - - - - - - - - - */
for( size_t i = 0; i < self->expressions.size; ++i )
{
char *expr = str_array_get(&self->expressions, i);
csv_filter(self->table, expr, oper);
}
}
/* ------------------------------------------------------------------------- *
* sp_csv_filter_handle_arguments
* ------------------------------------------------------------------------- */
void sp_csv_filter_handle_arguments(sp_csv_filter_t *self, int ac, char **av)
{
argvec_t *args = argvec_create(ac, av, app_opt, app_man);
#define SET_STRING(var,str) do { free(var); (var) = strdup(str); } while(0)
while( !argvec_done(args) )
{
int tag = 0;
char *par = 0;
if( !argvec_next(args, &tag, &par) )
{
msg_error("(use --help for usage)\n");
exit(1);
}
switch( tag )
{
case opt_help:
argvec_usage(args);
exit(EXIT_SUCCESS);
case opt_vers:
printf("%s\n", TOOL_VERS);
exit(EXIT_SUCCESS);
case opt_verbose:
msg_incverbosity();
break;
case opt_quiet:
msg_decverbosity();
break;
case opt_silent:
msg_setsilent();
break;
case opt_input:
SET_STRING(self->input, par);
break;
case opt_output:
SET_STRING(self->output, par);
break;
case opt_noswitch:
sp_csv_filter_add_expression(self, par);
break;
case opt_no_header:
self->table->csv_flags |= CTF_NO_HEADER;
break;
case opt_no_labels:
self->table->csv_flags |= CTF_NO_LABELS;
break;
case opt_data_only:
self->table->csv_flags |= (CTF_NO_HEADER | CTF_NO_LABELS |
CTF_NO_TERMINATOR);
break;
}
}
#undef SET_STRING
argvec_delete(args);
}
/* ========================================================================= *
* Main Entry Point
* ========================================================================= */
int main(int ac, char **av)
{
sp_csv_filter_t *app = sp_csv_filter_create();
sp_csv_filter_handle_arguments(app, ac, av);
sp_csv_filter_sanity(app);
if( sp_csv_filter_load_table(app) != 0 )
{
exit(EXIT_FAILURE);
}
csv_addvar(app->table, "filter", TOOL_NAME" "TOOL_VERS);
sp_csv_filter_handle_expressions(app);
if( sp_csv_filter_save_table(app) != 0 )
{
exit(EXIT_FAILURE);
}
sp_csv_filter_delete(app);
return 0;
}