34#ifdef HAVE_SYS_TYPES_H
44#include <lal/LALStdlib.h>
45#include <lal/LALStdio.h>
46#include <lal/LALString.h>
47#include <lal/StringInput.h>
48#include <lal/FileIO.h>
51#define UNUSED __attribute__ ((unused))
57#define WAI_FUNCSPEC static UNUSED
68 static LALFILE _lalstdin;
69 _lalstdin.fp = (
void*)stdin;
74 static LALFILE _lalstdout;
75 _lalstdout.fp = (
void*)stdout;
80 static LALFILE _lalstderr;
81 _lalstderr.fp = (
void*)stderr;
89XLALFileResolvePathLong (
const char *fname,
const char *fallbackpath )
93 const UINT4 fname_len = strlen ( fname );
95 XLALPrintInfo (
"%s(): trying to resolve fname='%s' ...\n", __func__, fname );
97 if ( strchr ( fname,
'/' ) != NULL )
100 XLALPrintInfo (
"%s(): strategy: absolute/relative path\n", __func__ );
101 XLALPrintInfo (
"%s(): trying '%s' -> '%s' ...\n", __func__, fname, fname );
102 if ( (tmp =
LALFopen ( fname,
"rb" )) != NULL ) {
104 XLALPrintInfo (
"%s(): success '%s' -> '%s'\n", __func__, fname, fname );
116 char *resolveFname = NULL;
119 XLALPrintInfo (
"%s(): strategy: local directory\n", __func__ );
121 sprintf ( resolveFname,
"./%s", fname );
122 XLALPrintInfo (
"%s(): trying '%s' -> '%s' ...\n", __func__, fname, resolveFname );
123 if ( (tmp =
LALFopen ( resolveFname,
"rb" )) != NULL ) {
125 XLALPrintInfo (
"%s(): success '%s' -> '%s'\n", __func__, fname, resolveFname );
130 const char *lal_data_path = getenv(
"LAL_DATA_PATH" );
131 if ( ! ( lal_data_path && (strlen (lal_data_path ) > 0 ) ) )
133 XLALPrintInfo (
"%s(): skip strategy LAL_DATA_PATH: environment variable not set\n", __func__ );
137 XLALPrintInfo (
"%s(): strategy: LAL_DATA_PATH='%s'\n", __func__, lal_data_path );
142 const char *subPath_i = subPaths->
tokens[i];
144 sprintf ( resolveFname,
"%s/%s", subPath_i, fname );
145 XLALPrintInfo (
"%s(): trying '%s' -> '%s' ...\n", __func__, fname, resolveFname );
146 if ( (tmp =
LALFopen ( resolveFname,
"rb" )) != NULL ) {
149 XLALPrintInfo (
"%s(): success '%s' -> '%s'\n", __func__, fname, resolveFname );
160 if ( ! fallbackpath ) {
161 XLALPrintInfo (
"%s(): skip strategy: fallbackpath=NULL\n", __func__ );
163 XLALPrintInfo (
"%s(): strategy: fallbackpath='%s'\n", __func__, fallbackpath );
166 char *module_path = NULL;
167 int module_path_length = wai_getModulePath ( NULL, 0, NULL );
168 int module_dirname_length = 0;
169 if ( module_path_length > 0 ) {
170 module_path =
XLALMalloc ( module_path_length + 1 );
173 module_path[module_path_length] =
'\0';
176 module_path[module_dirname_length] =
'\0';
184 const char *subPath_i = subPaths->
tokens[i];
185 const UINT4 subPath_i_len = strlen ( subPath_i );
186 if ( subPath_i_len == 0 ) {
187 XLALPrintInfo (
"%s(): skip empty fallbackpath element\n", __func__ );
190 if ( subPath_i[0] ==
'.' ) {
191 if ( ! module_path ) {
192 XLALPrintInfo (
"%s(): skip relative fallbackpath element '%s': location of liblalsupport.so unknown\n", __func__, subPath_i );
196 sprintf ( resolveFname,
"%s/%s/%s", module_path, subPath_i, fname );
199 sprintf ( resolveFname,
"%s/%s", subPath_i, fname );
201 XLALPrintInfo (
"%s(): trying '%s' -> '%s' ...\n", __func__, fname, resolveFname );
204 int glob_retn = glob( resolveFname, 0, NULL, &glob_buf );
205 if ( glob_retn == 0 ) {
208 strcpy( resolveFname, glob_buf.gl_pathv[0] );
209 globfree( &glob_buf );
212 XLALPrintInfo (
"%s(): success '%s' -> '%s'\n", __func__, fname, resolveFname );
215 char glob_errmsg[256];
218 snprintf( glob_errmsg,
sizeof( glob_errmsg ),
"out of memory" );
221 snprintf( glob_errmsg,
sizeof( glob_errmsg ),
"read error" );
224 snprintf( glob_errmsg,
sizeof( glob_errmsg ),
"no matches" );
227 snprintf( glob_errmsg,
sizeof( glob_errmsg ),
"unknown error %i", glob_retn );
229 XLALPrintInfo (
"%s(): trying '%s' -> '%s' ... glob failed (%s)\n", __func__, fname, resolveFname, glob_errmsg );
230 globfree( &glob_buf );
232 if ( (tmp =
LALFopen ( resolveFname,
"rb" )) != NULL ) {
236 XLALPrintInfo (
"%s(): success '%s' -> '%s'\n", __func__, fname, resolveFname );
260XLALFileResolvePath (
const char *fname )
278 char *dataBuffer = NULL;
279 size_t dataBufferLen = 0;
280 size_t numReadTotal = 0;
288 dataBufferLen += blobLen;
289 if ( (dataBuffer =
XLALRealloc ( dataBuffer, dataBufferLen + 1)) == NULL ) {
293 size_t numRead =
XLALFileRead ( dataBuffer + blobCounter*blobLen,
sizeof(
char), blobLen,
fp );
300 numReadTotal += numRead;
302 if ( numRead < blobLen ) {
313 dataBuffer[numReadTotal] = 0;
322 unsigned char magic[2] = { 0, 0 };
341 XLALPrintError(
"XLAL Error - %s: Cannot read compressed file\n", __func__ );
347 file->compression = compression;
369 XLALPrintWarning(
"XLAL Warning - %s: Compression not supported\n", __func__ );
374 file->compression = compression;
391 XLALPrintWarning(
"XLAL Warning - %s: Compression not supported\n", __func__ );
396 file->compression = compression;
414 compression =
ext ? ! strcmp(
ext,
".gz" ) : 0;
432 c =
file->compression ? gzclose(((gzFile)
file->fp)) : fclose(((FILE*)
file->fp));
434 c = fclose(((FILE*)
file->fp));
449 c =
file->compression ? (size_t)gzread( ((gzFile)
file->fp), ptr, size * nobj ) : fread( ptr, size, nobj, ((FILE*)
file->fp) );
451 c = fread( ptr, size, nobj, ((FILE*)
file->fp) );
453 if ( c == (
size_t)(-1) || (
file->compression == 0 && ferror((FILE*)(
file->fp))) )
464 c =
file->compression ? (size_t)gzwrite( ((gzFile)
file->fp), ptr, size * nobj ) : fwrite( ptr, size, nobj, ((FILE*)
file->fp) );
466 c = fwrite( ptr, size, nobj, (FILE*)(
file->fp) );
468 if ( c == 0 || (
file->compression == 0 && ferror((FILE*)(
file->fp))) )
479 c =
file->compression ? gzgetc(((gzFile)
file->fp)) : fgetc(((FILE*)
file->fp));
481 c = fgetc(((FILE*)
file->fp));
492 result =
file->compression ? gzputc(((gzFile)
file->fp), c) : fputc(c, ((FILE*)
file->fp));
494 result = fputc(c, (FILE*)(
file->fp));
507 c =
file->compression ? gzgets( ((gzFile)
file->fp), s, size ) : fgets( s, size, ((FILE*)
file->fp) );
509 c = fgets( s, size, ((FILE*)
file->fp) );
530 len = vsnprintf( buf,
sizeof(buf), fmt, ap );
533 if ( len >= (
int)
sizeof(buf) ) {
538 len = vsnprintf( s, len + 1, fmt, ap );
568 c =
file->compression ? gzflush(((gzFile)
file->fp), Z_FULL_FLUSH) : fflush(((FILE*)
file->fp));
570 c = fflush(((FILE*)
file->fp));
583 if (
file->compression && whence == SEEK_END ) {
584 XLALPrintError(
"XLAL Error - %s: SEEK_END not supported with compressed files\n", __func__ );
587 c =
file->compression ? gzseek(((gzFile)
file->fp), offset, whence) : fseek(((FILE*)
file->fp), offset, whence);
589 c = fseek(((FILE*)
file->fp), offset, whence);
602 c =
file->compression ? (long)gztell(((gzFile)
file->fp)) : ftell(((FILE*)
file->fp));
604 c = ftell(((FILE*)
file->fp));
616 file->compression ? (void)gzrewind(((gzFile)
file->fp)) : rewind(((FILE*)
file->fp));
618 rewind(((FILE*)
file->fp));
638 if ( !
file->compression ){
639 c = setvbuf(((FILE*)
file->fp), buf, mode, size);
642#if defined ZLIB_VER_MAJOR && ZLIB_VER_MAJOR >= 1 && ZLIB_VER_MINOR >= 2 && ZLIB_VER_REVISION >= 4
643 c = (int)gzbuffer(((gzFile)
file->fp), size);
645 XLAL_PRINT_WARNING(
"Ignored buffering: unsupported in zlib version %s", ZLIB_VERSION);
649 c = setvbuf(((FILE*)
file->fp), buf, mode, size);
663 c =
file->compression ? gzeof(((gzFile)
file->fp)) : feof((FILE*)(
file->fp));
665 c = feof((FILE*)(
file->fp));
693 (*fileLen) = (size_t)sb.st_size;
696 return (S_ISREG(sb.st_mode));
742 char *memblock = NULL;
744 char *outname = NULL;
763 XLALPrintError (
"%s: Unable to allocate memory for reading file.\n", __func__ );
782 strcat(outname,
".gz");
785 XLALPrintError(
"%s: Unable to create output filename.\n", __func__ );
791 XLALPrintError (
"%s: Unable to open output file %s.\n", __func__, outname );
796 XLALPrintError (
"%s: Unable to output gzipped data.\n", __func__ );
804 XLALPrintError (
"%s: Unable to remove original text file.\n", __func__ );
825 CHAR *memblock = NULL;
826 CHAR *outname = NULL;
834 if ( (gzpos = strstr(
filename,
".gz")) == NULL ){
842 if ( ( outname = strncpy(outname,
filename, n) ) == NULL ){
843 XLALPrintError (
"%s: Unable to strip extension from file string.\n", __func__ );
856 XLALPrintError (
"%s: Unable to open output file %s.\n", __func__, outname );
872 XLALPrintError (
"%s: Unable to remove original gzipped file.\n", __func__ );
static const size_t magic
int XLALPrintWarning(const char *fmt,...)
int XLALPrintInfo(const char *fmt,...)
int XLALPrintError(const char *fmt,...)
LALFILE * XLALFileOpenWrite(const char *path, int compression)
int XLALFileVPrintf(LALFILE *file, const char *fmt, va_list ap)
int XLALGunzipTextFile(const char *filename)
Use gzip to uncompress a compressed text file.
int XLALFileClose(LALFILE *file)
int XLALFilePutc(int c, LALFILE *file)
LALFILE * XLALFileOpenAppend(const char *path, int compression)
int XLALFileEOF(LALFILE *file)
LALFILE * XLALFileOpenRead(const char *path)
size_t XLALFileSize(const char *path)
Return the size of given file in bytes.
LALFILE * XLALFileOpen(const char *path, const char *mode)
int XLALFilePuts(const char *s, LALFILE *file)
int XLALFileIsRegular(const char *path)
Check if given file is 'regular' (rather than a directory or sth else)
size_t XLALFileRead(void *ptr, size_t size, size_t nobj, LALFILE *file)
int XLALFileSetBuffer(LALFILE *file, char *buf, int mode, size_t size)
Set buffering for file I/O.
int XLALFileIsRegularAndGetSize(const char *path, size_t *fileLen)
Check if path points to a 'regular file', rather than a directory or sth else.
int XLALFileSeek(LALFILE *file, long offset, int whence)
int XLALFileIsCompressed(const char *path)
size_t XLALFileWrite(const void *ptr, size_t size, size_t nobj, LALFILE *file)
int XLALFilePrintf(LALFILE *file, const char *fmt,...)
void XLALFileRewind(LALFILE *file)
int XLALGzipTextFile(const char *filename)
Use gzip to compress a text file This function will use the gzip compression routines in zlib to comp...
int XLALFileGetc(LALFILE *file)
#define XLAL_FILE_RESOLVE_PATH(fname)
'Resolve' a given filename 'fname', returning a file path where the file can successfully be opened b...
long XLALFileTell(LALFILE *file)
char * XLALFileGets(char *s, int size, LALFILE *file)
LALFILE * lalstderr(void)
char * XLALFileLoad(const char *path)
Read a complete data-file into memory as a string.
LALFILE * lalstdout(void)
int XLALFileFlush(LALFILE *file)
#define XLAL_NUM_ELEM(x)
MACRO which gives the number of elements in a fixed-size array.
char CHAR
One-byte signed integer, see Headers LAL(Atomic)Datatypes.h for more details.
uint32_t UINT4
Four-byte unsigned integer.
#define XLALRealloc(p, n)
char * XLALStringDuplicate(const char *s)
Like strdup but uses LAL allocation routines (free with LALFree).
#define XLAL_ERROR_VOID(...)
Macro to invoke a failure from a XLAL routine returning void.
#define XLAL_ERROR_NULL(...)
Macro to invoke a failure from a XLAL routine returning a pointer.
#define xlalErrno
Modifiable lvalue containing the XLAL error number.
#define XLAL_ERROR(...)
Macro to invoke a failure from a XLAL routine returning an integer.
#define XLAL_CHECK(assertion,...)
Macro to test an assertion and invoke a failure if it is not true in a function that returns an integ...
#define XLAL_PRINT_WARNING(...)
Macro that will print a warning message with a standard format.
#define XLAL_CHECK_NULL(assertion,...)
Macro to test an assertion and invoke a failure if it is not true in a function that returns a pointe...
@ XLAL_ENOMEM
Memory allocation error.
@ XLAL_SUCCESS
Success return value (not an error number)
@ XLAL_EFAULT
Invalid pointer.
@ XLAL_EFUNC
Internal function call failed bit: "or" this with existing error number.
@ XLAL_EERR
Internal error.
@ XLAL_EINVAL
Invalid argument.
@ XLAL_EFAILED
Generic failure.
This structure stores a number of null-terminated strings of arbitrary length.
CHAR ** tokens
A list of pointers to the individual tokens; the elements tokens[0..nTokens-1] point to tokens,...
UINT4 nTokens
The number of tokens in the list.