3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
10 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
11 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
12 * Copyright (c) 2001-2004, The GROMACS development team,
13 * check out http://www.gromacs.org for more information.
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * If you want to redistribute modifications, please consider that
21 * scientific software is very special. Version control is crucial -
22 * bugs must be traceable. We will be happy to consider code for
23 * inclusion in the official distribution, but derived work must not
24 * be called official GROMACS. Details are found in the README & COPYING
25 * files - if they are missing, get the official version at www.gromacs.org.
27 * To help us fund GROMACS development, we humbly ask that you cite
28 * the papers on the package - you can find them in the top README file.
30 * For more info, check our website at http://www.gromacs.org
33 * GROningen Mixture of Alchemy and Childrens' Stories
44 #include "gmx_fatal.h"
53 static char *trim_string(const char *s, char *out, int maxlen)
55 * Returns a pointer to a static area which contains a copy
56 * of s without leading or trailing spaces. Strings are
57 * truncated to BUFSIZE positions.
62 if (strlen(s) > (size_t)(maxlen-1))
64 gmx_fatal(FARGS, "String '%s' (%d) is longer than buffer (%d).\n",
65 s, strlen(s), maxlen-1);
68 for (; (*s) && ((*s) == ' '); s++)
72 for (len = strlen(s); (len > 0); len--)
83 for (i = 0; i < len; i++)
91 int lookup_symtab(t_symtab *symtab, char **name)
98 symbuf = symtab->symbuf;
99 while (symbuf != NULL)
101 index = name-symbuf->buf;
102 if ( ( index >= 0 ) && ( index < symbuf->bufsize ) )
108 base += symbuf->bufsize;
109 symbuf = symbuf->next;
112 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 != NULL)
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);
137 static t_symbuf *new_symbuf(void)
142 symbuf->bufsize = TABLESIZE;
143 snew(symbuf->buf, symbuf->bufsize);
149 static char **enter_buf(t_symtab *symtab, char *name)
155 if (symtab->symbuf == NULL)
157 symtab->symbuf = new_symbuf();
160 symbuf = symtab->symbuf;
163 for (i = 0; (i < symbuf->bufsize); i++)
165 if (symbuf->buf[i] == NULL)
168 symbuf->buf[i] = strdup(name);
169 return &(symbuf->buf[i]);
171 else if (strcmp(symbuf->buf[i], name) == 0)
173 return &(symbuf->buf[i]);
176 if (symbuf->next != NULL)
178 symbuf = symbuf->next;
188 symbuf->next = new_symbuf();
189 symbuf = symbuf->next;
192 symbuf->buf[0] = strdup(name);
193 return &(symbuf->buf[0]);
196 char **put_symtab(t_symtab *symtab, const char *name)
200 return enter_buf(symtab, trim_string(name, buf, 1023));
203 void open_symtab(t_symtab *symtab)
206 symtab->symbuf = NULL;
209 void close_symtab(t_symtab *symtab)
213 void done_symtab(t_symtab *symtab)
216 t_symbuf *symbuf, *freeptr;
218 close_symtab(symtab);
219 symbuf = symtab->symbuf;
220 while (symbuf != NULL)
222 for (i = 0; (i < symbuf->bufsize) && (i < symtab->nr); i++)
224 sfree(symbuf->buf[i]);
229 symbuf = symbuf->next;
232 symtab->symbuf = NULL;
235 gmx_incons("Freeing symbol table (symtab) structure");
239 void free_symtab(t_symtab *symtab)
241 t_symbuf *symbuf, *freeptr;
243 close_symtab(symtab);
244 symbuf = symtab->symbuf;
245 while (symbuf != NULL)
247 symtab->nr -= min(symbuf->bufsize, symtab->nr);
249 symbuf = symbuf->next;
252 symtab->symbuf = NULL;
255 gmx_incons("Freeing symbol table (symtab) structure");
259 void pr_symtab(FILE *fp, int indent, const char *title, t_symtab *symtab)
264 if (available(fp, symtab, indent, title))
266 indent = pr_title_n(fp, indent, title, symtab->nr);
269 symbuf = symtab->symbuf;
270 while (symbuf != NULL)
272 for (j = 0; (j < symbuf->bufsize) && (j < nr); j++)
274 pr_indent(fp, indent);
275 (void) fprintf(fp, "%s[%d]=\"%s\"\n", title, i++, symbuf->buf[j]);
278 symbuf = symbuf->next;
282 gmx_incons("Printing symbol table (symtab) structure");