Split lines with many copyright years
[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 by the GROMACS development team.
7  * Copyright (c) 2018,2019,2020, by the GROMACS development team, led by
8  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
9  * and including many others, as listed in the AUTHORS file in the
10  * top-level source directory and at http://www.gromacs.org.
11  *
12  * GROMACS is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public License
14  * as published by the Free Software Foundation; either version 2.1
15  * of the License, or (at your option) any later version.
16  *
17  * GROMACS is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with GROMACS; if not, see
24  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
26  *
27  * If you want to redistribute modifications to GROMACS, please
28  * consider that scientific software is very special. Version
29  * control is crucial - bugs must be traceable. We will be happy to
30  * consider code for inclusion in the official distribution, but
31  * derived work must not be called official GROMACS. Details are found
32  * in the README & COPYING files - if they are missing, get the
33  * official version at http://www.gromacs.org.
34  *
35  * To help us fund GROMACS development, we humbly ask that you cite
36  * the research papers on the package. Check out http://www.gromacs.org.
37  */
38 #include "gmxpre.h"
39
40 #include "symtab.h"
41
42 #include <cstdio>
43 #include <cstring>
44
45 #include <algorithm>
46
47 #include "gromacs/utility/basedefinitions.h"
48 #include "gromacs/utility/cstringutil.h"
49 #include "gromacs/utility/fatalerror.h"
50 #include "gromacs/utility/smalloc.h"
51 #include "gromacs/utility/txtdump.h"
52
53 constexpr int c_trimSize   = 1024;
54 constexpr int c_maxBufSize = 5;
55
56 static char* trim_string(const char* s, char* out, int maxlen)
57 /*
58  * Returns a pointer to a static area which contains a copy
59  * of s without leading or trailing spaces. Strings are
60  * truncated to c_trimSize positions.
61  *
62  * TODO This partially duplicates code in trim(), but perhaps
63  * replacing symtab with a std::map is a better fix.
64  */
65 {
66     int len, i;
67
68     if (strlen(s) > static_cast<size_t>(maxlen - 1))
69     {
70         gmx_fatal(FARGS, "String '%s' (%zu) is longer than buffer (%d).\n", s, strlen(s), maxlen - 1);
71     }
72
73     for (; (*s) == ' '; s++) {}
74     for (len = strlen(s); (len > 0); len--)
75     {
76         if (s[len - 1] != ' ')
77         {
78             break;
79         }
80     }
81     if (len >= c_trimSize)
82     {
83         len = c_trimSize - 1;
84     }
85     for (i = 0; i < len; i++)
86     {
87         out[i] = *(s++);
88     }
89     out[i] = 0;
90     return out;
91 }
92
93 int lookup_symtab(t_symtab* symtab, char** name)
94 {
95     int       base;
96     t_symbuf* symbuf;
97
98     base   = 0;
99     symbuf = symtab->symbuf;
100     while (symbuf != nullptr)
101     {
102         const int index = name - symbuf->buf;
103         if ((index >= 0) && (index < symbuf->bufsize))
104         {
105             return index + base;
106         }
107         else
108         {
109             base += symbuf->bufsize;
110             symbuf = symbuf->next;
111         }
112     }
113     gmx_fatal(FARGS, "symtab lookup \"%s\" not found", *name);
114 }
115
116 char** get_symtab_handle(t_symtab* symtab, int name)
117 {
118     t_symbuf* symbuf;
119
120     symbuf = symtab->symbuf;
121     while (symbuf != nullptr)
122     {
123         if (name < symbuf->bufsize)
124         {
125             return &(symbuf->buf[name]);
126         }
127         else
128         {
129             name -= symbuf->bufsize;
130             symbuf = symbuf->next;
131         }
132     }
133     gmx_fatal(FARGS, "symtab get_symtab_handle %d not found", name);
134 }
135
136 static t_symbuf* new_symbuf()
137 {
138     t_symbuf* symbuf;
139
140     snew(symbuf, 1);
141     symbuf->bufsize = c_maxBufSize;
142     snew(symbuf->buf, symbuf->bufsize);
143     symbuf->next = nullptr;
144
145     return symbuf;
146 }
147
148 static char** enter_buf(t_symtab* symtab, char* name)
149 {
150     int       i;
151     t_symbuf* symbuf;
152     gmx_bool  bCont;
153
154     if (symtab->symbuf == nullptr)
155     {
156         symtab->symbuf = new_symbuf();
157     }
158
159     symbuf = symtab->symbuf;
160     do
161     {
162         for (i = 0; (i < symbuf->bufsize); i++)
163         {
164             if (symbuf->buf[i] == nullptr)
165             {
166                 symtab->nr++;
167                 symbuf->buf[i] = gmx_strdup(name);
168                 return &(symbuf->buf[i]);
169             }
170             else if (strcmp(symbuf->buf[i], name) == 0)
171             {
172                 return &(symbuf->buf[i]);
173             }
174         }
175         if (symbuf->next != nullptr)
176         {
177             symbuf = symbuf->next;
178             bCont  = TRUE;
179         }
180         else
181         {
182             bCont = FALSE;
183         }
184     } while (bCont);
185
186     symbuf->next = new_symbuf();
187     symbuf       = symbuf->next;
188
189     symtab->nr++;
190     symbuf->buf[0] = gmx_strdup(name);
191     return &(symbuf->buf[0]);
192 }
193
194 char** put_symtab(t_symtab* symtab, const char* name)
195 {
196     char buf[1024];
197
198     return enter_buf(symtab, trim_string(name, buf, 1023));
199 }
200
201 void open_symtab(t_symtab* symtab)
202 {
203     symtab->nr     = 0;
204     symtab->symbuf = nullptr;
205 }
206
207 void close_symtab(t_symtab gmx_unused* symtab) {}
208
209 // TODO this will go away when we use a
210 // std::list<std::vector<std::string>>> for t_symtab.
211 t_symtab* duplicateSymtab(const t_symtab* symtab)
212 {
213     t_symtab* copySymtab;
214     snew(copySymtab, 1);
215     open_symtab(copySymtab);
216     t_symbuf* symbuf = symtab->symbuf;
217     if (symbuf != nullptr)
218     {
219         snew(copySymtab->symbuf, 1);
220     }
221     t_symbuf* copySymbuf = copySymtab->symbuf;
222     while (symbuf != nullptr)
223     {
224         snew(copySymbuf->buf, symbuf->bufsize);
225         copySymbuf->bufsize = symbuf->bufsize;
226         for (int i = 0; (i < symbuf->bufsize) && (i < symtab->nr); i++)
227         {
228             if (symbuf->buf[i])
229             {
230                 copySymbuf->buf[i] = gmx_strdup(symbuf->buf[i]);
231             }
232         }
233         symbuf = symbuf->next;
234         if (symbuf != nullptr)
235         {
236             snew(copySymbuf->next, 1);
237             copySymbuf = copySymbuf->next;
238         }
239     }
240     copySymtab->nr = symtab->nr;
241     return copySymtab;
242 }
243
244 void done_symtab(t_symtab* symtab)
245 {
246     int       i;
247     t_symbuf *symbuf, *freeptr;
248
249     close_symtab(symtab);
250     symbuf = symtab->symbuf;
251     while (symbuf != nullptr)
252     {
253         for (i = 0; (i < symbuf->bufsize) && (i < symtab->nr); i++)
254         {
255             sfree(symbuf->buf[i]);
256         }
257         symtab->nr -= i;
258         sfree(symbuf->buf);
259         freeptr = symbuf;
260         symbuf  = symbuf->next;
261         sfree(freeptr);
262     }
263     symtab->symbuf = nullptr;
264     if (symtab->nr != 0)
265     {
266         gmx_incons("Freeing symbol table (symtab) structure");
267     }
268 }
269
270 void free_symtab(t_symtab* symtab)
271 {
272     t_symbuf *symbuf, *freeptr;
273
274     close_symtab(symtab);
275     symbuf = symtab->symbuf;
276     while (symbuf != nullptr)
277     {
278         symtab->nr -= std::min(symbuf->bufsize, symtab->nr);
279         freeptr = symbuf;
280         symbuf  = symbuf->next;
281         sfree(freeptr);
282     }
283     symtab->symbuf = nullptr;
284     if (symtab->nr != 0)
285     {
286         gmx_incons("Freeing symbol table (symtab) structure");
287     }
288 }
289
290 void pr_symtab(FILE* fp, int indent, const char* title, t_symtab* symtab)
291 {
292     int       i, j, nr;
293     t_symbuf* symbuf;
294
295     if (available(fp, symtab, indent, title))
296     {
297         indent = pr_title_n(fp, indent, title, symtab->nr);
298         i      = 0;
299         nr     = symtab->nr;
300         symbuf = symtab->symbuf;
301         while (symbuf != nullptr)
302         {
303             for (j = 0; (j < symbuf->bufsize) && (j < nr); j++)
304             {
305                 pr_indent(fp, indent);
306                 (void)fprintf(fp, "%s[%d]=\"%s\"\n", title, i++, symbuf->buf[j]);
307             }
308             nr -= j;
309             symbuf = symbuf->next;
310         }
311         if (nr != 0)
312         {
313             gmx_incons("Printing symbol table (symtab) structure");
314         }
315     }
316 }