3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
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
33 * GROningen Mixture of Alchemy and Childrens' Stories
42 #include "gromacs/fileio/futil.h"
44 #include "gmx_fatal.h"
47 gmx_bool get_a_line(FILE *fp, char line[], int n)
56 if (!fgets(line0, n+1, fp))
61 dum = strchr(line0, '\n');
66 else if (strlen(line0) == n)
68 fprintf(stderr, "Warning: line length exceeds buffer length (%d), data might be corrupted\n", n);
73 fprintf(stderr, "Warning: file does not end with a newline, last line:\n%s\n",
76 dum = strchr(line0, ';');
81 strncpy(line, line0, n);
85 while (dum[0] == '\0');
91 gmx_bool get_header(char line[], char *header)
93 char temp[STRLEN], *dum;
96 dum = strchr(temp, '[');
102 dum = strchr(temp, ']');
105 gmx_fatal(FARGS, "header is not terminated on line:\n'%s'\n", line);
109 if (sscanf(temp, "%s%*s", header) != 1)
117 int get_strings(const char *db, char ***strings)
126 if (fscanf(in, "%d", &nstr) != 1)
128 gmx_warning("File %s is empty", db);
133 for (i = 0; (i < nstr); i++)
135 if (1 != fscanf(in, "%s", buf))
137 gmx_fatal(FARGS, "Cannot read string from buffer");
140 fprintf(stderr, "Have read: %s\n", buf);
142 ptr[i] = strdup(buf);
151 int search_str(int nstr, char **str, char *key)
156 for (i = 0; (i < nstr); i++)
158 if (gmx_strcasecmp(str[i], key) == 0)
167 int fget_lines(FILE *in, char ***strings)
174 pret = fgets(buf, 255, in);
175 if (pret == NULL || sscanf(buf, "%d", &nstr) != 1)
177 gmx_warning("File is empty");
183 for (i = 0; (i < nstr); i++)
185 fgets2(buf, 255, in);
186 ptr[i] = gmx_strdup(buf);
194 int get_lines(const char *db, char ***strings)
200 nstr = fget_lines(in, strings);
206 int get_file(const char *db, char ***strings)
216 while (fgets2(buf, STRLEN-1, in))
223 ptr[i] = strdup(buf);