Fixing copyright issues and code contributors
[alexxy/gromacs.git] / src / gmxlib / strdb.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,2013, 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 <stdlib.h>
44 #include "string2.h"
45 #include "futil.h"
46 #include "smalloc.h"
47 #include "gmx_fatal.h"
48 #include "strdb.h"
49
50 gmx_bool get_a_line(FILE *fp,char line[],int n)
51 {
52   char *line0;
53   char *dum;
54   
55   snew(line0,n+1);
56  
57   do {
58     if (!fgets(line0,n+1,fp)) {
59       sfree(line0);
60       return FALSE;
61     }
62     dum=strchr(line0,'\n');
63     if (dum) 
64       dum[0]='\0';
65     else if (strlen(line0)==n) {
66       fprintf(stderr,"Warning: line length exceeds buffer length (%d), data might be corrupted\n",n);
67       line0[n-1] ='\0';
68     } else
69       fprintf(stderr,"Warning: file does not end with a newline, last line:\n%s\n",
70               line0);
71     dum=strchr(line0,';');
72     if (dum) 
73       dum[0]='\0';
74     strncpy(line,line0,n);
75     dum=line0;
76     ltrim(dum);
77   } while (dum[0] == '\0'); 
78   
79   sfree(line0);
80   return TRUE;
81 }
82
83 gmx_bool get_header(char line[],char *header)
84 {
85   char temp[STRLEN],*dum;
86
87   strcpy(temp,line);
88   dum=strchr(temp,'[');
89   if (dum==NULL)
90     return FALSE;
91   dum[0]=' ';
92   dum=strchr(temp,']');
93   if (dum==NULL) {
94     gmx_fatal(FARGS,"header is not terminated on line:\n'%s'\n",line); 
95     return FALSE;
96   }
97   dum[0]='\0';
98   if (sscanf(temp,"%s%*s",header) != 1)
99     return FALSE;
100
101   return TRUE;
102 }
103
104 int get_strings(const char *db,char ***strings)
105 {
106   FILE *in;
107   char **ptr;
108   char buf[256];
109   int  i,nstr;
110
111   in=libopen(db);
112   
113   if (fscanf(in,"%d",&nstr) != 1) {
114     gmx_warning("File %s is empty",db);
115     ffclose(in);
116     return 0;
117   }
118   snew(ptr,nstr);
119   for(i=0; (i<nstr); i++) {
120     if(1 != fscanf(in,"%s",buf))
121     { 
122       gmx_fatal(FARGS,"Cannot read string from buffer");
123     }
124 #ifdef DEBUG
125     fprintf(stderr,"Have read: %s\n",buf);
126 #endif
127     ptr[i] = strdup(buf);
128   }
129   ffclose(in);
130
131   *strings=ptr;
132   
133   return nstr;
134 }
135
136 int search_str(int nstr,char **str,char *key)
137 {
138   int i;
139
140   /* Linear search */
141   for(i=0; (i<nstr); i++)
142     if (gmx_strcasecmp(str[i],key)==0)
143       return i;
144
145   return -1;
146 }
147
148 int fget_lines(FILE *in,char ***strings)
149 {
150   char **ptr;
151   char buf[256];
152   int  i,nstr;
153   char *pret;
154
155   pret = fgets(buf,255,in);  
156   if ( pret==NULL  || sscanf(buf,"%d",&nstr) != 1) 
157   {
158     gmx_warning("File is empty");
159     ffclose(in);
160     
161     return 0;
162   }
163   snew(ptr,nstr);
164   for(i=0; (i<nstr); i++) {
165     fgets2(buf,255,in);
166     ptr[i] = gmx_strdup(buf);
167   }
168   
169   (*strings) = ptr;
170   
171   return nstr;
172 }
173
174 int get_lines(const char *db,char ***strings)
175 {
176   FILE *in;
177   int  nstr;
178   
179   in   = libopen(db);
180   nstr = fget_lines(in,strings);
181   ffclose(in);
182
183   return nstr;
184 }
185
186 int get_file(const char *db,char ***strings)
187 {
188   FILE *in;
189   char **ptr=NULL;
190   char buf[STRLEN];
191   int  i,nstr,maxi;
192
193   in=libopen(db);
194   
195   i=maxi=0;
196   while (fgets2(buf,STRLEN-1,in)) {
197     if (i>=maxi) {
198       maxi+=50;
199       srenew(ptr,maxi);
200     }
201     ptr[i] = strdup(buf);
202     i++;
203   }
204   nstr=i;
205   ffclose(in);
206   srenew(ptr,nstr);
207   *strings=ptr;
208   
209   return nstr;
210 }
211