91a9ef713ea2dd4ca275934d4926112856825a94
[alexxy/gromacs.git] / src / gmxlib / strdb.c
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.
14
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.
19  * 
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.
26  * 
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.
29  * 
30  * For more info, check our website at http://www.gromacs.org
31  * 
32  * And Hey:
33  * GROningen Mixture of Alchemy and Childrens' Stories
34  */
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
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"
46
47 gmx_bool get_a_line(FILE *fp,char line[],int n)
48 {
49   char *line0;
50   char *dum;
51   
52   snew(line0,n+1);
53  
54   do {
55     if (!fgets(line0,n+1,fp)) {
56       sfree(line0);
57       return FALSE;
58     }
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'); 
75   
76   sfree(line0);
77   return TRUE;
78 }
79
80 gmx_bool get_header(char line[],char *header)
81 {
82   char temp[STRLEN],*dum;
83
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;
93   }
94   dum[0]='\0';
95   if (sscanf(temp,"%s%*s",header) != 1)
96     return FALSE;
97
98   return TRUE;
99 }
100
101 int get_strings(const char *db,char ***strings)
102 {
103   FILE *in;
104   char **ptr;
105   char buf[256];
106   int  i,nstr;
107
108   in=libopen(db);
109   
110   if (fscanf(in,"%d",&nstr) != 1) {
111     gmx_warning("File %s is empty",db);
112     ffclose(in);
113     return 0;
114   }
115   snew(ptr,nstr);
116   for(i=0; (i<nstr); i++) {
117     if(1 != fscanf(in,"%s",buf))
118     { 
119       gmx_fatal(FARGS,"Cannot read string from buffer");
120     }
121 #ifdef DEBUG
122     fprintf(stderr,"Have read: %s\n",buf);
123 #endif
124     ptr[i] = strdup(buf);
125   }
126   ffclose(in);
127
128   *strings=ptr;
129   
130   return nstr;
131 }
132
133 int search_str(int nstr,char **str,char *key)
134 {
135   int i;
136
137   /* Linear search */
138   for(i=0; (i<nstr); i++)
139     if (gmx_strcasecmp(str[i],key)==0)
140       return i;
141
142   return -1;
143 }
144
145 int fget_lines(FILE *in,char ***strings)
146 {
147   char **ptr;
148   char buf[256];
149   int  i,nstr;
150   char *pret;
151
152   pret = fgets(buf,255,in);  
153   if ( pret==NULL  || sscanf(buf,"%d",&nstr) != 1) 
154   {
155     gmx_warning("File is empty");
156     ffclose(in);
157     
158     return 0;
159   }
160   snew(ptr,nstr);
161   for(i=0; (i<nstr); i++) {
162     fgets2(buf,255,in);
163     ptr[i] = gmx_strdup(buf);
164   }
165   
166   (*strings) = ptr;
167   
168   return nstr;
169 }
170
171 int get_lines(const char *db,char ***strings)
172 {
173   FILE *in;
174   int  nstr;
175   
176   in   = libopen(db);
177   nstr = fget_lines(in,strings);
178   ffclose(in);
179
180   return nstr;
181 }
182
183 int get_file(const char *db,char ***strings)
184 {
185   FILE *in;
186   char **ptr=NULL;
187   char buf[STRLEN];
188   int  i,nstr,maxi;
189
190   in=libopen(db);
191   
192   i=maxi=0;
193   while (fgets2(buf,STRLEN-1,in)) {
194     if (i>=maxi) {
195       maxi+=50;
196       srenew(ptr,maxi);
197     }
198     ptr[i] = strdup(buf);
199     i++;
200   }
201   nstr=i;
202   ffclose(in);
203   srenew(ptr,nstr);
204   *strings=ptr;
205   
206   return nstr;
207 }
208