4 * This source code is part of
8 * GROningen MAchine for Chemical Simulations
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
34 * GROningen Mixture of Alchemy and Childrens' Stories
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.
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
++);
71 int lookup_symtab(t_symtab
*symtab
,char **name
)
78 symbuf
=symtab
->symbuf
;
79 while (symbuf
!=NULL
) {
80 index
=name
-symbuf
->buf
;
81 if ( ( index
>= 0 ) && ( index
< symbuf
->bufsize
) )
84 base
+=symbuf
->bufsize
;
88 fatal_error(0,"symtab lookup \"%s\" not found",*name
);
92 char **get_symtab_handle(t_symtab
*symtab
,int name
)
96 symbuf
=symtab
->symbuf
;
97 while (symbuf
!=NULL
) {
98 if (name
<symbuf
->bufsize
)
99 return &(symbuf
->buf
[name
]);
101 name
-=symbuf
->bufsize
;
105 fatal_error(0,"symtab get_symtab_handle %d not found",name
);
109 static t_symbuf
*new_symbuf(void)
114 symbuf
->bufsize
=TABLESIZE
;
115 snew(symbuf
->buf
,symbuf
->bufsize
);
121 static char **enter_buf(t_symtab
*symtab
,char *name
)
127 if (symtab
->symbuf
== NULL
)
128 symtab
->symbuf
=new_symbuf();
130 symbuf
=symtab
->symbuf
;
132 for(i
=0; (i
< symbuf
->bufsize
); i
++) {
133 if (symbuf
->buf
[i
]==NULL
) {
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
) {
148 symbuf
->next
=new_symbuf();
152 symbuf
->buf
[0]=strdup(name
);
153 return &(symbuf
->buf
[0]);
156 char **put_symtab(t_symtab
*symtab
,const char *name
)
160 return enter_buf(symtab
,trim_string(name
,buf
,255));
163 void open_symtab(t_symtab
*symtab
)
169 void close_symtab(t_symtab
*symtab
)
173 void done_symtab(t_symtab
*symtab
)
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
]);
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
);
206 assert(symtab
->nr
==0);
209 void pr_symtab(FILE *fp
,int indent
,char *title
,t_symtab
*symtab
)
214 if (available(fp
,symtab
,title
))
216 indent
=pr_title_n(fp
,indent
,title
,symtab
->nr
);
219 symbuf
=symtab
->symbuf
;
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
]);