2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
48 #include "gromacs/utility/cstringutil.h"
49 #include "gromacs/utility/futil.h"
50 #include "gromacs/utility/smalloc.h"
52 static const char *type[] = {
53 "button", "radiobuttons", "groupbox", "checkbox",
54 "pixmap", "statictext", "edittext", "defbutton"
57 void ReadDlgError(const char *infile, eDLGERR err, const char *s,
58 const char *file, int line)
60 fprintf(stderr, "Error: ");
64 fprintf(stderr, "Not enough values for %s", s);
67 fprintf(stderr, "'grid' expected instead of %s", s);
70 fprintf(stderr, "'{' expected instead of %s", s);
73 fprintf(stderr, "'}' expected instead of %s", s);
76 fprintf(stderr, "'group' expected instead of %s", s);
79 fprintf(stderr, "item expected instead of %s", s);
82 fprintf(stderr, "grid point for %s already in use", s);
85 fprintf(stderr, "grid too wide for %s", s);
88 fprintf(stderr, "grid too high for %s", s);
91 fprintf(stderr, "quote expected instead of %s", s);
94 fprintf(stderr, "????");
97 fprintf(stderr, " in file %s\n", infile);
98 fprintf(stderr, "source file: %s, line: %d\n", file, line);
102 #define ReadDlgErr(in, er, es) ReadDlgError(in, er, es, __FILE__, __LINE__)
104 static void ReadAccOpen(const char *infile, FILE *in)
109 result = fscanf(in, "%4s", buf);
110 if ((1 != result) || strcmp(buf, "{") != 0)
112 ReadDlgErr(infile, eACCOEXP, buf);
116 static void ReadAccClose(const char *infile, FILE *in)
121 result = fscanf(in, "%4s", buf);
122 if ((1 != result) || strcmp(buf, "}") != 0)
124 ReadDlgErr(infile, eACCCEXP, buf);
128 void ReadQuoteString(const char *infile, FILE *in, char *buf)
133 /* Read until first quote */
134 while ((c[0] = fgetc(in)) != '"')
139 ReadDlgErr(infile, eQUOTE, c);
142 /* Read until second quote */
143 while ((c[0] = fgetc(in)) != '"')
150 static void ReadQuoteStringOrAccClose(FILE *in, char *buf)
155 /* Read until first quote */
168 /* Read until second quote */
169 while ((c = fgetc(in)) != '"')
176 static bool bNotAccClose(const char *buf)
178 return (strcmp(buf, "}") != 0);
181 static t_fitem *NewFItem(void)
196 static t_fsimple *NewFSimple(void)
205 static void AddFItemName(t_fitem *fitem, char *name)
207 srenew(fitem->name, ++fitem->nname);
208 fitem->name[fitem->nname-1] = gmx_strdup(name);
211 static t_fgroup *NewFGroup(void)
218 fgroup->fitem = NULL;
223 static void AddFGroupFItem(t_fgroup *fgroup, t_fitem *fitem)
225 srenew(fgroup->fitem, ++fgroup->nfitem);
226 fgroup->fitem[fgroup->nfitem-1] = fitem;
229 static t_fgroup *AddFGridFGroup(t_fgrid *fgrid)
231 srenew(fgrid->fgroup, ++fgrid->nfgroup);
232 fgrid->fgroup[fgrid->nfgroup-1] = NewFGroup();
233 return fgrid->fgroup[fgrid->nfgroup-1];
236 static t_fsimple *AddFGridFSimple(t_fgrid *fgrid)
238 srenew(fgrid->fsimple, ++fgrid->nfsimple);
239 fgrid->fsimple[fgrid->nfsimple-1] = NewFSimple();
240 return fgrid->fsimple[fgrid->nfsimple-1];
243 static t_fgrid *NewFGrid(void)
251 fgrid->fgroup = NULL;
253 fgrid->fsimple = NULL;
258 static void DoneFItem(t_fitem *fitem)
262 for (i = 0; (i < fitem->nname); i++)
264 sfree(fitem->name[i]);
273 static void DoneFGroup(t_fgroup *fgroup)
278 for (i = 0; (i < fgroup->nfitem); i++)
280 DoneFItem(fgroup->fitem[i]);
282 sfree(fgroup->fitem);
285 static void DoneFSimple(t_fsimple *fsimple)
287 DoneFItem(fsimple->fitem);
288 sfree(fsimple->fitem);
291 void DoneFGrid(t_fgrid *fgrid)
295 for (i = 0; (i < fgrid->nfgroup); i++)
297 DoneFGroup(fgrid->fgroup[i]);
299 sfree(fgrid->fgroup);
300 for (i = 0; (i < fgrid->nfsimple); i++)
302 DoneFSimple(fgrid->fsimple[i]);
304 sfree(fgrid->fsimple);
307 static t_fitem *ScanFItem(const char *infile, FILE *in, char *buf)
309 char set[STRLEN], get[STRLEN], help[STRLEN], def[STRLEN];
315 for (edlg = 0; (edlg < edlgNR+1); edlg++)
317 if (strcmp(buf, type[edlg]) == 0)
328 if (edlg == edlgNR+1)
330 ReadDlgErr(infile, eITEMEXP, buf);
333 fitem->edlg = (edlgitem)edlg;
339 ReadQuoteString(infile, in, buf);
340 AddFItemName(fitem, buf);
344 ReadAccOpen(infile, in);
345 ReadQuoteStringOrAccClose(in, buf);
346 while (bNotAccClose(buf))
348 AddFItemName(fitem, buf);
349 ReadQuoteStringOrAccClose(in, buf);
354 ReadDlgErr(infile, eITEMEXP, type[edlg]);
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);
371 t_fgrid *FGridFromFile(const char *infile)
382 in = libopen(infile);
383 result = fscanf(in, "%6s", buf);
384 if ((1 != result) || strcmp(buf, "grid") != 0)
386 ReadDlgErr(infile, eGRIDEXP, buf);
389 if ((fscanf(in, "%5d%5d", &gridx, &gridy)) != 2)
391 ReadDlgErr(infile, eNOVALS, "grid w,h");
395 ReadAccOpen(infile, in);
396 result = fscanf(in, "%15s", buf);
397 while ((1 == result) && bNotAccClose(buf))
399 if (strcmp(buf, "group") == 0)
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)
406 ReadDlgErr(infile, eNOVALS, "group x,y,w,h");
408 if (fgroup->x+fgroup->w > gridx)
410 ReadDlgErr(infile, eTOOWIDE, buf);
412 if (fgroup->y+fgroup->h > gridy)
414 ReadDlgErr(infile, eTOOHIGH, buf);
416 ReadAccOpen(infile, in);
417 result = fscanf(in, "%15s", buf);
418 while ((1 == result) && bNotAccClose(buf))
420 AddFGroupFItem(fgroup, ScanFItem(infile, in, buf));
421 result = fscanf(in, "%15s", buf);
424 else if (strcmp(buf, "simple") == 0)
426 fsimple = AddFGridFSimple(fgrid);
427 if ((fscanf(in, "%5d%5d%5d%5d", &fsimple->x, &fsimple->y, &fsimple->w, &fsimple->h)) != 4)
429 ReadDlgErr(infile, eNOVALS, "simple x,y,w,h");
431 if (fsimple->x+fsimple->w > gridx)
433 ReadDlgErr(infile, eTOOWIDE, "simple");
435 if (fsimple->y+fsimple->h > gridy)
437 ReadDlgErr(infile, eTOOHIGH, "simple");
439 ReadAccOpen(infile, in);
440 result = fscanf(in, "%15s", buf);
443 fsimple->fitem = ScanFItem(infile, in, buf);
444 ReadAccClose(infile, in);
449 result = fscanf(in, "%15s", buf);
453 /* Since we always read one variable at a time the result from
454 * fscanf should always be 1.
458 ReadDlgErr(infile, eNOVALS, "fgrid");
464 static void DumpFItem(t_fitem *fitem)
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++)
472 printf(" '%s'", fitem->name[i]);
477 static void DumpFSimple(t_fsimple *fsimple)
479 printf("Simple %dx%d at %d,%d\n", fsimple->w, fsimple->h, fsimple->x, fsimple->y);
480 DumpFItem(fsimple->fitem);
483 static void DumpFGroup(t_fgroup *fgroup)
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++)
490 DumpFItem(fgroup->fitem[i]);
494 void DumpFGrid(t_fgrid *fgrid)
498 printf("Grid %dx%d\n", fgrid->w, fgrid->h);
499 for (i = 0; (i < fgrid->nfgroup); i++)
501 DumpFGroup(fgrid->fgroup[i]);
503 for (i = 0; (i < fgrid->nfsimple); i++)
505 DumpFSimple(fgrid->fsimple[i]);