changed reading hint
[gromacs/adressmacs.git] / src / gmxlib / futil.c
blob60949c67fd30a3ed89af010ab22792bfe2081242
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_futil_c = "$Id$";
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include "sysstuff.h"
34 #include "string2.h"
35 #include "futil.h"
36 #include "network.h"
37 #include "fatal.h"
38 #include "smalloc.h"
40 typedef struct t_pstack {
41 FILE *fp;
42 struct t_pstack *prev;
43 } t_pstack;
45 static t_pstack *pstack=NULL;
46 static bool bUnbuffered=FALSE;
48 void no_buffers(void)
50 bUnbuffered=TRUE;
53 void push_ps(FILE *fp)
55 t_pstack *ps;
57 snew(ps,1);
58 ps->fp = fp;
59 ps->prev = pstack;
62 #ifdef fclose
63 #undef fclose
64 #endif
66 void ffclose(FILE *fp)
68 t_pstack *ps,*tmp;
70 ps=pstack;
71 if (ps == NULL) {
72 fclose(fp);
73 return;
75 if (ps->fp == fp) {
76 pclose(fp);
77 pstack=pstack->prev;
78 sfree(ps);
80 else {
81 while ((ps->prev != NULL) && (ps->prev->fp != fp))
82 ps=ps->prev;
83 if (ps->prev->fp == fp) {
84 pclose(ps->prev->fp);
85 tmp=ps->prev;
86 ps->prev=ps->prev->prev;
87 sfree(tmp);
89 else {
90 fclose(fp);
91 return;
96 #ifdef rewind
97 #undef rewind
98 #endif
100 void frewind(FILE *fp)
102 t_pstack *ps;
104 ps=pstack;
105 while (ps != NULL) {
106 if (ps->fp == fp) {
107 fprintf(stderr,"Cannot rewind compressed file!\n");
108 return;
110 ps=ps->prev;
112 rewind(fp);
115 bool is_pipe(FILE *fp)
117 t_pstack *ps;
119 ps=pstack;
120 while (ps != NULL) {
121 if (ps->fp == fp) {
122 return TRUE;
124 ps=ps->prev;
126 return FALSE;
129 #ifdef NO_PIPE
130 static FILE *popen(char *nm,char *mode)
132 fatal_error(0,"Sorry no pipes...");
134 return NULL;
137 static int pclose(FILE *fp)
139 fatal_error(0,"Sorry no pipes...");
141 return 0;
143 #endif
146 FILE *uncompress(char *fn,char *mode)
148 FILE *fp;
149 char buf[256];
151 sprintf(buf,"uncompress -c < %s",fn);
152 fprintf(stderr,"Going to execute '%s'\n",buf);
153 if ((fp=popen(buf,mode)) == NULL)
154 fatal_error(0,"Could not open %s",fn);
155 push_ps(fp);
157 return fp;
160 FILE *gunzip(char *fn,char *mode)
162 FILE *fp;
163 char buf[256];
165 sprintf(buf,"gunzip -c < %s",fn);
166 fprintf(stderr,"Going to execute '%s'\n",buf);
167 if ((fp=popen(buf,mode)) == NULL)
168 fatal_error(0,"Could not open %s",fn);
169 push_ps(fp);
171 return fp;
174 bool fexist(char *fname)
176 FILE *test;
178 if (fname == NULL)
179 return FALSE;
180 test=fopen(fname,"r");
181 if (test == NULL)
182 return FALSE;
183 else {
184 fclose(test);
185 return TRUE;
189 bool eof(FILE *fp)
191 char data[4];
192 bool beof;
194 if (is_pipe(fp))
195 return feof(fp);
196 else {
197 if ((beof=fread(data,1,1,fp))==1)
198 fseek(fp,-1,SEEK_CUR);
199 return !beof;
203 char *backup_fn(char *file)
205 int i;
206 char *ptr;
207 static char buf[256];
209 for(i=strlen(file)-1; ((i > 0) && (file[i] != '/')); i--);
210 if (i > 0) {
211 ptr=strdup(file);
212 ptr[i]='\0';
213 sprintf(buf,"%s/#%s#",ptr,ptr+i+1);
214 sfree(ptr);
216 else
217 sprintf(buf,"#%s#",file);
219 return buf;
222 FILE *ffopen(char *file,char *mode)
224 FILE *ff=NULL;
225 char buf[256],*bf,*bufsize,*ptr;
226 bool bRead;
227 int bs;
229 #ifdef _amb_
230 fprintf(stderr,"Going to open %s on CPU %d with mode %s\n",
231 file,gmx_cpu_id(),mode);
232 #endif
233 if ((mode[0]=='w') && fexist(file)) {
234 bf=backup_fn(file);
235 if (rename(file,bf) == 0) {
236 fprintf(stderr,"\nBack Off! I just backed up %s to %s\n",file,bf);
238 else
239 fprintf(stderr,"Sorry, I couldn't backup %s to %s\n",file,bf);
241 where();
243 bRead= mode[0]=='r';
244 strcpy(buf,file);
245 if (fexist(buf) || !bRead) {
246 if ((ff=fopen(buf,mode))==NULL)
247 fatal_error(0,"Could not open %s",buf);
248 where();
249 /* Check whether we should be using buffering (default) or not
250 * (for debugging)
252 if (bUnbuffered || ((bufsize=getenv("LOG_BUFS")) != NULL)) {
253 /* Check whether to use completely unbuffered */
254 if (bUnbuffered)
255 bs = 0;
256 else
257 bs=atoi(bufsize);
258 if (bs <= 0)
259 setbuf(ff,NULL);
260 else {
261 snew(ptr,bs+8);
262 if (setvbuf(ff,ptr,_IOFBF,bs) != 0)
263 fatal_error(0,"Buffering File");
266 where();
268 else {
269 sprintf(buf,"%s.Z",file);
270 if (fexist(buf)) {
271 ff=uncompress(buf,mode);
273 else {
274 sprintf(buf,"%s.gz",file);
275 if (fexist(buf)) {
276 ff=gunzip(buf,mode);
278 else
279 fatal_error(0,"%s does not exist",file);
282 #ifdef _amb_
283 fprintf(stderr,"Opened %s on CPU %d with mode %s\n",
284 file,gmx_cpu_id(),mode);
285 #endif
286 return ff;
289 char *low_libfn(char *file,bool bFatal)
291 static char *libdir="GMXLIB";
292 char *ret=NULL,*lib;
293 static char buf[1024];
295 if (fexist(file))
296 ret=file;
297 else {
298 if ((lib=getenv(libdir)) == NULL) {
299 if (bFatal)
300 fatal_error(0,"%s environment variable not set and file %s not found",
301 libdir,file);
302 else
303 return NULL;
305 else {
306 sprintf(buf,"%s/%s",lib,file);
307 ret=buf;
308 if (bFatal && !fexist(ret))
309 fatal_error(0,"Library file %s found in current dir nor in libdir %s",
310 ret,lib);
314 return ret;
317 FILE *low_libopen(char *file,bool bFatal)
319 FILE *ff;
320 char *fn;
322 fn=low_libfn(file,bFatal);
324 if (fn==NULL) {
325 ff=NULL;
326 } else {
327 if (bFatal)
328 fprintf(stderr,"Opening library file %s\n",fn);
329 ff=fopen(fn,"r");
332 return ff;
335 char *libfn(char *file)
337 return low_libfn(file,TRUE);
340 FILE *libopen(char *file)
342 return low_libopen(file,TRUE);