aboutsummaryrefslogtreecommitdiff
path: root/pkg/tbtables/fitsio/ftcmps.f
blob: 3ba41e38e78ddf9862569c3708632d2611a49b2d (plain) (blame)
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
C--------------------------------------------------------------------------
        subroutine ftcmps(templt,string,casesn,match,exact)

C       compare the template to the string and test if they match.
C       The strings are limited to 68 characters or less (the max. length
C       of a FITS string keyword value.  This routine reports whether
C       the two strings match and whether the match is exact or
C       involves wildcards.

C       this algorithm is very similar to the way unix filename wildcards
C       work except that this first treats a wild card as a literal character
C       when looking for a match.  If there is no literal match, then
C       it interpretes it as a wild card.  So the template 'AB*DE'
C       is considered to be an exact rather than a wild card match to
C       the string 'AB*DE'.

C       templt    C input template (may includ % or * wild cards)
C       string    C input string to be compared to template
C       casesn    L should comparison be case sensitive?
C       match     L (output) does the template match the string?
C       exact     L (output) are the strings an exact match (true) or
C                            is it a wildcard match (false)

C       written by Wm Pence, HEASARC/GSFC, December 1994

        character*(*) templt,string
        logical casesn,match,exact
        character*68 temp,str
        integer tlen,slen,t1,s1

        tlen=len(templt)
        slen=len(string)
        tlen=min(tlen,68)
        slen=min(tlen,68)

        match=.false.
        exact=.true.
        temp=templt
        str=string
        if (.not. casesn)then
            call ftupch(temp)
            call ftupch(str)
        end if

C       check for exact match
        if (temp .eq. str)then
            match=.true.
            return
        else
C           the strings are not identical, any match cannot be exact
            exact=.false.
        end if

        t1=1
        s1=1
10      continue
        if (t1 .gt. tlen .or. s1 .gt. slen)then
C           completely scanned one or both strings, so it must be a match
            match=.true.
            return
        end if

C       see if the characters in the 2 strings are an exact match
        if (temp(t1:t1) .eq. str(s1:s1))then
            s1=s1+1
            t1=t1+1
        else
            exact=.false.
            if (temp(t1:t1) .eq. '?')then
C               The '?' wild card matches anything
                s1=s1+1
                t1=t1+1
            else if (temp(t1:t1) .eq. '*')then
C               get next character from template and look for it in the string
                t1=t1+1
                if (t1 .le. tlen)then
                    if (temp(t1:t1) .eq. ' ')then
C                       * is followed by a space, so a match is guaranteed
                        t1=tlen+1
                    else
20                      continue
                        if (temp(t1:t1) .eq. str(s1:s1))then
C                           found a matching character
                            t1=t1+1
                            s1=s1+1
                        else
C                           increment the string pointer and try again
                            s1=s1+1
                            if (s1 .le. slen)then
                                go to 20
                            else
C                               hit end of string and failed to find a match
                                return
                            end if
                        end if
                    end if
                end if
            else
C               match failed
                return
            end if
        end if
        go to 10
        end