LAL  7.5.0.1-b72065a
FileIO.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007 Jolien Creighton
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with with program; see the file COPYING. If not, write to the
16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17  * MA 02110-1301 USA
18  */
19 
20 #ifndef _FILEIO_H
21 #define _FILEIO_H
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 #include <stdio.h>
28 #include <stdarg.h>
29 #include <lal/LALStddef.h>
30 #include <lal/LALStdio.h>
31 
32 /**
33  * \defgroup FileIO_h Header FileIO.h
34  * \ingroup lal_support
35  *
36  * \brief Provides standard LAL support IO functions.
37  *
38  * ### Synopsis ###
39  *
40  * \code
41  * #include <lal/LALStdio.h>
42  * #include <lal/FileIO.h>
43  * \endcode
44  *
45  * Only use \ref FileIO.h in test code that links to the \c lalsupport library.
46  */
47 /** @{ */
48 
49 typedef struct tagLALFILE LALFILE;
50 
51 LALFILE *lalstdin(void);
52 LALFILE *lalstdout(void);
53 LALFILE *lalstderr(void);
54 #define LALSTDIN (lalstdin())
55 #define LALSTDOUT (lalstdout())
56 #define LALSTDERR (lalstderr())
57 
58 int XLALFileIsCompressed( const char *path );
59 LALFILE *XLALFileOpenRead( const char *path );
60 LALFILE *XLALFileOpenWrite( const char *path, int compression );
61 LALFILE *XLALFileOpenAppend( const char *path, int compression );
62 LALFILE *XLALFileOpen( const char *path, const char *mode );
63 int XLALFileClose( LALFILE *file );
64 size_t XLALFileRead( void *ptr, size_t size, size_t nobj, LALFILE *file );
65 size_t XLALFileWrite( const void *ptr, size_t size, size_t nobj, LALFILE *file );
66 int XLALFileGetc( LALFILE *file );
67 int XLALFilePutc( int c, LALFILE *file );
68 char *XLALFileGets( char *s, int size, LALFILE *file );
69 int XLALFilePuts( const char *s, LALFILE *file );
70 #ifndef SWIG /* exclude from SWIG interface */
71 int XLALFileVPrintf( LALFILE *file, const char *fmt, va_list ap );
72 #endif /* SWIG */
73 int XLALFilePrintf( LALFILE *file, const char *fmt, ... );
74 int XLALFileFlush( LALFILE *file );
75 int XLALFileSeek( LALFILE *file, long offset, int whence );
76 long XLALFileTell( LALFILE *file );
77 void XLALFileRewind( LALFILE *file );
78 int XLALFileSetBuffer( LALFILE *file, char *buf, int mode, size_t size );
79 int XLALFileEOF( LALFILE *file );
80 
81 int XLALFileIsRegularAndGetSize ( const char *path, size_t *fileLen );
82 int XLALFileIsRegular ( const char *path );
83 size_t XLALFileSize ( const char *path );
84 
85 /** \cond DONT_DOXYGEN */
86 char *XLALFileResolvePathLong ( const char *fname, const char *fallbackpath );
87 char *XLALFileResolvePath ( const char *fname );
88 /** \endcond */ // DONT_DOXYGEN
89 
90 /** 'Resolve' a given filename 'fname', returning a file path where the
91  * file can successfully be opened by fopen() using mode='rb'.
92  *
93  * Return: successful file-path or NULL if failed.
94  *
95  * Resolving uses the following algorithm: if 'fname' contains a
96  * i) (relative or absolute) path: only tries to open that path directly
97  * ii) pure filename: try
98  * 1) local dir, then
99  * 2) search $LAL_DATA_PATH, then
100  * 3) search 'fallbackpath'; relative paths are considered relative to location of liblalsupport.so (if known)
101  * return first successful hit
102  *
103  * Note: it is not an error if the given 'fname' cannot be resolved,
104  * this will simply return NULL but xlalErrno will not be set in that case.
105  *
106  * Note2: successfully resolving an 'fname' doesn't guarantee that the path points
107  * to a file, as directories can also be opened in 'rb'.
108  *
109  * Note3: the returned string is allocated here and must be XLALFree'ed by the caller.
110  *
111  * Note4: this function should only be used from within LALSuite, as it relies
112  * on macros defined by the LALSuite build system. For the public API, functions
113  * should be defined for specific types of data files, which then call
114  * XLAL_FILE_RESOLVE_PATH() internally.
115  */
116 #define XLAL_FILE_RESOLVE_PATH( fname ) XLALFileResolvePathLong ( fname, LAL_FALLBACK_DATA_PATH )
117 
118 char *XLALFileLoad ( const char *path );
119 
120 int XLALGzipTextFile( const char *path );
121 int XLALGunzipTextFile( const char *filename );
122 
123 /** @} */
124 
125 #ifdef __cplusplus
126 }
127 #endif
128 
129 #endif /* _FILEIO_H */
LALFILE * lalstdin(void)
Definition: FileIO.c:63
int XLALFileVPrintf(LALFILE *file, const char *fmt, va_list ap)
Definition: FileIO.c:489
int XLALGunzipTextFile(const char *filename)
Use gzip to uncompress a compressed text file.
Definition: FileIO.c:790
int XLALFileClose(LALFILE *file)
Definition: FileIO.c:389
int XLALFilePutc(int c, LALFILE *file)
Definition: FileIO.c:452
char * XLALFileLoad(const char *path)
Read a complete data-file into memory as a string.
Definition: FileIO.c:235
int XLALFileEOF(LALFILE *file)
Definition: FileIO.c:623
LALFILE * XLALFileOpenAppend(const char *path, int compression)
Definition: FileIO.c:326
LALFILE * lalstdout(void)
Definition: FileIO.c:69
size_t XLALFileSize(const char *path)
Return the size of given file in bytes.
Definition: FileIO.c:689
int XLALFilePuts(const char *s, LALFILE *file)
Definition: FileIO.c:480
int XLALFileIsRegular(const char *path)
Check if given file is 'regular' (rather than a directory or sth else)
Definition: FileIO.c:675
LALFILE * lalstderr(void)
Definition: FileIO.c:75
size_t XLALFileRead(void *ptr, size_t size, size_t nobj, LALFILE *file)
Definition: FileIO.c:409
int XLALFileSetBuffer(LALFILE *file, char *buf, int mode, size_t size)
Set buffering for file I/O.
Definition: FileIO.c:598
char * XLALFileGets(char *s, int size, LALFILE *file)
Definition: FileIO.c:467
int XLALFileIsRegularAndGetSize(const char *path, size_t *fileLen)
Check if path points to a 'regular file', rather than a directory or sth else.
Definition: FileIO.c:646
int XLALFileSeek(LALFILE *file, long offset, int whence)
Definition: FileIO.c:543
int XLALFileIsCompressed(const char *path)
Definition: FileIO.c:285
size_t XLALFileWrite(const void *ptr, size_t size, size_t nobj, LALFILE *file)
Definition: FileIO.c:424
LALFILE * XLALFileOpenRead(const char *path)
Definition: FileIO.c:299
int XLALFilePrintf(LALFILE *file, const char *fmt,...)
Definition: FileIO.c:515
void XLALFileRewind(LALFILE *file)
Definition: FileIO.c:577
int XLALGzipTextFile(const char *path)
Use gzip to compress a text file This function will use the gzip compression routines in zlib to comp...
Definition: FileIO.c:707
int XLALFileGetc(LALFILE *file)
Definition: FileIO.c:439
LALFILE * XLALFileOpen(const char *path, const char *mode)
Definition: FileIO.c:370
long XLALFileTell(LALFILE *file)
Definition: FileIO.c:562
LALFILE * XLALFileOpenWrite(const char *path, int compression)
Definition: FileIO.c:348
int XLALFileFlush(LALFILE *file)
Definition: FileIO.c:528
int compression
Definition: FileIO.c:59