ae840e1f005e4cbcd4f1b3af5e6667b0c20facff
[alexxy/gromacs.git] / src / programs / view / fgrid.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2013, The GROMACS development team.
6  * Copyright (c) 2013,2014, by the GROMACS development team, led by
7  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8  * and including many others, as listed in the AUTHORS file in the
9  * top-level source directory and at http://www.gromacs.org.
10  *
11  * GROMACS is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public License
13  * as published by the Free Software Foundation; either version 2.1
14  * of the License, or (at your option) any later version.
15  *
16  * GROMACS is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with GROMACS; if not, see
23  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
25  *
26  * If you want to redistribute modifications to GROMACS, please
27  * consider that scientific software is very special. Version
28  * control is crucial - bugs must be traceable. We will be happy to
29  * consider code for inclusion in the official distribution, but
30  * derived work must not be called official GROMACS. Details are found
31  * in the README & COPYING files - if they are missing, get the
32  * official version at http://www.gromacs.org.
33  *
34  * To help us fund GROMACS development, we humbly ask that you cite
35  * the research papers on the package. Check out http://www.gromacs.org.
36  */
37 #include "gmxpre.h"
38
39 #include "config.h"
40
41 #include <ctype.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45
46 #include "fgrid.h"
47
48 #include "gromacs/utility/cstringutil.h"
49 #include "gromacs/utility/futil.h"
50 #include "gromacs/utility/smalloc.h"
51
52 static const char *type[] = {
53     "button", "radiobuttons", "groupbox", "checkbox",
54     "pixmap", "statictext",   "edittext", "defbutton"
55 };
56
57 void ReadDlgError(const char *infile, eDLGERR err, const char *s,
58                   const char *file, int line)
59 {
60     fprintf(stderr, "Error: ");
61     switch (err)
62     {
63         case eNOVALS:
64             fprintf(stderr, "Not enough values for %s", s);
65             break;
66         case eGRIDEXP:
67             fprintf(stderr, "'grid' expected instead of %s", s);
68             break;
69         case eACCOEXP:
70             fprintf(stderr, "'{' expected instead of %s", s);
71             break;
72         case eACCCEXP:
73             fprintf(stderr, "'}' expected instead of %s", s);
74             break;
75         case eGRPEXP:
76             fprintf(stderr, "'group' expected instead of %s", s);
77             break;
78         case eITEMEXP:
79             fprintf(stderr, "item expected instead of %s", s);
80             break;
81         case eSAMEPOINT:
82             fprintf(stderr, "grid point for %s already in use", s);
83             break;
84         case eTOOWIDE:
85             fprintf(stderr, "grid too wide for %s", s);
86             break;
87         case eTOOHIGH:
88             fprintf(stderr, "grid too high for %s", s);
89             break;
90         case eQUOTE:
91             fprintf(stderr, "quote expected instead of %s", s);
92             break;
93         default:
94             fprintf(stderr, "????");
95             break;
96     }
97     fprintf(stderr, " in file %s\n", infile);
98     fprintf(stderr, "source file: %s, line: %d\n", file, line);
99     exit(1);
100 }
101
102 #define ReadDlgErr(in, er, es) ReadDlgError(in, er, es, __FILE__, __LINE__)
103
104 static void ReadAccOpen(const char *infile, FILE *in)
105 {
106     char buf[STRLEN];
107     int  result;
108
109     result = fscanf(in, "%4s", buf);
110     if ((1 != result) || strcmp(buf, "{") != 0)
111     {
112         ReadDlgErr(infile, eACCOEXP, buf);
113     }
114 }
115
116 static void ReadAccClose(const char *infile, FILE *in)
117 {
118     char buf[STRLEN];
119     int  result;
120
121     result = fscanf(in, "%4s", buf);
122     if ((1 != result) || strcmp(buf, "}") != 0)
123     {
124         ReadDlgErr(infile, eACCCEXP, buf);
125     }
126 }
127
128 void ReadQuoteString(const char *infile, FILE *in, char *buf)
129 {
130     char c[2];
131     int  i = 0;
132
133     /* Read until first quote */
134     while ((c[0] = fgetc(in)) != '"')
135     {
136         if (!isspace(c[0]))
137         {
138             c[1] = '\0';
139             ReadDlgErr(infile, eQUOTE, c);
140         }
141     }
142     /* Read until second quote */
143     while ((c[0] = fgetc(in)) != '"')
144     {
145         buf[i++] = c[0];
146     }
147     buf[i] = '\0';
148 }
149
150 static void ReadQuoteStringOrAccClose(FILE *in, char *buf)
151 {
152     char c;
153     int  i = 0;
154
155     /* Read until first quote */
156     do
157     {
158         c = fgetc(in);
159         if (c == '}')
160         {
161             buf[0] = c;
162             buf[1] = '\0';
163             return;
164         }
165     }
166     while (c != '"');
167
168     /* Read until second quote */
169     while ((c = fgetc(in)) != '"')
170     {
171         buf[i++] = c;
172     }
173     buf[i] = '\0';
174 }
175
176 static bool bNotAccClose(const char *buf)
177 {
178     return (strcmp(buf, "}") != 0);
179 }
180
181 static t_fitem *NewFItem(void)
182 {
183     t_fitem *fitem;
184
185     snew(fitem, 1);
186     fitem->nname = 0;
187     fitem->name  = NULL;
188     fitem->set   = NULL;
189     fitem->get   = NULL;
190     fitem->def   = NULL;
191     fitem->help  = NULL;
192
193     return fitem;
194 }
195
196 static t_fsimple *NewFSimple(void)
197 {
198     t_fsimple *fsimple;
199
200     snew(fsimple, 1);
201
202     return fsimple;
203 }
204
205 static void AddFItemName(t_fitem *fitem, char *name)
206 {
207     srenew(fitem->name, ++fitem->nname);
208     fitem->name[fitem->nname-1] = gmx_strdup(name);
209 }
210
211 static t_fgroup *NewFGroup(void)
212 {
213     t_fgroup *fgroup;
214
215     snew(fgroup, 1);
216     fgroup->name   = NULL;
217     fgroup->nfitem = 0;
218     fgroup->fitem  = NULL;
219
220     return fgroup;
221 }
222
223 static void AddFGroupFItem(t_fgroup *fgroup, t_fitem *fitem)
224 {
225     srenew(fgroup->fitem, ++fgroup->nfitem);
226     fgroup->fitem[fgroup->nfitem-1] = fitem;
227 }
228
229 static t_fgroup *AddFGridFGroup(t_fgrid *fgrid)
230 {
231     srenew(fgrid->fgroup, ++fgrid->nfgroup);
232     fgrid->fgroup[fgrid->nfgroup-1] = NewFGroup();
233     return fgrid->fgroup[fgrid->nfgroup-1];
234 }
235
236 static t_fsimple *AddFGridFSimple(t_fgrid *fgrid)
237 {
238     srenew(fgrid->fsimple, ++fgrid->nfsimple);
239     fgrid->fsimple[fgrid->nfsimple-1] = NewFSimple();
240     return fgrid->fsimple[fgrid->nfsimple-1];
241 }
242
243 static t_fgrid *NewFGrid(void)
244 {
245     t_fgrid *fgrid;
246
247     snew(fgrid, 1);
248     fgrid->w        = 0;
249     fgrid->h        = 0;
250     fgrid->nfgroup  = 0;
251     fgrid->fgroup   = NULL;
252     fgrid->nfsimple = 0;
253     fgrid->fsimple  = NULL;
254
255     return fgrid;
256 }
257
258 static void DoneFItem(t_fitem *fitem)
259 {
260     int i;
261
262     for (i = 0; (i < fitem->nname); i++)
263     {
264         sfree(fitem->name[i]);
265     }
266     sfree(fitem->name);
267     sfree(fitem->set);
268     sfree(fitem->get);
269     sfree(fitem->def);
270     sfree(fitem->help);
271 }
272
273 static void DoneFGroup(t_fgroup *fgroup)
274 {
275     int i;
276
277     sfree(fgroup->name);
278     for (i = 0; (i < fgroup->nfitem); i++)
279     {
280         DoneFItem(fgroup->fitem[i]);
281     }
282     sfree(fgroup->fitem);
283 }
284
285 static void DoneFSimple(t_fsimple *fsimple)
286 {
287     DoneFItem(fsimple->fitem);
288     sfree(fsimple->fitem);
289 }
290
291 void DoneFGrid(t_fgrid *fgrid)
292 {
293     int i;
294
295     for (i = 0; (i < fgrid->nfgroup); i++)
296     {
297         DoneFGroup(fgrid->fgroup[i]);
298     }
299     sfree(fgrid->fgroup);
300     for (i = 0; (i < fgrid->nfsimple); i++)
301     {
302         DoneFSimple(fgrid->fsimple[i]);
303     }
304     sfree(fgrid->fsimple);
305 }
306
307 static t_fitem *ScanFItem(const char *infile, FILE *in, char *buf)
308 {
309     char     set[STRLEN], get[STRLEN], help[STRLEN], def[STRLEN];
310     int      edlg;
311     t_fitem *fitem;
312
313     fitem = NewFItem();
314
315     for (edlg = 0; (edlg < edlgNR+1); edlg++)
316     {
317         if (strcmp(buf, type[edlg]) == 0)
318         {
319             break;
320         }
321     }
322     if (edlg == edlgNR)
323     {
324         /* Special case */
325         edlg        = edlgBN;
326         fitem->bDef = true;
327     }
328     if (edlg == edlgNR+1)
329     {
330         ReadDlgErr(infile, eITEMEXP, buf);
331     }
332
333     fitem->edlg = (edlgitem)edlg;
334     switch (edlg)
335     {
336         case edlgBN:
337         case edlgCB:
338         case edlgET:
339             ReadQuoteString(infile, in, buf);
340             AddFItemName(fitem, buf);
341             break;
342         case edlgST:
343         case edlgRB:
344             ReadAccOpen(infile, in);
345             ReadQuoteStringOrAccClose(in, buf);
346             while (bNotAccClose(buf))
347             {
348                 AddFItemName(fitem, buf);
349                 ReadQuoteStringOrAccClose(in, buf);
350             }
351             break;
352         case edlgPM:
353         case edlgGB:
354             ReadDlgErr(infile, eITEMEXP, type[edlg]);
355             break;
356         default:
357             break;
358     }
359     ReadQuoteString(infile, in, set);
360     ReadQuoteString(infile, in, get);
361     ReadQuoteString(infile, in, def);
362     ReadQuoteString(infile, in, help);
363     fitem->set  = gmx_strdup(set);
364     fitem->get  = gmx_strdup(get);
365     fitem->def  = gmx_strdup(def);
366     fitem->help = gmx_strdup(help);
367
368     return fitem;
369 }
370
371 t_fgrid *FGridFromFile(const char *infile)
372 {
373     FILE      *in;
374     char       buf[STRLEN];
375     int        result;
376
377     t_fgrid   *fgrid;
378     t_fgroup  *fgroup;
379     t_fsimple *fsimple;
380     int        gridx, gridy;
381
382     in     = libopen(infile);
383     result = fscanf(in, "%6s", buf);
384     if ((1 != result) || strcmp(buf, "grid") != 0)
385     {
386         ReadDlgErr(infile, eGRIDEXP, buf);
387     }
388     fgrid = NewFGrid();
389     if ((fscanf(in, "%5d%5d", &gridx, &gridy)) != 2)
390     {
391         ReadDlgErr(infile, eNOVALS, "grid w,h");
392     }
393     fgrid->w = gridx;
394     fgrid->h = gridy;
395     ReadAccOpen(infile, in);
396     result = fscanf(in, "%15s", buf);
397     while ((1 == result) && bNotAccClose(buf))
398     {
399         if (strcmp(buf, "group") == 0)
400         {
401             fgroup = AddFGridFGroup(fgrid);
402             ReadQuoteString(infile, in, buf);
403             fgroup->name = gmx_strdup(buf);
404             if ((fscanf(in, "%5d%5d%5d%5d", &fgroup->x, &fgroup->y, &fgroup->w, &fgroup->h)) != 4)
405             {
406                 ReadDlgErr(infile, eNOVALS, "group x,y,w,h");
407             }
408             if (fgroup->x+fgroup->w > gridx)
409             {
410                 ReadDlgErr(infile, eTOOWIDE, buf);
411             }
412             if (fgroup->y+fgroup->h > gridy)
413             {
414                 ReadDlgErr(infile, eTOOHIGH, buf);
415             }
416             ReadAccOpen(infile, in);
417             result = fscanf(in, "%15s", buf);
418             while ((1 == result) && bNotAccClose(buf))
419             {
420                 AddFGroupFItem(fgroup, ScanFItem(infile, in, buf));
421                 result = fscanf(in, "%15s", buf);
422             }
423         }
424         else if (strcmp(buf, "simple") == 0)
425         {
426             fsimple = AddFGridFSimple(fgrid);
427             if ((fscanf(in, "%5d%5d%5d%5d", &fsimple->x, &fsimple->y, &fsimple->w, &fsimple->h)) != 4)
428             {
429                 ReadDlgErr(infile, eNOVALS, "simple x,y,w,h");
430             }
431             if (fsimple->x+fsimple->w > gridx)
432             {
433                 ReadDlgErr(infile, eTOOWIDE, "simple");
434             }
435             if (fsimple->y+fsimple->h > gridy)
436             {
437                 ReadDlgErr(infile, eTOOHIGH, "simple");
438             }
439             ReadAccOpen(infile, in);
440             result = fscanf(in, "%15s", buf);
441             if (1 == result)
442             {
443                 fsimple->fitem = ScanFItem(infile, in, buf);
444                 ReadAccClose(infile, in);
445             }
446         }
447         if (1 == result)
448         {
449             result = fscanf(in, "%15s", buf);
450         }
451     }
452     gmx_ffclose(in);
453     /* Since we always read one variable at a time the result from
454      * fscanf should always be 1.
455      */
456     if (1 != result)
457     {
458         ReadDlgErr(infile, eNOVALS, "fgrid");
459     }
460
461     return fgrid;
462 }
463
464 static void DumpFItem(t_fitem *fitem)
465 {
466     int i;
467
468     printf("  type: %s, set: '%s', get: '%s', def: '%s', help: '%s'\n  {",
469            type[fitem->edlg], fitem->set, fitem->get, fitem->def, fitem->help);
470     for (i = 0; (i < fitem->nname); i++)
471     {
472         printf("  '%s'", fitem->name[i]);
473     }
474     printf("  }\n");
475 }
476
477 static void DumpFSimple(t_fsimple *fsimple)
478 {
479     printf("Simple %dx%d at %d,%d\n", fsimple->w, fsimple->h, fsimple->x, fsimple->y);
480     DumpFItem(fsimple->fitem);
481 }
482
483 static void DumpFGroup(t_fgroup *fgroup)
484 {
485     int i;
486
487     printf("Group %dx%d at %d,%d\n", fgroup->w, fgroup->h, fgroup->x, fgroup->y);
488     for (i = 0; (i < fgroup->nfitem); i++)
489     {
490         DumpFItem(fgroup->fitem[i]);
491     }
492 }
493
494 void DumpFGrid(t_fgrid *fgrid)
495 {
496     int i;
497
498     printf("Grid %dx%d\n", fgrid->w, fgrid->h);
499     for (i = 0; (i < fgrid->nfgroup); i++)
500     {
501         DumpFGroup(fgrid->fgroup[i]);
502     }
503     for (i = 0; (i < fgrid->nfsimple); i++)
504     {
505         DumpFSimple(fgrid->fsimple[i]);
506     }
507 }