Framework for printing help for selections.
[gromacs/adressmacs.git] / src / gmxlib / strdb.c
bloba42e134c6855a833b95eb774e5e369f2033b18ff
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.
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
32 * And Hey:
33 * GROningen Mixture of Alchemy and Childrens' Stories
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
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"
47 bool get_a_line(FILE *fp,char line[],int n)
49 char *line0;
50 char *dum;
52 snew(line0,n+1);
54 do {
55 if (!fgets(line0,n+1,fp)) {
56 sfree(line0);
57 return FALSE;
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');
76 sfree(line0);
77 return TRUE;
80 bool get_header(char line[],char *header)
82 char temp[STRLEN],*dum;
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;
94 dum[0]='\0';
95 if (sscanf(temp,"%s%*s",header) != 1)
96 return FALSE;
98 return TRUE;
101 int get_strings(const char *db,char ***strings)
103 FILE *in;
104 char **ptr;
105 char buf[256];
106 int i,nstr;
108 in=libopen(db);
110 set_warning_line(db,1);
111 if (fscanf(in,"%d",&nstr) != 1) {
112 sprintf(warn_buf,"File %s is empty",db);
113 warning(NULL);
114 fclose(in);
115 return 0;
117 snew(ptr,nstr);
118 for(i=0; (i<nstr); i++) {
119 if(1 != fscanf(in,"%s",buf))
121 gmx_fatal(FARGS,"Cannot read string from buffer");
123 #ifdef DEBUG
124 fprintf(stderr,"Have read: %s\n",buf);
125 #endif
126 ptr[i] = strdup(buf);
128 fclose(in);
130 *strings=ptr;
132 return nstr;
135 int search_str(int nstr,char **str,char *key)
137 int i;
139 /* Linear search */
140 for(i=0; (i<nstr); i++)
141 if (strcasecmp(str[i],key)==0)
142 return i;
144 return -1;
147 int fget_lines(FILE *in,char ***strings)
149 char **ptr;
150 char buf[256];
151 int i,nstr;
152 char *pret;
154 pret = fgets(buf,255,in);
155 if ( pret==NULL || sscanf(buf,"%d",&nstr) != 1)
157 sprintf(warn_buf,"File is empty");
158 warning(NULL);
159 fclose(in);
161 return 0;
163 snew(ptr,nstr);
164 for(i=0; (i<nstr); i++) {
165 fgets2(buf,255,in);
166 ptr[i] = strdup(buf);
169 (*strings) = ptr;
171 return nstr;
174 int get_lines(const char *db,char ***strings)
176 FILE *in;
177 int nstr;
179 set_warning_line(db,1);
180 in = libopen(db);
181 nstr = fget_lines(in,strings);
182 fclose(in);
184 return nstr;
187 int get_file(const char *db,char ***strings)
189 FILE *in;
190 char **ptr=NULL;
191 char buf[256];
192 int i,nstr,maxi;
194 in=libopen(db);
196 i=maxi=0;
197 while (fgets2(buf,255,in)) {
198 if (i>=maxi) {
199 maxi+=50;
200 srenew(ptr,maxi);
202 ptr[i] = strdup(buf);
203 i++;
205 nstr=i;
206 fclose(in);
207 srenew(ptr,nstr);
208 *strings=ptr;
210 return nstr;