31 #ifdef HAVE_SYS_STAT_H
34 #ifdef HAVE_SYS_TYPES_H
35 #include <sys/types.h>
41 #include <lal/LALStdlib.h>
42 #include <lal/LALStdio.h>
43 #include <lal/LALString.h>
44 #include <lal/StringInput.h>
45 #include <lal/FileIO.h>
48 #define UNUSED __attribute__ ((unused))
54 #define WAI_FUNCSPEC static UNUSED
65 static LALFILE _lalstdin;
66 _lalstdin.fp = (
void*)stdin;
71 static LALFILE _lalstdout;
72 _lalstdout.fp = (
void*)stdout;
77 static LALFILE _lalstderr;
78 _lalstderr.fp = (
void*)stderr;
86 XLALFileResolvePathLong (
const char *fname,
const char *fallbackpath )
90 const UINT4 fname_len = strlen ( fname );
92 XLALPrintInfo (
"%s(): trying to resolve fname='%s' ...\n", __func__, fname );
94 if ( strchr ( fname,
'/' ) != NULL )
97 XLALPrintInfo (
"%s(): strategy: absolute/relative path\n", __func__ );
98 XLALPrintInfo (
"%s(): trying '%s' -> '%s' ...\n", __func__, fname, fname );
99 if ( (tmp =
LALFopen ( fname,
"rb" )) != NULL ) {
101 XLALPrintInfo (
"%s(): success '%s' -> '%s'\n", __func__, fname, fname );
113 char *resolveFname = NULL;
116 XLALPrintInfo (
"%s(): strategy: local directory\n", __func__ );
118 sprintf ( resolveFname,
"./%s", fname );
119 XLALPrintInfo (
"%s(): trying '%s' -> '%s' ...\n", __func__, fname, resolveFname );
120 if ( (tmp =
LALFopen ( resolveFname,
"rb" )) != NULL ) {
122 XLALPrintInfo (
"%s(): success '%s' -> '%s'\n", __func__, fname, resolveFname );
127 const char *lal_data_path = getenv(
"LAL_DATA_PATH" );
128 if ( ! ( lal_data_path && (strlen (lal_data_path ) > 0 ) ) )
130 XLALPrintInfo (
"%s(): skip strategy LAL_DATA_PATH: environment variable not set\n", __func__ );
134 XLALPrintInfo (
"%s(): strategy: LAL_DATA_PATH='%s'\n", __func__, lal_data_path );
139 const char *subPath_i = subPaths->
tokens[i];
141 sprintf ( resolveFname,
"%s/%s", subPath_i, fname );
142 XLALPrintInfo (
"%s(): trying '%s' -> '%s' ...\n", __func__, fname, resolveFname );
143 if ( (tmp =
LALFopen ( resolveFname,
"rb" )) != NULL ) {
146 XLALPrintInfo (
"%s(): success '%s' -> '%s'\n", __func__, fname, resolveFname );
157 if ( ! fallbackpath ) {
158 XLALPrintInfo (
"%s(): skip strategy: fallbackpath=NULL\n", __func__ );
160 XLALPrintInfo (
"%s(): strategy: fallbackpath='%s'\n", __func__, fallbackpath );
163 char *module_path = NULL;
164 int module_path_length = wai_getModulePath ( NULL, 0, NULL );
165 int module_dirname_length = 0;
166 if ( module_path_length > 0 ) {
167 module_path =
XLALMalloc ( module_path_length + 1 );
170 module_path[module_path_length] =
'\0';
173 module_path[module_dirname_length] =
'\0';
181 const char *subPath_i = subPaths->
tokens[i];
182 const UINT4 subPath_i_len = strlen ( subPath_i );
183 if ( subPath_i_len == 0 ) {
184 XLALPrintInfo (
"%s(): skip empty fallbackpath element\n", __func__ );
187 if ( subPath_i[0] ==
'.' ) {
188 if ( ! module_path ) {
189 XLALPrintInfo (
"%s(): skip relative fallbackpath element '%s': location of liblalsupport.so unknown\n", __func__, subPath_i );
193 sprintf ( resolveFname,
"%s/%s/%s", module_path, subPath_i, fname );
196 sprintf ( resolveFname,
"%s/%s", subPath_i, fname );
198 XLALPrintInfo (
"%s(): trying '%s' -> '%s' ...\n", __func__, fname, resolveFname );
199 if ( (tmp =
LALFopen ( resolveFname,
"rb" )) != NULL ) {
203 XLALPrintInfo (
"%s(): success '%s' -> '%s'\n", __func__, fname, resolveFname );
226 XLALFileResolvePath (
const char *fname )
244 char *dataBuffer = NULL;
245 size_t dataBufferLen = 0;
246 size_t numReadTotal = 0;
254 dataBufferLen += blobLen;
255 if ( (dataBuffer =
XLALRealloc ( dataBuffer, dataBufferLen + 1)) == NULL ) {
259 size_t numRead =
XLALFileRead ( dataBuffer + blobCounter*blobLen,
sizeof(
char), blobLen,
fp );
266 numReadTotal += numRead;
268 if ( numRead < blobLen ) {
279 dataBuffer[numReadTotal] = 0;
288 unsigned char magic[2] = { 0, 0 };
307 XLALPrintError(
"XLAL Error - %s: Cannot read compressed file\n", __func__ );
313 file->compression = compression;
335 XLALPrintWarning(
"XLAL Warning - %s: Compression not supported\n", __func__ );
340 file->compression = compression;
357 XLALPrintWarning(
"XLAL Warning - %s: Compression not supported\n", __func__ );
362 file->compression = compression;
380 compression =
ext ? ! strcmp(
ext,
".gz" ) : 0;
398 c =
file->compression ? gzclose(((gzFile)
file->fp)) : fclose(((FILE*)
file->fp));
400 c = fclose(((FILE*)
file->fp));
415 c =
file->compression ? (size_t)gzread( ((gzFile)
file->fp), ptr, size * nobj ) : fread( ptr, size, nobj, ((FILE*)
file->fp) );
417 c = fread( ptr, size, nobj, ((FILE*)
file->fp) );
419 if ( c == (
size_t)(-1) || (
file->compression == 0 && ferror((FILE*)(
file->fp))) )
430 c =
file->compression ? (size_t)gzwrite( ((gzFile)
file->fp), ptr, size * nobj ) : fwrite( ptr, size, nobj, ((FILE*)
file->fp) );
432 c = fwrite( ptr, size, nobj, (FILE*)(
file->fp) );
434 if ( c == 0 || (
file->compression == 0 && ferror((FILE*)(
file->fp))) )
445 c =
file->compression ? gzgetc(((gzFile)
file->fp)) : fgetc(((FILE*)
file->fp));
447 c = fgetc(((FILE*)
file->fp));
458 result =
file->compression ? gzputc(((gzFile)
file->fp), c) : fputc(c, ((FILE*)
file->fp));
460 result = fputc(c, (FILE*)(
file->fp));
473 c =
file->compression ? gzgets( ((gzFile)
file->fp), s, size ) : fgets( s, size, ((FILE*)
file->fp) );
475 c = fgets( s, size, ((FILE*)
file->fp) );
496 len = vsnprintf( buf,
sizeof(buf), fmt, ap );
499 if ( len >= (
int)
sizeof(buf) ) {
504 len = vsnprintf( s, len + 1, fmt, ap );
534 c =
file->compression ? gzflush(((gzFile)
file->fp), Z_FULL_FLUSH) : fflush(((FILE*)
file->fp));
536 c = fflush(((FILE*)
file->fp));
549 if (
file->compression && whence == SEEK_END ) {
550 XLALPrintError(
"XLAL Error - %s: SEEK_END not supported with compressed files\n", __func__ );
553 c =
file->compression ? gzseek(((gzFile)
file->fp), offset, whence) : fseek(((FILE*)
file->fp), offset, whence);
555 c = fseek(((FILE*)
file->fp), offset, whence);
568 c =
file->compression ? (long)gztell(((gzFile)
file->fp)) : ftell(((FILE*)
file->fp));
570 c = ftell(((FILE*)
file->fp));
582 file->compression ? (void)gzrewind(((gzFile)
file->fp)) : rewind(((FILE*)
file->fp));
584 rewind(((FILE*)
file->fp));
604 if ( !
file->compression ){
605 c = setvbuf(((FILE*)
file->fp), buf, mode, size);
608 #if defined ZLIB_VER_MAJOR && ZLIB_VER_MAJOR >= 1 && ZLIB_VER_MINOR >= 2 && ZLIB_VER_REVISION >= 4
609 c = (int)gzbuffer(((gzFile)
file->fp), size);
611 XLAL_PRINT_WARNING(
"Ignored buffering: unsupported in zlib version %s", ZLIB_VERSION);
615 c = setvbuf(((FILE*)
file->fp), buf, mode, size);
629 c =
file->compression ? gzeof(((gzFile)
file->fp)) : feof((FILE*)(
file->fp));
631 c = feof((FILE*)(
file->fp));
659 (*fileLen) = (size_t)sb.st_size;
662 return (S_ISREG(sb.st_mode));
708 char *memblock = NULL;
710 char *outname = NULL;
729 XLALPrintError (
"%s: Unable to allocate memory for reading file.\n", __func__ );
748 strcat(outname,
".gz");
751 XLALPrintError(
"%s: Unable to create output filename.\n", __func__ );
757 XLALPrintError (
"%s: Unable to open output file %s.\n", __func__, outname );
762 XLALPrintError (
"%s: Unable to output gzipped data.\n", __func__ );
770 XLALPrintError (
"%s: Unable to remove original text file.\n", __func__ );
791 CHAR *memblock = NULL;
792 CHAR *outname = NULL;
800 if ( (gzpos = strstr(
filename,
".gz")) == NULL ){
808 if ( ( outname = strncpy(outname,
filename, n) ) == NULL ){
809 XLALPrintError (
"%s: Unable to strip extension from file string.\n", __func__ );
822 XLALPrintError (
"%s: Unable to open output file %s.\n", __func__, outname );
838 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,...)
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)
char * XLALFileLoad(const char *path)
Read a complete data-file into memory as a string.
int XLALFileEOF(LALFILE *file)
LALFILE * XLALFileOpenAppend(const char *path, int compression)
LALFILE * lalstdout(void)
size_t XLALFileSize(const char *path)
Return the size of given file in bytes.
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)
LALFILE * lalstderr(void)
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.
char * XLALFileGets(char *s, int size, LALFILE *file)
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)
LALFILE * XLALFileOpenRead(const char *path)
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)
LALFILE * XLALFileOpen(const char *path, const char *mode)
#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)
LALFILE * XLALFileOpenWrite(const char *path, int compression)
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.