Sort includes outside src/gromacs
[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 "fgrid.h"
40
41 #include <ctype.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45
46 #include "gromacs/utility/cstringutil.h"
47 #include "gromacs/utility/futil.h"
48 #include "gromacs/utility/smalloc.h"
49
50 static const char *type[] = {
51     "button", "radiobuttons", "groupbox", "checkbox",
52     "pixmap", "statictext",   "edittext", "defbutton"
53 };
54
55 void ReadDlgError(const char *infile, eDLGERR err, const char *s,
56                   const char *file, int line)
57 {
58     fprintf(stderr, "Error: ");
59     switch (err)
60     {
61         case eNOVALS:
62             fprintf(stderr, "Not enough values for %s", s);
63             break;
64         case eGRIDEXP:
65             fprintf(stderr, "'grid' expected instead of %s", s);
66             break;
67         case eACCOEXP:
68             fprintf(stderr, "'{' expected instead of %s", s);
69             break;
70         case eACCCEXP:
71             fprintf(stderr, "'}' expected instead of %s", s);
72             break;
73         case eGRPEXP:
74             fprintf(stderr, "'group' expected instead of %s", s);
75             break;
76         case eITEMEXP:
77             fprintf(stderr, "item expected instead of %s", s);
78             break;
79         case eSAMEPOINT:
80             fprintf(stderr, "grid point for %s already in use", s);
81             break;
82         case eTOOWIDE:
83             fprintf(stderr, "grid too wide for %s", s);
84             break;
85         case eTOOHIGH:
86             fprintf(stderr, "grid too high for %s", s);
87             break;
88         case eQUOTE:
89             fprintf(stderr, "quote expected instead of %s", s);
90             break;
91         default:
92             fprintf(stderr, "????");
93             break;
94     }
95     fprintf(stderr, " in file %s\n", infile);
96     fprintf(stderr, "source file: %s, line: %d\n", file, line);
97     exit(1);
98 }
99
100 #define ReadDlgErr(in, er, es) ReadDlgError(in, er, es, __FILE__, __LINE__)
101
102 static void ReadAccOpen(const char *infile, FILE *in)
103 {
104     char buf[STRLEN];
105     int  result;
106
107     result = fscanf(in, "%4s", buf);
108     if ((1 != result) || strcmp(buf, "{") != 0)
109     {
110         ReadDlgErr(infile, eACCOEXP, buf);
111     }
112 }
113
114 static void ReadAccClose(const char *infile, FILE *in)
115 {
116     char buf[STRLEN];
117     int  result;
118
119     result = fscanf(in, "%4s", buf);
120     if ((1 != result) || strcmp(buf, "}") != 0)
121     {
122         ReadDlgErr(infile, eACCCEXP, buf);
123     }
124 }
125
126 void ReadQuoteString(const char *infile, FILE *in, char *buf)
127 {
128     char c[2];
129     int  i = 0;
130
131     /* Read until first quote */
132     while ((c[0] = fgetc(in)) != '"')
133     {
134         if (!isspace(c[0]))
135         {
136             c[1] = '\0';
137             ReadDlgErr(infile, eQUOTE, c);
138         }
139     }
140     /* Read until second quote */
141     while ((c[0] = fgetc(in)) != '"')
142     {
143         buf[i++] = c[0];
144     }
145     buf[i] = '\0';
146 }
147
148 static void ReadQuoteStringOrAccClose(FILE *in, char *buf)
149 {
150     char c;
151     int  i = 0;
152
153     /* Read until first quote */
154     do
155     {
156         c = fgetc(in);
157         if (c == '}')
158         {
159             buf[0] = c;
160             buf[1] = '\0';
161             return;
162         }
163     }
164     while (c != '"');
165
166     /* Read until second quote */
167     while ((c = fgetc(in)) != '"')
168     {
169         buf[i++] = c;
170     }
171     buf[i] = '\0';
172 }
173
174 static bool bNotAccClose(const char *buf)
175 {
176     return (strcmp(buf, "}") != 0);
177 }
178
179 static t_fitem *NewFItem(void)
180 {
181     t_fitem *fitem;
182
183     snew(fitem, 1);
184     fitem->nname = 0;
185     fitem->name  = NULL;
186     fitem->set   = NULL;
187     fitem->get   = NULL;
188     fitem->def   = NULL;
189     fitem->help  = NULL;
190
191     return fitem;
192 }
193
194 static t_fsimple *NewFSimple(void)
195 {
196     t_fsimple *fsimple;
197
198     snew(fsimple, 1);
199
200     return fsimple;
201 }
202
203 static void AddFItemName(t_fitem *fitem, char *name)
204 {
205     srenew(fitem->name, ++fitem->nname);
206     fitem->name[fitem->nname-1] = gmx_strdup(name);
207 }
208
209 static t_fgroup *NewFGroup(void)
210 {
211     t_fgroup *fgroup;
212
213     snew(fgroup, 1);
214     fgroup->name   = NULL;
215     fgroup->nfitem = 0;
216     fgroup->fitem  = NULL;
217
218     return fgroup;
219 }
220
221 static void AddFGroupFItem(t_fgroup *fgroup, t_fitem *fitem)
222 {
223     srenew(fgroup->fitem, ++fgroup->nfitem);
224     fgroup->fitem[fgroup->nfitem-1] = fitem;
225 }
226
227 static t_fgroup *AddFGridFGroup(t_fgrid *fgrid)
228 {
229     srenew(fgrid->fgroup, ++fgrid->nfgroup);
230     fgrid->fgroup[fgrid->nfgroup-1] = NewFGroup();
231     return fgrid->fgroup[fgrid->nfgroup-1];
232 }
233
234 static t_fsimple *AddFGridFSimple(t_fgrid *fgrid)
235 {
236     srenew(fgrid->fsimple, ++fgrid->nfsimple);
237     fgrid->fsimple[fgrid->nfsimple-1] = NewFSimple();
238     return fgrid->fsimple[fgrid->nfsimple-1];
239 }
240
241 static t_fgrid *NewFGrid(void)
242 {
243     t_fgrid *fgrid;
244
245     snew(fgrid, 1);
246     fgrid->w        = 0;
247     fgrid->h        = 0;
248     fgrid->nfgroup  = 0;
249     fgrid->fgroup   = NULL;
250     fgrid->nfsimple = 0;
251     fgrid->fsimple  = NULL;
252
253     return fgrid;
254 }
255
256 static void DoneFItem(t_fitem *fitem)
257 {
258     int i;
259
260     for (i = 0; (i < fitem->nname); i++)
261     {
262         sfree(fitem->name[i]);
263     }
264     sfree(fitem->name);
265     sfree(fitem->set);
266     sfree(fitem->get);
267     sfree(fitem->def);
268     sfree(fitem->help);
269 }
270
271 static void DoneFGroup(t_fgroup *fgroup)
272 {
273     int i;
274
275     sfree(fgroup->name);
276     for (i = 0; (i < fgroup->nfitem); i++)
277     {
278         DoneFItem(fgroup->fitem[i]);
279     }
280     sfree(fgroup->fitem);
281 }
282
283 static void DoneFSimple(t_fsimple *fsimple)
284 {
285     DoneFItem(fsimple->fitem);
286     sfree(fsimple->fitem);
287 }
288
289 void DoneFGrid(t_fgrid *fgrid)
290 {
291     int i;
292
293     for (i = 0; (i < fgrid->nfgroup); i++)
294     {
295         DoneFGroup(fgrid->fgroup[i]);
296     }
297     sfree(fgrid->fgroup);
298     for (i = 0; (i < fgrid->nfsimple); i++)
299     {
300         DoneFSimple(fgrid->fsimple[i]);
301     }
302     sfree(fgrid->fsimple);
303 }
304
305 static t_fitem *ScanFItem(const char *infile, FILE *in, char *buf)
306 {
307     char     set[STRLEN], get[STRLEN], help[STRLEN], def[STRLEN];
308     int      edlg;
309     t_fitem *fitem;
310
311     fitem = NewFItem();
312
313     for (edlg = 0; (edlg < edlgNR+1); edlg++)
314     {
315         if (strcmp(buf, type[edlg]) == 0)
316         {
317             break;
318         }
319     }
320     if (edlg == edlgNR)
321     {
322         /* Special case */
323         edlg        = edlgBN;
324         fitem->bDef = true;
325     }
326     if (edlg == edlgNR+1)
327     {
328         ReadDlgErr(infile, eITEMEXP, buf);
329     }
330
331     fitem->edlg = (edlgitem)edlg;
332     switch (edlg)
333     {
334         case edlgBN:
335         case edlgCB:
336         case edlgET:
337             ReadQuoteString(infile, in, buf);
338             AddFItemName(fitem, buf);
339             break;
340         case edlgST:
341         case edlgRB:
342             ReadAccOpen(infile, in);
343             ReadQuoteStringOrAccClose(in, buf);
344             while (bNotAccClose(buf))
345             {
346                 AddFItemName(fitem, buf);
347                 ReadQuoteStringOrAccClose(in, buf);
348             }
349             break;
350         case edlgPM:
351         case edlgGB:
352             ReadDlgErr(infile, eITEMEXP, type[edlg]);
353             break;
354         default:
355             break;
356     }
357     ReadQuoteString(infile, in, set);
358     ReadQuoteString(infile, in, get);
359     ReadQuoteString(infile, in, def);
360     ReadQuoteString(infile, in, help);
361     fitem->set  = gmx_strdup(set);
362     fitem->get  = gmx_strdup(get);
363     fitem->def  = gmx_strdup(def);
364     fitem->help = gmx_strdup(help);
365
366     return fitem;
367 }
368
369 t_fgrid *FGridFromFile(const char *infile)
370 {
371     FILE      *in;
372     char       buf[STRLEN];
373     int        result;
374
375     t_fgrid   *fgrid;
376     t_fgroup  *fgroup;
377     t_fsimple *fsimple;
378     int        gridx, gridy;
379
380     in     = libopen(infile);
381     result = fscanf(in, "%6s", buf);
382     if ((1 != result) || strcmp(buf, "grid") != 0)
383     {
384         ReadDlgErr(infile, eGRIDEXP, buf);
385     }
386     fgrid = NewFGrid();
387     if ((fscanf(in, "%5d%5d", &gridx, &gridy)) != 2)
388     {
389         ReadDlgErr(infile, eNOVALS, "grid w,h");
390     }
391     fgrid->w = gridx;
392     fgrid->h = gridy;
393     ReadAccOpen(infile, in);
394     result = fscanf(in, "%15s", buf);
395     while ((1 == result) && bNotAccClose(buf))
396     {
397         if (strcmp(buf, "group") == 0)
398         {
399             fgroup = AddFGridFGroup(fgrid);
400             ReadQuoteString(infile, in, buf);
401             fgroup->name = gmx_strdup(buf);
402             if ((fscanf(in, "%5d%5d%5d%5d", &fgroup->x, &fgroup->y, &fgroup->w, &fgroup->h)) != 4)
403             {
404                 ReadDlgErr(infile, eNOVALS, "group x,y,w,h");
405             }
406             if (fgroup->x+fgroup->w > gridx)
407             {
408                 ReadDlgErr(infile, eTOOWIDE, buf);
409             }
410             if (fgroup->y+fgroup->h > gridy)
411             {
412                 ReadDlgErr(infile, eTOOHIGH, buf);
413             }
414             ReadAccOpen(infile, in);
415             result = fscanf(in, "%15s", buf);
416             while ((1 == result) && bNotAccClose(buf))
417             {
418                 AddFGroupFItem(fgroup, ScanFItem(infile, in, buf));
419                 result = fscanf(in, "%15s", buf);
420             }
421         }
422         else if (strcmp(buf, "simple") == 0)
423         {
424             fsimple = AddFGridFSimple(fgrid);
425             if ((fscanf(in, "%5d%5d%5d%5d", &fsimple->x, &fsimple->y, &fsimple->w, &fsimple->h)) != 4)
426             {
427                 ReadDlgErr(infile, eNOVALS, "simple x,y,w,h");
428             }
429             if (fsimple->x+fsimple->w > gridx)
430             {
431                 ReadDlgErr(infile, eTOOWIDE, "simple");
432             }
433             if (fsimple->y+fsimple->h > gridy)
434             {
435                 ReadDlgErr(infile, eTOOHIGH, "simple");
436             }
437             ReadAccOpen(infile, in);
438             result = fscanf(in, "%15s", buf);
439             if (1 == result)
440             {
441                 fsimple->fitem = ScanFItem(infile, in, buf);
442                 ReadAccClose(infile, in);
443             }
444         }
445         if (1 == result)
446         {
447             result = fscanf(in, "%15s", buf);
448         }
449     }
450     gmx_ffclose(in);
451     /* Since we always read one variable at a time the result from
452      * fscanf should always be 1.
453      */
454     if (1 != result)
455     {
456         ReadDlgErr(infile, eNOVALS, "fgrid");
457     }
458
459     return fgrid;
460 }
461
462 static void DumpFItem(t_fitem *fitem)
463 {
464     int i;
465
466     printf("  type: %s, set: '%s', get: '%s', def: '%s', help: '%s'\n  {",
467            type[fitem->edlg], fitem->set, fitem->get, fitem->def, fitem->help);
468     for (i = 0; (i < fitem->nname); i++)
469     {
470         printf("  '%s'", fitem->name[i]);
471     }
472     printf("  }\n");
473 }
474
475 static void DumpFSimple(t_fsimple *fsimple)
476 {
477     printf("Simple %dx%d at %d,%d\n", fsimple->w, fsimple->h, fsimple->x, fsimple->y);
478     DumpFItem(fsimple->fitem);
479 }
480
481 static void DumpFGroup(t_fgroup *fgroup)
482 {
483     int i;
484
485     printf("Group %dx%d at %d,%d\n", fgroup->w, fgroup->h, fgroup->x, fgroup->y);
486     for (i = 0; (i < fgroup->nfitem); i++)
487     {
488         DumpFItem(fgroup->fitem[i]);
489     }
490 }
491
492 void DumpFGrid(t_fgrid *fgrid)
493 {
494     int i;
495
496     printf("Grid %dx%d\n", fgrid->w, fgrid->h);
497     for (i = 0; (i < fgrid->nfgroup); i++)
498     {
499         DumpFGroup(fgrid->fgroup[i]);
500     }
501     for (i = 0; (i < fgrid->nfsimple); i++)
502     {
503         DumpFSimple(fgrid->fsimple[i]);
504     }
505 }