e4d01b6da19e23c46f02a119b0db6472f0a7effe
[alexxy/gromacs.git] / src / ngmx / fgrid.c
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
11  * 
12  * Copyright (c) 1991-1999
13  * BIOSON Research Institute, Dept. of Biophysical Chemistry
14  * University of Groningen, The Netherlands
15  * 
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)
20  * 
21  * Also check out our WWW page:
22  * http://md.chem.rug.nl/~gmx
23  * or e-mail to:
24  * gromacs@chem.rug.nl
25  * 
26  * And Hey:
27  * Great Red Oystrich Makes All Chemists Sane
28  */
29 static char *SRCID_fgrid_c = "$Id$";
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <ctype.h>
35 #include "string2.h"
36 #include "smalloc.h"
37 #include "fgrid.h"
38 #include "futil.h"
39
40 static char *type[] = { 
41   "button", "radiobuttons", "groupbox", "checkbox",
42   "pixmap", "statictext",   "edittext", "defbutton"
43   };
44
45 void ReadDlgError(char *infile,eDLGERR err,char *s,char *file,int line)
46 {
47   fprintf(stderr,"Error: ");
48   switch(err) {
49   case eNOVALS:
50     fprintf(stderr,"Not enough values for %s",s);
51     break;
52   case eGRIDEXP:
53     fprintf(stderr,"'grid' expected instead of %s",s);
54     break;
55   case eACCOEXP:
56     fprintf(stderr,"'{' expected instead of %s",s);
57     break;
58   case eACCCEXP:
59     fprintf(stderr,"'}' expected instead of %s",s);
60     break;
61   case eGRPEXP:
62     fprintf(stderr,"'group' expected instead of %s",s);
63     break;
64   case eITEMEXP:
65     fprintf(stderr,"item expected instead of %s",s);
66     break;
67   case eSAMEPOINT:
68     fprintf(stderr,"grid point for %s already in use",s);
69     break;
70   case eTOOWIDE:
71     fprintf(stderr,"grid too wide for %s",s);
72     break;
73   case eTOOHIGH:
74     fprintf(stderr,"grid too high for %s",s);
75     break;
76   case eQUOTE:
77     fprintf(stderr,"quote expected instead of %s",s);
78     break;
79   default:
80     fprintf(stderr,"????");
81     break;
82   }
83   fprintf(stderr," in file %s\n",infile);
84   fprintf(stderr,"C-File: %s, line: %d\n",file,line);
85   exit(1);
86 }
87
88 #define ReadDlgErr(in,er,es) ReadDlgError(in,er,es,__FILE__,__LINE__)
89
90 static void GetBuf(FILE *in, char *buf)
91 {
92   fscanf(in,"%s",buf);
93 }
94
95 static void ReadAccOpen(char *infile, FILE *in)
96 {
97   char buf[STRLEN];
98   
99   GetBuf(in,buf);
100   if (strcmp(buf,"{")!=0)
101     ReadDlgErr(infile,eACCOEXP,buf);
102 }
103
104 static void ReadAccClose(char *infile, FILE *in)
105 {
106   char buf[STRLEN];
107   
108   GetBuf(in,buf);
109   if (strcmp(buf,"}")!=0)
110     ReadDlgErr(infile,eACCCEXP,buf);
111 }
112
113 void ReadQuoteString(char *infile, FILE *in, char *buf)
114 {
115   char c[2];
116   int i=0;
117   
118   /* Read until first quote */
119   while ((c[0]=fgetc(in))!='"') 
120     if (!isspace(c[0])) {
121       c[1]='\0';
122       ReadDlgErr(infile,eQUOTE,c);
123     }
124   /* Read until second quote */
125   while ((c[0]=fgetc(in))!='"')
126     buf[i++]=c[0];
127   buf[i]='\0';
128 }
129
130 static void ReadQuoteStringOrAccClose(FILE *in, char *buf)
131 {
132   char c;
133   int i=0;
134   
135   /* Read until first quote */
136   do {
137     c=fgetc(in);
138     if (c=='}') {
139       buf[0]=c;
140       buf[1]='\0';
141       return;
142     }
143   } while (c != '"');
144   
145   /* Read until second quote */
146   while ((c=fgetc(in))!='"')
147     buf[i++]=c;
148   buf[i]='\0';
149 }
150
151 static bool bNotAccClose(char *buf)
152 {
153   return (strcmp(buf,"}")!=0);
154 }
155
156 static t_fitem *NewFItem(void)
157 {
158   t_fitem *fitem;
159   
160   snew(fitem,1);
161   fitem->nname=0;
162   fitem->name=NULL;
163   fitem->set=NULL;
164   fitem->get=NULL;
165   fitem->def=NULL;
166   fitem->help=NULL;
167
168   return fitem;
169 }
170
171 static t_fsimple *NewFSimple(void)
172 {
173   t_fsimple *fsimple;
174   
175   snew(fsimple,1);
176   
177   return fsimple;
178 }
179
180 static void AddFItemName(t_fitem *fitem, char *name)
181 {
182   srenew(fitem->name,++fitem->nname);
183   fitem->name[fitem->nname-1]=strdup(name);
184 }
185
186 static t_fgroup *NewFGroup(void)
187 {
188   t_fgroup *fgroup;
189   
190   snew(fgroup,1);
191   fgroup->name=NULL;
192   fgroup->nfitem=0;
193   fgroup->fitem=NULL;
194   
195   return fgroup;
196 }
197
198 static void AddFGroupFItem(t_fgroup *fgroup, t_fitem *fitem)
199 {
200   srenew(fgroup->fitem,++fgroup->nfitem);
201   fgroup->fitem[fgroup->nfitem-1]=fitem;
202 }
203
204 static t_fgroup *AddFGridFGroup(t_fgrid *fgrid)
205 {
206   srenew(fgrid->fgroup,++fgrid->nfgroup);
207   fgrid->fgroup[fgrid->nfgroup-1]=NewFGroup();
208   return fgrid->fgroup[fgrid->nfgroup-1];
209 }
210
211 static t_fsimple *AddFGridFSimple(t_fgrid *fgrid)
212 {
213   srenew(fgrid->fsimple,++fgrid->nfsimple);
214   fgrid->fsimple[fgrid->nfsimple-1]=NewFSimple();
215   return fgrid->fsimple[fgrid->nfsimple-1];
216 }
217
218 static t_fgrid *NewFGrid(void)
219 {
220   t_fgrid *fgrid;
221   
222   snew(fgrid,1);
223   fgrid->w=0;
224   fgrid->h=0;
225   fgrid->nfgroup=0;
226   fgrid->fgroup=NULL;
227   fgrid->nfsimple=0;
228   fgrid->fsimple=NULL;
229   
230   return fgrid;
231 }
232
233 static void DoneFItem(t_fitem *fitem)
234 {
235   int i;
236   
237   for(i=0; (i<fitem->nname); i++)
238     sfree(fitem->name[i]);
239   sfree(fitem->name);
240   sfree(fitem->set);
241   sfree(fitem->get);
242   sfree(fitem->def);
243   sfree(fitem->help);
244 }
245
246 static void DoneFGroup(t_fgroup *fgroup)
247 {
248   int i;
249   
250   sfree(fgroup->name);
251   for(i=0; (i<fgroup->nfitem); i++)
252     DoneFItem(fgroup->fitem[i]);
253   sfree(fgroup->fitem);
254 }
255
256 static void DoneFSimple(t_fsimple *fsimple)
257 {
258   DoneFItem(fsimple->fitem);
259   sfree(fsimple->fitem);
260 }
261
262 void DoneFGrid(t_fgrid *fgrid)
263 {
264   int i;
265   
266   for(i=0; (i<fgrid->nfgroup); i++) 
267     DoneFGroup(fgrid->fgroup[i]);
268   sfree(fgrid->fgroup);
269   for(i=0; (i<fgrid->nfsimple); i++) 
270     DoneFSimple(fgrid->fsimple[i]);
271   sfree(fgrid->fsimple);
272 }
273
274 static t_fitem *ScanFItem(char *infile, FILE *in, char *buf)
275 {
276   char set[STRLEN],get[STRLEN],help[STRLEN],def[STRLEN];
277   edlgitem edlg;
278   t_fitem *fitem;
279   
280   fitem=NewFItem();
281   
282   for(edlg=(edlgitem)0; (edlg<edlgNR+1); edlg++)
283     if (strcmp(buf,type[edlg])==0)
284       break;
285   if (edlg==edlgNR) {
286     /* Special case */
287     edlg=edlgBN;
288     fitem->bDef=TRUE;
289   }
290   if (edlg==edlgNR+1) {
291     ReadDlgErr(infile,eITEMEXP,buf);
292   }
293   
294   fitem->edlg=edlg;
295   switch (edlg) {
296   case edlgBN:
297   case edlgCB:
298   case edlgET:
299     ReadQuoteString(infile,in,buf);
300     AddFItemName(fitem,buf);
301     break;
302   case edlgST:
303   case edlgRB:
304     ReadAccOpen(infile,in);
305     ReadQuoteStringOrAccClose(in,buf);
306     while (bNotAccClose(buf)) {
307       AddFItemName(fitem,buf);
308       ReadQuoteStringOrAccClose(in,buf);
309     }
310     break;
311   case edlgPM:
312   case edlgGB:
313     ReadDlgErr(infile,eITEMEXP,type[edlg]);
314     break;
315   default:
316     break;
317   }
318   ReadQuoteString(infile,in,set);
319   ReadQuoteString(infile,in,get);
320   ReadQuoteString(infile,in,def);
321   ReadQuoteString(infile,in,help);
322   fitem->set=strdup(set);
323   fitem->get=strdup(get);
324   fitem->def=strdup(def);
325   fitem->help=strdup(help);
326   
327   return fitem;
328 }
329
330 t_fgrid *FGridFromFile(char *infile)
331 {
332   FILE *in;
333   char buf[STRLEN];
334   char *gmxlib;
335   char newinfile[STRLEN];
336   
337   t_fgrid   *fgrid;
338   t_fgroup  *fgroup;
339   t_fsimple *fsimple;
340   int       gridx,gridy;  
341   
342   in = libopen(infile);
343   GetBuf(in,buf);
344   if (strcmp(buf,"grid")!=0)
345     ReadDlgErr(infile,eGRIDEXP,buf);
346   fgrid=NewFGrid();
347   if ((fscanf(in,"%d%d",&gridx,&gridy))!=2)
348     ReadDlgErr(infile,eNOVALS,"grid w,h");
349   fgrid->w=gridx;
350   fgrid->h=gridy;
351   ReadAccOpen(infile,in);
352   GetBuf(in,buf);
353   while (bNotAccClose(buf)) {
354     if (strcmp(buf,"group")==0) {
355       fgroup=AddFGridFGroup(fgrid);
356       ReadQuoteString(infile,in,buf);
357       fgroup->name=strdup(buf);
358       if ((fscanf(in,"%d%d%d%d",&fgroup->x,&fgroup->y,&fgroup->w,&fgroup->h))!=4)
359         ReadDlgErr(infile,eNOVALS,"group x,y,w,h");
360       if (fgroup->x+fgroup->w > gridx)
361         ReadDlgErr(infile,eTOOWIDE,buf);
362       if (fgroup->y+fgroup->h > gridy)
363         ReadDlgErr(infile,eTOOHIGH,buf);
364       ReadAccOpen(infile,in);
365       GetBuf(in,buf);
366       while (bNotAccClose(buf)) {
367         AddFGroupFItem(fgroup,ScanFItem(infile,in,buf));
368         GetBuf(in,buf);
369       }
370     }
371     else if (strcmp(buf,"simple")==0) {
372       fsimple=AddFGridFSimple(fgrid);
373       if ((fscanf(in,"%d%d%d%d",&fsimple->x,&fsimple->y,&fsimple->w,&fsimple->h))!=4)
374         ReadDlgErr(infile,eNOVALS,"simple x,y,w,h");
375       if (fsimple->x+fsimple->w > gridx)
376         ReadDlgErr(infile,eTOOWIDE,"simple");
377       if (fsimple->y+fsimple->h > gridy)
378         ReadDlgErr(infile,eTOOHIGH,"simple");
379       ReadAccOpen(infile,in);
380       GetBuf(in,buf);
381       fsimple->fitem=ScanFItem(infile,in,buf);
382       ReadAccClose(infile,in);
383     }
384     GetBuf(in,buf);
385   }
386   fclose(in);
387   
388   return fgrid;
389 }
390
391 static void DumpFItem(t_fitem *fitem)
392 {
393   int i;
394   
395   printf("  type: %s, set: '%s', get: '%s', def: '%s', help: '%s'\n  {",
396          type[fitem->edlg],fitem->set,fitem->get,fitem->def,fitem->help);
397   for(i=0; (i<fitem->nname); i++)
398     printf("  '%s'",fitem->name[i]);
399   printf("  }\n");
400 }
401
402 static void DumpFSimple(t_fsimple *fsimple)
403 {
404   printf("Simple %dx%d at %d,%d\n",fsimple->w,fsimple->h,fsimple->x,fsimple->y);
405   DumpFItem(fsimple->fitem);
406 }
407
408 static void DumpFGroup(t_fgroup *fgroup)
409 {
410   int i;
411   
412   printf("Group %dx%d at %d,%d\n",fgroup->w,fgroup->h,fgroup->x,fgroup->y);
413   for(i=0; (i<fgroup->nfitem); i++)
414     DumpFItem(fgroup->fitem[i]);
415 }
416
417 void DumpFGrid(t_fgrid *fgrid)
418 {
419   int i;
420   
421   printf("Grid %dx%d\n",fgrid->w,fgrid->h);
422   for(i=0; (i<fgrid->nfgroup); i++)
423     DumpFGroup(fgrid->fgroup[i]);
424   for(i=0; (i<fgrid->nfsimple); i++)
425     DumpFSimple(fgrid->fsimple[i]);
426 }