73eff50ba6620c5feb5dacf48f61523b781b5419
[alexxy/gromacs.git] / src / gmxlib / symtab.c
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  * check out http://www.gromacs.org for more information.
7  * Copyright (c) 2012, by the GROMACS development team, led by
8  * David van der Spoel, Berk Hess, Erik Lindahl, and including many
9  * others, as listed in the AUTHORS file in the top-level source
10  * directory and at http://www.gromacs.org.
11  *
12  * GROMACS is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public License
14  * as published by the Free Software Foundation; either version 2.1
15  * of the License, or (at your option) any later version.
16  *
17  * GROMACS is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with GROMACS; if not, see
24  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
26  *
27  * If you want to redistribute modifications to GROMACS, please
28  * consider that scientific software is very special. Version
29  * control is crucial - bugs must be traceable. We will be happy to
30  * consider code for inclusion in the official distribution, but
31  * derived work must not be called official GROMACS. Details are found
32  * in the README & COPYING files - if they are missing, get the
33  * official version at http://www.gromacs.org.
34  *
35  * To help us fund GROMACS development, we humbly ask that you cite
36  * the research papers on the package. Check out http://www.gromacs.org.
37  */
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
41
42 #include <stdio.h>
43 #include <string.h>
44 #include "sysstuff.h"
45 #include "string2.h"
46 #include "typedefs.h"
47 #include "gmx_fatal.h"
48 #include "smalloc.h"
49 #include "txtdump.h"
50 #include "symtab.h"
51 #include "macros.h"
52
53 #define BUFSIZE                 1024
54 #define TABLESIZE               5
55
56 static char *trim_string(const char *s,char *out, int maxlen)
57      /*
58       * Returns a pointer to a static area which contains a copy 
59       * of s without leading or trailing spaces. Strings are
60       * truncated to BUFSIZE positions.
61       */      
62 {
63   int len,i;
64  
65   if(strlen(s)>(size_t)(maxlen-1))
66     gmx_fatal(FARGS,"String '%s' (%d) is longer than buffer (%d).\n", 
67               s, strlen(s), maxlen-1);
68   
69   for (; (*s)&&((*s)==' '); s++);
70   for (len=strlen(s); (len>0); len--) if (s[len-1]!=' ') break;
71   if (len>=BUFSIZE) len=BUFSIZE-1;
72   for (i=0; i<len; i++) out[i]=*(s++);
73   out[i]=0;
74   return out;
75 }
76
77 int lookup_symtab(t_symtab *symtab,char **name)
78 {
79   int base,index;
80   t_symbuf *symbuf;
81   
82   base=0;
83   index=0;
84   symbuf=symtab->symbuf;
85   while (symbuf!=NULL) {
86     index=name-symbuf->buf;
87     if ( ( index >= 0 ) && ( index < symbuf->bufsize ) )
88       return index+base;
89     else {
90       base+=symbuf->bufsize;
91       symbuf=symbuf->next;
92     }
93   }
94   gmx_fatal(FARGS,"symtab lookup \"%s\" not found",*name);
95   return -1;
96 }
97
98 char **get_symtab_handle(t_symtab *symtab,int name)
99 {
100   t_symbuf *symbuf;
101   
102   symbuf=symtab->symbuf;
103   while (symbuf!=NULL) {
104     if (name<symbuf->bufsize)
105       return &(symbuf->buf[name]);
106     else {
107       name-=symbuf->bufsize;
108       symbuf=symbuf->next;
109     }
110   }
111   gmx_fatal(FARGS,"symtab get_symtab_handle %d not found",name);
112   return NULL;
113 }
114
115 static t_symbuf *new_symbuf(void)
116 {
117   t_symbuf *symbuf;
118
119   snew(symbuf,1);
120   symbuf->bufsize=TABLESIZE;
121   snew(symbuf->buf,symbuf->bufsize);
122   symbuf->next=NULL;
123
124   return symbuf;
125 }
126
127 static char **enter_buf(t_symtab *symtab,char *name)
128 {
129   int      i;
130   t_symbuf *symbuf;
131   gmx_bool     bCont;
132   
133   if (symtab->symbuf == NULL)
134     symtab->symbuf=new_symbuf();
135
136   symbuf=symtab->symbuf;
137   do {
138     for(i=0; (i < symbuf->bufsize); i++) {
139       if (symbuf->buf[i]==NULL) {
140         symtab->nr++;
141         symbuf->buf[i]=strdup(name);
142         return &(symbuf->buf[i]);
143       } else if (strcmp(symbuf->buf[i],name)==0)
144         return &(symbuf->buf[i]);
145     }
146     if (symbuf->next != NULL) {
147       symbuf=symbuf->next;
148       bCont = TRUE;
149     }
150     else
151       bCont = FALSE;
152   } while (bCont);
153
154   symbuf->next=new_symbuf();
155   symbuf=symbuf->next;
156
157   symtab->nr++;
158   symbuf->buf[0]=strdup(name);
159   return &(symbuf->buf[0]);
160 }
161
162 char **put_symtab(t_symtab *symtab,const char *name)
163 {
164   char buf[1024];
165   
166   return enter_buf(symtab,trim_string(name,buf,1023));
167 }
168
169 void open_symtab(t_symtab *symtab)
170 {
171   symtab->nr=0;
172   symtab->symbuf=NULL;
173 }
174
175 void close_symtab(t_symtab *symtab)
176 {
177 }
178
179 void done_symtab(t_symtab *symtab)
180 {
181   int i;
182   t_symbuf *symbuf,*freeptr;
183   
184   close_symtab(symtab);
185   symbuf=symtab->symbuf;
186   while (symbuf!=NULL) {
187     for (i=0; (i < symbuf->bufsize) && (i < symtab->nr); i++)
188       sfree(symbuf->buf[i]);
189     symtab->nr-=i;
190     sfree(symbuf->buf);
191     freeptr=symbuf;
192     symbuf=symbuf->next;
193     sfree(freeptr);
194   }
195   symtab->symbuf=NULL;
196   if (symtab->nr != 0)
197     gmx_incons("Freeing symbol table (symtab) structure");
198 }
199
200 void free_symtab(t_symtab *symtab)
201 {
202   t_symbuf *symbuf,*freeptr;
203   
204   close_symtab(symtab);
205   symbuf=symtab->symbuf;
206   while (symbuf!=NULL) {
207     symtab->nr-=min(symbuf->bufsize,symtab->nr);
208     freeptr=symbuf;
209     symbuf=symbuf->next;
210     sfree(freeptr);
211   }
212   symtab->symbuf=NULL;
213   if (symtab->nr != 0)
214     gmx_incons("Freeing symbol table (symtab) structure");
215 }
216
217 void pr_symtab(FILE *fp,int indent,const char *title,t_symtab *symtab)
218 {
219   int i,j,nr;
220   t_symbuf *symbuf;
221   
222   if (available(fp,symtab,indent,title)) 
223     {
224       indent=pr_title_n(fp,indent,title,symtab->nr);
225       i=0;
226       nr=symtab->nr;
227       symbuf=symtab->symbuf;
228       while (symbuf!=NULL)
229         {
230           for (j=0; (j < symbuf->bufsize) && (j < nr); j++)
231             {
232               pr_indent(fp,indent);
233               (void) fprintf(fp,"%s[%d]=\"%s\"\n",title,i++,symbuf->buf[j]);
234             }
235           nr-=j;
236           symbuf=symbuf->next;
237         }
238       if (nr != 0)
239         gmx_incons("Printing symbol table (symtab) structure");
240     }
241 }