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 1468004 : bool WildCard::ImpMatch( const char *pWild, const char *pStr ) const
29 : {
30 1468004 : int pos=0;
31 1468004 : int flag=0;
32 :
33 7185422 : while ( *pWild || flag )
34 : {
35 5715488 : switch (*pWild)
36 : {
37 : case '?':
38 20608 : if ( *pStr == '\0' )
39 0 : return false;
40 20608 : break;
41 :
42 : default:
43 5358057 : if ( (*pWild == '\\') && ((*(pWild+1)=='?') || (*(pWild+1) == '*')) )
44 0 : pWild++;
45 5358057 : if ( *pWild != *pStr )
46 1129251 : if ( !pos )
47 1129251 : return false;
48 : else
49 0 : pWild += pos;
50 : else
51 4228806 : break; // WARNING: may cause execution of next case
52 : // in some circumstances!
53 : case '*':
54 1010469 : while ( *pWild == '*' )
55 336823 : pWild++;
56 336823 : if ( *pWild == '\0' )
57 336823 : 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 4249414 : if ( *pWild != '\0' )
76 4249414 : pWild++;
77 4249414 : if ( *pStr != '\0' )
78 4249414 : pStr++;
79 : else
80 0 : flag = 0;
81 4249414 : if ( flag )
82 0 : pos--;
83 : }
84 1930 : return ( *pStr == '\0' ) && ( *pWild == '\0' );
85 : }
86 :
87 1468004 : bool WildCard::Matches( const OUString& rString ) const
88 : {
89 1468004 : OString aTmpWild = aWildString;
90 2936008 : OString aString(OUStringToOString(rString, osl_getThreadTextEncoding()));
91 :
92 : sal_Int32 nSepPos;
93 :
94 1468004 : 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 1468004 : if ( ImpMatch( aTmpWild.getStr(), aString.getStr() ) )
106 338751 : return true;
107 : else
108 2597257 : return false;
109 : }
110 :
111 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|