Bump version to 21.06.18.1
[LibreOffice.git] / tools / source / fsys / wldcrd.cxx
blobbf5155dee88bbe604808e554f6c0ac1f1656e2c3
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <tools/wldcrd.hxx>
22 /** Tests, whether a wildcard in pWild will match for pStr.
24 * '*' in pWild means n chars for n > 0.
25 * '?' in pWild mean match exactly one character.
28 bool WildCard::ImpMatch( const char *pWild, const char *pStr )
30 int pos=0;
31 int flag=0;
33 while ( *pWild || flag )
35 switch (*pWild)
37 case '?':
38 if ( *pStr == '\0' )
39 return false;
40 break;
42 default:
43 if ( (*pWild == '\\') && ((*(pWild+1)=='?') || (*(pWild+1) == '*')) )
44 pWild++;
45 if ( *pWild != *pStr )
46 if ( !pos )
47 return false;
48 else
49 pWild += pos;
50 else
51 break;
52 // WARNING/TODO: may cause execution of next case in some
53 // circumstances!
54 [[fallthrough]];
55 case '*':
56 while ( *pWild == '*' )
57 pWild++;
58 if ( *pWild == '\0' )
59 return true;
60 flag = 1;
61 pos = 0;
62 if ( *pStr == '\0' )
63 return ( *pWild == '\0' );
64 while ( *pStr && *pStr != *pWild )
66 if ( *pWild == '?' ) {
67 pWild++;
68 while ( *pWild == '*' )
69 pWild++;
71 pStr++;
72 if ( *pStr == '\0' )
73 return ( *pWild == '\0' );
75 break;
77 if ( *pWild != '\0' )
78 pWild++;
79 if ( *pStr != '\0' )
80 pStr++;
81 else
82 flag = 0;
83 if ( flag )
84 pos--;
86 return ( *pStr == '\0' ) && ( *pWild == '\0' );
89 bool WildCard::Matches( const OUString& rString ) const
91 OString aTmpWild = aWildString;
92 OString aString(OUStringToOString(rString, osl_getThreadTextEncoding()));
94 sal_Int32 nSepPos;
96 if ( cSepSymbol != '\0' )
98 while ( (nSepPos = aTmpWild.indexOf(cSepSymbol)) != -1 )
100 // Check all split wildcards
101 if ( ImpMatch( aTmpWild.copy( 0, nSepPos ).getStr(), aString.getStr() ) )
102 return true;
103 aTmpWild = aTmpWild.copy(nSepPos + 1); // remove separator
107 return ImpMatch( aTmpWild.getStr(), aString.getStr() );
110 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */