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-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.
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.
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.
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.
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.
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.
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"
53 constexpr int c_trimSize = 1024;
54 constexpr int c_maxBufSize = 5;
56 static char* trim_string(const char* s, char* out, int maxlen)
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.
62 * TODO This partially duplicates code in trim(), but perhaps
63 * replacing symtab with a std::map is a better fix.
68 if (strlen(s) > static_cast<size_t>(maxlen - 1))
70 gmx_fatal(FARGS, "String '%s' (%zu) is longer than buffer (%d).\n", s, strlen(s), maxlen - 1);
73 for (; (*s) == ' '; s++) {}
74 for (len = strlen(s); (len > 0); len--)
76 if (s[len - 1] != ' ')
81 if (len >= c_trimSize)
85 for (i = 0; i < len; i++)
93 int lookup_symtab(t_symtab* symtab, char** name)
99 symbuf = symtab->symbuf;
100 while (symbuf != nullptr)
102 const int index = name - symbuf->buf;
103 if ((index >= 0) && (index < symbuf->bufsize))
109 base += symbuf->bufsize;
110 symbuf = symbuf->next;
113 gmx_fatal(FARGS, "symtab lookup \"%s\" not found", *name);
116 char** get_symtab_handle(t_symtab* symtab, int name)
120 symbuf = symtab->symbuf;
121 while (symbuf != nullptr)
123 if (name < symbuf->bufsize)
125 return &(symbuf->buf[name]);
129 name -= symbuf->bufsize;
130 symbuf = symbuf->next;
133 gmx_fatal(FARGS, "symtab get_symtab_handle %d not found", name);
136 static t_symbuf* new_symbuf()
141 symbuf->bufsize = c_maxBufSize;
142 snew(symbuf->buf, symbuf->bufsize);
143 symbuf->next = nullptr;
148 static char** enter_buf(t_symtab* symtab, char* name)
154 if (symtab->symbuf == nullptr)
156 symtab->symbuf = new_symbuf();
159 symbuf = symtab->symbuf;
162 for (i = 0; (i < symbuf->bufsize); i++)
164 if (symbuf->buf[i] == nullptr)
167 symbuf->buf[i] = gmx_strdup(name);
168 return &(symbuf->buf[i]);
170 else if (strcmp(symbuf->buf[i], name) == 0)
172 return &(symbuf->buf[i]);
175 if (symbuf->next != nullptr)
177 symbuf = symbuf->next;
186 symbuf->next = new_symbuf();
187 symbuf = symbuf->next;
190 symbuf->buf[0] = gmx_strdup(name);
191 return &(symbuf->buf[0]);
194 char** put_symtab(t_symtab* symtab, const char* name)
198 return enter_buf(symtab, trim_string(name, buf, 1023));
201 void open_symtab(t_symtab* symtab)
204 symtab->symbuf = nullptr;
207 void close_symtab(t_symtab gmx_unused* symtab) {}
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)
213 t_symtab* copySymtab;
215 open_symtab(copySymtab);
216 t_symbuf* symbuf = symtab->symbuf;
217 if (symbuf != nullptr)
219 snew(copySymtab->symbuf, 1);
221 t_symbuf* copySymbuf = copySymtab->symbuf;
222 while (symbuf != nullptr)
224 snew(copySymbuf->buf, symbuf->bufsize);
225 copySymbuf->bufsize = symbuf->bufsize;
226 for (int i = 0; (i < symbuf->bufsize) && (i < symtab->nr); i++)
230 copySymbuf->buf[i] = gmx_strdup(symbuf->buf[i]);
233 symbuf = symbuf->next;
234 if (symbuf != nullptr)
236 snew(copySymbuf->next, 1);
237 copySymbuf = copySymbuf->next;
240 copySymtab->nr = symtab->nr;
244 void done_symtab(t_symtab* symtab)
247 t_symbuf *symbuf, *freeptr;
249 close_symtab(symtab);
250 symbuf = symtab->symbuf;
251 while (symbuf != nullptr)
253 for (i = 0; (i < symbuf->bufsize) && (i < symtab->nr); i++)
255 sfree(symbuf->buf[i]);
260 symbuf = symbuf->next;
263 symtab->symbuf = nullptr;
266 gmx_incons("Freeing symbol table (symtab) structure");
270 void free_symtab(t_symtab* symtab)
272 t_symbuf *symbuf, *freeptr;
274 close_symtab(symtab);
275 symbuf = symtab->symbuf;
276 while (symbuf != nullptr)
278 symtab->nr -= std::min(symbuf->bufsize, symtab->nr);
280 symbuf = symbuf->next;
283 symtab->symbuf = nullptr;
286 gmx_incons("Freeing symbol table (symtab) structure");
290 void pr_symtab(FILE* fp, int indent, const char* title, t_symtab* symtab)
295 if (available(fp, symtab, indent, title))
297 indent = pr_title_n(fp, indent, title, symtab->nr);
300 symbuf = symtab->symbuf;
301 while (symbuf != nullptr)
303 for (j = 0; (j < symbuf->bufsize) && (j < nr); j++)
305 pr_indent(fp, indent);
306 (void)fprintf(fp, "%s[%d]=\"%s\"\n", title, i++, symbuf->buf[j]);
309 symbuf = symbuf->next;
313 gmx_incons("Printing symbol table (symtab) structure");