36 #include <lal/LALStdio.h>
37 #include <lal/LALStdlib.h>
38 #include <lal/LALString.h>
40 #include <lal/LALCache.h>
41 #include <lal/FileIO.h>
44 #define UNUSED __attribute__ ((unused))
51 #define NAME_MAX FILENAME_MAX
61 if (!strchr(s,
'\n')) {
78 char s[PATH_MAX + 4 * (
NAME_MAX + 1)];
93 const char *sepstr =
" \t\n";
97 len = strcspn(s, sepstr);
103 s += strspn(s, sepstr);
113 char *f1, *f2, *f3, *f4, *f5;
122 if (strcmp(f3,
"-")) {
123 if (strspn(f3,
"0123456789") != strlen(f3))
126 entry->t0 = atoi(f3);
129 if (strcmp(f4,
"-")) {
130 if (strspn(f4,
"0123456789") != strlen(f4))
133 entry->dt = atoi(f4);
176 for (i = 0; i < cache->
length; ++i) {
195 for (i = 0; i < cache->
length; ++i)
216 for (i = 0; i < cache1->
length; ++i)
218 for (i = 0; i < cache2->
length; ++i)
228 char s[PATH_MAX + 4*(
NAME_MAX + 1)];
241 for (i = 0; i < n; ++i)
267 static int XLALCacheFilenameParseEntry(
LALCacheEntry * entry,
279 if (!realpath(fname,
path))
283 base = strrchr(
path,
'/');
284 base = base ? base + 1 :
path;
295 c = sscanf(base,
"%[a-zA-Z0-9_+#]-%[a-zA-Z0-9_+#]-%d-%d",
src, dsc,
302 if (!entry->
src || !entry->
dsc) {
322 fnptrn = fnptrn ? fnptrn :
"*";
323 dirstr = dirstr ? dirstr :
".";
326 && (fnptrn[0] ==
'/' || (fnptrn[0] ==
'.' && fnptrn[1]
329 && fnptrn[2] ==
'/')))))
330 glob(fnptrn, globflags, NULL, &g);
333 char dirname[PATH_MAX];
335 strncpy(dirname, dirstr,
sizeof(dirname) - 1);
338 if ((nextdir = strchr(dirname,
':')))
340 if (snprintf(
path,
sizeof(
path),
"%s/%s", *dirname ? dirname :
".",
341 fnptrn) >= (
int)
sizeof(
path)) {
345 glob(
path, globflags, NULL, &g);
347 globflags |= GLOB_APPEND;
362 for (i = 0; i < g.gl_pathc; ++i) {
364 if (0 > XLALCacheFilenameParseEntry(entry, g.gl_pathv[i])) {
378 "Glob is unsupported on non-posix system");
389 for (i = 0; i < cache->
length; ++i) {
423 const char *s1 = ((
const struct tagLALCacheEntry *) p1)->src;
424 const char *s2 = ((
const struct tagLALCacheEntry *) p2)->src;
425 return strcmp(s1 ? s1 :
"", s2 ? s2 :
"");
431 const char *s1 = ((
const struct tagLALCacheEntry *) p1)->dsc;
432 const char *s2 = ((
const struct tagLALCacheEntry *) p2)->dsc;
433 return strcmp(s1 ? s1 :
"", s2 ? s2 :
"");
439 double t1 = ((
const struct tagLALCacheEntry *) p1)->t0;
440 double t2 = ((
const struct tagLALCacheEntry *) p2)->t0;
441 return (t1 > t2) - (t1 < t2);
447 double t1 = ((
const struct tagLALCacheEntry *) p1)->dt;
448 double t2 = ((
const struct tagLALCacheEntry *) p2)->dt;
449 return (t1 > t2) - (t1 < t2);
486 for (j = 0; j < cache->
length; ++j) {
491 if (j + 1 == cache->
length
495 for (j = i; j < cache->
length; ++j) {
511 INT4 t1, regex_t * srcreg, regex_t * dscreg,
514 if (t1 > 0 && !(entry->
t0 < t1))
516 if (t0 > 0 && !(entry->
t0 + entry->
dt > t0))
519 if (!entry->
src || regexec(srcreg, entry->
src, 0, NULL, 0))
522 if (!entry->
dsc || regexec(dscreg, entry->
dsc, 0, NULL, 0))
525 if (!entry->
url || regexec(urlreg, entry->
url, 0, NULL, 0))
535 if (t1 > 0 && !(entry->
t0 < t1))
537 if (t0 > 0 && !(entry->
t0 + entry->
dt > t0))
545 const char *srcregex,
const char *dscregex,
546 const char *urlregex)
556 if (srcregex || dscregex || urlregex) {
558 "Regular expression matching is not supported");
567 regcomp(&srcreg, srcregex, REG_NOSUB);
569 regcomp(&dscreg, dscregex, REG_NOSUB);
571 regcomp(&urlreg, urlregex, REG_NOSUB);
574 for (i = 0; i < cache->
length; ++i) {
578 XLALCacheEntryMatch(cache->
list + i, t0, t1,
579 srcregex ? &srcreg : NULL,
580 dscregex ? &dscreg : NULL,
581 urlregex ? &urlreg : NULL);
605 if (n && !cache->
list)
630 if (
t > entry->
t0 + entry->
dt)
638 if (k->
first == ptr ||
t > entry[-1].
t0 + entry[-1].
dt)
644 if (k->
first != ptr &&
t < entry[-1].
t0 + entry[-1].
dt)
659 return bsearch(&key, cache->
list, cache->
length,
sizeof(*cache->
list),
674 if (nextslash && nextcolon && nextcolon < nextslash
675 && 0 == strncmp(nextcolon,
"://", 3)) {
678 "Unsupported protocol in URL %s (only file supported)",
685 "Cannot read files from remote host in URL %s (only localhost supported)",
static char * XLALCacheFileNextField(char **ps)
static int XLALCacheFileReadRow(char *s, size_t len, LALFILE *fp, int *line)
static int XLALCacheCompareStartTime(void UNUSED *p, const void *p1, const void *p2)
static int XLALCacheEntryMatchTime(const LALCacheEntry *entry, INT4 t0, INT4 t1)
static int XLALCacheEntryCopy(LALCacheEntry *dst, const LALCacheEntry *src)
static int XLALCacheFileCountRows(LALFILE *fp)
static int XLALCacheCompareSource(void UNUSED *p, const void *p1, const void *p2)
static int XLALCacheEntryBsearchCompare(const void *key, const void *ptr)
static int XLALCacheCompareDescription(void UNUSED *p, const void *p1, const void *p2)
static int XLALCacheCompareDuration(void UNUSED *p, const void *p1, const void *p2)
static int XLALCacheCompareEntryMetadata(void *p, const void *p1, const void *p2)
static int XLALCacheFileParseEntry(struct tagLALCacheEntry *entry, char *s)
int XLALFileClose(LALFILE *file)
int XLALFileEOF(LALFILE *file)
char * XLALFileGets(char *s, int size, LALFILE *file)
LALFILE * XLALFileOpenRead(const char *path)
int XLALFilePrintf(LALFILE *file, const char *fmt,...)
void XLALFileRewind(LALFILE *file)
LALFILE * XLALFileOpen(const char *path, const char *mode)
LALCache * XLALCreateCache(UINT4 length)
Creates a LALCache structure.
LALFILE * XLALCacheEntryOpen(const LALCacheEntry *entry)
Open a file identified by an entry in a LALCache structure.
LALCache * XLALCacheImport(const char *fname)
Reads a LAL cache file and produces a LALCache structure.
LALCache * XLALCacheMerge(const LALCache *cache1, const LALCache *cache2)
Returns a new LALCache structure that is the merge of two.
void XLALDestroyCache(LALCache *cache)
Destroys a LALCache structure.
LALCache * XLALCacheGlob(const char *dirstr, const char *fnptrn)
Globs a directory and construct LALCache from matching entries.
LALCacheEntry * XLALCacheEntrySeek(const LALCache *cache, double t)
Finds the first entry that contains the requested time, or the first entry after the time if the time...
int XLALCacheFileWrite(LALFILE *fp, const LALCache *cache)
Writes a LALCache structure to output LALFILE.
LALCache * XLALCacheFileRead(LALFILE *fp)
Reads a LAL cache file and produces a LALCache structure.
int XLALCacheUniq(LALCache *cache)
Prunes duplicate entries keeping the second one; cache is reduced in length if there are.
int XLALCacheExport(const LALCache *cache, const char *fname)
Exports a LALCache structure to an output LAL cache file.
int XLALCacheSort(LALCache *cache)
Sorts entries in a LALCache structure.
int XLALCacheSieve(LALCache *cache, INT4 t0, INT4 t1, const char *srcregex, const char *dscregex, const char *urlregex)
Selects only matching entries in a LALCache structure – other entries are deleted from the LALCache s...
LALCache * XLALCacheDuplicate(const LALCache *cache)
Duplicates a LALCache structure.
#define XLAL_LAST_ELEM(x)
MACRO to access the last element in a fixed-size array.
uint32_t UINT4
Four-byte unsigned integer.
int32_t INT4
Four-byte signed integer.
#define XLALRealloc(p, n)
char * XLALStringDuplicate(const char *s)
Like strdup but uses LAL allocation routines (free with LALFree).
char * XLALStringAppend(char *s, const char *append)
Like strcat but dynamically reallocates string with LALRealloc.
int XLALInsertionSort(void *base, size_t nobj, size_t size, void *params, int(*compar)(void *, const void *, const void *))
#define XLAL_ERROR_NULL(...)
Macro to invoke a failure from a XLAL routine returning a pointer.
#define XLAL_ERROR(...)
Macro to invoke a failure from a XLAL routine returning an integer.
#define XLAL_PRINT_WARNING(...)
Macro that will print a warning message with a standard format.
@ XLAL_ENOMEM
Memory allocation error.
@ XLAL_EFAULT
Invalid pointer.
@ XLAL_EFUNC
Internal function call failed bit: "or" this with existing error number.
@ XLAL_EFAILED
Generic failure.
def duplicate(series)
Duplicate a TimeSeries or FrequencySeries.
CHAR * src
File source field.
CHAR * dsc
File description field.
INT4 t0
GPS time (seconds) of beginning of data in file.
INT4 dt
Duration (seconds) of data in file.
The LALCache structure is an array of entries.