changed reading hint
[gromacs/adressmacs.git] / src / gmxlib / strdb.c
blob9fb1f3bfc220f601699541749b92a6ed69502d17
1 /*
2 * $Id$
3 *
4 * This source code is part of
5 *
6 * G R O M A C S
7 *
8 * GROningen MAchine for Chemical Simulations
9 *
10 * VERSION 2.0
12 * Copyright (c) 1991-1999
13 * BIOSON Research Institute, Dept. of Biophysical Chemistry
14 * University of Groningen, The Netherlands
16 * Please refer to:
17 * GROMACS: A message-passing parallel molecular dynamics implementation
18 * H.J.C. Berendsen, D. van der Spoel and R. van Drunen
19 * Comp. Phys. Comm. 91, 43-56 (1995)
21 * Also check out our WWW page:
22 * http://md.chem.rug.nl/~gmx
23 * or e-mail to:
24 * gromacs@chem.rug.nl
26 * And Hey:
27 * Green Red Orange Magenta Azure Cyan Skyblue
29 static char *SRCID_strdb_c = "$Id$";
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include "string2.h"
34 #include "futil.h"
35 #include "smalloc.h"
36 #include "fatal.h"
37 #include "strdb.h"
39 bool get_a_line(FILE *fp,char line[],int n)
41 static char *line0=NULL;
42 static int nalloc=0;
43 char *dum;
45 if (n>nalloc) {
46 nalloc=n;
47 srenew(line0,nalloc+1);
49 do {
50 if (!fgets(line0,n+1,fp)) {
51 return FALSE;
53 dum=strchr(line0,'\n');
54 if (dum)
55 dum[0]='\0';
56 else if (strlen(line0)==n) {
57 fprintf(stderr,"Warning: line length exceeds buffer length (%d), data might be corrupted\n",n);
58 line0[n-1] ='\0';
59 } else
60 fprintf(stderr,"Warning: file does end with a newline, last line:\n%s\n",
61 line0);
62 dum=strchr(line0,';');
63 if (dum)
64 dum[0]='\0';
65 strcpy(line,line0);
66 dum=line0;
67 ltrim(dum);
68 } while (dum[0] == '\0');
70 return TRUE;
73 bool get_header(char line[],char *header)
75 char temp[STRLEN],*dum;
77 strcpy(temp,line);
78 dum=strchr(temp,'[');
79 if (dum==NULL)
80 return FALSE;
81 dum[0]=' ';
82 dum=strchr(temp,']');
83 if (dum==NULL) {
84 fatal_error(0,"header is not terminated on line:\n'%s'\n",line);
85 return FALSE;
87 dum[0]='\0';
88 if (sscanf(temp,"%s%*s",header) != 1)
89 return FALSE;
91 return TRUE;
94 int get_strings(char *db,char ***strings)
96 FILE *in;
97 char **ptr;
98 char buf[256];
99 int i,nstr;
101 in=libopen(db);
103 set_warning_line(db,1);
104 if (fscanf(in,"%d",&nstr) != 1) {
105 sprintf(warn_buf,"File %s is empty",db);
106 warning(NULL);
107 fclose(in);
108 return 0;
110 snew(ptr,nstr);
111 for(i=0; (i<nstr); i++) {
112 fscanf(in,"%s",buf);
113 #ifdef DEBUG
114 fprintf(stderr,"Have read: %s\n",buf);
115 #endif
116 ptr[i] = strdup(buf);
118 fclose(in);
120 *strings=ptr;
122 return nstr;
125 int search_str(int nstr,char **str,char *key)
127 int i;
129 /* Linear search */
130 for(i=0; (i<nstr); i++)
131 if (strcasecmp(str[i],key)==0)
132 return i;
134 return -1;
137 int fget_lines(FILE *in,char ***strings)
139 char **ptr;
140 char buf[256];
141 int i,nstr;
143 fgets(buf,255,in);
144 if (sscanf(buf,"%d",&nstr) != 1) {
145 sprintf(warn_buf,"File is empty");
146 warning(NULL);
147 fclose(in);
149 return 0;
151 snew(ptr,nstr);
152 for(i=0; (i<nstr); i++) {
153 fgets2(buf,255,in);
154 ptr[i] = strdup(buf);
157 (*strings) = ptr;
159 return nstr;
162 int get_lines(char *db,char ***strings)
164 FILE *in;
165 int nstr;
167 set_warning_line(db,1);
168 in = libopen(db);
169 nstr = fget_lines(in,strings);
170 fclose(in);
172 return nstr;
175 int get_file(char *db,char ***strings)
177 FILE *in;
178 char **ptr=NULL;
179 char buf[256];
180 int i,nstr,maxi;
182 in=libopen(db);
184 i=maxi=0;
185 while (fgets2(buf,255,in)) {
186 if (i>=maxi) {
187 maxi+=50;
188 srenew(ptr,maxi);
190 ptr[i] = strdup(buf);
191 i++;
193 nstr=i;
194 fclose(in);
195 srenew(ptr,nstr);
196 *strings=ptr;
198 return nstr;