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