Revised wording in pdb2gmx.c, hopefully clearer now.
[gromacs/rigid-bodies.git] / src / gmxlib / strdb.c
blob91a9ef713ea2dd4ca275934d4926112856825a94
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 <stdlib.h>
41 #include "string2.h"
42 #include "futil.h"
43 #include "smalloc.h"
44 #include "gmx_fatal.h"
45 #include "strdb.h"
47 gmx_bool get_a_line(FILE *fp,char line[],int n)
49 char *line0;
50 char *dum;
52 snew(line0,n+1);
54 do {
55 if (!fgets(line0,n+1,fp)) {
56 sfree(line0);
57 return FALSE;
59 dum=strchr(line0,'\n');
60 if (dum)
61 dum[0]='\0';
62 else if (strlen(line0)==n) {
63 fprintf(stderr,"Warning: line length exceeds buffer length (%d), data might be corrupted\n",n);
64 line0[n-1] ='\0';
65 } else
66 fprintf(stderr,"Warning: file does not end with a newline, last line:\n%s\n",
67 line0);
68 dum=strchr(line0,';');
69 if (dum)
70 dum[0]='\0';
71 strncpy(line,line0,n);
72 dum=line0;
73 ltrim(dum);
74 } while (dum[0] == '\0');
76 sfree(line0);
77 return TRUE;
80 gmx_bool get_header(char line[],char *header)
82 char temp[STRLEN],*dum;
84 strcpy(temp,line);
85 dum=strchr(temp,'[');
86 if (dum==NULL)
87 return FALSE;
88 dum[0]=' ';
89 dum=strchr(temp,']');
90 if (dum==NULL) {
91 gmx_fatal(FARGS,"header is not terminated on line:\n'%s'\n",line);
92 return FALSE;
94 dum[0]='\0';
95 if (sscanf(temp,"%s%*s",header) != 1)
96 return FALSE;
98 return TRUE;
101 int get_strings(const char *db,char ***strings)
103 FILE *in;
104 char **ptr;
105 char buf[256];
106 int i,nstr;
108 in=libopen(db);
110 if (fscanf(in,"%d",&nstr) != 1) {
111 gmx_warning("File %s is empty",db);
112 ffclose(in);
113 return 0;
115 snew(ptr,nstr);
116 for(i=0; (i<nstr); i++) {
117 if(1 != fscanf(in,"%s",buf))
119 gmx_fatal(FARGS,"Cannot read string from buffer");
121 #ifdef DEBUG
122 fprintf(stderr,"Have read: %s\n",buf);
123 #endif
124 ptr[i] = strdup(buf);
126 ffclose(in);
128 *strings=ptr;
130 return nstr;
133 int search_str(int nstr,char **str,char *key)
135 int i;
137 /* Linear search */
138 for(i=0; (i<nstr); i++)
139 if (gmx_strcasecmp(str[i],key)==0)
140 return i;
142 return -1;
145 int fget_lines(FILE *in,char ***strings)
147 char **ptr;
148 char buf[256];
149 int i,nstr;
150 char *pret;
152 pret = fgets(buf,255,in);
153 if ( pret==NULL || sscanf(buf,"%d",&nstr) != 1)
155 gmx_warning("File is empty");
156 ffclose(in);
158 return 0;
160 snew(ptr,nstr);
161 for(i=0; (i<nstr); i++) {
162 fgets2(buf,255,in);
163 ptr[i] = gmx_strdup(buf);
166 (*strings) = ptr;
168 return nstr;
171 int get_lines(const char *db,char ***strings)
173 FILE *in;
174 int nstr;
176 in = libopen(db);
177 nstr = fget_lines(in,strings);
178 ffclose(in);
180 return nstr;
183 int get_file(const char *db,char ***strings)
185 FILE *in;
186 char **ptr=NULL;
187 char buf[STRLEN];
188 int i,nstr,maxi;
190 in=libopen(db);
192 i=maxi=0;
193 while (fgets2(buf,STRLEN-1,in)) {
194 if (i>=maxi) {
195 maxi+=50;
196 srenew(ptr,maxi);
198 ptr[i] = strdup(buf);
199 i++;
201 nstr=i;
202 ffclose(in);
203 srenew(ptr,nstr);
204 *strings=ptr;
206 return nstr;