Remove unnecessary config.h includes
[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, 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.
10  *
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.
15  *
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.
20  *
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.
25  *
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.
33  *
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.
36  */
37 #include "gmxpre.h"
38
39 #include "symtab.h"
40
41 #include <stdio.h>
42 #include <string.h>
43
44 #include <algorithm>
45
46 #include "gromacs/legacyheaders/txtdump.h"
47
48 #include "gromacs/utility/basedefinitions.h"
49 #include "gromacs/utility/fatalerror.h"
50 #include "gromacs/utility/smalloc.h"
51
52 #define BUFSIZE         1024
53 #define TABLESIZE       5
54
55 static char *trim_string(const char *s, char *out, int maxlen)
56 /*
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.
60  */
61 {
62     int len, i;
63
64     if (strlen(s) > (size_t)(maxlen-1))
65     {
66         gmx_fatal(FARGS, "String '%s' (%d) is longer than buffer (%d).\n",
67                   s, strlen(s), maxlen-1);
68     }
69
70     for (; (*s) && ((*s) == ' '); s++)
71     {
72         ;
73     }
74     for (len = strlen(s); (len > 0); len--)
75     {
76         if (s[len-1] != ' ')
77         {
78             break;
79         }
80     }
81     if (len >= BUFSIZE)
82     {
83         len = BUFSIZE-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 != NULL)
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     return -1;
115 }
116
117 char **get_symtab_handle(t_symtab *symtab, int name)
118 {
119     t_symbuf *symbuf;
120
121     symbuf = symtab->symbuf;
122     while (symbuf != NULL)
123     {
124         if (name < symbuf->bufsize)
125         {
126             return &(symbuf->buf[name]);
127         }
128         else
129         {
130             name  -= symbuf->bufsize;
131             symbuf = symbuf->next;
132         }
133     }
134     gmx_fatal(FARGS, "symtab get_symtab_handle %d not found", name);
135     return NULL;
136 }
137
138 static t_symbuf *new_symbuf(void)
139 {
140     t_symbuf *symbuf;
141
142     snew(symbuf, 1);
143     symbuf->bufsize = TABLESIZE;
144     snew(symbuf->buf, symbuf->bufsize);
145     symbuf->next = NULL;
146
147     return symbuf;
148 }
149
150 static char **enter_buf(t_symtab *symtab, char *name)
151 {
152     int          i;
153     t_symbuf    *symbuf;
154     gmx_bool     bCont;
155
156     if (symtab->symbuf == NULL)
157     {
158         symtab->symbuf = new_symbuf();
159     }
160
161     symbuf = symtab->symbuf;
162     do
163     {
164         for (i = 0; (i < symbuf->bufsize); i++)
165         {
166             if (symbuf->buf[i] == NULL)
167             {
168                 symtab->nr++;
169                 symbuf->buf[i] = gmx_strdup(name);
170                 return &(symbuf->buf[i]);
171             }
172             else if (strcmp(symbuf->buf[i], name) == 0)
173             {
174                 return &(symbuf->buf[i]);
175             }
176         }
177         if (symbuf->next != NULL)
178         {
179             symbuf = symbuf->next;
180             bCont  = TRUE;
181         }
182         else
183         {
184             bCont = FALSE;
185         }
186     }
187     while (bCont);
188
189     symbuf->next = new_symbuf();
190     symbuf       = symbuf->next;
191
192     symtab->nr++;
193     symbuf->buf[0] = gmx_strdup(name);
194     return &(symbuf->buf[0]);
195 }
196
197 char **put_symtab(t_symtab *symtab, const char *name)
198 {
199     char buf[1024];
200
201     return enter_buf(symtab, trim_string(name, buf, 1023));
202 }
203
204 void open_symtab(t_symtab *symtab)
205 {
206     symtab->nr     = 0;
207     symtab->symbuf = NULL;
208 }
209
210 void close_symtab(t_symtab gmx_unused *symtab)
211 {
212 }
213
214 void done_symtab(t_symtab *symtab)
215 {
216     int       i;
217     t_symbuf *symbuf, *freeptr;
218
219     close_symtab(symtab);
220     symbuf = symtab->symbuf;
221     while (symbuf != NULL)
222     {
223         for (i = 0; (i < symbuf->bufsize) && (i < symtab->nr); i++)
224         {
225             sfree(symbuf->buf[i]);
226         }
227         symtab->nr -= i;
228         sfree(symbuf->buf);
229         freeptr = symbuf;
230         symbuf  = symbuf->next;
231         sfree(freeptr);
232     }
233     symtab->symbuf = NULL;
234     if (symtab->nr != 0)
235     {
236         gmx_incons("Freeing symbol table (symtab) structure");
237     }
238 }
239
240 void free_symtab(t_symtab *symtab)
241 {
242     t_symbuf *symbuf, *freeptr;
243
244     close_symtab(symtab);
245     symbuf = symtab->symbuf;
246     while (symbuf != NULL)
247     {
248         symtab->nr -= std::min(symbuf->bufsize, symtab->nr);
249         freeptr     = symbuf;
250         symbuf      = symbuf->next;
251         sfree(freeptr);
252     }
253     symtab->symbuf = NULL;
254     if (symtab->nr != 0)
255     {
256         gmx_incons("Freeing symbol table (symtab) structure");
257     }
258 }
259
260 void pr_symtab(FILE *fp, int indent, const char *title, t_symtab *symtab)
261 {
262     int       i, j, nr;
263     t_symbuf *symbuf;
264
265     if (available(fp, symtab, indent, title))
266     {
267         indent = pr_title_n(fp, indent, title, symtab->nr);
268         i      = 0;
269         nr     = symtab->nr;
270         symbuf = symtab->symbuf;
271         while (symbuf != NULL)
272         {
273             for (j = 0; (j < symbuf->bufsize) && (j < nr); j++)
274             {
275                 pr_indent(fp, indent);
276                 (void) fprintf(fp, "%s[%d]=\"%s\"\n", title, i++, symbuf->buf[j]);
277             }
278             nr    -= j;
279             symbuf = symbuf->next;
280         }
281         if (nr != 0)
282         {
283             gmx_incons("Printing symbol table (symtab) structure");
284         }
285     }
286 }