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