Apply clang-format to source tree
[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,2019, 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 <cstdio>
42 #include <cstring>
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 constexpr int c_trimSize   = 1024;
53 constexpr int c_maxBufSize = 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 c_trimSize 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", s, strlen(s), maxlen - 1);
70     }
71
72     for (; (*s) == ' '; s++) {}
73     for (len = strlen(s); (len > 0); len--)
74     {
75         if (s[len - 1] != ' ')
76         {
77             break;
78         }
79     }
80     if (len >= c_trimSize)
81     {
82         len = c_trimSize - 1;
83     }
84     for (i = 0; i < len; i++)
85     {
86         out[i] = *(s++);
87     }
88     out[i] = 0;
89     return out;
90 }
91
92 int lookup_symtab(t_symtab* symtab, char** name)
93 {
94     int       base;
95     t_symbuf* symbuf;
96
97     base   = 0;
98     symbuf = symtab->symbuf;
99     while (symbuf != nullptr)
100     {
101         const int index = name - symbuf->buf;
102         if ((index >= 0) && (index < symbuf->bufsize))
103         {
104             return index + base;
105         }
106         else
107         {
108             base += symbuf->bufsize;
109             symbuf = symbuf->next;
110         }
111     }
112     gmx_fatal(FARGS, "symtab lookup \"%s\" not found", *name);
113 }
114
115 char** get_symtab_handle(t_symtab* symtab, int name)
116 {
117     t_symbuf* symbuf;
118
119     symbuf = symtab->symbuf;
120     while (symbuf != nullptr)
121     {
122         if (name < symbuf->bufsize)
123         {
124             return &(symbuf->buf[name]);
125         }
126         else
127         {
128             name -= symbuf->bufsize;
129             symbuf = symbuf->next;
130         }
131     }
132     gmx_fatal(FARGS, "symtab get_symtab_handle %d not found", name);
133 }
134
135 static t_symbuf* new_symbuf()
136 {
137     t_symbuf* symbuf;
138
139     snew(symbuf, 1);
140     symbuf->bufsize = c_maxBufSize;
141     snew(symbuf->buf, symbuf->bufsize);
142     symbuf->next = nullptr;
143
144     return symbuf;
145 }
146
147 static char** enter_buf(t_symtab* symtab, char* name)
148 {
149     int       i;
150     t_symbuf* symbuf;
151     gmx_bool  bCont;
152
153     if (symtab->symbuf == nullptr)
154     {
155         symtab->symbuf = new_symbuf();
156     }
157
158     symbuf = symtab->symbuf;
159     do
160     {
161         for (i = 0; (i < symbuf->bufsize); i++)
162         {
163             if (symbuf->buf[i] == nullptr)
164             {
165                 symtab->nr++;
166                 symbuf->buf[i] = gmx_strdup(name);
167                 return &(symbuf->buf[i]);
168             }
169             else if (strcmp(symbuf->buf[i], name) == 0)
170             {
171                 return &(symbuf->buf[i]);
172             }
173         }
174         if (symbuf->next != nullptr)
175         {
176             symbuf = symbuf->next;
177             bCont  = TRUE;
178         }
179         else
180         {
181             bCont = FALSE;
182         }
183     } while (bCont);
184
185     symbuf->next = new_symbuf();
186     symbuf       = symbuf->next;
187
188     symtab->nr++;
189     symbuf->buf[0] = gmx_strdup(name);
190     return &(symbuf->buf[0]);
191 }
192
193 char** put_symtab(t_symtab* symtab, const char* name)
194 {
195     char buf[1024];
196
197     return enter_buf(symtab, trim_string(name, buf, 1023));
198 }
199
200 void open_symtab(t_symtab* symtab)
201 {
202     symtab->nr     = 0;
203     symtab->symbuf = nullptr;
204 }
205
206 void close_symtab(t_symtab gmx_unused* symtab) {}
207
208 // TODO this will go away when we use a
209 // std::list<std::vector<std::string>>> for t_symtab.
210 t_symtab* duplicateSymtab(const t_symtab* symtab)
211 {
212     t_symtab* copySymtab;
213     snew(copySymtab, 1);
214     open_symtab(copySymtab);
215     t_symbuf* symbuf = symtab->symbuf;
216     if (symbuf != nullptr)
217     {
218         snew(copySymtab->symbuf, 1);
219     }
220     t_symbuf* copySymbuf = copySymtab->symbuf;
221     while (symbuf != nullptr)
222     {
223         snew(copySymbuf->buf, symbuf->bufsize);
224         copySymbuf->bufsize = symbuf->bufsize;
225         for (int i = 0; (i < symbuf->bufsize) && (i < symtab->nr); i++)
226         {
227             if (symbuf->buf[i])
228             {
229                 copySymbuf->buf[i] = gmx_strdup(symbuf->buf[i]);
230             }
231         }
232         symbuf = symbuf->next;
233         if (symbuf != nullptr)
234         {
235             snew(copySymbuf->next, 1);
236             copySymbuf = copySymbuf->next;
237         }
238     }
239     copySymtab->nr = symtab->nr;
240     return copySymtab;
241 }
242
243 void done_symtab(t_symtab* symtab)
244 {
245     int       i;
246     t_symbuf *symbuf, *freeptr;
247
248     close_symtab(symtab);
249     symbuf = symtab->symbuf;
250     while (symbuf != nullptr)
251     {
252         for (i = 0; (i < symbuf->bufsize) && (i < symtab->nr); i++)
253         {
254             sfree(symbuf->buf[i]);
255         }
256         symtab->nr -= i;
257         sfree(symbuf->buf);
258         freeptr = symbuf;
259         symbuf  = symbuf->next;
260         sfree(freeptr);
261     }
262     symtab->symbuf = nullptr;
263     if (symtab->nr != 0)
264     {
265         gmx_incons("Freeing symbol table (symtab) structure");
266     }
267 }
268
269 void free_symtab(t_symtab* symtab)
270 {
271     t_symbuf *symbuf, *freeptr;
272
273     close_symtab(symtab);
274     symbuf = symtab->symbuf;
275     while (symbuf != nullptr)
276     {
277         symtab->nr -= std::min(symbuf->bufsize, symtab->nr);
278         freeptr = symbuf;
279         symbuf  = symbuf->next;
280         sfree(freeptr);
281     }
282     symtab->symbuf = nullptr;
283     if (symtab->nr != 0)
284     {
285         gmx_incons("Freeing symbol table (symtab) structure");
286     }
287 }
288
289 void pr_symtab(FILE* fp, int indent, const char* title, t_symtab* symtab)
290 {
291     int       i, j, nr;
292     t_symbuf* symbuf;
293
294     if (available(fp, symtab, indent, title))
295     {
296         indent = pr_title_n(fp, indent, title, symtab->nr);
297         i      = 0;
298         nr     = symtab->nr;
299         symbuf = symtab->symbuf;
300         while (symbuf != nullptr)
301         {
302             for (j = 0; (j < symbuf->bufsize) && (j < nr); j++)
303             {
304                 pr_indent(fp, indent);
305                 (void)fprintf(fp, "%s[%d]=\"%s\"\n", title, i++, symbuf->buf[j]);
306             }
307             nr -= j;
308             symbuf = symbuf->next;
309         }
310         if (nr != 0)
311         {
312             gmx_incons("Printing symbol table (symtab) structure");
313         }
314     }
315 }