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