summaryrefslogtreecommitdiffstats
path: root/funtools/filter/symbols.c
blob: e6bb899be07ed95d4c973514c6341f779f793194 (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
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
/*
 *	Copyright (c) 1999-2003 Smithsonian Astrophysical Observatory
 */

/*
 *
 * symbols.c -- management of filter symbols
 *
 */

#include <filter.h>

#define SYMINC  100	/* increment for allocating symbols */

static char *bincols=NULL;
static char xbin[SZ_LINE];
static char ybin[SZ_LINE];

extern char *filtinchar;

/*
 *
 * Public Routines
 *
 */

/*
 *
 *  FilterSymbolInit -- init the symbol table
 *
 */
#ifdef ANSI_FUNC
int
FilterSymbolInit(Filter filter)
#else
int FilterSymbolInit(filter)
     Filter filter;
#endif
{
  /* make sure we are init'ed */
  if( filter == NULL ){
    gerror(stderr, "symbol table not initialized\n");
    return(0);
  }
  filter->symtab = (FilterSymbols)xcalloc(SYMINC, sizeof(FilterSymRec));
  filter->nsyms = 0;
  filter->maxsyms = SYMINC;
  return(SYMINC);
}

/*
 *
 * FilterSymbolEnter -- enter a new symbol in the symbol table
 * The symbol will either be a column name, or
 * a parameter in the table header, or
 * a parameter in the primary header
 *
 */
#ifdef ANSI_FUNC
char *
FilterSymbolEnter(Filter filter, char *s, int *got)
#else
char *FilterSymbolEnter(filter, s, got)
     Filter filter;
     char *s;
     int *got;
#endif
{
  int brack=0;
  int i, j;
  char *t;
  FilterSymbols sp;
  char name[SZ_LINE];
  FITSCard card;
  FITSHead fhd;

  /* see return value */
  if( got ) *got =-3;

  /* make sure we are init'ed */
  if( filter == NULL ){
    gerror(stderr, "symbol table not initialized\n");
    return(NULL);
  }

  /* make sure we are init'ed */
  if( filter->fhd == NULL ){
    gerror(stderr, "symbol table not initialized\n");
    return(NULL);
  }
  fhd = filter->fhd;

  /* make sure we have something */
  if( !s || !*s )
    return(NULL);

  /* this is the name of the variable in the filter program */
  nowhite(s, name);
  /* but strip off brackets */
  if( (t=strchr(name, '[')) != NULL ){
    brack=1;
    *t = '\0';
  }

  /* loop through the symbol table and look for existing symbol */
  for(i=0; i<filter->nsyms; i++){
    sp = &(filter->symtab[i]);
    /* skip accidentally empty ones */
    if( (sp->name == NULL) || (*sp->name == '\0') )
      continue;
    /* is this name already entered? */
    if( !strcasecmp(sp->name, name) ){
      if( got ) *got = 1;
      return (char *)sp->name;
    }
  }

  /* make sure there is room for a new symbol */
  if( i >= filter->maxsyms ){
    filter->maxsyms += SYMINC;
    filter->symtab = (FilterSymbols)xrealloc(filter->symtab,
		                    filter->maxsyms*sizeof(FilterSymRec));
  }

  /* this is where the next symbol will be entered */
  sp = &(filter->symtab[i]);

  /* look for name in the list of columns */
  if( fhd->table ){
    for(j=0; j<fhd->table->tfields; j++){
      if( !strcasecmp(name, fhd->table->col[j].name) ){
	if( brack && (fhd->table->col[j].n==1) ){
	  if( got ) *got = -2;
	  return(NULL);
	}
	sp->type = SYM_COL;
	sp->name = (char *)xstrdup(name);
	sp->idx = j;
	filter->size += fhd->table->col[j].size;
	filter->nsyms++;
	if( got ) *got = 1;
	return (char *)sp->name;
      }
    }
  }
  /* look for name in the table header */
  if( (t=ft_headgets(fhd, name, 0, NULL, &card)) ){
    sp->type = SYM_PAR;
    sp->name = xstrdup(name);
    sp->value = t;
    sp->idx = -1;
    filter->nsyms++;
    if( got ) *got = 2;
    return (char *)sp->name;
  }
  /* look for name in the primary header */
  else if( fhd->primary &&
	   (t=ft_headgets(fhd->primary, name, 0, NULL, &card)) ){
    sp->type = SYM_PAR;
    sp->name = xstrdup(name);
    sp->value = t;
    sp->idx = -1;
    filter->nsyms++;
    if( got ) *got = 2;
    return (char *)sp->name;
  }
  /* did not find the symbol anywhere */
  else{
    gerror(stderr, "can't find '%s' in table\n", s);
    if( got ) *got = -1;
    return(NULL);
  }
} /* EnterSymbol */


/*
 *
 * FilterSymbolLookup -- lookup a symbol in the symbol table
 *
 */
#ifdef ANSI_FUNC
FilterSymbols
FilterSymbolLookup(Filter filter, char *name)
#else
FilterSymbols FilterSymbolLookup(filter, name)
     Filter filter;
     char *s;
#endif
{
  int i;

  /* sanity check */
  if( !name || !*name ) return NULL;

  /* loop through the symbol table and look for existing symbol */
  for(i=0; i<filter->nsyms; i++){
    /* skip accidentally empty ones */
    if( (filter->symtab[i].name == NULL) || (*filter->symtab[i].name == '\0') )
      continue;
    /* look for name */
    if( !strcasecmp(filter->symtab[i].name, name) ){
      /* return symbol table record if found */
      return &filter->symtab[i];
    }
  }
  return NULL;
}

/*
 *
 * FilterSymbolDefaults -- enter default symbols for X and Y columns
 *
 */
#ifdef ANSI_FUNC
int
FilterSymbolDefaults(Filter filter, int enter)
#else
int FilterSymbolDefaults(filter, enter)
     Filter filter;
     int enter;
#endif
{
  int ip=0;
  char tbuf[SZ_LINE];
  char *mbuf;
  char *k;
  char *s;

  /* this is only for events */
  if( !filter || !filter->fhd || !filter->fhd->table )
    return 0;

  /* parse bincols variable */
  if( !bincols ){
    mbuf = xstrdup(filter->mode);
    if( !keyword(mbuf, "bincols", tbuf, SZ_LINE) )
      return 0;
    if( mbuf ) xfree(mbuf);
    s = tbuf;
    /* parse the bincols string and add the symbols */
    newdtable(",:)");
    bincols = xstrdup(s);
    k = bincols;
    /* point past first paren */
    if( *bincols == '(' )
      k++;
    if( !word(k, xbin, &ip) || !word(k, ybin, &ip) ){
      freedtable();
      return 0;
    }
    freedtable();
    /* set the filter bin strings */
    if( filter->xbin ) xfree(filter->xbin);
    filter->xbin = xstrdup(xbin);
    if( filter->ybin ) xfree(filter->ybin);
    filter->ybin = xstrdup(ybin);
  }
  else if( !filter->xbin || !filter->ybin ){
    /* set the filter bin strings */
    if( filter->xbin ) xfree(filter->xbin);
    filter->xbin = xstrdup(xbin);
    if( filter->ybin ) xfree(filter->ybin);
    filter->ybin = xstrdup(ybin);
  }

  /* enter symbols if necessary */
  if( enter ){
    if( !FilterSymbolEnter(filter, xbin, NULL) ||
	!FilterSymbolEnter(filter, ybin, NULL) )
      return 0;
  }
  return 1;
}

/*
 *
 * FilterSymbolFree -- free space from symbol table
 *
 */
#ifdef ANSI_FUNC
void
FilterSymbolFree(Filter filter)
#else
void FilterSymbolFree(filter)
     Filter filter;
#endif
{
  int i;
  FilterSymbols sp;

  /* if we have no symbols, just return */
  if( !filter || !filter->nsyms )
    return;

  /* loop through the symbol table and free up records */
  for(i=0; i<filter->nsyms; i++){
    sp = &(filter->symtab[i]);
    /* skip accidentally empty ones */
    if( (sp->name == NULL) || (*sp->name == '\0') )
      continue;
    /* process this type of symbol */
    switch(sp->type){
    case SYM_COL:
      xfree(sp->name);
      sp->name = NULL;
      break;
    case SYM_PAR:
      xfree(sp->name);
      xfree(sp->value);
      sp->name = NULL;
      break;
    }
  }
  /* no symbols */
  filter->nsyms = 0;
  if( bincols != NULL ){
    xfree(bincols);
    bincols = NULL;
    *xbin = '\0';
    *ybin = '\0';
  }
}