forked from EarthScope/libmseed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
selection.c
716 lines (626 loc) · 17.5 KB
/
selection.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
/***************************************************************************
* selection.c:
*
* Generic routines to manage selection lists.
*
* Written by Chad Trabant unless otherwise noted
* IRIS Data Management Center
*
* modified: 2014.197
***************************************************************************/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "libmseed.h"
static int ms_globmatch (char *string, char *pattern);
/***************************************************************************
* ms_matchselect:
*
* Test the specified parameters for a matching selection entry. The
* srcname parameter may contain globbing characters. The NULL value
* (matching any times) for the start and end times is HPTERROR.
*
* Return Selections pointer to matching entry on successful match and
* NULL for no match or error.
***************************************************************************/
Selections *
ms_matchselect (Selections *selections, char *srcname, hptime_t starttime,
hptime_t endtime, SelectTime **ppselecttime)
{
Selections *findsl = NULL;
SelectTime *findst = NULL;
SelectTime *matchst = NULL;
if (selections)
{
findsl = selections;
while (findsl)
{
if (ms_globmatch (srcname, findsl->srcname))
{
findst = findsl->timewindows;
while (findst)
{
if (starttime != HPTERROR && findst->starttime != HPTERROR &&
(starttime < findst->starttime && !(starttime <= findst->starttime && endtime >= findst->starttime)))
{
findst = findst->next;
continue;
}
else if (endtime != HPTERROR && findst->endtime != HPTERROR &&
(endtime > findst->endtime && !(starttime <= findst->endtime && endtime >= findst->endtime)))
{
findst = findst->next;
continue;
}
matchst = findst;
break;
}
}
if (matchst)
break;
else
findsl = findsl->next;
}
}
if (ppselecttime)
*ppselecttime = matchst;
return (matchst) ? findsl : NULL;
} /* End of ms_matchselect() */
/***************************************************************************
* msr_matchselect:
*
* A simple wrapper for calling ms_matchselect() using details from a
* MSRecord struct.
*
* Return Selections pointer to matching entry on successful match and
* NULL for no match or error.
***************************************************************************/
Selections *
msr_matchselect (Selections *selections, MSRecord *msr, SelectTime **ppselecttime)
{
char srcname[50];
hptime_t endtime;
if (!selections || !msr)
return NULL;
msr_srcname (msr, srcname, 1);
endtime = msr_endtime (msr);
return ms_matchselect (selections, srcname, msr->starttime, endtime,
ppselecttime);
} /* End of msr_matchselect() */
/***************************************************************************
* ms_addselect:
*
* Add select parameters to a specified selection list. The srcname
* argument may contain globbing parameters. The NULL value (matching
* any value) for the start and end times is HPTERROR.
*
* Return 0 on success and -1 on error.
***************************************************************************/
int
ms_addselect (Selections **ppselections, char *srcname,
hptime_t starttime, hptime_t endtime)
{
Selections *newsl = NULL;
SelectTime *newst = NULL;
if (!ppselections || !srcname)
return -1;
/* Allocate new SelectTime and populate */
if (!(newst = (SelectTime *)calloc (1, sizeof (SelectTime))))
{
ms_log (2, "Cannot allocate memory\n");
return -1;
}
newst->starttime = starttime;
newst->endtime = endtime;
/* Add new Selections struct to begining of list */
if (!*ppselections)
{
/* Allocate new Selections and populate */
if (!(newsl = (Selections *)calloc (1, sizeof (Selections))))
{
ms_log (2, "Cannot allocate memory\n");
return -1;
}
strncpy (newsl->srcname, srcname, sizeof (newsl->srcname));
newsl->srcname[sizeof (newsl->srcname) - 1] = '\0';
/* Add new Selections struct as first in list */
*ppselections = newsl;
newsl->timewindows = newst;
}
else
{
Selections *findsl = *ppselections;
Selections *matchsl = 0;
/* Search for matching Selectlink entry */
while (findsl)
{
if (!strcmp (findsl->srcname, srcname))
{
matchsl = findsl;
break;
}
findsl = findsl->next;
}
if (matchsl)
{
/* Add time window selection to beginning of window list */
newst->next = matchsl->timewindows;
matchsl->timewindows = newst;
}
else
{
/* Allocate new Selections and populate */
if (!(newsl = (Selections *)calloc (1, sizeof (Selections))))
{
ms_log (2, "Cannot allocate memory\n");
return -1;
}
strncpy (newsl->srcname, srcname, sizeof (newsl->srcname));
newsl->srcname[sizeof (newsl->srcname) - 1] = '\0';
/* Add new Selections to beginning of list */
newsl->next = *ppselections;
*ppselections = newsl;
newsl->timewindows = newst;
}
}
return 0;
} /* End of ms_addselect() */
/***************************************************************************
* ms_addselect_comp:
*
* Add select parameters to a specified selection list based on
* separate name components. The network, station, location, channel
* and quality arguments may contain globbing parameters. The NULL
* value (matching any value) for the start and end times is HPTERROR.
*
* If any of the naming parameters are not supplied (pointer is NULL)
* a wildcard for all matches is substituted. As a special case, if
* the location ID (loc) is set to "--" to match a space-space/blank
* ID it will be translated to an empty string to match libmseed's
* notation.
*
* Return 0 on success and -1 on error.
***************************************************************************/
int
ms_addselect_comp (Selections **ppselections, char *net, char *sta, char *loc,
char *chan, char *qual, hptime_t starttime, hptime_t endtime)
{
char srcname[100];
char selnet[20];
char selsta[20];
char selloc[20];
char selchan[20];
char selqual[20];
if (!ppselections)
return -1;
if (net)
{
strncpy (selnet, net, sizeof (selnet));
selnet[sizeof (selnet) - 1] = '\0';
}
else
strcpy (selnet, "*");
if (sta)
{
strncpy (selsta, sta, sizeof (selsta));
selsta[sizeof (selsta) - 1] = '\0';
}
else
strcpy (selsta, "*");
if (loc)
{
/* Test for special case blank location ID */
if (!strcmp (loc, "--"))
selloc[0] = '\0';
else
{
strncpy (selloc, loc, sizeof (selloc));
selloc[sizeof (selloc) - 1] = '\0';
}
}
else
strcpy (selloc, "*");
if (chan)
{
strncpy (selchan, chan, sizeof (selchan));
selchan[sizeof (selchan) - 1] = '\0';
}
else
strcpy (selchan, "*");
if (qual)
{
strncpy (selqual, qual, sizeof (selqual));
selqual[sizeof (selqual) - 1] = '\0';
}
else
strcpy (selqual, "?");
/* Create the srcname globbing match for this entry */
snprintf (srcname, sizeof (srcname), "%s_%s_%s_%s_%s",
selnet, selsta, selloc, selchan, selqual);
/* Add selection to list */
if (ms_addselect (ppselections, srcname, starttime, endtime))
return -1;
return 0;
} /* End of ms_addselect_comp() */
/***************************************************************************
* ms_readselectionsfile:
*
* Read a list of data selections from a file and them to the
* specified selections list. On errors this routine will leave
* allocated memory unreachable (leaked), it is expected that this is
* a program failing condition.
*
* As a special case if the filename is "-", selection lines will be
* read from stdin.
*
* Returns count of selections added on success and -1 on error.
***************************************************************************/
int
ms_readselectionsfile (Selections **ppselections, char *filename)
{
FILE *fp;
hptime_t starttime;
hptime_t endtime;
char selectline[200];
char *selnet;
char *selsta;
char *selloc;
char *selchan;
char *selqual;
char *selstart;
char *selend;
char *cp;
char next;
int selectcount = 0;
int linecount = 0;
if (!ppselections || !filename)
return -1;
if (strcmp (filename, "-"))
{
if (!(fp = fopen (filename, "rb")))
{
ms_log (2, "Cannot open file %s: %s\n", filename, strerror (errno));
return -1;
}
}
else
{
/* Use stdin as special case */
fp = stdin;
}
while (fgets (selectline, sizeof (selectline) - 1, fp))
{
selnet = 0;
selsta = 0;
selloc = 0;
selchan = 0;
selqual = 0;
selstart = 0;
selend = 0;
linecount++;
/* Guarantee termination */
selectline[sizeof (selectline) - 1] = '\0';
/* End string at first newline character if any */
if ((cp = strchr (selectline, '\n')))
*cp = '\0';
/* Skip empty lines */
if (!strlen (selectline))
continue;
/* Skip comment lines */
if (*selectline == '#')
continue;
/* Parse: identify components of selection and terminate */
cp = selectline;
next = 1;
while (*cp)
{
if (*cp == ' ' || *cp == '\t')
{
*cp = '\0';
next = 1;
}
else if (*cp == '#')
{
*cp = '\0';
break;
}
else if (next && !selnet)
{
selnet = cp;
next = 0;
}
else if (next && !selsta)
{
selsta = cp;
next = 0;
}
else if (next && !selloc)
{
selloc = cp;
next = 0;
}
else if (next && !selchan)
{
selchan = cp;
next = 0;
}
else if (next && !selqual)
{
selqual = cp;
next = 0;
}
else if (next && !selstart)
{
selstart = cp;
next = 0;
}
else if (next && !selend)
{
selend = cp;
next = 0;
}
else if (next)
{
*cp = '\0';
break;
}
cp++;
}
/* Skip line if network, station, location and channel are not defined */
if (!selnet || !selsta || !selloc || !selchan)
{
ms_log (2, "[%s] Skipping data selection line number %d\n", filename, linecount);
continue;
}
if (selstart)
{
starttime = ms_seedtimestr2hptime (selstart);
if (starttime == HPTERROR)
{
ms_log (2, "Cannot convert data selection start time (line %d): %s\n", linecount, selstart);
return -1;
}
}
else
{
starttime = HPTERROR;
}
if (selend)
{
endtime = ms_seedtimestr2hptime (selend);
if (endtime == HPTERROR)
{
ms_log (2, "Cannot convert data selection end time (line %d): %s\n", linecount, selend);
return -1;
}
}
else
{
endtime = HPTERROR;
}
/* Add selection to list */
if (ms_addselect_comp (ppselections, selnet, selsta, selloc, selchan, selqual, starttime, endtime))
{
ms_log (2, "[%s] Error adding selection on line %d\n", filename, linecount);
return -1;
}
selectcount++;
}
if (fp != stdin)
fclose (fp);
return selectcount;
} /* End of ms_readselectionsfile() */
/***************************************************************************
* ms_freeselections:
*
* Free all memory associated with a Selections struct.
***************************************************************************/
void
ms_freeselections (Selections *selections)
{
Selections *select;
Selections *selectnext;
SelectTime *selecttime;
SelectTime *selecttimenext;
if (selections)
{
select = selections;
while (select)
{
selectnext = select->next;
selecttime = select->timewindows;
while (selecttime)
{
selecttimenext = selecttime->next;
free (selecttime);
selecttime = selecttimenext;
}
free (select);
select = selectnext;
}
}
} /* End of ms_freeselections() */
/***************************************************************************
* ms_printselections:
*
* Print the selections list using the ms_log() facility.
***************************************************************************/
void
ms_printselections (Selections *selections)
{
Selections *select;
SelectTime *selecttime;
char starttime[50];
char endtime[50];
if (!selections)
return;
select = selections;
while (select)
{
ms_log (0, "Selection: %s\n", select->srcname);
selecttime = select->timewindows;
while (selecttime)
{
if (selecttime->starttime != HPTERROR)
ms_hptime2seedtimestr (selecttime->starttime, starttime, 1);
else
strncpy (starttime, "No start time", sizeof (starttime) - 1);
if (selecttime->endtime != HPTERROR)
ms_hptime2seedtimestr (selecttime->endtime, endtime, 1);
else
strncpy (endtime, "No end time", sizeof (endtime) - 1);
ms_log (0, " %30s %30s\n", starttime, endtime);
selecttime = selecttime->next;
}
select = select->next;
}
} /* End of ms_printselections() */
/***********************************************************************
* robust glob pattern matcher
* ozan s. yigit/dec 1994
* public domain
*
* glob patterns:
* * matches zero or more characters
* ? matches any single character
* [set] matches any character in the set
* [^set] matches any character NOT in the set
* where a set is a group of characters or ranges. a range
* is written as two characters seperated with a hyphen: a-z denotes
* all characters between a to z inclusive.
* [-set] set matches a literal hypen and any character in the set
* []set] matches a literal close bracket and any character in the set
*
* char matches itself except where char is '*' or '?' or '['
* \char matches char, including any pattern character
*
* examples:
* a*c ac abc abbc ...
* a?c acc abc aXc ...
* a[a-z]c aac abc acc ...
* a[-a-z]c a-c aac abc ...
*
* Revision 1.4 2004/12/26 12:38:00 ct
* Changed function name (amatch -> globmatch), variables and
* formatting for clarity. Also add matching header globmatch.h.
*
* Revision 1.3 1995/09/14 23:24:23 oz
* removed boring test/main code.
*
* Revision 1.2 94/12/11 10:38:15 oz
* charset code fixed. it is now robust and interprets all
* variations of charset [i think] correctly, including [z-a] etc.
*
* Revision 1.1 94/12/08 12:45:23 oz
* Initial revision
***********************************************************************/
#define GLOBMATCH_TRUE 1
#define GLOBMATCH_FALSE 0
#define GLOBMATCH_NEGATE '^' /* std char set negation char */
/***********************************************************************
* ms_globmatch:
*
* Check if a string matches a globbing pattern.
*
* Return 0 if string does not match pattern and non-zero otherwise.
**********************************************************************/
static int
ms_globmatch (char *string, char *pattern)
{
int negate;
int match;
int c;
while (*pattern)
{
if (!*string && *pattern != '*')
return GLOBMATCH_FALSE;
switch (c = *pattern++)
{
case '*':
while (*pattern == '*')
pattern++;
if (!*pattern)
return GLOBMATCH_TRUE;
if (*pattern != '?' && *pattern != '[' && *pattern != '\\')
while (*string && *pattern != *string)
string++;
while (*string)
{
if (ms_globmatch (string, pattern))
return GLOBMATCH_TRUE;
string++;
}
return GLOBMATCH_FALSE;
case '?':
if (*string)
break;
return GLOBMATCH_FALSE;
/* set specification is inclusive, that is [a-z] is a, z and
* everything in between. this means [z-a] may be interpreted
* as a set that contains z, a and nothing in between.
*/
case '[':
if (*pattern != GLOBMATCH_NEGATE)
negate = GLOBMATCH_FALSE;
else
{
negate = GLOBMATCH_TRUE;
pattern++;
}
match = GLOBMATCH_FALSE;
while (!match && (c = *pattern++))
{
if (!*pattern)
return GLOBMATCH_FALSE;
if (*pattern == '-') /* c-c */
{
if (!*++pattern)
return GLOBMATCH_FALSE;
if (*pattern != ']')
{
if (*string == c || *string == *pattern ||
(*string > c && *string < *pattern))
match = GLOBMATCH_TRUE;
}
else
{ /* c-] */
if (*string >= c)
match = GLOBMATCH_TRUE;
break;
}
}
else /* cc or c] */
{
if (c == *string)
match = GLOBMATCH_TRUE;
if (*pattern != ']')
{
if (*pattern == *string)
match = GLOBMATCH_TRUE;
}
else
break;
}
}
if (negate == match)
return GLOBMATCH_FALSE;
/* If there is a match, skip past the charset and continue on */
while (*pattern && *pattern != ']')
pattern++;
if (!*pattern++) /* oops! */
return GLOBMATCH_FALSE;
break;
case '\\':
if (*pattern)
c = *pattern++;
default:
if (c != *string)
return GLOBMATCH_FALSE;
break;
}
string++;
}
return !*string;
} /* End of ms_globmatch() */