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 : * If they match, return 1, otherwise 0.
25 : *
26 : * '*' in pWild means n chars for n > 0.
27 : * '?' in pWild mean match exactly one character.
28 : *
29 : */
30 162934 : sal_uInt16 WildCard::ImpMatch( const char *pWild, const char *pStr ) const
31 : {
32 162934 : int pos=0;
33 162934 : int flag=0;
34 :
35 568105 : while ( *pWild || flag )
36 : {
37 404600 : switch (*pWild)
38 : {
39 : case '?':
40 10256 : if ( *pStr == '\0' )
41 0 : return 0;
42 10256 : break;
43 :
44 : default:
45 288233 : if ( (*pWild == '\\') && ((*(pWild+1)=='?') || (*(pWild+1) == '*')) )
46 0 : pWild++;
47 288233 : if ( *pWild != *pStr )
48 56252 : if ( !pos )
49 56252 : return 0;
50 : else
51 0 : pWild += pos;
52 : else
53 231981 : break; // WARNING: may cause execution of next case
54 : // in some circumstances!
55 : case '*':
56 318333 : while ( *pWild == '*' )
57 106111 : pWild++;
58 106111 : if ( *pWild == '\0' )
59 106111 : return 1;
60 0 : flag = 1;
61 0 : pos = 0;
62 0 : if ( *pStr == '\0' )
63 0 : return ( *pWild == '\0' );
64 0 : while ( *pStr && *pStr != *pWild )
65 : {
66 0 : if ( *pWild == '?' ) {
67 0 : pWild++;
68 0 : while ( *pWild == '*' )
69 0 : pWild++;
70 : }
71 0 : pStr++;
72 0 : if ( *pStr == '\0' )
73 0 : return ( *pWild == '\0' );
74 : }
75 0 : break;
76 : }
77 242237 : if ( *pWild != '\0' )
78 242237 : pWild++;
79 242237 : if ( *pStr != '\0' )
80 242237 : pStr++;
81 : else
82 0 : flag = 0;
83 242237 : if ( flag )
84 0 : pos--;
85 : }
86 571 : return ( *pStr == '\0' ) && ( *pWild == '\0' );
87 : }
88 :
89 162934 : bool WildCard::Matches( const OUString& rString ) const
90 : {
91 162934 : OString aTmpWild = aWildString;
92 325868 : OString aString(OUStringToOString(rString, osl_getThreadTextEncoding()));
93 :
94 : sal_Int32 nSepPos;
95 :
96 162934 : if ( cSepSymbol != '\0' )
97 : {
98 0 : while ( (nSepPos = aTmpWild.indexOf(cSepSymbol)) != -1 )
99 : {
100 : // Check all splitted wildcards
101 0 : if ( ImpMatch( aTmpWild.copy( 0, nSepPos ).getStr(), aString.getStr() ) )
102 0 : return true;
103 0 : aTmpWild = aTmpWild.copy(nSepPos + 1); // remove separator
104 : }
105 : }
106 :
107 162934 : if ( ImpMatch( aTmpWild.getStr(), aString.getStr() ) )
108 106681 : return true;
109 : else
110 219187 : return false;
111 : }
112 :
113 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|