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