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