Simplify code structure for C++ analysis tools
[alexxy/gromacs.git] / src / gromacs / fileio / futil.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team,
6  * Copyright (c) 2013, by the GROMACS development team, led by
7  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8  * and including many others, as listed in the AUTHORS file in the
9  * top-level source directory and at http://www.gromacs.org.
10  *
11  * GROMACS is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public License
13  * as published by the Free Software Foundation; either version 2.1
14  * of the License, or (at your option) any later version.
15  *
16  * GROMACS is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with GROMACS; if not, see
23  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
25  *
26  * If you want to redistribute modifications to GROMACS, please
27  * consider that scientific software is very special. Version
28  * control is crucial - bugs must be traceable. We will be happy to
29  * consider code for inclusion in the official distribution, but
30  * derived work must not be called official GROMACS. Details are found
31  * in the README & COPYING files - if they are missing, get the
32  * official version at http://www.gromacs.org.
33  *
34  * To help us fund GROMACS development, we humbly ask that you cite
35  * the research papers on the package. Check out http://www.gromacs.org.
36  */
37
38 #ifndef GMX_FILEIO_FUTIL_H
39 #define GMX_FILEIO_FUTIL_H
40
41 #include <stdio.h>
42 #include "../legacyheaders/typedefs.h"
43 #include "../legacyheaders/types/commrec.h"
44
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48 #if 0
49 }
50 #endif
51
52 /* Native windows uses backslash path separators.
53  * Cygwin and everybody else in the world use slash.
54  * When reading the PATH environment variable, Unix separates entries
55  * with colon, while windows uses semicolon.
56  */
57 #include "../utility/gmx_header_config.h"
58 #ifdef GMX_NATIVE_WINDOWS
59 #define DIR_SEPARATOR '\\'
60 #define PATH_SEPARATOR ";"
61 #else
62 #define DIR_SEPARATOR '/'
63 #define PATH_SEPARATOR ":"
64 #endif
65
66
67 /* Now get the maximum path size. */
68 #ifdef PATH_MAX
69 #  define GMX_PATH_MAX PATH_MAX
70 #elif defined MAX_PATH
71 #  define GMX_PATH_MAX MAX_PATH
72 #else
73 #  define GMX_PATH_MAX 4096
74 #endif
75
76
77 #ifdef HAVE_FSEEKO
78 typedef off_t              gmx_off_t;
79 #  define SIZEOF_GMX_OFF_T   SIZEOF_OFF_T
80 #elif defined HAVE__FSEEKI64
81 typedef __int64            gmx_off_t;
82 #  define SIZEOF_GMX_OFF_T   8
83 #else
84 /* Almost certainly 64 bits, and guaranteed to be available */
85 typedef gmx_large_int_t    gmx_off_t;
86 #  define SIZEOF_GMX_OFF_T   SIZEOF_GMX_LARGE_INT
87 #endif
88
89
90
91 void no_buffers(void);
92 /* Turn off buffering of files (which is default) for debugging purposes */
93
94 gmx_bool gmx_fexist(const char *fname);
95 /* Return TRUE when fname exists, FALSE otherwise */
96
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_bool make_backup(const char * file);
110
111 FILE *ffopen(const char *file, const char *mode);
112 /* Return a valid file pointer when successful, exits otherwise
113  * If the file is in compressed format, open a pipe which uncompresses
114  * the file! Therefore, files must be closed with ffclose (see below)
115  */
116
117 int ffclose(FILE *fp);
118 /* Close files or pipes */
119
120
121 void frewind(FILE *fp);
122 /* Does not rewind pipes, but does so for normal files */
123
124 #define rewind frewind
125
126
127 int gmx_fseek(FILE *stream, gmx_off_t offset, int whence);
128 /* OS-independent fseek. 64-bit when available */
129
130 gmx_off_t gmx_ftell(FILE *stream);
131 /* OS-independent fseek. 64-bit when available. */
132
133
134 gmx_bool is_pipe(FILE *fp);
135
136 char *gmxlibfn(const char *file);
137 /* allocates and returns a string with the full file name for a library file */
138
139 FILE *libopen(const char *file);
140 /* Open a library file for reading. This looks in the current directory
141  * first, and then in the library directory. If the file is not found,
142  * it terminates with a fatal_error
143  */
144
145 /* Opaque data type to list directories */
146 typedef struct gmx_directory *
147     gmx_directory_t;
148
149 /* Open a directory for reading. The first argument should be a pointer
150  * to a declared gmx_directory_t variable. Returns 0 on success.
151  */
152 int
153 gmx_directory_open(gmx_directory_t *p_gmxdir, const char *dirname);
154
155
156 /* Given an initialized gmx_directory_t, if there are more files in
157  * the directory this routine returns 0 and write the next name
158  * into the USER-PROVIDED buffer name. The last argument is the max
159  * number of characters that will be written. Just as strncpy, the
160  * string will NOT be terminated it it is longer than maxlength_name.
161  */
162 int
163 gmx_directory_nextfile(gmx_directory_t gmxdir, char *name, int maxlength_name);
164
165 /* Release all data for a directory structure */
166 int
167 gmx_directory_close(gmx_directory_t gmxdir);
168
169
170
171 void get_libdir(char *libdir);
172
173 char *low_gmxlibfn(const char *file, gmx_bool bAddCWD, gmx_bool bFatal);
174
175 FILE *low_libopen(const char *file, gmx_bool bFatal);
176 /* The same as the above, but does not terminate if (!bFatal) */
177
178 /* Create unique name for temp file (wrapper around mkstemp).
179  * Buf should be at least 7 bytes long
180  */
181 void gmx_tmpnam(char *buf);
182
183 /* truncte the file to the specified length */
184 int gmx_truncatefile(char *path, gmx_off_t length);
185
186 /* rename/move the file (atomically, if the OS makes that available) oldname
187    to newname */
188 int gmx_file_rename(const char *oldname, const char *newname);
189
190 /* copy the file (data only) oldname to newname. if copy_if_empty==FALSE,
191    the file won't be copied if it's empty.*/
192 int gmx_file_copy(const char *oldname, const char *newname, gmx_bool copy_if_empty);
193
194 /* do an fsync() on an open file pointer.
195    Only use this during checkpointing! */
196 int gmx_fsync(FILE *fp);
197
198 void gmx_chdir(const char *directory);
199 void gmx_getcwd(char *buffer, size_t size);
200
201 #ifdef __cplusplus
202 }
203 #endif
204
205 #endif  /* GMX_FILEIO_FUTIL_H */