xref: /aoo42x/main/tools/source/fsys/wldcrd.cxx (revision 89b56da7)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_tools.hxx"
26 #include <tools/wldcrd.hxx>
27 
28 /*************************************************************************
29 |*
30 |*    WildCard::Match()
31 |*
32 |*    Beschreibung      WLDCRD.SDW
33 |*    Ersterstellung    MA 19.06.91
34 |*    Letzte Aenderung  MA 03.07.91
35 |*
36 *************************************************************************/
37 
38 /* Diese Methode ueberprueft, ob die Wilde Karte in pWild mit dem String
39  * in pStr matscht.
40  * Vertragen sich die beiden, so wird 1 zurueckgegeben, sonst 0.
41  *
42  * ein '*' in pWild bedeutet n beliebige Zeichen, mit n>=0
43  * ein '?' in pWild bedeutet genau ein beliebiges Zeichen
44  *
45  */
46 
47 sal_uInt16 WildCard::ImpMatch( const char *pWild, const char *pStr ) const
48 {
49     int    pos=0;
50     int    flag=0;
51 
52     while ( *pWild || flag )
53     {
54         switch (*pWild)
55         {
56             case '?':
57                 if ( *pStr == '\0' )
58                     return 0;
59                 break;
60 
61             default:
62                 if ( (*pWild == '\\') && ((*(pWild+1)=='?') || (*(pWild+1) == '*')) )
63                     pWild++;
64                 if ( *pWild != *pStr )
65                     if ( !pos )
66                         return 0;
67                     else
68                         pWild += pos;
69                 else
70                     break;          // ACHTUNG laeuft unter bestimmten
71                                     // Umstaenden in den nachsten case rein!!
72             case '*':
73                 while ( *pWild == '*' )
74                     pWild++;
75                 if ( *pWild == '\0' )
76                     return 1;
77                 flag = 1;
78                 pos  = 0;
79                 if ( *pStr == '\0' )
80                     return ( *pWild == '\0' );
81                 while ( *pStr && *pStr != *pWild )
82                 {
83                     if ( *pWild == '?' ) {
84                         pWild++;
85                         while ( *pWild == '*' )
86                             pWild++;
87                     }
88                     pStr++;
89                     if ( *pStr == '\0' )
90                         return ( *pWild == '\0' );
91                 }
92                 break;
93         }
94         if ( *pWild != '\0' )
95             pWild++;
96         if ( *pStr != '\0' )
97             pStr++;
98         else
99             flag = 0;
100         if ( flag )
101             pos--;
102     }
103     return ( *pStr == '\0' ) && ( *pWild == '\0' );
104 }
105 
106 /*************************************************************************
107 |*
108 |*    WildCard::Matches()
109 |*
110 |*    Beschreibung      WLDCRD.SDW
111 |*    Ersterstellung    MA 19.06.91
112 |*    Letzte Aenderung  TH 02.02.96
113 |*
114 *************************************************************************/
115 
116 sal_Bool WildCard::Matches( const String& rString ) const
117 {
118     ByteString aTmpWild = aWildString;
119 	ByteString aString(rString, osl_getThreadTextEncoding());
120 
121     sal_uInt16  nSepPos;
122 
123     if ( cSepSymbol != '\0' )
124     {
125         while ( (nSepPos = aTmpWild.Search( cSepSymbol )) != STRING_NOTFOUND )
126         {
127             // alle getrennten WildCard's pruefen
128             if ( ImpMatch( aTmpWild.Copy( 0, nSepPos ).GetBuffer(), aString.GetBuffer() ) )
129                 return sal_True;
130             aTmpWild.Erase( 0, nSepPos + 1 ); // Trennsymbol entfernen
131         }
132         // und noch den hinter dem letzen Trennsymbol bzw. den einzigen
133     }
134 
135     if ( ImpMatch( aTmpWild.GetBuffer(), aString.GetBuffer() ) )
136         return sal_True;
137     else
138         return sal_False;
139 }
140