Revised wording in pdb2gmx.c, hopefully clearer now.
[gromacs/rigid-bodies.git] / src / gmxlib / symtab.c
blob76bc2a8222a8056c8a01db9325d01e8337415986
1 /*
2 *
3 * This source code is part of
4 *
5 * G R O M A C S
6 *
7 * GROningen MAchine for Chemical Simulations
8 *
9 * VERSION 3.2.0
10 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
11 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
12 * Copyright (c) 2001-2004, The GROMACS development team,
13 * check out http://www.gromacs.org for more information.
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * If you want to redistribute modifications, please consider that
21 * scientific software is very special. Version control is crucial -
22 * bugs must be traceable. We will be happy to consider code for
23 * inclusion in the official distribution, but derived work must not
24 * be called official GROMACS. Details are found in the README & COPYING
25 * files - if they are missing, get the official version at www.gromacs.org.
27 * To help us fund GROMACS development, we humbly ask that you cite
28 * the papers on the package - you can find them in the top README file.
30 * For more info, check our website at http://www.gromacs.org
32 * And Hey:
33 * GROningen Mixture of Alchemy and Childrens' Stories
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
39 #include <stdio.h>
40 #include <string.h>
41 #include "sysstuff.h"
42 #include "string2.h"
43 #include "typedefs.h"
44 #include "gmx_fatal.h"
45 #include "smalloc.h"
46 #include "txtdump.h"
47 #include "symtab.h"
48 #include "macros.h"
50 #define BUFSIZE 1024
51 #define TABLESIZE 5
53 static char *trim_string(const char *s,char *out, int maxlen)
55 * Returns a pointer to a static area which contains a copy
56 * of s without leading or trailing spaces. Strings are
57 * truncated to BUFSIZE positions.
58 */
60 int len,i;
62 if(strlen(s)>(size_t)(maxlen-1))
63 gmx_fatal(FARGS,"Character buffer size too small\n");
65 for (; (*s)&&((*s)==' '); s++);
66 for (len=strlen(s); (len>0); len--) if (s[len-1]!=' ') break;
67 if (len>=BUFSIZE) len=BUFSIZE-1;
68 for (i=0; i<len; i++) out[i]=*(s++);
69 out[i]=0;
70 return out;
73 int lookup_symtab(t_symtab *symtab,char **name)
75 int base,index;
76 t_symbuf *symbuf;
78 base=0;
79 index=0;
80 symbuf=symtab->symbuf;
81 while (symbuf!=NULL) {
82 index=name-symbuf->buf;
83 if ( ( index >= 0 ) && ( index < symbuf->bufsize ) )
84 return index+base;
85 else {
86 base+=symbuf->bufsize;
87 symbuf=symbuf->next;
90 gmx_fatal(FARGS,"symtab lookup \"%s\" not found",*name);
91 return -1;
94 char **get_symtab_handle(t_symtab *symtab,int name)
96 t_symbuf *symbuf;
98 symbuf=symtab->symbuf;
99 while (symbuf!=NULL) {
100 if (name<symbuf->bufsize)
101 return &(symbuf->buf[name]);
102 else {
103 name-=symbuf->bufsize;
104 symbuf=symbuf->next;
107 gmx_fatal(FARGS,"symtab get_symtab_handle %d not found",name);
108 return NULL;
111 static t_symbuf *new_symbuf(void)
113 t_symbuf *symbuf;
115 snew(symbuf,1);
116 symbuf->bufsize=TABLESIZE;
117 snew(symbuf->buf,symbuf->bufsize);
118 symbuf->next=NULL;
120 return symbuf;
123 static char **enter_buf(t_symtab *symtab,char *name)
125 int i;
126 t_symbuf *symbuf;
127 gmx_bool bCont;
129 if (symtab->symbuf == NULL)
130 symtab->symbuf=new_symbuf();
132 symbuf=symtab->symbuf;
133 do {
134 for(i=0; (i < symbuf->bufsize); i++) {
135 if (symbuf->buf[i]==NULL) {
136 symtab->nr++;
137 symbuf->buf[i]=strdup(name);
138 return &(symbuf->buf[i]);
139 } else if (strcmp(symbuf->buf[i],name)==0)
140 return &(symbuf->buf[i]);
142 if (symbuf->next != NULL) {
143 symbuf=symbuf->next;
144 bCont = TRUE;
146 else
147 bCont = FALSE;
148 } while (bCont);
150 symbuf->next=new_symbuf();
151 symbuf=symbuf->next;
153 symtab->nr++;
154 symbuf->buf[0]=strdup(name);
155 return &(symbuf->buf[0]);
158 char **put_symtab(t_symtab *symtab,const char *name)
160 char buf[256];
162 return enter_buf(symtab,trim_string(name,buf,255));
165 void open_symtab(t_symtab *symtab)
167 symtab->nr=0;
168 symtab->symbuf=NULL;
171 void close_symtab(t_symtab *symtab)
175 void done_symtab(t_symtab *symtab)
177 int i;
178 t_symbuf *symbuf,*freeptr;
180 close_symtab(symtab);
181 symbuf=symtab->symbuf;
182 while (symbuf!=NULL) {
183 for (i=0; (i < symbuf->bufsize) && (i < symtab->nr); i++)
184 sfree(symbuf->buf[i]);
185 symtab->nr-=i;
186 sfree(symbuf->buf);
187 freeptr=symbuf;
188 symbuf=symbuf->next;
189 sfree(freeptr);
191 symtab->symbuf=NULL;
192 if (symtab->nr != 0)
193 gmx_incons("Freeing symbol table (symtab) structure");
196 void free_symtab(t_symtab *symtab)
198 t_symbuf *symbuf,*freeptr;
200 close_symtab(symtab);
201 symbuf=symtab->symbuf;
202 while (symbuf!=NULL) {
203 symtab->nr-=min(symbuf->bufsize,symtab->nr);
204 freeptr=symbuf;
205 symbuf=symbuf->next;
206 sfree(freeptr);
208 symtab->symbuf=NULL;
209 if (symtab->nr != 0)
210 gmx_incons("Freeing symbol table (symtab) structure");
213 void pr_symtab(FILE *fp,int indent,const char *title,t_symtab *symtab)
215 int i,j,nr;
216 t_symbuf *symbuf;
218 if (available(fp,symtab,indent,title))
220 indent=pr_title_n(fp,indent,title,symtab->nr);
221 i=0;
222 nr=symtab->nr;
223 symbuf=symtab->symbuf;
224 while (symbuf!=NULL)
226 for (j=0; (j < symbuf->bufsize) && (j < nr); j++)
228 pr_indent(fp,indent);
229 (void) fprintf(fp,"%s[%d]=\"%s\"\n",title,i++,symbuf->buf[j]);
231 nr-=j;
232 symbuf=symbuf->next;
234 if (nr != 0)
235 gmx_incons("Printing symbol table (symtab) structure");