This repository has been archived by the owner on Nov 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
funtext.c
1201 lines (1140 loc) · 29.6 KB
/
funtext.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
863
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
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 1999-2005 Smithsonian Astrophysical Observatory
*/
#include <funtoolsP.h>
/*
*
* private routines
*
*/
/* struct for getting column info from headers */
typedef struct _hcstruct{
int args;
int eflag;
int clen;
int maxlen;
char *fmt;
char *expr;
char mbuf[SZ_LINE];
char tbuf[SZ_LINE];
} *HC, HCRec;
/* parser characteristics struct */
typedef struct _ptyperec{
char *delims;
char *comchars;
char *eot;
int nullvalue;
int whitespace;
int units;
int comeot;
int lazyeot;
char *hcolfmt;
} *PType, PTypeRec;
static PTypeRec ptype[] =
{{" \t,\n", "#\n", NULL, 0, 0, 0, 1, 1, NULL}, /* funtools */
{" \t\n", "#\n", NULL, 0, 0, 0, 1, 1, NULL}, /* spaces */
{",\n", "#\n", NULL, 1, 0, 0, 1, 1, NULL}, /* commas */
{"\t\n", "#\n", "\f\n", 1, 0, 0, 1, 0, NULL}, /* rdb */
{"\t\n", "#\n", NULL, 1, 0, 1, 1, 1, "Column $col ($fmt)"}, /* VizieR */
{"|;\n", "#\n", NULL, 1, 0, 0, 1, 1, NULL}}; /* other */
/* macro return buffer */
static char macrobuf[SZ_LINE];
/*
*
* _HCCB -- callback for macro expansion
*
*/
#ifdef ANSI_FUNC
static char *_HCCB(char *name, void *client_data)
#else
static char *_HCCB(name, client_data)
char *name;
void *client_data;
#endif
{
HC hc = (HC)client_data;
/* perform macro replacement */
if( !strcmp(name, "name") || !strcmp(name, "col") ){
if( strchr(hc->mbuf, 'n') ){
hc->eflag = 1;
gerror(stderr,
"$%s can only be specified once in headcol format\n", name);
return NULL;
}
strncpy(macrobuf, "%s", SZ_LINE);
strncat(hc->mbuf, "n", SZ_LINE);
hc->args ;
return macrobuf;
}
else if( !strcmp(name, "format") || !strcmp(name, "fmt") ){
if( strchr(hc->mbuf, 'f') ){
hc->eflag = 1;
gerror(stderr,
"$%s can only be specified once in headcol format\n", name);
return NULL;
}
strncpy(macrobuf, "%[a-zA-Z0-9.]", SZ_LINE);
strncat(hc->mbuf, "f", SZ_LINE);
hc->args ;
return macrobuf;
}
else if( !strcmp(name, "skip") ){
strncpy(macrobuf, "%*s", SZ_LINE);
return macrobuf;
}
else if( !strcmp(name, "skipi") ){
strncpy(macrobuf, "%*d", SZ_LINE);
return macrobuf;
}
else if( !strcmp(name, "skipf") ){
strncpy(macrobuf, "%*f", SZ_LINE);
return macrobuf;
}
else{
return NULL;
}
}
/*
*
* HCFree -- free up headercolumn struct
*
*/
#ifdef ANSI_FUNC
static void
HCFree(HC hc)
#else
static void
HCFree(hc)
HC hc;
#endif
{
/* sanity check */
if( !hc ) return;
if( hc->fmt ) xfree(hc->fmt);
if( hc->expr ) xfree(hc->expr);
xfree(hc);
}
/*
*
* HCNew -- allocate header/column struct and generate scanf expression
*
*/
#ifdef ANSI_FUNC
static HC
HCNew(char *s)
#else
static HC
HCNew(s)
char *s;
#endif
{
HC hc=NULL;
/* sanity check */
if( !s ) return NULL;
/* allocate record */
if( !(hc=xcalloc(1, sizeof(HCRec))) ) return NULL;
/* expand format specification to make a scanf format */
if( !(hc->fmt = ExpandMacro(s, NULL, NULL, 0, _HCCB, hc)) ||
hc->eflag ){
HCFree(hc);
return NULL;
}
return hc;
}
/*
*
* HCProcess -- process a header line to see if it contains column info
*
*/
#ifdef ANSI_FUNC
static char *
HCProcess(HC hc, char *s)
#else
static char *
HCProcess(hc, s)
HC hc;
char *s;
#endif
{
int got;
int len;
char tbuf[SZ_LINE];
char tbuf1[SZ_LINE];
char tbuf2[SZ_LINE];
/* sanity check */
if( !hc || !*hc->mbuf || !hc->fmt || !s ) return NULL;
/* clear buffers */
*tbuf = '\0';
*tbuf1 = '\0';
*tbuf2 = '\0';
/* skip leading white space */
while( isspace((int)*s) ) s ;
/* try to recognize a type specification */
if( !strcmp(hc->mbuf, "n") ){
if( (got = sscanf(s, hc->fmt, tbuf1)) != hc->args ) return NULL;
strncpy(tbuf, tbuf1, SZ_LINE);
}
else if( !strcmp(hc->mbuf, "f") ){
if( (got = sscanf(s, hc->fmt, tbuf1)) != hc->args ) return NULL;
strncpy(tbuf, tbuf1, SZ_LINE);
}
else if( !strcmp(hc->mbuf, "nf") ){
if( (got = sscanf(s, hc->fmt, tbuf1, tbuf2)) != hc->args ) return NULL;
switch(*tbuf2){
case 'I':
snprintf(tbuf, SZ_LINE, "%s:J", tbuf1);
break;
case 'F':
snprintf(tbuf, SZ_LINE, "%s:D", tbuf1);
break;
case 'A':
snprintf(tbuf, SZ_LINE, "%s:%sA", tbuf1, &tbuf2[1]?&tbuf2[1]:"");
break;
default:
snprintf(tbuf, SZ_LINE, "%s:%s", tbuf1, tbuf2);
break;
}
}
else if( !strcmp(hc->mbuf, "fn") ){
if( (got = sscanf(s, hc->fmt, tbuf1, tbuf2)) != hc->args ) return NULL;
switch(*tbuf2){
case 'I':
snprintf(tbuf, SZ_LINE, "%s:J", tbuf2);
break;
case 'F':
snprintf(tbuf, SZ_LINE, "%s:D", tbuf2);
break;
case 'A':
snprintf(tbuf, SZ_LINE, "%s:%sA", tbuf2, &tbuf1[1]?&tbuf1[1]:"");
break;
default:
snprintf(tbuf, SZ_LINE, "%s:%s", tbuf2, tbuf1);
break;
}
}
else{
*tbuf = '\0';
}
/* add to expression */
if( *hc->tbuf ){
len = strlen(tbuf);
/* make sure we have ennough room */
if( (len hc->clen 1) >= hc->maxlen ){
while( (len hc->clen 1) > hc->maxlen ){
hc->maxlen = SZ_LINE;
}
if( hc->clen == 0 )
hc->expr = (char *)xcalloc(hc->maxlen, sizeof(char));
else
hc->expr = (char *)xrealloc(hc->expr, hc->maxlen);
}
/* eventdef syntax requires a comma between column arguments */
if( *hc->expr ) strncat(hc->expr, ",", SZ_LINE-1);
/* add new column */
strncat(hc->expr, tbuf, SZ_LINE-1);
}
/* return what we just got */
return hc->tbuf;
}
/*
*
* _FunTextGetLine -- get a line of text (using existing line if possible)
*
*/
#ifdef ANSI_FUNC
static int
_FunTextGetLine(Fun fun, char *iline, char *lbuf, int llen)
#else
static int _FunTextGetLine(fun, iline, lbuf, llen)
Fun fun;
char *iline;
char *lbuf;
int llen;
#endif
{
int len;
unsigned int i;
char *lptr=NULL;
/* initialize */
*lbuf = '\0';
/* seed with previously-read partial line, if necessary */
if( iline && iline[0] ){
strncpy(lbuf, iline, llen-1);
lbuf[llen-1] = '\0';
iline[0] = '\0';
/* get actual length of string */
len = strlen(lbuf);
/* check for complete line (EOL at end of string) */
if( lbuf[len-1] == '\n' )
lptr = NULL;
/* otherwise prepare to read rest of line */
else
lptr = &lbuf[len];
}
/* need a whole line */
else{
lptr = lbuf;
}
/* read more if we need it */
if( lptr ){
/* get next line, error if none */
if( !ggets(fun->gio, lptr, llen) ){
return 0;
}
}
/* make sure all characters are valid */
for(i=0; i<strlen(lbuf); i ){
if( !isprint(lbuf[i]) && !isspace(lbuf[i]) ){
return 0;
}
}
return strlen(lbuf);
}
/*
*
* semi-public routines, used by other modules
*
*/
/*
*
* FunTextParam -- parse a line, looking for a valid parameter
*
*/
#ifdef ANSI_FUNC
int
FunTextParam(char *pdelims,
char *lbuf, char *kbuf, char *vbuf, char *cbuf, int maxlen)
#else
int FunTextParam(pdelims, lbuf, kbuf, vbuf, cbuf, maxlen)
char *pdelims;
char *lbuf, *kbuf, *vbuf, *cbuf;
int maxlen;
#endif
{
int i;
int hstate=0;
int got=0;
int kgot=0;
int len=0;
int docom=0;
int dtable[PARSE_TABLE_SIZE];
char *s;
char *kptr, *vptr, *cptr;
char *tptr;
char *tbuf;
char quote='\0';
kptr = kbuf;
vptr = vbuf;
cptr = cbuf;
*kptr = '\0';
*vptr = '\0';
*cptr = '\0';
/* set up delim table for removing enclosing chars from keyword strings */
memset(dtable, 0, PARSE_TABLE_SIZE*sizeof(int));
if( pdelims && *pdelims ){
/* set the delim table */
for(s=pdelims; s && *s; s ){
if( (i=(int)*s) == '\\' ){
s ;
if( *s == 'n' ) i = '\n';
else if( *s == 't' ) i = '\t';
else if( *s == 'r' ) i = '\r';
else if( *s == 'f' ) i = '\014';
}
dtable[i] = 1;
}
}
else{
dtable[(int)'='] = 1;
dtable[(int)':'] = 1;
}
/* get modifiable string */
tbuf = xstrdup(lbuf);
nocr(tbuf);
nowhite(tbuf, tbuf);
/* process it */
for(tptr=tbuf; *tptr;){
switch(hstate){
/* gather up keyword */
case 0:
if( (*tptr == ' ') || (*tptr == '\t') || dtable[(int)*tptr] ){
/* check for FITS-style comment */
if( *tptr == '=' ) docom=1;
hstate = 1;
len = 0;
}
else{
*kptr = *tptr;
*kptr = '\0';
if( len >= maxlen ) goto done;
}
tptr ;
break;
/* process whitespace, including = and : */
case 1:
/* skip past whitespace before value */
if( (*tptr == ' ') || (*tptr == '\t') )
tptr ;
else if( dtable[(int)*tptr] ){
/* check for FITS-style comment */
if( *tptr == '=' ) docom=1;
/*only one = or : allowed */
kgot ;
if( kgot > 1 ){
hstate = 2;
len = 0;
}
else{
tptr ;
}
}
else{
hstate = 2;
len = 0;
}
break;
/* check for quoted string */
case 2:
if( *tptr == '"' ){
quote = '"';
docom = 1;
hstate = 3;
len = 0;
}
else if( *tptr == '\'' ){
quote = '\'';
docom = 1;
hstate = 3;
len = 0;
}
else{
*vptr = *tptr;
*vptr = '\0';;
if( len >= maxlen ) goto done;
hstate = 4;
len = 0;
}
tptr ;
break;
/* gather up value in quoted string */
case 3:
if( *tptr == quote ){
hstate = 5;
len = 0;
}
else{
*vptr = *tptr;
*vptr = '\0';
if( len >= maxlen ) goto done;
}
tptr ;
break;
/* gather value up to whitespace */
case 4:
/* gather up value */
if( (*tptr == ' ') || (*tptr == '\t') ){
hstate = 5;
len = 0;
}
else{
*vptr = *tptr;
*vptr = '\0';
if( len >= maxlen ) goto done;
}
tptr ;
break;
/* skip past whitespace before possible comment */
case 5:
if( docom ){
if( (*tptr == ' ') || (*tptr == '\t') ){
tptr ;
}
else{
hstate = 6;
len = 0;
}
}
/* if comments are not wanted, then everything is value */
else{
*vptr = *tptr;
*vptr = '\0';
tptr ;
if( len >= maxlen ) goto done;
}
break;
/* look for comment */
case 6:
if( *tptr == '/' ){
tptr ;
hstate = 7;
len = 0;
}
/* extra chars but no comment char */
else{
got = 4;
goto done2;
}
break;
/* skip past whitespace before possible comment */
case 7:
if( (*tptr == ' ') || (*tptr == '\t') ){
tptr ;
}
else{
hstate = 8;
len = 0;
}
break;
/* gather up comment to end of line */
case 8:
*cptr = *tptr ;
*cptr = '\0';
if( len >= maxlen ) goto done;
break;
default:
gerror(stderr, "unknown state (%d) processing text header\n", hstate);
break;
}
}
/* result code depends on what we gathered up */
done:
if( *cbuf )
got = 3;
else if( *vbuf )
got = 2;
else if( *kbuf )
got = 1;
else
got = 0;
done2:
if( tbuf ) xfree(tbuf);
return got;
}
/*
*
* FunTextParamHeader -- put param into header
*
*/
#ifdef ANSI_FUNC
void
FunTextParamHeader(FITSHead theader,
char *lbuf, char *key, char *val, char *com, int pgot)
#else
void FunTextParamHeader(theader, lbuf, key, val, com, pgot)
FITSHead theader;
char *lbuf, *key, *val, *com;
int pgot;
#endif
{
char *lptr;
longlong ival;
int dtype;
double dval;
switch(pgot){
case -1:
gerror(stderr, "internal text parser error: processing params\n");
break;
case 0:
break;
case 1:
ft_headapps(theader, "COMMENT", 0, key, NULL);
break;
case 2:
case 3:
dtype = ParseDataType(val, &dval, &ival);
switch( dtype ){
case PARSE_COMMENT:
case PARSE_DASH:
case PARSE_EOL:
case PARSE_NULL:
case PARSE_EOT:
break;
case PARSE_FLOAT:
ft_headsetr(theader, key, 0, dval, 7, com, 1);
break;
case PARSE_HEXINT:
case PARSE_INTEGER:
ft_headseti(theader, key, 0, (int)ival, com, 1);
break;
case PARSE_STRING:
ft_headsets(theader, key, 0, val, com, 1);
break;
}
break;
case 4:
/* clean up line */
nocr(lbuf);
nowhite(lbuf, lbuf);
for(lptr=lbuf; *lptr; lptr )
if( *lptr == '\t' ) *lptr = ' ';
ft_headapps(theader, "COMMENT", 0, lbuf, NULL);
break;
}
}
/*
*
* FunTextOpen -- open a ascii text events file,
* set up binning and filtering parameters
*
*/
#ifdef ANSI_FUNC
Fun
FunTextOpen(char *fname, char *mode, char *iline, GIO ifd)
#else
Fun FunTextOpen(fname, mode, iline, ifd)
char *fname;
char *mode;
char *iline;
GIO ifd;
#endif
{
int i=0, p=0, q=0, t=0;
int got;
int state;
int alen=0;
int indx=0;
int pgot=0;
int nheader=0;
int ntheader=0;
char *s;
char *delim=NULL, *comchars=NULL, *eot=NULL, *textp=NULL;
char **hcolfmts=NULL;
char tdelims[SZ_LINE];
char tdebug[SZ_LINE];
char pdelims[SZ_LINE];
char tcomchars[SZ_LINE];
char thcolfmt[SZ_LINE];
char teot[SZ_LINE];
char tnull1[SZ_LINE];
char lbuf[SZ_LINE];
char tbuf[SZ_LINE];
char tbuf2[SZ_LINE];
char pmode[SZ_LINE];
char key[SZ_LINE];
char val[SZ_LINE];
char com[SZ_LINE];
char eventdef[SZ_LINE];
char file[SZ_LINE];
char extn[SZ_LINE];
char textparams[SZ_LINE];
char tail[SZ_LINE];
char ttail[SZ_LINE];
char tname[SZ_LINE];
Parse parser;
Parse fakep=NULL;
ParsedLine line=NULL, header=NULL, data1=NULL;
FITSHead theader=NULL, tmerge=NULL;
Fun fun=NULL;
HC *hc=NULL;
/* intialize */
*tdebug = '\0';
*tdelims = '\0';
*pdelims = '\0';
*tcomchars = '\0';
*thcolfmt = '\0';
*eventdef = '\0';
*teot = '\0';
*tnull1 = '\0';
*tbuf = '\0';
*extn = '\0';
*textparams = '\0';
*tail = '\0';
*ttail = '\0';
*tname = '\0';
*file = '\0';
/* allocate a fun record */
if( !(fun = _FunNew()) )
return NULL;
/* save filename and mode */
fun->fname = xstrdup(fname);
fun->mode = xstrdup(mode);
/* parse filename and get extension, index and tail */
ft_parsefilename(fname, file, extn, SZ_LINE, &indx, ttail, SZ_LINE);
/* if TEXT() specified, strip it out and place in textparams */
if( !_FunSpecialFile(ttail, "TEXT", tname, tail, textparams, SZ_LINE) ){
strncpy(tail, ttail, SZ_LINE-1);
}
/* use passed-in fd or try to open the file */
if( ifd )
fun->gio = ifd;
else if( !(fun->gio=gopen(file, mode)) )
goto error;
/* process section and keywords (but not mode keywords) */
textp = textparams;
_FunKeyword(textp, "debug", "TEXT_DEBUG", tdebug, SZ_LINE);
_FunKeyword(textp, "delims", "TEXT_DELIMS", tdelims, SZ_LINE);
_FunKeyword(textp, "pdelims", "TEXT_DELIMS", pdelims, SZ_LINE);
_FunKeyword(textp, "comchars", "TEXT_COMCHARS", tcomchars, SZ_LINE);
_FunKeyword(textp, "hcolfmt", "TEXT_HCOLFMT", thcolfmt, SZ_LINE);
_FunKeyword(textp, "cols", "TEXT_COLUMNS", eventdef, SZ_LINE);
_FunKeyword(textp, "eot", "TEXT_EOT", teot, SZ_LINE);
_FunKeyword(textp, "null1", "TEXT_NULL1", tnull1, SZ_LINE);
_FunKeyword(textp, "alen", "TEXT_ALEN", tbuf, SZ_LINE);
if( *tbuf ) alen = atoi(tbuf);
if( *textparams ){
if( strlen(textp) && !strchr(textp, '=') ){
/* the whole string is the column spec */
strncpy(eventdef, textp, SZ_LINE-1);
textp = NULL;
}
}
/* make sure CR is in the delim table */
if(*tdelims && !strstr(tdelims, "\\n")) strncat(tdelims, "\\n", SZ_LINE-1);
/* create parsers */
if( *tdelims ){
fun->nparser = 1;
fun->parsers = (Parse *)xcalloc(fun->nparser, sizeof(Parse));
hcolfmts = xcalloc(fun->nparser, sizeof(char *));
hc = xcalloc(fun->nparser, sizeof(HCRec));
delim = tdelims;
comchars = (*tcomchars ? tcomchars : PARSE_DEFAULT_COMCHARS);
hcolfmts[0] = (*thcolfmt ? thcolfmt : NULL);
hc[0] = HCNew(hcolfmts[0]);
eot = (*teot ? teot : NULL);
*pmode = '\0';
if( textp && *textp ){
strncat(pmode, textp, SZ_LINE-1);
strncat(pmode, ",", SZ_LINE-1);
}
strncat(pmode, PARSE_DEFAULT_NULLVALUES, SZ_LINE-1);
strncat(pmode, ",", SZ_LINE-1);
strncat(pmode, PARSE_DEFAULT_WHITESPACE, SZ_LINE-1);
strncat(pmode, ",", SZ_LINE-1);
strncat(pmode, PARSE_DEFAULT_UNITS, SZ_LINE-1);
strncat(pmode, ",", SZ_LINE-1);
strncat(pmode, PARSE_DEFAULT_COMEOT, SZ_LINE-1);
strncat(pmode, ",", SZ_LINE-1);
strncat(pmode, PARSE_DEFAULT_LAZYEOT, SZ_LINE-1);
if( !(fun->parsers[0] = ParseNew(delim, comchars, eot, pmode)) ){
gwarning(stderr, "could not create parser #%d (%s)\n", p, delim);
return NULL;
}
}
else{
fun->nparser = sizeof(ptype)/sizeof(PTypeRec);
fun->parsers = (Parse *)xcalloc(fun->nparser, sizeof(Parse));
hcolfmts = xcalloc(fun->nparser, sizeof(char *));
hc = xcalloc(fun->nparser, sizeof(HCRec));
for(p=0; p<fun->nparser; p ){
delim = ptype[p].delims;
comchars = (*tcomchars ? tcomchars : ptype[p].comchars);
hcolfmts[p] = (*thcolfmt ? thcolfmt : ptype[p].hcolfmt);
hc[p] = HCNew(hcolfmts[p]);
eot = (*teot ? teot : ptype[p].eot);
*pmode = '\0';
if( textp && *textp ){
strncat(pmode, textp, SZ_LINE-1);
}
if( *pmode ) strncat(pmode, ",", SZ_LINE-1);
if( ptype[p].nullvalue ){
strncat(pmode, "nullvalues=true", SZ_LINE-1);
}
else{
strncat(pmode, "nullvalues=false", SZ_LINE-1);
}
if( *pmode ) strncat(pmode, ",", SZ_LINE-1);
if( ptype[p].whitespace ){
strncat(pmode, "whitespace=true", SZ_LINE-1);
}
else{
strncat(pmode, "whitespace=false", SZ_LINE-1);
}
if( *pmode ) strncat(pmode, ",", SZ_LINE-1);
if( ptype[p].units ){
strncat(pmode, "units=true", SZ_LINE-1);
}
else{
strncat(pmode, "units=false", SZ_LINE-1);
}
if( *pmode ) strncat(pmode, ",", SZ_LINE-1);
snprintf(tbuf, SZ_LINE-1, "comeot=%d", ptype[p].comeot);
strncat(pmode, tbuf, SZ_LINE-1);
if( *pmode ) strncat(pmode, ",", SZ_LINE-1);
snprintf(tbuf, SZ_LINE-1, "lazyeot=%d", ptype[p].lazyeot);
strncat(pmode, tbuf, SZ_LINE-1);
if( *tdebug ){
if( *pmode ) strncat(pmode, ",", SZ_LINE-1);
if( istrue(tdebug) ) strcpy(tdebug, "1");
else if( isfalse(tdebug) ) strcpy(tdebug, "0");
snprintf(tbuf, SZ_LINE-1, "debug=%d", atoi(tdebug));
strncat(pmode, tbuf, SZ_LINE-1);
}
if( !(fun->parsers[p] = ParseNew(delim, comchars, eot, pmode)) ){
gwarning(stderr, "could not create text parser #%d (%s)\n", p, delim);
goto error;
}
}
}
/* look for section specification in environment, if none was specified */
if ( !*extn && (indx <=0) ) {
if ( (s = (char *)getenv("TEXT_EXTNAME")) ) {
strncpy(extn, s, SZ_LINE-1);
extn[SZ_LINE-1] = '\0';
}
if ( (s = (char *)getenv("TEXT_EXTNUM")) ) {
indx = atoi(s);
}
}
/* skip to the specified table -- loop requires explicit break, which
we do through a goto to the header processing section or to error */
i = 0;
ext:
i ;
/* if no extension was specified, we are at the right place already */
if( !*extn && (indx <=0) ) goto ext2;
/* index was found, break out and process header */
if( i == indx ) goto ext2;
/* grab next table: get next line, error if none */
while( 1 ){
if( !_FunTextGetLine(fun, iline, lbuf, SZ_LINE) ){
gwarning(stderr,
"text parser failure looking for extension: %s/%d\n",
*extn?extn:"unnamed", (indx!=-1)?indx:1);
goto error;
}
/* analyze line and make sure one parser succeeded (even if its EOT) */
if( !ParseAnalyze(fun->parsers, fun->nparser, lbuf) ){
gwarning(stderr, "text parser failure analyzing line:\n%s", lbuf);
goto error;
}
/* look for that valid parser */
for(got=0, parser=NULL, p=0; p<fun->nparser; p ){
if( fun->parsers[p]->state & PARSE_STATE_BAD) continue;
if( fun->parsers[p]->state & PARSE_STATE_EOT){
for(q=0; q<fun->nparser; q ){
if( fun->parsers[q]->state & PARSE_STATE_BAD) continue;
/* a parser finding eot is still valid */
if( fun->parsers[q]->state & PARSE_STATE_EOT) continue;
/* all other parsers missed this eot, so they are "bad" */
fun->parsers[q]->state = PARSE_STATE_BADMATCH;
}
got ;
parser = NULL;
break;
}
else{
parser = fun->parsers[p];
}
}
/* check for extn, if necessary */
if( parser ){
if( *extn ){
if( parser->types[0] == PARSE_COMMENT ){
if( FunTextParam(pdelims, &lbuf[1], key, val, com, SZ_LINE) >=2 ){
/* initialize fitsy header, if necessary */
if( !theader ) theader = ft_headinit(NULL, 0);
/* add to temp header */
FunTextParamHeader(theader, &lbuf[1], key, val, com, pgot);
/* see if this is the right extension */
nowhite(val, val);
if( !strcasecmp(key, "extname") && !strcasecmp(val, extn) ){
goto ext2;
}
}
}
}
}
/* found another EOT */
else{
/* did not find named extension, free temp header for this section */
if( *extn ){
ft_headfree(theader, 1);
theader = NULL;
}
/* if we still have parsers, we can continue with the next section */
if( got ){
/* bad parsers are kept bad, but others are reset */
for(p=0; p<fun->nparser; p ){
if( fun->parsers[p]->state & PARSE_STATE_BAD) continue;
/* if we read and analyzed into the next table, back up a bit */
if( fun->parsers[p]->state & PARSE_STATE_NEXTLINE ){
line = ParseLineDup(fun->parsers[p], fun->parsers[p]->cur);
state = fun->parsers[p]->state;
state &= ~(PARSE_STATE_NEXTLINE|PARSE_STATE_EOT);
ParseReset(fun->parsers[p], line, state);
}
/* for comments, we just reprocess the line */
else if( fun->parsers[p]->state & PARSE_STATE_REDOLINE ){
ParseReset(fun->parsers[p], NULL, 0);
strncpy(tbuf, lbuf, SZ_LINE-1);
iline = tbuf;
}
else{
ParseReset(fun->parsers[p], NULL, 0);
}
}
/* go back to the outermost loop and look for next extension */
goto ext;
}
else{
gwarning(stderr,
"text parser failure looking for data table %d\n",
indx);
goto error;
}
}
}
ext2:
/* look for a header */
header:
/* get line, seeding with previously-read partial line, if necessary */
if( !_FunTextGetLine(fun, iline, lbuf, SZ_LINE) ){
/* if we ran out of file but eventdef was passed, we have an empty table */
if( *eventdef ){
goto evdef;
}
/* otherwise we're probably in trouble */
/* we'll make some last-ditch checks for a header, which should be in
parse.c but would be really hard to add there */
else{
/* it might be an empty table with a proper header */
if( !nheader ) goto headguess;
/* or, if all strings have ':' characters, it might be a header line */
for(nheader=0, p=0; p<fun->nparser; p ){
if( fun->parsers[p]->state & PARSE_STATE_BAD ) continue;
if( fun->parsers[p]->types[0] == PARSE_STRING ){
for(t=0; t<fun->parsers[p]->ntoken; t ){
if( !fun->parsers[p]->tokens[t].sval ||
!strchr(fun->parsers[p]->tokens[t].sval, ':') ){
nheader ;
break;
}
}
/* flag that we are past the header and into the data */
fun->parsers[p]->state = PARSE_STATE_DATA;
fun->parsers[p]->header =
ParseLineDup(fun->parsers[p], fun->parsers[p]->cur);
}
else{
nheader ;
break;
}
}
if( !nheader ) goto headguess;
/* give up */
gwarning(stderr, "text parser failure looking for header (section %d)\n",
indx);
goto error;
}
}
/* parse and analyze line */
if( !ParseAnalyze(fun->parsers, fun->nparser, lbuf) ){
gwarning(stderr, "text parser failure analyzing header\n");
goto error;
}
/* see if all are in data state, meaning we have found the header */
for(p=0, nheader=0, ntheader=0; p<fun->nparser; p ){
if( fun->parsers[p]->state & PARSE_STATE_BAD ) continue;
/* process comment into a header parameter */
if( fun->parsers[p]->types[0] == PARSE_COMMENT ){
if( strlen(lbuf) ){
/* try to process parameter as a header column spec */
if( !hc[p] || !HCProcess(hc[p], &lbuf[1]) ){
/* if not, process as an ordinary parameter (first time only) */
if( !ntheader ){
pgot = FunTextParam(pdelims, &lbuf[1], key, val, com, SZ_LINE);
/* initialize fitsy header, if necessary */
if( !theader ) theader = ft_headinit(NULL, 0);
FunTextParamHeader(theader, &lbuf[1], key, val, com, pgot);
ntheader ;
}
}
}
}
if( !(fun->parsers[p]->state & PARSE_STATE_DATA) ) nheader ;
}
/* if we're not all in the data state, to back for more */
if( nheader ) goto header;
/* first line of data tells data types */
for(p=0; p<fun->nparser; p ){
if( !(fun->parsers[p]->state & PARSE_STATE_BAD) ){
if( fun->parsers[p]->data1 ){
data1 = fun->parsers[p]->data1;
break;
}