Fix part of old-style casting
[alexxy/gromacs.git] / src / gromacs / topology / symtab.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-2004, The GROMACS development team.
6  * Copyright (c) 2013,2014,2015,2016,2017,2018, 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 "symtab.h"
40
41 #include <stdio.h>
42 #include <string.h>
43
44 #include <algorithm>
45
46 #include "gromacs/utility/basedefinitions.h"
47 #include "gromacs/utility/cstringutil.h"
48 #include "gromacs/utility/fatalerror.h"
49 #include "gromacs/utility/smalloc.h"
50 #include "gromacs/utility/txtdump.h"
51
52 #define BUFSIZE         1024
53 #define TABLESIZE       5
54
55 static char *trim_string(const char *s, char *out, int maxlen)
56 /*
57  * Returns a pointer to a static area which contains a copy
58  * of s without leading or trailing spaces. Strings are
59  * truncated to BUFSIZE positions.
60  *
61  * TODO This partially duplicates code in trim(), but perhaps
62  * replacing symtab with a std::map is a better fix.
63  */
64 {
65     int len, i;
66
67     if (strlen(s) > static_cast<size_t>(maxlen-1))
68     {
69         gmx_fatal(FARGS, "String '%s' (%zu) is longer than buffer (%d).\n",
70                   s, strlen(s), maxlen-1);
71     }
72
73     for (; (*s) == ' '; s++)
74     {
75         ;
76     }
77     for (len = strlen(s); (len > 0); len--)
78     {
79         if (s[len-1] != ' ')
80         {
81             break;
82         }
83     }
84     if (len >= BUFSIZE)
85     {
86         len = BUFSIZE-1;
87     }
88     for (i = 0; i < len; i++)
89     {
90         out[i] = *(s++);
91     }
92     out[i] = 0;
93     return out;
94 }
95
96 int lookup_symtab(t_symtab *symtab, char **name)
97 {
98     int       base;
99     t_symbuf *symbuf;
100
101     base   = 0;
102     symbuf = symtab->symbuf;
103     while (symbuf != nullptr)
104     {
105         const int index = name-symbuf->buf;
106         if ( ( index >= 0 ) && ( index < symbuf->bufsize ) )
107         {
108             return index+base;
109         }
110         else
111         {
112             base  += symbuf->bufsize;
113             symbuf = symbuf->next;
114         }
115     }
116     gmx_fatal(FARGS, "symtab lookup \"%s\" not found", *name);
117 }
118
119 char **get_symtab_handle(t_symtab *symtab, int name)
120 {
121     t_symbuf *symbuf;
122
123     symbuf = symtab->symbuf;
124     while (symbuf != nullptr)
125     {
126         if (name < symbuf->bufsize)
127         {
128             return &(symbuf->buf[name]);
129         }
130         else
131         {
132             name  -= symbuf->bufsize;
133             symbuf = symbuf->next;
134         }
135     }
136     gmx_fatal(FARGS, "symtab get_symtab_handle %d not found", name);
137 }
138
139 static t_symbuf *new_symbuf(void)
140 {
141     t_symbuf *symbuf;
142
143     snew(symbuf, 1);
144     symbuf->bufsize = TABLESIZE;
145     snew(symbuf->buf, symbuf->bufsize);
146     symbuf->next = nullptr;
147
148     return symbuf;
149 }
150
151 static char **enter_buf(t_symtab *symtab, char *name)
152 {
153     int          i;
154     t_symbuf    *symbuf;
155     gmx_bool     bCont;
156
157     if (symtab->symbuf == nullptr)
158     {
159         symtab->symbuf = new_symbuf();
160     }
161
162     symbuf = symtab->symbuf;
163     do
164     {
165         for (i = 0; (i < symbuf->bufsize); i++)
166         {
167             if (symbuf->buf[i] == nullptr)
168             {
169                 symtab->nr++;
170                 symbuf->buf[i] = gmx_strdup(name);
171                 return &(symbuf->buf[i]);
172             }
173             else if (strcmp(symbuf->buf[i], name) == 0)
174             {
175                 return &(symbuf->buf[i]);
176             }
177         }
178         if (symbuf->next != nullptr)
179         {
180             symbuf = symbuf->next;
181             bCont  = TRUE;
182         }
183         else
184         {
185             bCont = FALSE;
186         }
187     }
188     while (bCont);
189
190     symbuf->next = new_symbuf();
191     symbuf       = symbuf->next;
192
193     symtab->nr++;
194     symbuf->buf[0] = gmx_strdup(name);
195     return &(symbuf->buf[0]);
196 }
197
198 char **put_symtab(t_symtab *symtab, const char *name)
199 {
200     char buf[1024];
201
202     return enter_buf(symtab, trim_string(name, buf, 1023));
203 }
204
205 void open_symtab(t_symtab *symtab)
206 {
207     symtab->nr     = 0;
208     symtab->symbuf = nullptr;
209 }
210
211 void close_symtab(t_symtab gmx_unused *symtab)
212 {
213 }
214
215 void done_symtab(t_symtab *symtab)
216 {
217     int       i;
218     t_symbuf *symbuf, *freeptr;
219
220     close_symtab(symtab);
221     symbuf = symtab->symbuf;
222     while (symbuf != nullptr)
223     {
224         for (i = 0; (i < symbuf->bufsize) && (i < symtab->nr); i++)
225         {
226             sfree(symbuf->buf[i]);
227         }
228         symtab->nr -= i;
229         sfree(symbuf->buf);
230         freeptr = symbuf;
231         symbuf  = symbuf->next;
232         sfree(freeptr);
233     }
234     symtab->symbuf = nullptr;
235     if (symtab->nr != 0)
236     {
237         gmx_incons("Freeing symbol table (symtab) structure");
238     }
239 }
240
241 void free_symtab(t_symtab *symtab)
242 {
243     t_symbuf *symbuf, *freeptr;
244
245     close_symtab(symtab);
246     symbuf = symtab->symbuf;
247     while (symbuf != nullptr)
248     {
249         symtab->nr -= std::min(symbuf->bufsize, symtab->nr);
250         freeptr     = symbuf;
251         symbuf      = symbuf->next;
252         sfree(freeptr);
253     }
254     symtab->symbuf = nullptr;
255     if (symtab->nr != 0)
256     {
257         gmx_incons("Freeing symbol table (symtab) structure");
258     }
259 }
260
261 void pr_symtab(FILE *fp, int indent, const char *title, t_symtab *symtab)
262 {
263     int       i, j, nr;
264     t_symbuf *symbuf;
265
266     if (available(fp, symtab, indent, title))
267     {
268         indent = pr_title_n(fp, indent, title, symtab->nr);
269         i      = 0;
270         nr     = symtab->nr;
271         symbuf = symtab->symbuf;
272         while (symbuf != nullptr)
273         {
274             for (j = 0; (j < symbuf->bufsize) && (j < nr); j++)
275             {
276                 pr_indent(fp, indent);
277                 (void) fprintf(fp, "%s[%d]=\"%s\"\n", title, i++, symbuf->buf[j]);
278             }
279             nr    -= j;
280             symbuf = symbuf->next;
281         }
282         if (nr != 0)
283         {
284             gmx_incons("Printing symbol table (symtab) structure");
285         }
286     }
287 }