Upped the version to 3.2.0
[gromacs.git] / src / gmxlib / symtab.c
bloba7737883b8d265c108b1e10708f8f7f404fbe3fb
1 /*
2 * $Id$
3 *
4 * This source code is part of
5 *
6 * G R O M A C S
7 *
8 * GROningen MAchine for Chemical Simulations
9 *
10 * VERSION 3.2.0
11 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
12 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
13 * Copyright (c) 2001-2004, The GROMACS development team,
14 * check out http://www.gromacs.org for more information.
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version 2
19 * of the License, or (at your option) any later version.
21 * If you want to redistribute modifications, please consider that
22 * scientific software is very special. Version control is crucial -
23 * bugs must be traceable. We will be happy to consider code for
24 * inclusion in the official distribution, but derived work must not
25 * be called official GROMACS. Details are found in the README & COPYING
26 * files - if they are missing, get the official version at www.gromacs.org.
28 * To help us fund GROMACS development, we humbly ask that you cite
29 * the papers on the package - you can find them in the top README file.
31 * For more info, check our website at http://www.gromacs.org
33 * And Hey:
34 * GROningen Mixture of Alchemy and Childrens' Stories
36 #include <stdio.h>
37 #include <string.h>
38 #include "sysstuff.h"
39 #include "string2.h"
40 #include "assert.h"
41 #include "typedefs.h"
42 #include "fatal.h"
43 #include "smalloc.h"
44 #include "txtdump.h"
45 #include "symtab.h"
46 #include "macros.h"
48 #define BUFSIZE 1024
49 #define TABLESIZE 5
51 static char *trim_string(char *s,char *out, int maxlen)
53 * Returns a pointer to a static area which contains a copy
54 * of s without leading or trailing spaces. Strings are
55 * truncated to BUFSIZE positions.
56 */
58 int len,i;
60 if(strlen(s)>maxlen-1)
61 fatal_error(0,"Character buffer size too small\n");
63 for (; (*s)&&((*s)==' '); s++);
64 for (len=strlen(s); (len>0); len--) if (s[len-1]!=' ') break;
65 if (len>=BUFSIZE) len=BUFSIZE-1;
66 for (i=0; i<len; i++) out[i]=*(s++);
67 out[i]=0;
68 return out;
71 int lookup_symtab(t_symtab *symtab,char **name)
73 int base,index;
74 t_symbuf *symbuf;
76 base=0;
77 index=0;
78 symbuf=symtab->symbuf;
79 while (symbuf!=NULL) {
80 index=name-symbuf->buf;
81 if ( ( index >= 0 ) && ( index < symbuf->bufsize ) )
82 return index+base;
83 else {
84 base+=symbuf->bufsize;
85 symbuf=symbuf->next;
88 fatal_error(0,"symtab lookup \"%s\" not found",*name);
89 return -1;
92 char **get_symtab_handle(t_symtab *symtab,int name)
94 t_symbuf *symbuf;
96 symbuf=symtab->symbuf;
97 while (symbuf!=NULL) {
98 if (name<symbuf->bufsize)
99 return &(symbuf->buf[name]);
100 else {
101 name-=symbuf->bufsize;
102 symbuf=symbuf->next;
105 fatal_error(0,"symtab get_symtab_handle %d not found",name);
106 return NULL;
109 static t_symbuf *new_symbuf(void)
111 t_symbuf *symbuf;
113 snew(symbuf,1);
114 symbuf->bufsize=TABLESIZE;
115 snew(symbuf->buf,symbuf->bufsize);
116 symbuf->next=NULL;
118 return symbuf;
121 static char **enter_buf(t_symtab *symtab,char *name)
123 int i;
124 t_symbuf *symbuf;
125 bool bCont;
127 if (symtab->symbuf == NULL)
128 symtab->symbuf=new_symbuf();
130 symbuf=symtab->symbuf;
131 do {
132 for(i=0; (i < symbuf->bufsize); i++) {
133 if (symbuf->buf[i]==NULL) {
134 symtab->nr++;
135 symbuf->buf[i]=strdup(name);
136 return &(symbuf->buf[i]);
137 } else if (strcmp(symbuf->buf[i],name)==0)
138 return &(symbuf->buf[i]);
140 if (symbuf->next != NULL) {
141 symbuf=symbuf->next;
142 bCont = TRUE;
144 else
145 bCont = FALSE;
146 } while (bCont);
148 symbuf->next=new_symbuf();
149 symbuf=symbuf->next;
151 symtab->nr++;
152 symbuf->buf[0]=strdup(name);
153 return &(symbuf->buf[0]);
156 char **put_symtab(t_symtab *symtab,const char *name)
158 char buf[256];
160 return enter_buf(symtab,trim_string(name,buf,255));
163 void open_symtab(t_symtab *symtab)
165 symtab->nr=0;
166 symtab->symbuf=NULL;
169 void close_symtab(t_symtab *symtab)
173 void done_symtab(t_symtab *symtab)
175 int i;
176 t_symbuf *symbuf,*freeptr;
178 close_symtab(symtab);
179 symbuf=symtab->symbuf;
180 while (symbuf!=NULL) {
181 for (i=0; (i < symbuf->bufsize) && (i < symtab->nr); i++)
182 sfree(symbuf->buf[i]);
183 symtab->nr-=i;
184 sfree(symbuf->buf);
185 freeptr=symbuf;
186 symbuf=symbuf->next;
187 sfree(freeptr);
189 symtab->symbuf=NULL;
190 assert(symtab->nr==0);
193 void free_symtab(t_symtab *symtab)
195 t_symbuf *symbuf,*freeptr;
197 close_symtab(symtab);
198 symbuf=symtab->symbuf;
199 while (symbuf!=NULL) {
200 symtab->nr-=min(symbuf->bufsize,symtab->nr);
201 freeptr=symbuf;
202 symbuf=symbuf->next;
203 sfree(freeptr);
205 symtab->symbuf=NULL;
206 assert(symtab->nr==0);
209 void pr_symtab(FILE *fp,int indent,char *title,t_symtab *symtab)
211 int i,j,nr;
212 t_symbuf *symbuf;
214 if (available(fp,symtab,title))
216 indent=pr_title_n(fp,indent,title,symtab->nr);
217 i=0;
218 nr=symtab->nr;
219 symbuf=symtab->symbuf;
220 while (symbuf!=NULL)
222 for (j=0; (j < symbuf->bufsize) && (j < nr); j++)
224 pr_indent(fp,indent);
225 (void) fprintf(fp,"%s[%d]=\"%s\"\n",title,i++,symbuf->buf[j]);
227 nr-=j;
228 symbuf=symbuf->next;
230 assert(nr==0);