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,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.
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.
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 constexpr int c_trimSize
= 1024;
53 constexpr int c_maxBufSize
= 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 c_trimSize positions.
61 * TODO This partially duplicates code in trim(), but perhaps
62 * replacing symtab with a std::map is a better fix.
67 if (strlen(s
) > static_cast<size_t>(maxlen
-1))
69 gmx_fatal(FARGS
, "String '%s' (%zu) is longer than buffer (%d).\n",
70 s
, strlen(s
), maxlen
-1);
73 for (; (*s
) == ' '; s
++)
77 for (len
= strlen(s
); (len
> 0); len
--)
84 if (len
>= c_trimSize
)
88 for (i
= 0; i
< len
; i
++)
96 int lookup_symtab(t_symtab
*symtab
, char **name
)
102 symbuf
= symtab
->symbuf
;
103 while (symbuf
!= nullptr)
105 const int index
= name
-symbuf
->buf
;
106 if ( ( index
>= 0 ) && ( index
< symbuf
->bufsize
) )
112 base
+= symbuf
->bufsize
;
113 symbuf
= symbuf
->next
;
116 gmx_fatal(FARGS
, "symtab lookup \"%s\" not found", *name
);
119 char **get_symtab_handle(t_symtab
*symtab
, int name
)
123 symbuf
= symtab
->symbuf
;
124 while (symbuf
!= nullptr)
126 if (name
< symbuf
->bufsize
)
128 return &(symbuf
->buf
[name
]);
132 name
-= symbuf
->bufsize
;
133 symbuf
= symbuf
->next
;
136 gmx_fatal(FARGS
, "symtab get_symtab_handle %d not found", name
);
139 static t_symbuf
*new_symbuf()
144 symbuf
->bufsize
= c_maxBufSize
;
145 snew(symbuf
->buf
, symbuf
->bufsize
);
146 symbuf
->next
= nullptr;
151 static char **enter_buf(t_symtab
*symtab
, char *name
)
157 if (symtab
->symbuf
== nullptr)
159 symtab
->symbuf
= new_symbuf();
162 symbuf
= symtab
->symbuf
;
165 for (i
= 0; (i
< symbuf
->bufsize
); i
++)
167 if (symbuf
->buf
[i
] == nullptr)
170 symbuf
->buf
[i
] = gmx_strdup(name
);
171 return &(symbuf
->buf
[i
]);
173 else if (strcmp(symbuf
->buf
[i
], name
) == 0)
175 return &(symbuf
->buf
[i
]);
178 if (symbuf
->next
!= nullptr)
180 symbuf
= symbuf
->next
;
190 symbuf
->next
= new_symbuf();
191 symbuf
= symbuf
->next
;
194 symbuf
->buf
[0] = gmx_strdup(name
);
195 return &(symbuf
->buf
[0]);
198 char **put_symtab(t_symtab
*symtab
, const char *name
)
202 return enter_buf(symtab
, trim_string(name
, buf
, 1023));
205 void open_symtab(t_symtab
*symtab
)
208 symtab
->symbuf
= nullptr;
211 void close_symtab(t_symtab gmx_unused
*symtab
)
215 // TODO this will go away when we use a
216 // std::list<std::vector<std::string>>> for t_symtab.
217 t_symtab
*duplicateSymtab(const t_symtab
*symtab
)
219 t_symtab
*copySymtab
;
221 open_symtab(copySymtab
);
222 t_symbuf
*symbuf
= symtab
->symbuf
;
223 if (symbuf
!= nullptr)
225 snew(copySymtab
->symbuf
, 1);
227 t_symbuf
*copySymbuf
= copySymtab
->symbuf
;
228 while (symbuf
!= nullptr)
230 snew(copySymbuf
->buf
, symbuf
->bufsize
);
231 copySymbuf
->bufsize
= symbuf
->bufsize
;
232 for (int i
= 0; (i
< symbuf
->bufsize
) && (i
< symtab
->nr
); i
++)
236 copySymbuf
->buf
[i
] = gmx_strdup(symbuf
->buf
[i
]);
239 symbuf
= symbuf
->next
;
240 if (symbuf
!= nullptr)
242 snew(copySymbuf
->next
, 1);
243 copySymbuf
= copySymbuf
->next
;
246 copySymtab
->nr
= symtab
->nr
;
250 void done_symtab(t_symtab
*symtab
)
253 t_symbuf
*symbuf
, *freeptr
;
255 close_symtab(symtab
);
256 symbuf
= symtab
->symbuf
;
257 while (symbuf
!= nullptr)
259 for (i
= 0; (i
< symbuf
->bufsize
) && (i
< symtab
->nr
); i
++)
261 sfree(symbuf
->buf
[i
]);
266 symbuf
= symbuf
->next
;
269 symtab
->symbuf
= nullptr;
272 gmx_incons("Freeing symbol table (symtab) structure");
276 void free_symtab(t_symtab
*symtab
)
278 t_symbuf
*symbuf
, *freeptr
;
280 close_symtab(symtab
);
281 symbuf
= symtab
->symbuf
;
282 while (symbuf
!= nullptr)
284 symtab
->nr
-= std::min(symbuf
->bufsize
, symtab
->nr
);
286 symbuf
= symbuf
->next
;
289 symtab
->symbuf
= nullptr;
292 gmx_incons("Freeing symbol table (symtab) structure");
296 void pr_symtab(FILE *fp
, int indent
, const char *title
, t_symtab
*symtab
)
301 if (available(fp
, symtab
, indent
, title
))
303 indent
= pr_title_n(fp
, indent
, title
, symtab
->nr
);
306 symbuf
= symtab
->symbuf
;
307 while (symbuf
!= nullptr)
309 for (j
= 0; (j
< symbuf
->bufsize
) && (j
< nr
); j
++)
311 pr_indent(fp
, indent
);
312 (void) fprintf(fp
, "%s[%d]=\"%s\"\n", title
, i
++, symbuf
->buf
[j
]);
315 symbuf
= symbuf
->next
;
319 gmx_incons("Printing symbol table (symtab) structure");