summaryrefslogtreecommitdiffstats
path: root/funtools/scan.c
blob: f6f222781d9051587594a64954e40dd9da749011 (plain)
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
#include <stdio.h>
#include <string.h>
#include <funtoolsP.h>

static char macrobuf[SZ_LINE];

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;

static char *_HeadColumnCB(char *name, void *client_data)
{
  HC hc = (HC)client_data;

  /* 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;
  }
}

void HeadColumnFree(HC hc)
{
  /* sanity check */
  if( !hc ) return;
  if( hc->fmt ) xfree(hc->fmt);
  xfree(hc);
}

HC HeadColumnNew(char *s)
{
  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, _HeadColumnCB, hc)) ||
      hc->eflag ){
    HeadColumnFree(hc);
    return NULL;
  }
  fprintf(stderr, "format: %s mbuf=%s args=%d\n", hc->fmt, hc->mbuf, hc->args);
  return hc;
}

char *HeadColumnProcess(HC hc, char *s)
{
  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';

  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:%s", tbuf1, tbuf2);
      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:%s", tbuf2, tbuf1);
      break;
    default:
      snprintf(tbuf, SZ_LINE, "%s:%s", tbuf2, tbuf1);
      break;
    }
  }
  else{
    *tbuf = '\0';
  }

  /* add to expression */
  if( hc->tbuf ){
    len = strlen(tbuf);
    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);
    }
    if( *hc->expr ) strncat(hc->expr, ",", SZ_LINE-1);
    strncat(hc->expr, tbuf, SZ_LINE-1);
  }

  /* return what we just got */
  return hc->tbuf;
}

char *HeadColumnExpr(HC hc)
{
  /* sanity check */
  if( !hc ) return NULL;
  /* return current expression */
  return hc->expr;
}

int main(int argc, char **argv)
{
  int i;
  char *fptr=NULL;
  char *expr=NULL;
  char *lptr;
  char lbuf[SZ_LINE];
  HC hc;

  if( argc >= 2 ){
    fptr = argv[1];
  }
  else{
    fptr = "Column $name ($format)";
  }

  /* initialize columnheader priocessing */
  if( !(hc = HeadColumnNew(fptr)) ){
    gerror(stderr, "can't init headcolumn\n");
    return 1;
  }

  /* read comment lines and look for column specifications */
  if( argc > 2 ){
    for(i=2; i<argc; i++){
      strncpy(lbuf, argv[i], SZ_LINE);
      lptr = lbuf;
      if( *lptr == '#' ) lptr++;
      if( HeadColumnProcess(hc, lptr) ){
	fprintf(stderr, "+");
      }
    }
  }
  else{
    while( fgets(lbuf, SZ_LINE, stdin) ){
      lptr = lbuf;
      if( *lptr == '#' ) lptr++;
      if( HeadColumnProcess(hc, lptr) ){
	fprintf(stderr, "+");
      }
    }
  }

  /* display result -- the column specification */
  if( HeadColumnExpr(hc) ){
    fprintf(stderr, "\n%s\n", HeadColumnExpr(hc));
  }

  /* clean up */
  HeadColumnFree(hc);
  return 0;
}