8ca4fd7fd9bcb48512c74c93bb999fac1885e036
[alexxy/gromacs.git] / include / futil.h
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  * Gromacs Runs On Most of All Computer Systems
34  */
35
36 #ifndef _futil_h
37 #define _futil_h
38
39 #include <stdio.h>
40 #include "typedefs.h"
41
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 #if 0
46 }
47 #endif
48
49 /* Native windows uses backslash path separators.
50  * Cygwin and everybody else in the world use slash.
51  * When reading the PATH environment variable, Unix separates entries
52  * with colon, while windows uses semicolon.
53  */
54 #include "gmx_header_config.h"
55 #ifdef GMX_NATIVE_WINDOWS
56 #define DIR_SEPARATOR '\\'
57 #define PATH_SEPARATOR ";"
58 #else
59 #define DIR_SEPARATOR '/'
60 #define PATH_SEPARATOR ":"
61 #endif
62
63
64 /* Now get the maximum path size. */
65 #ifdef PATH_MAX
66 #  define GMX_PATH_MAX PATH_MAX
67 #elif defined MAX_PATH
68 #  define GMX_PATH_MAX MAX_PATH
69 #else
70 #  define GMX_PATH_MAX 4096
71 #endif
72
73
74 #ifdef HAVE_FSEEKO
75    typedef off_t              gmx_off_t;
76 #  define SIZEOF_GMX_OFF_T   SIZEOF_OFF_T
77 #elif defined HAVE__FSEEKI64 
78    typedef __int64            gmx_off_t;
79 #  define SIZEOF_GMX_OFF_T   8
80 #else
81    /* Almost certainly 64 bits, and guaranteed to be available */
82    typedef gmx_large_int_t    gmx_off_t;
83 #  define SIZEOF_GMX_OFF_T   SIZEOF_GMX_LARGE_INT
84 #endif    
85
86
87   
88 void no_buffers(void);
89 /* Turn off buffering of files (which is default) for debugging purposes */
90
91 gmx_bool gmx_fexist(const char *fname);
92 /* Return TRUE when fname exists, FALSE otherwise */
93
94 gmx_bool gmx_fexist_master(const char *fname, t_commrec *cr);
95 /* Return TRUE when fname exists, FALSE otherwise, bcast from master to others */
96
97 gmx_bool gmx_eof(FILE *fp);
98 /* Return TRUE on end-of-file, FALSE otherwise */
99
100 gmx_bool is_pipe(FILE *fp);
101 /* Check whether the file (opened by ffopen) is a pipe */
102
103 /*  Make a backup of file if necessary.  
104     Return false if there was a problem.
105 */
106 gmx_bool make_backup(const char * file);
107
108 FILE *ffopen(const char *file, const char *mode);
109 /* Return a valid file pointer when successful, exits otherwise 
110  * If the file is in compressed format, open a pipe which uncompresses
111  * the file! Therefore, files must be closed with ffclose (see below)
112  */
113
114 int ffclose(FILE *fp);
115 /* Close files or pipes */
116
117
118 void frewind(FILE *fp);
119 /* Does not rewind pipes, but does so for normal files */
120
121 #define rewind frewind
122
123
124 int gmx_fseek(FILE *stream, gmx_off_t offset, int whence); 
125 /* OS-independent fseek. 64-bit when available */
126
127 gmx_off_t gmx_ftell(FILE *stream); 
128 /* OS-independent fseek. 64-bit when available. */
129
130
131 gmx_bool is_pipe(FILE *fp);
132
133 char *gmxlibfn(const char *file);
134 /* allocates and returns a string with the full file name for a library file */
135
136 FILE *libopen(const char *file);
137 /* Open a library file for reading. This looks in the current directory
138  * first, and then in the library directory. If the file is not found,
139  * it terminates with a fatal_error
140  */
141
142 /* Opaque data type to list directories */
143 typedef struct gmx_directory *
144 gmx_directory_t;
145
146 /* Open a directory for reading. The first argument should be a pointer
147  * to a declared gmx_directory_t variable. Returns 0 on success.
148  */
149 int
150 gmx_directory_open(gmx_directory_t *p_gmxdir,const char *dirname);
151
152     
153 /* Given an initialized gmx_directory_t, if there are more files in
154  * the directory this routine returns 0 and write the next name
155  * into the USER-PROVIDED buffer name. The last argument is the max
156  * number of characters that will be written. Just as strncpy, the
157  * string will NOT be terminated it it is longer than maxlength_name.
158  */
159 int
160 gmx_directory_nextfile(gmx_directory_t gmxdir,char *name,int maxlength_name);
161     
162 /* Release all data for a directory structure */
163 int 
164 gmx_directory_close(gmx_directory_t gmxdir);
165     
166
167     
168 gmx_bool get_libdir(char *libdir);
169
170 char *low_gmxlibfn(const char *file,gmx_bool bAddCWD,gmx_bool bFatal);
171
172 FILE *low_libopen(const char *file,gmx_bool bFatal);
173 /* The same as the above, but does not terminate if (!bFatal) */
174
175 /* Create unique name for temp file (wrapper around mkstemp). 
176  * Buf should be at least 7 bytes long 
177  */
178 void gmx_tmpnam(char *buf);
179
180 /* truncte the file to the specified length */
181 int gmx_truncatefile(char *path, gmx_off_t length);
182
183 /* rename/move the file (atomically, if the OS makes that available) oldname 
184    to newname */
185 int gmx_file_rename(const char *oldname, const char *newname);
186
187 /* copy the file (data only) oldname to newname. if copy_if_empty==FALSE,
188    the file won't be copied if it's empty.*/
189 int gmx_file_copy(const char *oldname, const char *newname, gmx_bool copy_if_empty);
190
191 /* do an fsync() on an open file pointer. 
192    Only use this during checkpointing! */
193 int gmx_fsync(FILE *fp);
194
195 #ifdef GMX_NATIVE_WINDOWS
196 #define chdir _chdir
197 #define getcwd _getcwd
198 #endif
199
200 #ifdef __cplusplus
201 }
202 #endif
203
204 #endif  /* _futil_h */