Debian Codegraph @

applic/general/src/keyset.c

raw
1 /*===========================================================================
2 Copyright (C) 1991-2009 European Southern Observatory (ESO)
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of
7 the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public
15 License along with this program; if not, write to the Free
16 Software Foundation, Inc., 675 Massachusetts Ave, Cambridge,
17 MA 02139, USA.
18
19 Correspondence concerning ESO-MIDAS should be addressed as follows:
20 Internet e-mail: [email protected]
21 Postal address: European Southern Observatory
22 Data Management Division
23 Karl-Schwarzschild-Strasse 2
24 D 85748 Garching bei Muenchen
25 GERMANY
26 ===========================================================================*/
27
28 /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
29 /* .IDENT keyset.c */
30 /* .AUTHOR Pascal Ballester, ESO - Garching */
31 /* .KEYWORDS Session Manager */
32 /* .PURPOSE Parse set/.. line and initialize keywords */
33
34 /* .VERSION 1.0 Creation 15-JAN-1991 PB */
35
36 /* 090710 last modif */
37
38 /* ------------------------------------------------------------- */
39
40 #include <tbldef.h>
41 #include <midas_def.h>
42 #include <str.h>
43
44 #include <stdio.h>
45 #include <string.h>
46
47
48 extern int strindex();
49
50 int findkey(), writekey();
51
52
53 int tid, nrow, colnam, coltyp;
54
55 int main()
56 {
57
58 char file[61], record[80], keynam[80];
59 char value[135], fullnam[61], *line, linorg[235];
60 int actvals, null, kunit;
61 int found;
62 int ntoken, car, mode, pos, cnt, inter;
63 int fnb; /* First non-blank character */
64 char separ;
65 int ncol, nsort, allcol, allrow;
66
67 SCSPRO("keyset");
68
69 SCKRDI("INPUTI", 1, 1, &actvals, &ntoken, &kunit, &null);
70 SCKGETC("IN_A", 1, 60, &actvals, file);
71
72 separ = ' ';
73
74 if (ntoken > 0 ) {
75
76 SCKGETC("LINE", 1,132, &actvals, linorg);
77
78 TCTOPN(file,F_I_MODE,&tid);
79 TCIGET(tid, &ncol, &nrow, &nsort, &allcol, &allrow);
80 TCCSER(tid,"KEY",&colnam);
81 TCCSER(tid,"TYPE",&coltyp);
82
83 /* 0. Counts the real number of tokens */
84
85 ntoken = 0; /* number of tokens */
86 cnt = 0;
87 inter = 1; /* 0 = in a token, 1 = between tokens */
88
89 /* Go to the first non-blank character */
90 while (linorg[cnt] == ' ' && linorg[cnt] != '\0') cnt++;
91
92 line = (char *) (linorg+cnt);
93 fnb = cnt = 0;
94
95 if (cnt < (int)(strlen(line)-1)) {
96
97 while (line[cnt] != '\0') {
98 if (line[cnt] != ' ' && inter != 0) {inter=0; ntoken++;}
99 if (line[cnt] == ' ' && inter == 0) {inter = 1;}
100 cnt++;
101 }
102 }
103
104 ntoken--; /* First token is the command itself */
105
106 /* 1. Parse command line to extract all tokens of syntax <name>=<value> */
107 /* The parser is controlled by the variable mode, which value can be:
108 mode = 0 : The parser is currently reading a keyword name. All
109 blanks in the keyword name are removed. This
110 mode will be stopped by a separation character (=).
111 mode = 1 : A separation character was found and the parser removes
112 all blanks until it finds either a double quote (")
113 or any other character than blank and double quote
114 and then switches to mode=2
115 mode = 2 : The parser is currently reading a value. Depending on
116 the character which started the mode (double quote or
117 any other), the separator will be a double quote or
118 a blank (variable separ). The current mode is stopped at
119 the separator or a character null (\0). The value is
120 assigned and the parser returns to mode=0.
121 */
122
123 mode = 0;
124 pos = 0;
125
126 for (car=(int)strindex((line+fnb)," ")+1; car<=(int)strlen(line); car++) {
127 switch(mode) {
128
129 case 0 : {
130 if (line[car] != ' ') {
131 if (line[car] == '=')
132 {keynam[pos]='\0'; pos=0; mode=1;}
133 else keynam[pos++] = line[car];
134 }
135 break;
136 }
137
138 case 1 : {
139 if (line[car] != ' ') {
140 mode = 2;
141 if (line[car] == '"') separ='"';
142 else {separ = ' '; value[pos++] = line[car];}
143 }
144 break;
145 }
146
147 case 2 : {
148 if (line[car] == separ || line[car] == '\0') {
149 value[pos] = '\0';
150 pos=0;
151 mode=0;
152 if (findkey(keynam, record, fullnam) == 1)
153 writekey(fullnam, value);
154 break;
155 }
156
157 value[pos++] = line[car];
158 break;
159 }
160
161 } /* Matches switch(mode) */
162
163 } /* Matches for (car= ... */
164
165 } /* Matches if ntoken > 0 ... */
166
167 else {
168
169 SCKGETC("IN_B", 1, 60, &actvals, keynam);
170 found = findkey(keynam, record, fullnam);
171 if (found == 1)
172 SCKWRC("OUT_A", 1, fullnam, 1, 60, &kunit);
173 SCKWRI("OUTPUTI",&found,1,1,&kunit);
174
175 }
176
177 SCSEPI();
178 return 0;
179 }
180
181
182 int findkey (keynam, record, fullnam)
183
184 /* Associates the name of the keyword (keynam) to its complete */
185 /* definition (stored in file) */
186
187 char *keynam, *record, *fullnam;
188
189 {
190
191 int found, matched;
192 int reclen, index, knull;
193 char line[80], match[80], type[20];
194
195
196 found = 0;
197 matched = 0;
198
199 for (reclen=1; reclen<=nrow; reclen++) {
200
201 TCERDC(tid, reclen, colnam, line, &knull);
202 strcat(line,"/");
203 TCERDC(tid, reclen, coltyp, type, &knull);
204 strcat(line,type);
205
206 if (stuindex(line,keynam) == 0) { /* Case unsensitive comparison */
207 found++;
208 strcpy(record,line);
209 index = strindex(record,"/");
210 if (strlen(keynam) == index) {
211 matched = 1;
212 strcpy(match,line);
213 }
214 /* printf("found %d matched %d keynam %s line %s index %d\n",found, */
215 /* matched,keynam,line,index); */
216 }
217
218 }
219
220 switch (found) {
221
222 case(0) : {
223 printf("Could not find keyword %s. Ignored.\n",keynam);
224 break;
225 }
226
227 case(1) : {
228 strcpy(fullnam,record);
229 index = strindex(fullnam,"/");
230 fullnam[index] = '\0';
231 matched = 1;
232 break;
233 }
234
235 default : {
236
237 switch (matched) {
238
239 case(1) : {
240 strcpy(record,match);
241 strcpy(fullnam,record);
242 index = strindex(fullnam,"/");
243 fullnam[index] = '\0';
244 break;
245 }
246
247 default : {
248 printf("Ambiguous : %s. Ignored. \n",keynam);
249 break;
250 }
251 } /* Matches switch(matched) */
252
253 break;
254 } /* Matches default: */
255
256 } /* Matches switch(found) */
257 return(matched);
258 }
259
260
261
262 int writekey (fullnam, value)
263
264 char *value, *fullnam;
265
266 {
267 int pos, str, valid[50], errflag;
268 char ctype, eos, c, string[80];
269 int noelm, ncoma, bytelem;
270 int kunit;
271 double buffer[50], buffd;
272 int buffi, elm;
273 float buffr;
274
275 SCKFND(fullnam, &ctype, &noelm, &bytelem);
276 ctype = toupper(ctype);
277
278 if (ctype != 'C') {
279
280 /* Counts number of comas in string value: ncoma is nb. of comas+1 */
281 ncoma = 1;
282 pos = 0;
283 while (value[pos] != '\0')
284 if (value[pos++] == ',') ncoma++;
285
286 /* noelm becomes the minimum of noelm and ncoma */
287 noelm = (noelm < ncoma) ? noelm : ncoma;
288
289 /* Copy noelm elements in a double array */
290 pos = 0;
291 for (elm=0; elm<noelm; elm++) {
292
293 if (elm == (noelm-1)) eos = '\0'; /* End Of String */
294 else eos = ',';
295 str = 0;
296 while (value[pos] != eos) {
297 c = value[pos++];
298 errflag = 1;
299 /* Only accepted signs are 0-9 and +/- and . and e and E */
300 if (c >= '0' && c <= '9') errflag = 0;
301 if (c == '+' || c == '-') errflag = 0;
302 if (c == 'e' || c == 'E') errflag = 0;
303 if (c == '.' ) errflag = 0;
304 if (errflag == 0)
305 string[str++] = c;
306 }
307 string[str] = '\0';
308 pos++;
309 if (str != 0) {
310 sscanf(string, "%lf", &buffer[elm]);
311 valid[elm] = 1;
312 }
313 else
314 valid[elm] = 0;
315 } /* Matches for (elm=0 ... */
316 } /* Matches if (ctype != 'C') .. */
317
318 switch (ctype) {
319
320 case('C') : {
321 SCKWRC(fullnam, bytelem, value, 1, noelm, &kunit);
322 break;
323 }
324
325 case('I') : {
326 for (elm=1; elm<=noelm; elm++) {
327 if (valid[elm-1] != 0) {
328 buffi = (int) buffer[elm-1];
329 SCKWRI(fullnam, &buffi, elm, 1, &kunit);
330 }
331 }
332 break;
333 }
334
335 case('R') : {
336 for (elm=1; elm<=noelm; elm++) {
337 if (valid[elm-1] != 0) {
338 buffr = (float) buffer[elm-1];
339 SCKWRR(fullnam, &buffr, elm, 1, &kunit);
340 }
341 }
342 break;
343 }
344
345 case('D') : {
346 for (elm=1; elm<=noelm; elm++) {
347 if (valid[elm-1] != 0) {
348 buffd = buffer[elm-1];
349 SCKWRD(fullnam, &buffd, elm, 1, &kunit);
350 }
351 }
352 break;
353 }
354 } /* Matches switch(ctype) */
355
356 return(noelm);
357 }
358
359
360
361
362
363