xref: /aoo42x/main/tools/source/fsys/wldcrd.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_tools.hxx"
30 #include <tools/wldcrd.hxx>
31 
32 /*************************************************************************
33 |*
34 |*    WildCard::Match()
35 |*
36 |*    Beschreibung      WLDCRD.SDW
37 |*    Ersterstellung    MA 19.06.91
38 |*    Letzte Aenderung  MA 03.07.91
39 |*
40 *************************************************************************/
41 
42 /* Diese Methode ueberprueft, ob die Wilde Karte in pWild mit dem String
43  * in pStr matscht.
44  * Vertragen sich die beiden, so wird 1 zurueckgegeben, sonst 0.
45  *
46  * ein '*' in pWild bedeutet n beliebige Zeichen, mit n>=0
47  * ein '?' in pWild bedeutet genau ein beliebiges Zeichen
48  *
49  */
50 
51 sal_uInt16 WildCard::ImpMatch( const char *pWild, const char *pStr ) const
52 {
53     int    pos=0;
54     int    flag=0;
55 
56     while ( *pWild || flag )
57     {
58         switch (*pWild)
59         {
60             case '?':
61                 if ( *pStr == '\0' )
62                     return 0;
63                 break;
64 
65             default:
66                 if ( (*pWild == '\\') && ((*(pWild+1)=='?') || (*(pWild+1) == '*')) )
67                     pWild++;
68                 if ( *pWild != *pStr )
69                     if ( !pos )
70                         return 0;
71                     else
72                         pWild += pos;
73                 else
74                     break;          // ACHTUNG laeuft unter bestimmten
75                                     // Umstaenden in den nachsten case rein!!
76             case '*':
77                 while ( *pWild == '*' )
78                     pWild++;
79                 if ( *pWild == '\0' )
80                     return 1;
81                 flag = 1;
82                 pos  = 0;
83                 if ( *pStr == '\0' )
84                     return ( *pWild == '\0' );
85                 while ( *pStr && *pStr != *pWild )
86                 {
87                     if ( *pWild == '?' ) {
88                         pWild++;
89                         while ( *pWild == '*' )
90                             pWild++;
91                     }
92                     pStr++;
93                     if ( *pStr == '\0' )
94                         return ( *pWild == '\0' );
95                 }
96                 break;
97         }
98         if ( *pWild != '\0' )
99             pWild++;
100         if ( *pStr != '\0' )
101             pStr++;
102         else
103             flag = 0;
104         if ( flag )
105             pos--;
106     }
107     return ( *pStr == '\0' ) && ( *pWild == '\0' );
108 }
109 
110 /*************************************************************************
111 |*
112 |*    WildCard::Matches()
113 |*
114 |*    Beschreibung      WLDCRD.SDW
115 |*    Ersterstellung    MA 19.06.91
116 |*    Letzte Aenderung  TH 02.02.96
117 |*
118 *************************************************************************/
119 
120 sal_Bool WildCard::Matches( const String& rString ) const
121 {
122     ByteString aTmpWild = aWildString;
123 	ByteString aString(rString, osl_getThreadTextEncoding());
124 
125     sal_uInt16  nSepPos;
126 
127     if ( cSepSymbol != '\0' )
128     {
129         while ( (nSepPos = aTmpWild.Search( cSepSymbol )) != STRING_NOTFOUND )
130         {
131             // alle getrennten WildCard's pruefen
132             if ( ImpMatch( aTmpWild.Copy( 0, nSepPos ).GetBuffer(), aString.GetBuffer() ) )
133                 return sal_True;
134             aTmpWild.Erase( 0, nSepPos + 1 ); // Trennsymbol entfernen
135         }
136         // und noch den hinter dem letzen Trennsymbol bzw. den einzigen
137     }
138 
139     if ( ImpMatch( aTmpWild.GetBuffer(), aString.GetBuffer() ) )
140         return sal_True;
141     else
142         return sal_False;
143 }
144