Fork me on GitHub

source: svn/trunk/Utilities/mcfio/mcf_ntuBldDbinc.c@ 730

Last change on this file since 730 was 3, checked in by Xavier Rouby, 16 years ago

first commit

File size: 16.5 KB
RevLine 
[3]1/*
2 * dbin.cc
3 *
4 * C++ utility routines for the dbin package: see dbin.lex
5 *
6 * N.B. The Strings class from the CLHEP library is used.
7 *
8 * Torre Wenaus 04/01/1994
9 *
10 * Modifications:
11 * 8/21/95 T. Wenaus Mod history started
12 * 8/21/95 TW Strings class removed from dbin generated code.
13 * 8/22/95 TW Strings class removed from dbinc.cc
14 *
15 * November 1995: some clean up to be able to run this code and
16 * standard dbin simulateneously..
17 * Make some routine & variable static, and change the name of routine
18 * called from the outside, following the Nirvana/mcfio conventions.
19 *
20 */
21
22#include <stdlib.h>
23#include <string.h>
24#include <stdio.h>
25#include <limits.h>
26#include "mcf_ntuBldDbinc.h"
27#include "mcf_ntubld_db.h"
28
29static void dbin_debug();
30static void lineparse();
31static void dbinparse(char* str, char* typ, char* nam,
32 char* var, char* com, char* dim);
33static void getmembers(long nmems);
34static void getvalues();
35static char* stlower(char*);
36static void chrcat(char* str, char chr);
37static char* token(char** str, char* sep);
38static int testsep(char chr, char *sep);
39static void mcf_ntubld_interpret();
40
41static char varname[40], objname[40], curstruct[40];
42static char chvalues[500], *values, dim[20];
43static char tok1[30], tok2[30], tok3[100], com[100];
44static char line[1000];
45static int n_instance_line_title, n_instance_header, n_instance_variable;
46
47static double dvar[100];
48static float rvar[100];
49static char chvar[100][80];
50static char dbpath[FILENAME_MAX+1], filename[FILENAME_MAX+1];
51static long nvars, morevalues, n_templates;
52static long inc_depth, n_instance, debug_on;
53static int isl;
54static long n_significant, lnlen=0;
55static FILE *inFile, *curFile1, *curFile2, *curFile3, *curFile4, *curFile5;
56static const char *fnamep;
57static void dbin_getrec(char* fname[],void (*)(void));
58
59static void dbin_getrec(char* fname[],void (*interpret)(void))
60{
61 char chr;
62 int istat;
63 long inc_depth_old = 0;
64 /*
65 ** Start be intializing all these globals, to be able to call this routine
66 ** more than once..
67 */
68 inc_depth = 0;
69 n_instance = 0;
70 lnlen = 0;
71 /* extract path from filename */
72 strcpy(filename,*fname);
73 if (strrchr(filename,'/') != NULL) {
74 strcpy(dbpath,filename);
75 *(strrchr(dbpath,'/')+1)='\0';
76 } else {
77 dbpath[0] = '\0';
78 }
79
80 /* open file */
81 inFile = fopen(*fname,"r");
82 if (inFile == NULL) {
83 fprintf(stdout,"Error opening %s\n",*fname);
84 return;
85 }
86 else
87 {
88 if (debug_on) fprintf(stdout,"Opened %s\n",*fname);
89 }
90 /* read a line */
91 while (inc_depth>=0) {
92 istat=1;
93 while (istat!=EOF) {
94 if (inc_depth > inc_depth_old) /* need to open new file */
95 {
96 long ifstat;
97 ifstat=1;
98 fnamep = (const char *)filename;
99 if (inc_depth==1) {curFile1 = fopen(fnamep,"r");
100 if (curFile1==NULL) {
101 fprintf(stdout,"Error opening %s\n",fnamep);
102 ifstat=0;
103 }
104 else {
105 if (debug_on) fprintf(stdout,"Opened %s\n",fnamep);
106 }
107 }
108 if (inc_depth==2) {curFile2 = fopen(fnamep,"r");
109 if (curFile2==NULL) {
110 fprintf(stdout,"Error opening %s\n",fnamep);
111 ifstat=0;
112 }
113 else {
114 if (debug_on) fprintf(stdout,"Opened %s\n",fnamep);
115 }
116 }
117 if (inc_depth==3) {curFile3 = fopen(fnamep,"r");
118 if (curFile3==NULL) {
119 fprintf(stdout,"Error opening %s\n",fnamep);
120 ifstat=0;
121 }
122 else {
123 if (debug_on) fprintf(stdout,"Opened %s\n",fnamep);
124 }
125 }
126 if (inc_depth==4) {curFile4 = fopen(fnamep,"r");
127 if (curFile4==NULL) {
128 fprintf(stdout,"Error opening %s\n",fnamep);
129 ifstat=0;
130 }
131 else {
132 if (debug_on) fprintf(stdout,"Opened %s\n",fnamep);
133 }
134 }
135 if (inc_depth==5) {curFile5 = fopen(fnamep,"r");
136 if (curFile5==NULL) {
137 fprintf(stdout,"Error opening %s\n",fnamep);
138 ifstat=0;
139 }
140 else {
141 if (debug_on) fprintf(stdout,"Opened %s\n",fnamep);
142 }
143 }
144 }
145 inc_depth_old = inc_depth;
146 if (inc_depth==0) istat=fgetc(inFile);
147 if (inc_depth==1) istat=fgetc(curFile1);
148 if (inc_depth==2) istat=fgetc(curFile2);
149 if (inc_depth==3) istat=fgetc(curFile3);
150 if (inc_depth==4) istat=fgetc(curFile4);
151 if (inc_depth==5) istat=fgetc(curFile5);
152 chr = istat;
153 if (chr == '\t') chr = ' '; /* remove tabs */
154 if (chr == '\n') { /* line is complete; process it */
155 if (morevalues == 1) { /* line extension containing values */
156 /* if final significant char is '/', mark next
157 line as values continuation */
158 int i;
159 isl=0;
160 for (i=0;i<strlen(line);i++) {
161 if (line[i] == '!') i=strlen(line);
162 if (line[i] == '/') isl=i;
163 }
164 if (isl != 0) {
165 n_significant = 0;
166 for (i=isl;i<strlen(line);i++) {
167 if (line[i] == '!') i=strlen(line);
168 if (line[i]!='/' && line[i]!=' ' && line[i]!='\t'
169 && i < strlen(line) ) n_significant++;
170 }
171 if (n_significant != 0) morevalues = 0;
172 } else {
173 morevalues = 0;
174 }
175 strcat(values," ");
176 if (morevalues == 0) {
177 strcat(values,line);
178 } else {
179 strncat(values,line,isl-1);
180 }
181 } else { /* line is not an extension. Parse it. */
182 dbinparse(line, tok1, tok2, tok3, com, dim);
183 }
184 if (morevalues == 0) {
185 /* no more line extensions to read. Process line. */
186 /* now interpret the line */
187 if (tok1[0] != '\0') {
188 if (debug_on) fprintf(stdout,"%s %s %s\n",tok1,tok2,values);
189 lineparse();
190 (*interpret)();
191 }
192 }
193 line[0] = '\0';
194 lnlen = 0;
195 } else {
196 /* add to line */
197 if (chr != '\r') { line[lnlen++]=chr; line[lnlen]='\0'; }
198 }
199 }
200 inc_depth--; line[0] = '\0';
201 }
202 return;
203}
204
205/****************************************************************************/
206static void lineparse()
207{
208 char* tokn, *env, *envName, *tmp1, *tmp2;
209 long l, in_template;
210 varname[0] = '\0';
211 objname[0] = '\0';
212 if (!strcmp(tok1,"end")) {strcpy(curstruct,"--"); in_template = 0;}
213 if (!strcmp(tok1,"structure")) {strcpy(curstruct,tok2);}
214 if (!strcmp(tok1,"database")) ;
215 if (!strcmp(tok1,"incname")) ;
216 if (!strcmp(tok1,"index")) ;
217 if (!strcmp(tok1,"provide")) ;
218 if (!strcmp(tok1,"parent")) ;
219 if (!strcmp(tok1,"child")) ;
220 if (!strcmp(tok1,"dimension")) ;
221 if (!strcmp(tok1,"template")) {in_template = 1; strcpy(curstruct,tok2);
222 n_instance = 0;}
223 if (!strcmp(tok1,"command")) {in_template = 1; strcpy(curstruct,tok2);}
224 if (!strcmp(tok1,"include")) { /* switch input to specified file */
225 /*
226 ** Commented out, we use absolute path name in the includes.
227 ** This allows us to go to more than one directory..
228 */
229/* strcpy(filename,dbpath); */
230/* strcat(filename,tok2); */
231/*
232** We now implement translation of environmental variable
233**
234*/
235 if (tok2[0] == '$') {
236 tmp1 = strchr(&tok2[1], '/');
237 if (tmp1 == NULL) {
238 fprintf(stderr, "DBin error, Unkonw path %s\n", tok2);
239 return;
240 }
241 envName = (char *) malloc(sizeof(char) * (strlen(tok2)+1));
242 strcpy(envName, &tok2[1]);
243 tmp2 = strchr(envName, '/'); *tmp2 = '\0';
244 env = getenv(envName);
245 free(envName);
246
247 if (env == NULL) {
248 fprintf(stderr, "DBin error, Unkonw path %s\n", tok2);
249 return;
250 }
251 strcpy(filename,env); l = strlen(env); filename[l] = '/'; l++;
252 strcpy(&filename[l], tmp1);
253
254 } else strcpy(filename, tok2);
255 inc_depth++;
256 }
257 if (!strcmp(tok1,"make")) {
258 n_instance++;
259 strcpy(varname,"TEMPLATE_");
260 strcat(varname,stlower(tok2));
261 }
262 if (!strcmp(tok1,"define")) {
263 /* get first token (name) from values list */
264 tokn = token(&values," \t");
265 strcpy(varname,"TEMPLATE_");
266 strcat(varname,tok2);
267 strcpy(objname,tok2);
268 strcat(objname,"_");
269 strcat(objname,tokn);
270 }
271 if (!strcmp(tok1,"call")) {
272 /* get first token (name) from values list */
273 tokn = token(&values," \t");
274 strcpy(varname,"COMMAND_");
275 strcat(varname,tok2);
276 }
277 if (!strncmp(tok1,"int",3) || !strcmp(tok1,"real") || !strcmp(tok1,"double") ||
278 !strncmp(tok1,"char",4) || !strcmp(tok1,"material") ) {
279 if ((! strncmp(curstruct,"--",2)) && (in_template == 0)) {
280 fprintf(stdout,"dbin: Parameter \"%s\" not in structure; ignored:\n",
281 tok2);
282 fprintf(stdout," %s\n",line);
283 } else {
284 /* parse values */
285 strcpy(varname,curstruct);
286 strcat(varname,".");
287 strcat(varname,tok2);
288 getvalues();
289 }
290 }
291}
292
293/****************************************************************************/
294static void dbinparse(char* str, char* typ,
295 char* nam, char* var, char* com, char* dim)
296{
297/* Parse from line the type, name, value, comment */
298 int i;
299 long nc = 0;
300
301 nvars = 1;
302 chvalues[0] = dim[0] = typ[0] = nam[0] = var[0] = com[0] = '\0';
303 values = chvalues;
304
305/* if final significant char is '/', mark next line as values continuation */
306 isl=strlen(str);
307 for (i=0;i<strlen(str);i++) {
308 if (str[i] == '!') i=strlen(str);
309 if (str[i] == '/') isl=i;
310 }
311 morevalues = 0;
312 if (isl != strlen(str)) {
313 n_significant = 0;
314 for (i=isl;i<strlen(str);i++) {
315 if (str[i] == '!') i=strlen(str);
316 if (str[i]!='/' && str[i]!=' ' && str[i]!='\t'
317 && i < strlen(line) ) n_significant++;
318 }
319 if (n_significant == 0) morevalues = 1;
320 }
321
322 /* initial whitespace, type, whitespace */
323 while ((str[nc] == ' ') || (str[nc] == '\t')) ++nc;
324 while ((str[nc] != ' ') && (str[nc] != '\t')
325 && (nc < strlen(str))) chrcat(typ,str[nc++]);
326 while ((str[nc] == ' ') || (str[nc] == '\t')) ++nc;
327 /* name, whitespace, dimension? */
328 while ((str[nc] != ' ') && (str[nc] != '\t') && (str[nc] != '(' )
329 && (nc < strlen(str))) chrcat(nam,str[nc++]);
330 while ((str[nc] == ' ') || (str[nc] == '\t')
331 && (nc < strlen(str))) ++nc;
332 if (str[nc] == '(') { /* have a dimensioned array */
333 /* get dimension */
334 while (str[++nc] != ')') chrcat(dim,str[nc]); nc++;
335 nvars = atol(dim);
336 }
337 /* skip over value(s) to comment */
338 while ( (str[nc] != '!') &&
339 (str[nc] != '/' || ( morevalues && (nc != isl) ) ) &&
340 ( (nc < strlen(str)) || ( morevalues && (nc < isl)) ) ) chrcat(chvalues,str[nc++]);
341 /* comment */
342 while (((str[nc] == '!') || (str[nc] == '\t'))
343 && (nc < strlen(str))) ++nc;
344 while (nc <= strlen(str)) { chrcat(com,str[nc++]); }
345 /* turn mnemonic num into variable name var */
346 var = nam;
347}
348
349/****************************************************************************/
350
351static void getvalues()
352{
353 char* tokn;
354 long nv=0; while (nv < nvars) {
355 /* get next token and trim it from the values list. */
356 if (!strncmp(tok1,"char",4) || !strncmp(tok1,"material",8) ) {
357 char *iq1, *iq2;
358 iq1 = strchr(values,'"');
359 iq2 = strrchr(values,'"');
360 if (iq1 != NULL) {
361 strncpy(chvar[nv],iq1+1,iq2-iq1-1);
362 chvar[nv][iq2-iq1-1] = '\0';
363 }
364 else
365 strcpy(chvar[nv],values);
366 } else {
367 tokn = token(&values," \t");
368 if (tokn != NULL) {
369 if (!strncmp(tok1,"int",3)) rvar[nv] = atol(tokn);
370 if (!strcmp(tok1,"real")) rvar[nv] = atof(tokn);
371 if (!strcmp(tok1,"double")) dvar[nv] = atof(tokn);
372 }
373 }
374 nv++;
375 }
376}
377
378/****************************************************************************/
379
380static void getmembers(long nmems)
381{
382 char *tokn, *iq1, *iq2;
383 long n, nq, nv=0;
384 /* fill string interiors with '@' so they are delineated as tokens */
385 n=0; nq=0; while (n<strlen(values)) {
386 if (values[n]=='"') nq++;
387 if ((values[n]==' '||values[n]=='\t') && nq%2==1) values[n] = '@';
388 n++;
389 }
390 while (nv < nmems) {
391 /* get next token and trim it from the values list. */
392 tokn = token(&values," \t");
393 if ( tokn[0]=='"' ) {
394 n=0; while (n<strlen(tokn))
395 { if (tokn[n]=='@') tokn[n] = ' '; n++; }
396 iq1 = strchr(tokn,'"');
397 iq2 = strrchr(tokn,'"');
398 strncpy(chvar[nv],iq1+1,iq2-iq1-1);
399 chvar[nv][iq2-iq1-1] = '\0';
400 } else {
401 strcpy(chvar[nv],tokn);
402 }
403 rvar[nv] = atof(tokn);
404 nv++;
405 }
406}
407
408/****************************************************************************/
409static void dbin_debug()
410{
411 debug_on = 1;
412}
413
414/****************************************************************************/
415static void chrcat(char* str, char chr)
416{
417 int ln;
418 ln = strlen(str);
419 str[ln] = chr;
420 str[ln+1]='\0';
421}
422
423/****************************************************************************/
424static char * stlower(char* st) {
425 int i=0;
426 while (st[i] != '\0') {
427 if (st[i] >= 'A' && st[i] <= 'Z') st[i] = st[i] + 'a' - 'A';
428 i++;
429 }
430 return st;
431}
432
433/****************************************************************************/
434static char* token(char** str, char* sep)
435{
436 int i=0;
437 char *if1=NULL, *if2=NULL, *strend = *str + strlen(*str);
438 /* if1 = rel. pointer to 1st token char */
439 i=0; while (if1 == NULL && i<strlen(*str)) {
440 if (!testsep(*(*str+i),sep))
441 if1= *str+i;
442 i++;
443 }
444 if (if1 == NULL) return if1;
445 /* if2 = 1st char past the token */
446 i=0; while (if2 == NULL && i<strlen(if1))
447 { if (testsep(if1[i],sep)) if2=&if1[i]; i++; }
448 if (if2<strend && if2 != NULL) {
449 if (if2 != NULL) *if2 = '\0';
450 *str = if2+1;
451 } else {
452 *str = strend;
453 }
454 return if1;
455}
456
457/****************************************************************************/
458static int testsep(char chr, char *sep)
459{
460 int ist=0, i=0;
461 while (sep[i] != '\0')
462 if (sep[i++] == chr || chr == '\0' || chr == '\n' ) ist=1;
463 return ist;
464}
465/*** Database read routine ***/
466/*** Generated automatically using the dbin tool. */
467/*** Not to be modified by user. */
468/*
469** Modifiedt by P.L., to abe able to load all the templates into
470** one file... And included in this file, to avoid defining too many
471** global symbols. This clearly breaks the dbin mold, to be discussed
472** later..
473*/
474void mcf_ntubldRead(char* fname)
475{
476 void (*pf)(); /* pointer to interpreter */
477 inc_depth = 0;
478 n_instance =0;
479 lnlen=0;
480 debug_on = 0;
481 pf = &mcf_ntubld_interpret;
482 mcf_ntubldInit();
483 n_instance_line_title = 0;
484 n_instance_header = 0;
485 n_instance_variable = 0;
486 dbin_getrec(&fname,pf);
487}
488static void mcf_ntubld_interpret()
489{
490int inum, index, i, iok;
491iok=0;
492if ( !strcmp(varname,"TEMPLATE_line_title") ) {
493 inum = 0; iok = 1;
494 getmembers(n_el_line_title);
495 index = n_instance_line_title;
496 *n_obj_line_title = n_instance_line_title+1;
497 strcpy(line_title[index].line,chvar[inum++]);
498 n_instance_line_title++;
499}
500if ( !strcmp(varname,"TEMPLATE_header") ) {
501 inum = 0; iok = 1;
502 getmembers(n_el_header);
503 index = n_instance_header;
504 *n_obj_header = n_instance_header+1;
505 strcpy(header[index].title,chvar[inum++]);
506 strcpy(header[index].version,chvar[inum++]);
507 strcpy(header[index].namemaxindex,chvar[inum++]);
508 header[index].maxmult = rvar[inum++];
509 header[index].orgstyle = rvar[inum++];
510 header[index].nvar = rvar[inum++];
511 n_instance_header++;
512}
513if ( !strcmp(varname,"TEMPLATE_variable") ) {
514 inum = 0; iok = 1;
515 getmembers(n_el_variable);
516 index = n_instance_variable;
517 *n_obj_variable = n_instance_variable+1;
518 strcpy(variable[index].name,chvar[inum++]);
519 strcpy(variable[index].description,chvar[inum++]);
520 variable[index].type = rvar[inum++];
521 strcpy(variable[index].isfixedsize,chvar[inum++]);
522 variable[index].numdim = rvar[inum++];
523 for (i=0;i<5;i++) variable[index].dimensions[i] = rvar[inum++];
524 n_instance_variable++;
525}
526}
Note: See TracBrowser for help on using the repository browser.