Add conserved quantity for Berendsen P-couple
[gromacs.git] / src / gromacs / topology / symtab.cpp
blob55d3a59d47e343ae1dc4eaaaa996efeb496ded2a
1 /*
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, 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.
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.
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.
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.
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.
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.
37 #include "gmxpre.h"
39 #include "symtab.h"
41 #include <stdio.h>
42 #include <string.h>
44 #include <algorithm>
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"
52 #define BUFSIZE 1024
53 #define TABLESIZE 5
55 static char *trim_string(const char *s, char *out, int maxlen)
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.
61 * TODO This partially duplicates code in trim(), but perhaps
62 * replacing symtab with a std::map is a better fix.
65 int len, i;
67 if (strlen(s) > (size_t)(maxlen-1))
69 gmx_fatal(FARGS, "String '%s' (%d) is longer than buffer (%d).\n",
70 s, strlen(s), maxlen-1);
73 for (; (*s) == ' '; s++)
77 for (len = strlen(s); (len > 0); len--)
79 if (s[len-1] != ' ')
81 break;
84 if (len >= BUFSIZE)
86 len = BUFSIZE-1;
88 for (i = 0; i < len; i++)
90 out[i] = *(s++);
92 out[i] = 0;
93 return out;
96 int lookup_symtab(t_symtab *symtab, char **name)
98 int base;
99 t_symbuf *symbuf;
101 base = 0;
102 symbuf = symtab->symbuf;
103 while (symbuf != nullptr)
105 const int index = name-symbuf->buf;
106 if ( ( index >= 0 ) && ( index < symbuf->bufsize ) )
108 return index+base;
110 else
112 base += symbuf->bufsize;
113 symbuf = symbuf->next;
116 gmx_fatal(FARGS, "symtab lookup \"%s\" not found", *name);
117 return -1;
120 char **get_symtab_handle(t_symtab *symtab, int name)
122 t_symbuf *symbuf;
124 symbuf = symtab->symbuf;
125 while (symbuf != nullptr)
127 if (name < symbuf->bufsize)
129 return &(symbuf->buf[name]);
131 else
133 name -= symbuf->bufsize;
134 symbuf = symbuf->next;
137 gmx_fatal(FARGS, "symtab get_symtab_handle %d not found", name);
138 return nullptr;
141 static t_symbuf *new_symbuf(void)
143 t_symbuf *symbuf;
145 snew(symbuf, 1);
146 symbuf->bufsize = TABLESIZE;
147 snew(symbuf->buf, symbuf->bufsize);
148 symbuf->next = nullptr;
150 return symbuf;
153 static char **enter_buf(t_symtab *symtab, char *name)
155 int i;
156 t_symbuf *symbuf;
157 gmx_bool bCont;
159 if (symtab->symbuf == nullptr)
161 symtab->symbuf = new_symbuf();
164 symbuf = symtab->symbuf;
167 for (i = 0; (i < symbuf->bufsize); i++)
169 if (symbuf->buf[i] == nullptr)
171 symtab->nr++;
172 symbuf->buf[i] = gmx_strdup(name);
173 return &(symbuf->buf[i]);
175 else if (strcmp(symbuf->buf[i], name) == 0)
177 return &(symbuf->buf[i]);
180 if (symbuf->next != nullptr)
182 symbuf = symbuf->next;
183 bCont = TRUE;
185 else
187 bCont = FALSE;
190 while (bCont);
192 symbuf->next = new_symbuf();
193 symbuf = symbuf->next;
195 symtab->nr++;
196 symbuf->buf[0] = gmx_strdup(name);
197 return &(symbuf->buf[0]);
200 char **put_symtab(t_symtab *symtab, const char *name)
202 char buf[1024];
204 return enter_buf(symtab, trim_string(name, buf, 1023));
207 void open_symtab(t_symtab *symtab)
209 symtab->nr = 0;
210 symtab->symbuf = nullptr;
213 void close_symtab(t_symtab gmx_unused *symtab)
217 void done_symtab(t_symtab *symtab)
219 int i;
220 t_symbuf *symbuf, *freeptr;
222 close_symtab(symtab);
223 symbuf = symtab->symbuf;
224 while (symbuf != nullptr)
226 for (i = 0; (i < symbuf->bufsize) && (i < symtab->nr); i++)
228 sfree(symbuf->buf[i]);
230 symtab->nr -= i;
231 sfree(symbuf->buf);
232 freeptr = symbuf;
233 symbuf = symbuf->next;
234 sfree(freeptr);
236 symtab->symbuf = nullptr;
237 if (symtab->nr != 0)
239 gmx_incons("Freeing symbol table (symtab) structure");
243 void free_symtab(t_symtab *symtab)
245 t_symbuf *symbuf, *freeptr;
247 close_symtab(symtab);
248 symbuf = symtab->symbuf;
249 while (symbuf != nullptr)
251 symtab->nr -= std::min(symbuf->bufsize, symtab->nr);
252 freeptr = symbuf;
253 symbuf = symbuf->next;
254 sfree(freeptr);
256 symtab->symbuf = nullptr;
257 if (symtab->nr != 0)
259 gmx_incons("Freeing symbol table (symtab) structure");
263 void pr_symtab(FILE *fp, int indent, const char *title, t_symtab *symtab)
265 int i, j, nr;
266 t_symbuf *symbuf;
268 if (available(fp, symtab, indent, title))
270 indent = pr_title_n(fp, indent, title, symtab->nr);
271 i = 0;
272 nr = symtab->nr;
273 symbuf = symtab->symbuf;
274 while (symbuf != nullptr)
276 for (j = 0; (j < symbuf->bufsize) && (j < nr); j++)
278 pr_indent(fp, indent);
279 (void) fprintf(fp, "%s[%d]=\"%s\"\n", title, i++, symbuf->buf[j]);
281 nr -= j;
282 symbuf = symbuf->next;
284 if (nr != 0)
286 gmx_incons("Printing symbol table (symtab) structure");