changed reading hint
[gromacs/adressmacs.git] / src / ngmx / fgrid.c
blob60e6a331bc1e2dd404c2c5958a00b93332f49adc
1 /*
2 * $Id$
3 *
4 * This source code is part of
5 *
6 * G R O M A C S
7 *
8 * GROningen MAchine for Chemical Simulations
9 *
10 * VERSION 2.0
12 * Copyright (c) 1991-1999
13 * BIOSON Research Institute, Dept. of Biophysical Chemistry
14 * University of Groningen, The Netherlands
16 * Please refer to:
17 * GROMACS: A message-passing parallel molecular dynamics implementation
18 * H.J.C. Berendsen, D. van der Spoel and R. van Drunen
19 * Comp. Phys. Comm. 91, 43-56 (1995)
21 * Also check out our WWW page:
22 * http://md.chem.rug.nl/~gmx
23 * or e-mail to:
24 * gromacs@chem.rug.nl
26 * And Hey:
27 * Great Red Oystrich Makes All Chemists Sane
29 static char *SRCID_fgrid_c = "$Id$";
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <string2.h>
35 #include <ctype.h>
36 #include <smalloc.h>
37 #include <fgrid.h>
39 static char *type[] = {
40 "button", "radiobuttons", "groupbox", "checkbox",
41 "pixmap", "statictext", "edittext", "defbutton"
44 void ReadDlgError(char *infile,eDLGERR err,char *s,char *file,int line)
46 fprintf(stderr,"Error: ");
47 switch(err) {
48 case eNOVALS:
49 fprintf(stderr,"Not enough values for %s",s);
50 break;
51 case eGRIDEXP:
52 fprintf(stderr,"'grid' expected instead of %s",s);
53 break;
54 case eACCOEXP:
55 fprintf(stderr,"'{' expected instead of %s",s);
56 break;
57 case eACCCEXP:
58 fprintf(stderr,"'}' expected instead of %s",s);
59 break;
60 case eGRPEXP:
61 fprintf(stderr,"'group' expected instead of %s",s);
62 break;
63 case eITEMEXP:
64 fprintf(stderr,"item expected instead of %s",s);
65 break;
66 case eSAMEPOINT:
67 fprintf(stderr,"grid point for %s already in use",s);
68 break;
69 case eTOOWIDE:
70 fprintf(stderr,"grid too wide for %s",s);
71 break;
72 case eTOOHIGH:
73 fprintf(stderr,"grid too high for %s",s);
74 break;
75 case eQUOTE:
76 fprintf(stderr,"quote expected instead of %s",s);
77 break;
78 default:
79 fprintf(stderr,"????");
80 break;
82 fprintf(stderr," in file %s\n",infile);
83 fprintf(stderr,"C-File: %s, line: %d\n",file,line);
84 exit(1);
87 #define ReadDlgErr(in,er,es) ReadDlgError(in,er,es,__FILE__,__LINE__)
89 static void GetBuf(FILE *in, char *buf)
91 fscanf(in,"%s",buf);
94 static void ReadAccOpen(char *infile, FILE *in)
96 char buf[STRLEN];
98 GetBuf(in,buf);
99 if (strcmp(buf,"{")!=0)
100 ReadDlgErr(infile,eACCOEXP,buf);
103 static void ReadAccClose(char *infile, FILE *in)
105 char buf[STRLEN];
107 GetBuf(in,buf);
108 if (strcmp(buf,"}")!=0)
109 ReadDlgErr(infile,eACCCEXP,buf);
112 void ReadQuoteString(char *infile, FILE *in, char *buf)
114 char c[2];
115 int i=0;
117 /* Read until first quote */
118 while ((c[0]=fgetc(in))!='"')
119 if (!isspace(c[0])) {
120 c[1]='\0';
121 ReadDlgErr(infile,eQUOTE,c);
123 /* Read until second quote */
124 while ((c[0]=fgetc(in))!='"')
125 buf[i++]=c[0];
126 buf[i]='\0';
129 static void ReadQuoteStringOrAccClose(FILE *in, char *buf)
131 char c;
132 int i=0;
134 /* Read until first quote */
135 do {
136 c=fgetc(in);
137 if (c=='}') {
138 buf[0]=c;
139 buf[1]='\0';
140 return;
142 } while (c != '"');
144 /* Read until second quote */
145 while ((c=fgetc(in))!='"')
146 buf[i++]=c;
147 buf[i]='\0';
150 static bool bNotAccClose(char *buf)
152 return (strcmp(buf,"}")!=0);
155 static t_fitem *NewFItem(void)
157 t_fitem *fitem;
159 snew(fitem,1);
160 fitem->nname=0;
161 fitem->name=NULL;
162 fitem->set=NULL;
163 fitem->get=NULL;
164 fitem->def=NULL;
165 fitem->help=NULL;
167 return fitem;
170 static t_fsimple *NewFSimple(void)
172 t_fsimple *fsimple;
174 snew(fsimple,1);
176 return fsimple;
179 static void AddFItemName(t_fitem *fitem, char *name)
181 srenew(fitem->name,++fitem->nname);
182 fitem->name[fitem->nname-1]=strdup(name);
185 static t_fgroup *NewFGroup(void)
187 t_fgroup *fgroup;
189 snew(fgroup,1);
190 fgroup->name=NULL;
191 fgroup->nfitem=0;
192 fgroup->fitem=NULL;
194 return fgroup;
197 static void AddFGroupFItem(t_fgroup *fgroup, t_fitem *fitem)
199 srenew(fgroup->fitem,++fgroup->nfitem);
200 fgroup->fitem[fgroup->nfitem-1]=fitem;
203 static t_fgroup *AddFGridFGroup(t_fgrid *fgrid)
205 srenew(fgrid->fgroup,++fgrid->nfgroup);
206 fgrid->fgroup[fgrid->nfgroup-1]=NewFGroup();
207 return fgrid->fgroup[fgrid->nfgroup-1];
210 static t_fsimple *AddFGridFSimple(t_fgrid *fgrid)
212 srenew(fgrid->fsimple,++fgrid->nfsimple);
213 fgrid->fsimple[fgrid->nfsimple-1]=NewFSimple();
214 return fgrid->fsimple[fgrid->nfsimple-1];
217 static t_fgrid *NewFGrid(void)
219 t_fgrid *fgrid;
221 snew(fgrid,1);
222 fgrid->w=0;
223 fgrid->h=0;
224 fgrid->nfgroup=0;
225 fgrid->fgroup=NULL;
226 fgrid->nfsimple=0;
227 fgrid->fsimple=NULL;
229 return fgrid;
232 static void DoneFItem(t_fitem *fitem)
234 int i;
236 for(i=0; (i<fitem->nname); i++)
237 sfree(fitem->name[i]);
238 sfree(fitem->name);
239 sfree(fitem->set);
240 sfree(fitem->get);
241 sfree(fitem->def);
242 sfree(fitem->help);
245 static void DoneFGroup(t_fgroup *fgroup)
247 int i;
249 sfree(fgroup->name);
250 for(i=0; (i<fgroup->nfitem); i++)
251 DoneFItem(fgroup->fitem[i]);
252 sfree(fgroup->fitem);
255 static void DoneFSimple(t_fsimple *fsimple)
257 DoneFItem(fsimple->fitem);
258 sfree(fsimple->fitem);
261 void DoneFGrid(t_fgrid *fgrid)
263 int i;
265 for(i=0; (i<fgrid->nfgroup); i++)
266 DoneFGroup(fgrid->fgroup[i]);
267 sfree(fgrid->fgroup);
268 for(i=0; (i<fgrid->nfsimple); i++)
269 DoneFSimple(fgrid->fsimple[i]);
270 sfree(fgrid->fsimple);
273 static t_fitem *ScanFItem(char *infile, FILE *in, char *buf)
275 char set[STRLEN],get[STRLEN],help[STRLEN],def[STRLEN];
276 edlgitem edlg;
277 t_fitem *fitem;
279 fitem=NewFItem();
281 for(edlg=(edlgitem)0; (edlg<edlgNR+1); edlg++)
282 if (strcmp(buf,type[edlg])==0)
283 break;
284 if (edlg==edlgNR) {
285 /* Special case */
286 edlg=edlgBN;
287 fitem->bDef=TRUE;
289 if (edlg==edlgNR+1) {
290 ReadDlgErr(infile,eITEMEXP,buf);
293 fitem->edlg=edlg;
294 switch (edlg) {
295 case edlgBN:
296 case edlgCB:
297 case edlgET:
298 ReadQuoteString(infile,in,buf);
299 AddFItemName(fitem,buf);
300 break;
301 case edlgST:
302 case edlgRB:
303 ReadAccOpen(infile,in);
304 ReadQuoteStringOrAccClose(in,buf);
305 while (bNotAccClose(buf)) {
306 AddFItemName(fitem,buf);
307 ReadQuoteStringOrAccClose(in,buf);
309 break;
310 case edlgPM:
311 case edlgGB:
312 ReadDlgErr(infile,eITEMEXP,type[edlg]);
313 break;
314 default:
315 break;
317 ReadQuoteString(infile,in,set);
318 ReadQuoteString(infile,in,get);
319 ReadQuoteString(infile,in,def);
320 ReadQuoteString(infile,in,help);
321 fitem->set=strdup(set);
322 fitem->get=strdup(get);
323 fitem->def=strdup(def);
324 fitem->help=strdup(help);
326 return fitem;
329 t_fgrid *FGridFromFile(char *infile)
331 FILE *in;
332 char buf[STRLEN];
333 char *gmxlib;
334 char newinfile[STRLEN];
336 t_fgrid *fgrid;
337 t_fgroup *fgroup;
338 t_fsimple *fsimple;
339 int gridx,gridy;
341 if ((in=fopen(infile,"r"))==NULL) {
342 /* Try in the GMXLIB directory */
343 if ((gmxlib=getenv("GMXLIB"))==NULL) {
344 fprintf(stderr,"No GMXLIB environment variable and no %s dlg-script"
345 "in current directory\n",infile);
346 exit(1);
348 sprintf(newinfile,"%s/%s",gmxlib,infile);
349 if ((in=fopen(newinfile,"r"))==NULL) {
350 perror(infile);
351 exit(1);
354 GetBuf(in,buf);
355 if (strcmp(buf,"grid")!=0)
356 ReadDlgErr(infile,eGRIDEXP,buf);
357 fgrid=NewFGrid();
358 if ((fscanf(in,"%d%d",&gridx,&gridy))!=2)
359 ReadDlgErr(infile,eNOVALS,"grid w,h");
360 fgrid->w=gridx;
361 fgrid->h=gridy;
362 ReadAccOpen(infile,in);
363 GetBuf(in,buf);
364 while (bNotAccClose(buf)) {
365 if (strcmp(buf,"group")==0) {
366 fgroup=AddFGridFGroup(fgrid);
367 ReadQuoteString(infile,in,buf);
368 fgroup->name=strdup(buf);
369 if ((fscanf(in,"%d%d%d%d",&fgroup->x,&fgroup->y,&fgroup->w,&fgroup->h))!=4)
370 ReadDlgErr(infile,eNOVALS,"group x,y,w,h");
371 if (fgroup->x+fgroup->w > gridx)
372 ReadDlgErr(infile,eTOOWIDE,buf);
373 if (fgroup->y+fgroup->h > gridy)
374 ReadDlgErr(infile,eTOOHIGH,buf);
375 ReadAccOpen(infile,in);
376 GetBuf(in,buf);
377 while (bNotAccClose(buf)) {
378 AddFGroupFItem(fgroup,ScanFItem(infile,in,buf));
379 GetBuf(in,buf);
382 else if (strcmp(buf,"simple")==0) {
383 fsimple=AddFGridFSimple(fgrid);
384 if ((fscanf(in,"%d%d%d%d",&fsimple->x,&fsimple->y,&fsimple->w,&fsimple->h))!=4)
385 ReadDlgErr(infile,eNOVALS,"simple x,y,w,h");
386 if (fsimple->x+fsimple->w > gridx)
387 ReadDlgErr(infile,eTOOWIDE,"simple");
388 if (fsimple->y+fsimple->h > gridy)
389 ReadDlgErr(infile,eTOOHIGH,"simple");
390 ReadAccOpen(infile,in);
391 GetBuf(in,buf);
392 fsimple->fitem=ScanFItem(infile,in,buf);
393 ReadAccClose(infile,in);
395 GetBuf(in,buf);
397 fclose(in);
399 return fgrid;
402 static void DumpFItem(t_fitem *fitem)
404 int i;
406 printf(" type: %s, set: '%s', get: '%s', def: '%s', help: '%s'\n {",
407 type[fitem->edlg],fitem->set,fitem->get,fitem->def,fitem->help);
408 for(i=0; (i<fitem->nname); i++)
409 printf(" '%s'",fitem->name[i]);
410 printf(" }\n");
413 static void DumpFSimple(t_fsimple *fsimple)
415 printf("Simple %dx%d at %d,%d\n",fsimple->w,fsimple->h,fsimple->x,fsimple->y);
416 DumpFItem(fsimple->fitem);
419 static void DumpFGroup(t_fgroup *fgroup)
421 int i;
423 printf("Group %dx%d at %d,%d\n",fgroup->w,fgroup->h,fgroup->x,fgroup->y);
424 for(i=0; (i<fgroup->nfitem); i++)
425 DumpFItem(fgroup->fitem[i]);
428 void DumpFGrid(t_fgrid *fgrid)
430 int i;
432 printf("Grid %dx%d\n",fgrid->w,fgrid->h);
433 for(i=0; (i<fgrid->nfgroup); i++)
434 DumpFGroup(fgrid->fgroup[i]);
435 for(i=0; (i<fgrid->nfsimple); i++)
436 DumpFSimple(fgrid->fsimple[i]);