/
filter.c
786 lines (764 loc) · 23.8 KB
/
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
/*
* filter.c
*
* Copyright 2019 Ryan Koehler, VerdAscend Sciences, ryan@verdascend.com
*
* The programs and source code of the vertools collection are free software.
* They are distributed in the hope that they will be useful,
* WITHOUT ANY WARRANTY OF FITNESS FOR ANY PARTICULAR PURPOSE.
*
* Permission is granted for research, educational, and possibly commercial use
* and modification as long as 1) Code and any derived works are not
* redistributed for any fee, and 2) Proper credit is given to the authors.
* If you wish to include this software in a product, or use it commercially,
* please contact the authors.
*
* See https://www.verdascend.com/ for more
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define __MAIN__
#include "prim.h"
#include "wordlist.h"
#include "filter.h"
/**************************************************************************/
int main(int argc, char **argv)
{ Init(argc,argv); exit( AllDoneI(Filter_numsI(argc,argv),NULL) );}
/*******************************************************************/
void Filter_numsUse()
{
VersionSplash(NULL,VERSION_S,"# ",TRUE);
printf("Usage: <infile> ['-' for stdin] [...options]\n");
printf(" <infile> File to be filtered\n");
printf(" -out XXX Output to file XXX\n");
printf(" -col # Take values from column # (def = 1)\n");
printf(" -sc # Skip (ignore) characters up to position # / line\n");
printf(" -tsv Input is tab separated value format\n");
printf(" -csv Input is comma separated value format\n");
printf(" -rg # # Qualify line if value is in range from # to # inclusive\n");
printf(" -gt # Qualify line if value is greater than or equal to #\n");
printf(" -lt # Qualify line if value is less than or equal to #\n");
printf(" -vex Value exclusive filtering; e.g. make -rg, -lt, -gt NOT inclusive\n");
printf(" -abs Use absolute value\n");
printf(" -rsum Use running sum value\n");
printf(" -lrg # # Qualify line number range # to # [NOTE: Only data lines count]\n");
printf(" -brg # # Qualify block number range # to # [NOTE: Only data lines count]\n");
printf(" -wlis XXX Qualify line with words (first token) listed in XXX\n");
printf(" -kc Keep case for token comparison (default ignore)\n");
printf(" -wst Word start only needs to match line (not full token)\n");
printf(" -wsub Word substring only needs to match line (not full token)\n");
printf(" -rann # Qualify random number of lines # [NOTE: Exact, NO stdin]\n");
printf(" -ranf # Qualify random fraction (0 - 1) # [NOTE: Exact, NO stdin]\n");
printf(" -ranp # Qualify random probability (0 -1) # [NOTE: Approximate]\n");
printf(" -seed # Set random seed to #\n");
printf(" -maxout # Limit output lines to #\n");
printf(" -A # Report # lines After qualifiying lines (like grep -A)\n");
printf(" -B # Report # lines Before qualifiying lines [NOTE: NO stdin]\n");
printf(" -all All lines considered; Default ignores blank and # comments\n");
printf(" -blk # Process lines in 'blocks' of size #; default = 1\n");
printf(" -bof # Start blocks offset # lines; default = 0 = skip no lines\n");
printf(" -blis XXX Comma seperated list of within-block line numbers; first = 1\n");
printf(" -bnot Invert within-block line list)\n");
printf(" -icbn Ignore chars before numbers (i.e. strip leading chars)\n");
printf(" -not Invert line qualification test(s)\n");
printf(" -flag Preceed lines with 1/0 for good/bad\n");
printf(" -pln Preceed lines with line number\n");
printf(" -pfn Preceed lines with filename (i.e. echo input name like grep -H)\n");
printf(" -pre XXX Prefix lines with XXX\n");
printf(" -suf XXX Suffix lines with XXX\n");
printf(" -ssuf Suffix lines with running sum value\n");
printf(" -stat Report only stats about values\n");
printf(" -quiet No summary report\n");
printf("\n");
printf("NOTE: Filtered file lines limited to %d chars wide\n",FILTBUF_SIZE);
printf("\n");
}
/**************************************************************************/
int Filter_numsI(int argc, char **argv)
{
char bufS[FILTBUF_SIZE+1];
int ok,nok,prev_ok,b_line,line,outline,pout,extra;
FILTER *filtPO;
filtPO = CreateFilterPO();
if(!ParseArgsI(argc, argv,
"S -not B -stat B -rg D2 -gt D -lt D -col I -out S\
-seed I -ranf D -ranp D\
-rann I -sc I -flag B -icbn B\
-lrg I2 -wlis S -kc B -wst B -wsub B\
-pln B -A I -B I -qu B -vex B -all B -bof I -blk I\
-brg I2 -bnot B -blis S -maxout I -abs B -pfn B\
-pre S -suf S -rsum B -ssuf B -csv B -tsv B",
filtPO->inname, &filtPO->do_not, &filtPO->do_stat,
&filtPO->min,&filtPO->max, &filtPO->min, &filtPO->max,
&filtPO->col, filtPO->outname,
&filtPO->seed, &filtPO->ranf, &filtPO->ranp,
&filtPO->rann, &filtPO->skipc, &filtPO->do_flag,
&filtPO->do_icbn,
&filtPO->firstl,&filtPO->lastl, filtPO->wlisname,
&filtPO->do_kc, &filtPO->do_wst, &filtPO->do_wsub,
&filtPO->do_pln, &filtPO->do_A, &filtPO->do_B,
&filtPO->do_quiet, &filtPO->do_vex, &filtPO->do_all,
&filtPO->l_bof, &filtPO->l_blk, &filtPO->firstb,&filtPO->lastb,
&filtPO->do_bm_not, filtPO->blk_mlis, &filtPO->maxout,
&filtPO->do_abs, &filtPO->do_pfn,
filtPO->do_pre, filtPO->do_suf,
&filtPO->do_rsum, &filtPO->do_ssuf,
&filtPO->do_csv, &filtPO->do_tsv,
(int *)NULL))
{
Filter_numsUse();
CHECK_FILTER(filtPO);
return(FALSE);
}
/***
* Check options and set things up
*/
if(!OpenFilterFilesI(filtPO)) {
CHECK_FILTER(filtPO);
return(FALSE);
}
if(!CheckFilterOptionsI(filtPO)) {
CHECK_FILTER(filtPO);
return(FALSE);
}
/***
* Process lines
*/
line = outline = nok = extra = pout = 0;
prev_ok = TRUE;
while(fgets(bufS,FILTBUF_SIZE,filtPO->in) != NULL)
{
if( SkipThisLineI(filtPO, bufS) ) {
continue;
}
line++;
/***
* If start-of-block, calc and save flag
*/
b_line = LineInFiltBlockI(filtPO, line);
if( b_line == 1 ) {
ok = IsFiltLineOkI(filtPO, line, bufS, TRUE);
prev_ok = ok;
}
/* Before blocks start */
else if ( b_line < 1 ) {
ok = FALSE;
}
/* In block, but not start; Use previous value, and possibly mask if ok */
else {
ok = prev_ok;
if(ok) {
ok = LineInBlockOkI(filtPO, b_line);
}
}
/* Count ok, and set for any -After extra */
if(ok) {
nok++;
if(filtPO->do_A > 0) {
extra = filtPO->do_A + 1;
}
}
/* Not only stats = per-line output */
if( !filtPO->do_stat) {
pout = FALSE;
if( (filtPO->do_flag) || (ok) || (extra>0) ) {
pout++;
outline++;
}
/* If maxout is set, unset print flag if too many lines */
if( (filtPO->maxout > 0) && (outline > filtPO->maxout) ) {
pout = FALSE;
}
if(pout) {
/***
* Any line prefixes?
*/
if(!NO_S(filtPO->do_pre)) {
fprintf(filtPO->out,"%s\t",filtPO->do_pre);
}
if(filtPO->do_pfn) {
fprintf(filtPO->out,"%s\t",filtPO->inname);
}
if(filtPO->do_pln) {
fprintf(filtPO->out,"%d\t",line);
}
if(filtPO->do_flag) {
fprintf(filtPO->out,"%d\t",ok);
}
/***
* Suffix? If not dump line with suffix, just dump
*/
if(! DumpLineWithSufI(filtPO, bufS)) {
fputs(bufS,filtPO->out);
}
}
}
extra--;
}
ReportFilterStats(filtPO, line, nok);
CHECK_FILTER(filtPO);
return(TRUE);
}
/*************************************************************************
* If dumping suffix, print out here and return TRUE
* Return FALSE if nothing printed
*/
int DumpLineWithSufI(FILTER *filtPO, char *bufS)
{
int dump;
dump = FALSE;
/*
printf("\n>> DumpLineWithSufI |%s|\n",bufS);
*/
/* Anything to append? Strip newline and dump line */
if( (filtPO->do_ssuf) || (!NO_S(filtPO->do_suf)) ) {
Chomp(bufS);
fprintf(filtPO->out,"%s",bufS);
dump++;
}
if(filtPO->do_ssuf) {
fprintf(filtPO->out, DEF_RSUM_PRINT, filtPO->rsum);
}
if(!NO_S(filtPO->do_suf)) {
fprintf(filtPO->out,"\t%s", filtPO->do_suf);
}
if(dump) {
fprintf(filtPO->out,"\n");
}
/*
printf("\n<< DumpLineWithSufI\n\n");
*/
return(dump);
}
/*************************************************************************/
int SkipThisLineI(FILTER *filtPO, char *bufS)
{
int skip;
skip = 0;
/* comment, blank? */
if( COM_LINE(bufS) || BlankStringI(bufS) ) {
if( ! filtPO->do_all ) {
skip++;
}
}
return(skip);
}
/*************************************************************************/
int SetBeforeMaskingI(FILTER *filtPO)
{
char bufS[FILTBUF_SIZE+1];
int line, keep, rand;
/***
* Screen all lines, setting mask for qualified ones
*/
rand = DoingRandFilterI(filtPO, TRUE);
line = 0;
while(fgets(bufS,FILTBUF_SIZE,filtPO->in) != NULL)
{
if( SkipThisLineI(filtPO, bufS) ) {
continue;
}
line ++;
if(IsFiltLineOkI(filtPO, line, bufS, rand)) {
filtPO->mask[line-1] = 1;
}
}
rewind(filtPO->in);
/***
* Mark all mask lines before qualified ones
*/
keep = 0;
while(line > 0)
{
if(filtPO->mask[line-1]) {
keep = filtPO->do_B + 1;
}
if(keep > 0) {
filtPO->mask[line-1] = 1;
}
keep--;
line--;
}
return(TRUE);
}
/*************************************************************************/
FILTER *CreateFilterPO()
{
FILTER *fpPO;
if(!(fpPO=(FILTER *)ALLOC(1,sizeof(FILTER)))) {
return(NULL);
}
fpPO->ID = FILTER_ID;
InitFilter(fpPO);
/* String word token struc */
fpPO->sw = CreateStringwordsPO(-1, 0);
if( ! fpPO->sw ) {
CHECK_FILTER(fpPO);
return(NULL);
}
return(fpPO);
}
/*************************************************************************/
int DestroyFilterI(FILTER *fpPO)
{
VALIDATE(fpPO,FILTER_ID);
CHECK_FILE(fpPO->in);
CHECK_NFILE(fpPO->out,fpPO->outname);
CHECK_FREE(fpPO->mask);
CHECK_FREE(fpPO->blk_mask);
CHECK_WORDLIST(fpPO->wlis);
CHECK_STRINGWORDS(fpPO->sw);
FREE(fpPO);
return(TRUE);
}
/*************************************************************************
* Init structure
*/
void InitFilter(FILTER *fpPO)
{
VALIDATE(fpPO,FILTER_ID);
INIT_S(fpPO->inname);
fpPO->in = NULL;
fpPO->mask = NULL;
fpPO->n_mask = fpPO->n_block = 0;
INIT_S(fpPO->outname);
fpPO->out = NULL;
INIT_S(fpPO->wlisname);
fpPO->wlis = NULL;
fpPO->sw = NULL;
fpPO->col = DEF_COL;
fpPO->min = -TOO_BIG_D;
fpPO->max = TOO_BIG_D;
fpPO->firstl = fpPO->lastl = BAD_I;
fpPO->firstb = fpPO->lastb = BAD_I;
INIT_S(fpPO->blk_mlis);
fpPO->blk_mask = NULL;
fpPO->do_bm_not = FALSE;
fpPO->l_bof = 0;
fpPO->l_blk = 1;
fpPO->seed = BAD_I;
fpPO->rann = -1;
fpPO->ranf = fpPO->ranp = -1.0;
fpPO->maxout = -1;
fpPO->do_not = FALSE;
fpPO->do_stat = FALSE;
fpPO->do_kc = fpPO->do_wst = fpPO->do_wsub = FALSE;
fpPO->do_icbn = FALSE;
fpPO->skipc = 0;
fpPO->do_flag = fpPO->do_pln = fpPO->do_pfn = FALSE;
INIT_S(fpPO->do_pre);
INIT_S(fpPO->do_suf);
fpPO->do_A = fpPO->do_B = 0;
fpPO->do_quiet = FALSE;
fpPO->do_vex = FALSE;
fpPO->do_all = FALSE;
fpPO->do_abs = FALSE;
fpPO->rsum = 0.0;
fpPO->do_rsum = FALSE;
fpPO->do_ssuf = FALSE;
fpPO->do_csv = fpPO->do_tsv = FALSE;
}
/*************************************************************************
* Initialize things
*/
int CheckFilterOptionsI(FILTER *filtPO)
{
char sepC;
Srand(filtPO->seed);
/* Set up line block story */
if(!HandleFilterBlocksI(filtPO)) {
return(FALSE);
}
/* Set up masking */
if(!HandleFilterMaskingI(filtPO)) {
return(FALSE);
}
/* Set qual-line-before masking */
if(filtPO->do_B > 0) {
SetBeforeMaskingI(filtPO);
}
if(!HandleFilterListSetsI(filtPO)) {
return(FALSE);
}
/* Input format (col separator); 0 = default = whitespace */
sepC = 0;
sepC = (filtPO->do_csv) ? ',' : sepC;
sepC = (filtPO->do_tsv) ? '\t' : sepC;
SetStringwordsSep(filtPO->sw, sepC);
return(TRUE);
}
/*************************************************************************/
int HandleFilterBlocksI(FILTER *filtPO)
{
int i;
char *cPC;
if( (filtPO->l_blk < 1) || (filtPO->l_blk > MAX_BLK_SIZE) || (filtPO->l_bof < 0) ) {
printf("Line block size (%d) and offset (%d) won't work\n", filtPO->l_blk, filtPO->l_bof);
return(FALSE);
}
/***
* Within-block masking?
*/
if( (filtPO->l_blk > 1) && (!NO_S(filtPO->blk_mlis)) ) {
/* Allocate mask then parse list and set; 1-based so add 1 extra */
filtPO->blk_mask = (char *)ALLOC(filtPO->l_blk + 1, sizeof(char));
ReplaceChars(',', filtPO->blk_mlis, ' ', filtPO->blk_mlis);
cPC = filtPO->blk_mlis;
while(ISLINE(*cPC))
{
i = -1;
sscanf(cPC,"%d",&i);
if(i < 1) {
printf("Problem with block mask list:\n|%s| |%s| %d\n", filtPO->blk_mlis, cPC, i);
return(FALSE);
}
if(i <= filtPO->l_blk) {
filtPO->blk_mask[i] = 1;
}
NEXT_WORD(cPC);
}
/* Invert? */
if(filtPO->do_bm_not) {
InvertMask(filtPO->blk_mask, filtPO->l_blk + 1);
}
}
return(TRUE);
}
/*************************************************************************
* Check options and allocate / set mask if needed
*/
int HandleFilterMaskingI(FILTER *filtPO)
{
int block;
DOUB fracD;
if( NeedFilterMaskingI(filtPO) ) {
if( IsFileStdinI(filtPO->in) ) {
PROBLINE;
printf("Options not compatible with stdin\n\n");
return(FALSE);
}
/***
* Last two args mean ignore comments and blanks
* Then rewind file and allocate space for line mask
*/
filtPO->n_mask = FileLinesI(filtPO->in,TRUE,TRUE);
filtPO->n_block = INT((filtPO->n_mask - filtPO->l_bof) / filtPO->l_blk);
rewind(filtPO->in);
filtPO->mask = (char *)ALLOC(filtPO->n_mask, sizeof(char));
if(!filtPO->mask) {
PROBLINE;
printf("Can't allocate mask for %d input lines\n",filtPO->n_mask);
return(FALSE);
}
/***
* If doing random fraction, calc frac and set
*/
fracD = -1.0;
if( filtPO->rann > 0) {
if( filtPO->l_blk > 1) {
block = BlockForFiltLineI(filtPO, filtPO->n_mask);
fracD = RNUM(filtPO->rann) / RNUM(block);
}
else {
fracD = RNUM(filtPO->rann) / RNUM(filtPO->n_mask);
}
}
else if( filtPO->ranf >= 0.0) {
fracD = filtPO->ranf;
}
if(fracD >= 0.0) {
SetRandFilterMaskingI(filtPO, fracD);
}
/*
DumpArray(filtPO->mask, IS_CHAR, 0, filtPO->n_mask, " M=%d\n", NULL);
*/
}
return(TRUE);
}
/***********************************************************************
* Returns 1-based block number for line number (i.e. first = 1)
* Lines assumed 1-based (i.e. first = 1);
*/
int BlockForFiltLineI(FILTER *filtPO, int line)
{
int block;
if( line <= filtPO->l_bof ) {
block = 0;
}
else {
block = 1 + INT((line - 1 - filtPO->l_bof) / filtPO->l_blk);
}
return(block);
}
/************************************************************************
* Return the 1-based within-block line number for line number; 1 = first
* Lines assumed 1-based (i.e. first = 1)
*/
int LineInFiltBlockI(FILTER *filtPO, int line)
{
int bline;
if( line <= filtPO->l_bof ) {
bline = 0;
}
else {
bline = 1 + (line - 1 - filtPO->l_bof) % filtPO->l_blk;
}
return(bline);
}
/************************************************************************/
int LineInBlockOkI(FILTER *filtPO, int line)
{
int ok;
ok = TRUE;
if( filtPO->blk_mask && (line <= filtPO->l_blk) ) {
ok = filtPO->blk_mask[line];
}
return(ok);
}
/************************************************************************
* Set random mask; Handles lines and blocks
*/
int SetRandFilterMaskingI(FILTER *filtPO, DOUB fracD)
{
int i, line;
char *tmaskPC;
LIMIT_NUM(fracD, 0.0, 1.0);
if( filtPO->l_blk > 1) {
tmaskPC = (char *)ALLOC(filtPO->n_block, sizeof(char));
MaskRandSubsetI(tmaskPC, filtPO->n_block, RNUM(fracD));
for(i=0; i<filtPO->n_block; i++)
{
line = filtPO->l_bof + (filtPO->l_blk * i);
BOG_CHECK(line >= filtPO->n_mask);
if(tmaskPC[i]) {
filtPO->mask[line] = 1;
}
}
CHECK_FREE(tmaskPC);
}
else {
MaskRandSubsetI(filtPO->mask, filtPO->n_mask, RNUM(fracD));
}
return(TRUE);
}
/************************************************************************/
int DoingRandFilterI(FILTER *filtPO, int do_ranp)
{
int rand;
rand = FALSE;
rand = ( filtPO->rann > 0 ) ? TRUE : rand;
rand = ( filtPO->ranf > 0.0 ) ? TRUE : rand;
if(do_ranp) {
rand = ( filtPO->ranp > 0.0 ) ? TRUE : rand;
}
return(rand);
}
/************************************************************************
* Do we need to mask lines? Some options require two passes
*/
int NeedFilterMaskingI(FILTER *filtPO)
{
int need;
/***
* Need If random number or fraction (exact; not probability)
*/
need = DoingRandFilterI(filtPO, FALSE);
/* Need if reporting do_Before lines */
if( filtPO->do_B > 0 ) {
/* Random probability but do_Before too; Set fractional prob */
if( filtPO->ranp >= 0.0 ) {
filtPO->ranf = filtPO->ranp;
}
need = TRUE;
}
return(need);
}
/*************************************************************************
* Loading words from files?
*/
int HandleFilterListSetsI(FILTER *filtPO)
{
if(!NO_S(filtPO->wlisname)) {
if( ! (filtPO->wlis = CreateWordlistPO(filtPO->wlisname,NSIZE))) {
PROBLINE;
printf("Failed to get tokens from %s\n",filtPO->wlisname);
return(FALSE);
}
}
return(TRUE);
}
/*************************************************************************
* Open in / out files
*/
int OpenFilterFilesI(FILTER *filtPO)
{
if(!(filtPO->in = OpenUFilePF(filtPO->inname,"r",NULL))) {
return(FALSE);
}
if(!NO_S(filtPO->outname)) {
if(!(filtPO->out = OpenUFilePF(filtPO->outname,"w",NULL)))
{
return(FALSE);
}
}
HAND_NFILE(filtPO->out);
return(TRUE);
}
/**************************************************************************/
void ReportFilterStats(FILTER *filtPO, int num, int nok)
{
if(!filtPO->do_quiet) {
printf("# %d data lines\n",num);
printf("# %d qualified data lines (%5.2f%%)\n",
nok, PERCENT_R(nok,num));
}
}
/*************************************************************************
* Get number value from word and set it to pointer
*/
int FiltGetWordNumValI(FILTER *filtPO, char *wordS, DOUB *valPD)
{
int w;
char *wPC, rwordS[DEF_BS], cwordS[DEF_BS];
DOUB rD;
/***
* Ignore chars before digits?
*/
w = 0;
if(filtPO->do_icbn) {
while( (isgraph(INT(wordS[w]))) && (!isdigit(INT(wordS[w]))) ) {
w++;
}
if( (w>0) && (wordS[w-1]=='-') ) {
w--;
}
}
wPC = &wordS[w];
/***
* Copy to clean up extra chars, then try to get number
*/
sscanf(wPC,"%s",rwordS);
RemoveChars("[(,)]", rwordS, cwordS);
rD = BAD_D;
sscanf(cwordS,"%lf",&rD);
if(BAD_DOUB(rD)) {
return(FALSE);
}
*valPD = rD;
return(TRUE);
}
/*************************************************************************
* Is the line pointed to by cPC OK based on all criteria?
* line = 1-based line number
*/
int IsFiltLineOkI(FILTER *filtPO, int line, char *cPC, int use_mask)
{
char wordS[FILTBUF_SIZE+1];
int j, n, ok, test, block;
DOUB rD;
ok = TRUE;
test = 0;
/* Line number ok? */
if( !BAD_INT(filtPO->firstl) ) {
if( (line < filtPO->firstl) || (line > filtPO->lastl) ) {
ok = FALSE;
}
test++;
}
/* Block number ok? */
if( ok && (!BAD_INT(filtPO->firstb)) ) {
block = BlockForFiltLineI(filtPO, line);
if( (block < filtPO->firstb) || (block > filtPO->lastb) ) {
ok = FALSE;
}
test++;
}
/* Mask? */
if( ok && (filtPO->mask) && (use_mask) ) {
/***
* If mask is smaller than number of lines, disqualify
*/
if(line > filtPO->n_mask) {
ok = FALSE;
}
else {
ok = INT(filtPO->mask[line-1]);
}
test++;
}
/* Random probability? */
if( !test && (filtPO->ranp >= 0.0) ) {
rD = RandD(1.0);
ok = (rD < filtPO->ranp);
test++;
}
/***
* Get column entry then check
* If can't get word or number from column, simply set not ok
*/
if( !test ) {
/* Ignore chars up to? */
for(j=0;j<filtPO->skipc;j++)
{
if(!ISLINE(*cPC)) {
break;
}
cPC++;
}
/* Parse line into words */
n = LoadStringwordsI(filtPO->sw, cPC, -1);
if(n > 0) {
ok = GetStringwordsWordI(filtPO->sw, filtPO->col - 1, wordS, -1);
}
else {
ok = FALSE;
}
/***
* Get number from word (if not token or number list)
*/
if( ok && (!filtPO->wlis) ) {
ok = FiltGetWordNumValI(filtPO, wordS, &rD);
/***
* Absolute value?
*/
if( ok && filtPO->do_abs && (rD < 0.0) ) {
rD = -rD;
}
/***
* Running sum
*/
if(ok) {
filtPO->rsum += rD;
}
if( filtPO->do_rsum ) {
rD = filtPO->rsum;
}
/***
* Too big or small? Inclusive or exclusive bounds differ
*/
if( ok ) {
if (( filtPO->do_vex ) && ((rD <= filtPO->min) || (rD >= filtPO->max)) ) {
ok = FALSE;
}
else if ((rD < filtPO->min) || (rD > filtPO->max)) {
ok = FALSE;
}
}
}
/***
* In number or token list?
*/
if( ok && (filtPO->wlis) ) {
ok = WordInWordlistI(filtPO->wlis, wordS, filtPO->do_kc, filtPO->do_wst,
filtPO->do_wsub, NULL);
}
}
/* Inverting qualificaiton? */
if(filtPO->do_not) {
ok = !ok;
}
return(ok);
}