-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathselection.c
931 lines (822 loc) · 25.7 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
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
/***************************************************************************
* Generic routines to manage selection lists.
*
* This file is part of the miniSEED Library.
*
* Copyright (c) 2024 Chad Trabant, EarthScope Data Services
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
***************************************************************************/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "libmseed.h"
static int ms_isinteger (const char *string);
static int ms_globmatch (const char *string, const char *pattern);
/**********************************************************************/ /**
* @brief Test the specified parameters for a matching selection entry
*
* Search the ::MS3Selections for an entry matching the provided
* parameters. The ::MS3Selections.sidpattern may contain globbing
* characters. The ::MS3Selections.timewindows many contain start and
* end times set to ::NSTUNSET to denote "open" times.
*
* Positive matching requires:
* @parblock
* -# glob match of sid against sidpattern in selection
* -# time window intersection with range in selection
* -# equal pubversion if selection pubversion > 0
* @endparblock
*
* @param[in] selections ::MS3Selections to search
* @param[in] sid Source ID to match
* @param[in] starttime Start time to match
* @param[in] endtime End time to match
* @param[in] pubversion Publication version to match
* @param[out] ppselecttime Pointer-to-pointer to return the matching ::MS3SelectTime entry
*
* @returns A pointer to matching ::MS3Selections entry successful
* match and NULL for no match or error.
***************************************************************************/
const MS3Selections *
ms3_matchselect (const MS3Selections *selections, const char *sid, nstime_t starttime,
nstime_t endtime, int pubversion, const MS3SelectTime **ppselecttime)
{
const MS3Selections *findsl = NULL;
const MS3SelectTime *findst = NULL;
const MS3SelectTime *matchst = NULL;
if (selections)
{
findsl = selections;
while (findsl)
{
if (ms_globmatch (sid, findsl->sidpattern))
{
if (findsl->pubversion > 0 && findsl->pubversion != pubversion)
{
findsl = findsl->next;
continue;
}
/* If no time selection, this is a match */
if (!findsl->timewindows)
{
if (ppselecttime)
*ppselecttime = NULL;
return findsl;
}
/* Otherwise, search the time selections */
findst = findsl->timewindows;
while (findst)
{
if (starttime != NSTERROR && starttime != NSTUNSET &&
findst->starttime != NSTERROR && findst->starttime != NSTUNSET &&
(starttime < findst->starttime && !(starttime <= findst->starttime && endtime >= findst->starttime)))
{
findst = findst->next;
continue;
}
else if (endtime != NSTERROR && endtime != NSTUNSET &&
findst->endtime != NSTERROR && findst->endtime != NSTUNSET &&
(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 ms3_matchselect() */
/**********************************************************************/ /**
* @brief Test the ::MS3Record for a matching selection entry
*
* Search the ::MS3Selections for an entry matching the provided
* parameters.
*
* Positive matching requires:
* @parblock
* -# glob match of sid against sidpattern in selection
* -# time window intersection with range in selection
* -# equal pubversion if selection pubversion > 0
* @endparblock
*
* @param[in] selections ::MS3Selections to search
* @param[in] msr ::MS3Record to match against selections
* @param[out] ppselecttime Pointer-to-pointer to return the matching ::MS3SelectTime entry
*
* @returns A pointer to matching ::MS3Selections entry successful
* match and NULL for no match or error.
***************************************************************************/
const MS3Selections *
msr3_matchselect (const MS3Selections *selections, const MS3Record *msr,
const MS3SelectTime **ppselecttime)
{
nstime_t endtime;
if (!selections || !msr)
return NULL;
endtime = msr3_endtime (msr);
return ms3_matchselect (selections, msr->sid, msr->starttime, endtime,
msr->pubversion, ppselecttime);
} /* End of msr3_matchselect() */
/**********************************************************************/ /**
* @brief Add selection parameters to selection list.
*
* The \a sidpattern may contain globbing characters.
*
* The \a starttime and \a endtime may be set to ::NSTUNSET to denote
* "open" times.
*
* The \a pubversion may be set to 0 to match any publication
* version.
*
* @param[in] ppselections ::MS3Selections to add new selection to
* @param[in] sidpattern Source ID pattern, may contain globbing characters
* @param[in] starttime Start time for selection, ::NSTUNSET for open
* @param[in] endtime End time for selection, ::NSTUNSET for open
* @param[in] pubversion Publication version for selection, 0 for any
*
* @returns 0 on success and -1 on error.
*
* \ref MessageOnError - this function logs a message on error
***************************************************************************/
int
ms3_addselect (MS3Selections **ppselections, const char *sidpattern,
nstime_t starttime, nstime_t endtime, uint8_t pubversion)
{
MS3Selections *newsl = NULL;
MS3SelectTime *newst = NULL;
if (!ppselections || !sidpattern)
{
ms_log (2, "%s(): Required input not defined: 'ppselections' or 'sidpattern'\n",
__func__);
return -1;
}
/* Allocate new SelectTime and populate */
if (!(newst = (MS3SelectTime *)libmseed_memory.malloc (sizeof (MS3SelectTime))))
{
ms_log (2, "Cannot allocate memory\n");
return -1;
}
memset (newst, 0, sizeof (MS3SelectTime));
newst->starttime = starttime;
newst->endtime = endtime;
/* Add new Selections struct to begining of list */
if (!*ppselections)
{
/* Allocate new Selections and populate */
if (!(newsl = (MS3Selections *)libmseed_memory.malloc (sizeof (MS3Selections))))
{
ms_log (2, "Cannot allocate memory\n");
return -1;
}
memset (newsl, 0, sizeof (MS3Selections));
strncpy (newsl->sidpattern, sidpattern, sizeof (newsl->sidpattern));
newsl->sidpattern[sizeof (newsl->sidpattern) - 1] = '\0';
newsl->pubversion = pubversion;
/* Add new MS3SelectTime struct as first in list */
*ppselections = newsl;
newsl->timewindows = newst;
}
else
{
MS3Selections *findsl = *ppselections;
MS3Selections *matchsl = 0;
/* Search for matching MS3Selections entry */
while (findsl)
{
if (!strcmp (findsl->sidpattern, sidpattern) && findsl->pubversion == pubversion)
{
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 MS3Selections and populate */
if (!(newsl = (MS3Selections *)libmseed_memory.malloc (sizeof (MS3Selections))))
{
ms_log (2, "Cannot allocate memory\n");
return -1;
}
memset (newsl, 0, sizeof (MS3Selections));
strncpy (newsl->sidpattern, sidpattern, sizeof (newsl->sidpattern));
newsl->sidpattern[sizeof (newsl->sidpattern) - 1] = '\0';
newsl->pubversion = pubversion;
/* Add new MS3Selections to beginning of list */
newsl->next = *ppselections;
*ppselections = newsl;
newsl->timewindows = newst;
}
}
return 0;
} /* End of ms3_addselect() */
/**********************************************************************/ /**
* @brief Add selection parameters to a selection list based on
* separate source name codes
*
* The \a network, \a station, \a location, and \a channel arguments may
* contain globbing parameters.
* The \a starttime and \a endtime may be set to ::NSTUNSET to denote
* "open" times.
*
* The \a pubversion may be set to 0 to match any publication
* version.
*
* 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 code (loc) is set to \c "--" to
* match an empty location code it will be translated to an empty string
* to match libmseed's notation.
*
* @param[in] ppselections ::MS3Selections to add new selection to
* @param[in] network Network code, may contain globbing characters
* @param[in] station Statoin code, may contain globbing characters
* @param[in] location Location code, may contain globbing characters
* @param[in] channel channel code, may contain globbing characters
* @param[in] starttime Start time for selection, ::NSTUNSET for open
* @param[in] endtime End time for selection, ::NSTUNSET for open
* @param[in] pubversion Publication version for selection, 0 for any
*
* @return 0 on success and -1 on error.
*
* \ref MessageOnError - this function logs a message on error
***************************************************************************/
int
ms3_addselect_comp (MS3Selections **ppselections, char *network, char *station,
char *location, char *channel, nstime_t starttime,
nstime_t endtime, uint8_t pubversion)
{
char sidpattern[100];
char selnet[20];
char selsta[20];
char selloc[20];
char selchan[20];
if (!ppselections)
{
ms_log (2, "%s(): Required input not defined: 'ppselections'\n", __func__);
return -1;
}
if (network)
{
strncpy (selnet, network, sizeof (selnet));
selnet[sizeof (selnet) - 1] = '\0';
}
else
{
strcpy (selnet, "*");
}
if (station)
{
strncpy (selsta, station, sizeof (selsta));
selsta[sizeof (selsta) - 1] = '\0';
}
else
{
strcpy (selsta, "*");
}
if (location)
{
/* Test for special case blank location ID */
if (!strcmp (location, "--"))
{
selloc[0] = '\0';
}
else
{
strncpy (selloc, location, sizeof (selloc));
selloc[sizeof (selloc) - 1] = '\0';
}
}
else
{
strcpy (selloc, "*");
}
if (channel)
{
/* Convert a 3-character SEED 2.x channel code to an extended code */
if (ms_globmatch (channel, "[?*a-zA-Z0-9][?*a-zA-Z0-9][?*a-zA-Z0-9]"))
{
ms_seedchan2xchan (selchan, channel);
}
else
{
strncpy (selchan, channel, sizeof (selchan));
selchan[sizeof (selchan) - 1] = '\0';
}
}
else
{
strcpy (selchan, "*");
}
/* Create the source identifier globbing match for this entry */
if (ms_nslc2sid (sidpattern, sizeof (sidpattern), 0,
selnet, selsta, selloc, selchan) < 0)
return -1;
/* Add selection to list */
if (ms3_addselect (ppselections, sidpattern, starttime, endtime, pubversion))
return -1;
return 0;
} /* End of ms3_addselect_comp() */
#define MAX_SELECTION_FIELDS 8
/**********************************************************************/ /**
* @brief Read data selections from a file
*
* Selections from a file are added 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.
*
* Each line of the file contains a single selection and may be one of
* these two line formats:
* @code
* SourceID [Starttime [Endtime [Pubversion]]]
* @endcode
* or
* @code
* Network Station Location Channel [Pubversion [Starttime [Endtime]]]
* @endcode
*
* The \c Starttime and \c Endtime values must be in a form recognized
* by ms_timestr2nstime() and include a full date (i.e. just a year is
* not allowed).
*
* In the latter version, if the "Channel" field is a SEED 2.x channel
* (3-characters) it will automatically be converted into extended
* channel form (band_source_subsource).
*
* In the latter version, the "Pubversion" field, which was "Quality"
* in earlier versions of the library, is assumed to be a publication
* version if it is an integer, otherwise it is ignored.
*
* @returns Count of selections added on success and -1 on error.
*
* \ref MessageOnError - this function logs a message on error
***************************************************************************/
int
ms3_readselectionsfile (MS3Selections **ppselections, const char *filename)
{
FILE *fp;
nstime_t starttime;
nstime_t endtime;
uint8_t pubversion;
char selectline[200];
char *line;
char *fields[MAX_SELECTION_FIELDS];
char *cp;
char next;
int selectcount = 0;
int linecount = 0;
int fieldidx;
uint8_t isstart2;
uint8_t isend3;
uint8_t isstart6;
uint8_t isend7;
if (!ppselections || !filename)
{
ms_log (2, "%s(): Required input not defined: 'ppselections' or 'filename'\n",
__func__);
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))
{
linecount++;
/* Reset fields array */
for (fieldidx = 0; fieldidx < MAX_SELECTION_FIELDS; fieldidx++)
fields[fieldidx] = NULL;
/* Guarantee termination */
selectline[sizeof (selectline) - 1] = '\0';
line = selectline;
/* Trim trailing whitespace (including newlines, carriage returns, etc.) if any */
cp = line;
while (*cp != '\0')
{
cp++;
}
cp--;
while (cp >= line && isspace((int)(*cp)))
{
*cp = '\0';
cp--;
}
/* Trim leading whitespace if any */
cp = line;
while (isspace((int)(*cp)))
{
line = cp = cp+1;
}
/* Skip empty lines */
if (!strlen (line))
continue;
/* Skip comment lines */
if (*line == '#')
continue;
/* Set fields array to whitespace delimited fields */
cp = line;
next = 0; /* For this loop: 0 = whitespace, 1 = non-whitespace */
fieldidx = 0;
while (*cp && fieldidx < MAX_SELECTION_FIELDS)
{
if (!isspace ((int)(*cp)))
{
/* Field starts at transition from whitespace to non-whitespace */
if (next == 0)
{
fields[fieldidx] = cp;
fieldidx++;
}
next = 1;
}
else
{
/* Field ends at transition from non-whitespace to whitespace */
if (next == 1)
*cp = '\0';
next = 0;
}
cp++;
}
/* Globbing pattern to match the beginning of a date "YYYY[-/,]#..." */
#define INITDATEGLOB "[0-9][0-9][0-9][0-9][-/,][0-9]*"
isstart2 = (fields[1]) ? ms_globmatch (fields[1], INITDATEGLOB) : 0;
isend3 = (fields[2]) ? ms_globmatch (fields[2], INITDATEGLOB) : 0;
isstart6 = (fields[5]) ? ms_globmatch (fields[5], INITDATEGLOB) : 0;
isend7 = (fields[6]) ? ms_globmatch (fields[6], INITDATEGLOB) : 0;
/* Convert starttime to nstime_t */
starttime = NSTUNSET;
cp = NULL;
if (isstart2)
cp = fields[1];
else if (isstart6)
cp = fields[5];
if (cp)
{
starttime = ms_timestr2nstime (cp);
if (starttime == NSTUNSET)
{
ms_log (2, "Cannot convert data selection start time (line %d): %s\n", linecount, cp);
return -1;
}
}
/* Convert endtime to nstime_t */
endtime = NSTUNSET;
cp = NULL;
if (isend3)
cp = fields[2];
else if (isend7)
cp = fields[6];
if (cp)
{
endtime = ms_timestr2nstime (cp);
if (endtime == NSTUNSET)
{
ms_log (2, "Cannot convert data selection end time (line %d): %s\n", linecount, cp);
return -1;
}
}
/* Test for "SourceID [Starttime [Endtime [Pubversion]]]" */
if (fieldidx == 1 ||
(fieldidx == 2 && isstart2) ||
(fieldidx == 3 && isstart2 && isend3) ||
(fieldidx == 4 && isstart2 && isend3 && ms_isinteger(fields[3])))
{
/* Convert publication version to integer */
pubversion = 0;
if (fields[3])
{
long int longpver;
longpver = strtol (fields[3], NULL, 10);
if (longpver < 0 || longpver > 255 )
{
ms_log (2, "Cannot convert publication version (line %d): %s\n", linecount, fields[3]);
return -1;
}
else
{
pubversion = (uint8_t) longpver;
}
}
/* Add selection to list */
if (ms3_addselect (ppselections, fields[0], starttime, endtime, pubversion))
{
ms_log (2, "%s: Error adding selection on line %d\n", filename, linecount);
return -1;
}
}
/* Test for "Network Station Location Channel [Quality [Starttime [Endtime]]]" */
else if (fieldidx == 4 || fieldidx == 5 ||
(fieldidx == 6 && isstart6) ||
(fieldidx == 7 && isstart6 && isend7))
{
/* Convert quality field to publication version if integer */
pubversion = 0;
if (fields[4] && ms_isinteger(fields[4]))
{
long int longpver;
longpver = strtol (fields[4], NULL, 10);
if (longpver < 0 || longpver > 255 )
{
ms_log (2, "Cannot convert publication version (line %d): %s\n", linecount, fields[4]);
return -1;
}
else
{
pubversion = (uint8_t) longpver;
}
}
if (ms3_addselect_comp (ppselections, fields[0], fields[1], fields[2], fields[3],
starttime, endtime, pubversion))
{
ms_log (2, "%s: Error adding selection on line %d\n", filename, linecount);
return -1;
}
}
else
{
ms_log (1, "%s: Skipping unrecognized data selection on line %d\n", filename, linecount);
}
selectcount++;
}
if (fp != stdin)
fclose (fp);
return selectcount;
} /* End of ms_readselectionsfile() */
/**********************************************************************/ /**
* @brief Free all memory associated with a ::MS3Selections
*
* All memory from one or more ::MS3Selections (in a linked list) are freed.
*
* @param[in] selections Start of ::MS3Selections to free
***************************************************************************/
void
ms3_freeselections (MS3Selections *selections)
{
MS3Selections *select;
MS3Selections *selectnext;
MS3SelectTime *selecttime;
MS3SelectTime *selecttimenext;
if (selections)
{
select = selections;
while (select)
{
selectnext = select->next;
selecttime = select->timewindows;
while (selecttime)
{
selecttimenext = selecttime->next;
libmseed_memory.free (selecttime);
selecttime = selecttimenext;
}
libmseed_memory.free (select);
select = selectnext;
}
}
} /* End of ms3_freeselections() */
/**********************************************************************/ /**
* @brief Print the selections list using the ms_log() facility.
*
* All selections are printed with simple formatting.
*
* @param[in] selections Start of ::MS3Selections to print
***************************************************************************/
void
ms3_printselections (const MS3Selections *selections)
{
const MS3Selections *select;
MS3SelectTime *selecttime;
char starttime[50];
char endtime[50];
if (!selections)
return;
select = selections;
while (select)
{
ms_log (0, "Selection: %s pubversion: %d\n",
select->sidpattern, select->pubversion);
selecttime = select->timewindows;
while (selecttime)
{
if (selecttime->starttime != NSTERROR && selecttime->starttime != NSTUNSET)
ms_nstime2timestr (selecttime->starttime, starttime, ISOMONTHDAY_Z, NANO_MICRO_NONE);
else
strncpy (starttime, "No start time", sizeof (starttime) - 1);
if (selecttime->endtime != NSTERROR && selecttime->endtime != NSTUNSET)
ms_nstime2timestr (selecttime->endtime, endtime, ISOMONTHDAY_Z, NANO_MICRO_NONE);
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 ms3_printselections() */
/***************************************************************************
* ms_isinteger:
*
* Test a string for all digits, i.e. and integer
*
* Returns 1 if is integer and 0 otherwise.
***************************************************************************/
static int
ms_isinteger (const char *string)
{
while (*string)
{
if (!isdigit((int)(*string)))
return 0;
string++;
}
return 1;
}
/***********************************************************************
* 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 (const char *string, const 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++;
break;
default:
if (c != *string)
return GLOBMATCH_FALSE;
break;
}
string++;
}
return !*string;
} /* End of ms_globmatch() */