Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <tools/wldcrd.hxx>
21 :
22 : /** Tests, whether a wildcard in pWild will match for pStr.
23 : *
24 : * '*' in pWild means n chars for n > 0.
25 : * '?' in pWild mean match exactly one character.
26 : *
27 : */
28 683958 : bool WildCard::ImpMatch( const char *pWild, const char *pStr )
29 : {
30 683958 : int pos=0;
31 683958 : int flag=0;
32 :
33 2815385 : while ( *pWild || flag )
34 : {
35 2130478 : switch (*pWild)
36 : {
37 : case '?':
38 10000 : if ( *pStr == '\0' )
39 0 : return false;
40 10000 : break;
41 :
42 : default:
43 1899424 : if ( (*pWild == '\\') && ((*(pWild+1)=='?') || (*(pWild+1) == '*')) )
44 0 : pWild++;
45 1899424 : if ( *pWild != *pStr )
46 461955 : if ( !pos )
47 461955 : return false;
48 : else
49 0 : pWild += pos;
50 : else
51 1437469 : break; // WARNING: may cause execution of next case
52 : // in some circumstances!
53 : case '*':
54 663162 : while ( *pWild == '*' )
55 221054 : pWild++;
56 221054 : if ( *pWild == '\0' )
57 221054 : return true;
58 0 : flag = 1;
59 0 : pos = 0;
60 0 : if ( *pStr == '\0' )
61 0 : return ( *pWild == '\0' );
62 0 : while ( *pStr && *pStr != *pWild )
63 : {
64 0 : if ( *pWild == '?' ) {
65 0 : pWild++;
66 0 : while ( *pWild == '*' )
67 0 : pWild++;
68 : }
69 0 : pStr++;
70 0 : if ( *pStr == '\0' )
71 0 : return ( *pWild == '\0' );
72 : }
73 0 : break;
74 : }
75 1447469 : if ( *pWild != '\0' )
76 1447469 : pWild++;
77 1447469 : if ( *pStr != '\0' )
78 1447469 : pStr++;
79 : else
80 0 : flag = 0;
81 1447469 : if ( flag )
82 0 : pos--;
83 : }
84 949 : return ( *pStr == '\0' ) && ( *pWild == '\0' );
85 : }
86 :
87 683958 : bool WildCard::Matches( const OUString& rString ) const
88 : {
89 683958 : OString aTmpWild = aWildString;
90 1367916 : OString aString(OUStringToOString(rString, osl_getThreadTextEncoding()));
91 :
92 : sal_Int32 nSepPos;
93 :
94 683958 : if ( cSepSymbol != '\0' )
95 : {
96 0 : while ( (nSepPos = aTmpWild.indexOf(cSepSymbol)) != -1 )
97 : {
98 : // Check all splitted wildcards
99 0 : if ( ImpMatch( aTmpWild.copy( 0, nSepPos ).getStr(), aString.getStr() ) )
100 0 : return true;
101 0 : aTmpWild = aTmpWild.copy(nSepPos + 1); // remove separator
102 : }
103 : }
104 :
105 683958 : if ( ImpMatch( aTmpWild.getStr(), aString.getStr() ) )
106 222002 : return true;
107 : else
108 1145914 : return false;
109 : }
110 :
111 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|