Clone
ixen
committed
on 17 Nov 20
Changed file filtering to use path objects instead of plain strings.
src/libchcore/TStringPattern.cpp (+26 -2)
  1 // ============================================================================
  2 //  Copyright (C) 2001-2020 by Jozef Starosczyk
  3 //  ixen {at} copyhandler [dot] com
  4 //
  5 //  This program is free software; you can redistribute it and/or modify
  6 //  it under the terms of the GNU Library General Public License
  7 //  (version 2) as published by the Free Software Foundation;
  8 //
  9 //  This program is distributed in the hope that it will be useful,
  10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
  11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12 //  GNU General Public License for more details.
  13 //
  14 //  You should have received a copy of the GNU Library General Public
  15 //  License along with this program; if not, write to the
  16 //  Free Software Foundation, Inc.,
  17 //  59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  18 // ============================================================================
1 19 #include "stdafx.h"
2 20 #include "TStringPattern.h"
3 21 #include <tchar.h>
4 22
5 23 using namespace string;
6 24
7 25 namespace chcore
8 26 {
9 27         namespace
10 28         {
11 29                 bool _tcicmp(TCHAR c1, TCHAR c2)
12 30                 {
13 31                         TCHAR ch1[2] = { c1, 0 }, ch2[2] = { c2, 0 };
14 32                         return (_tcsicmp(ch1, ch2) == 0);
15 33                 }
16 34         }
17 35
18 36         TStringPattern::TStringPattern(EPatternType ePatternType) :
19 37                 m_ePatternType(ePatternType)
20 38         {
 
112 130                         return true;
113 131                 // else search substring
114 132                 LPCTSTR wdsCopy = lpszMask;
115 133                 LPCTSTR lpszStringCopy = lpszString;
116 134                 bool bMatch = true;
117 135                 do
118 136                 {
119 137                         if (!MatchMask(lpszMask, lpszString)) lpszStringCopy++;
120 138                         lpszMask = wdsCopy;
121 139                         lpszString = lpszStringCopy;
122 140                         while (!(_tcicmp(*lpszMask, *lpszString)) && (*lpszString != '\0')) lpszString++;
123 141                         wdsCopy = lpszMask;
124 142                         lpszStringCopy = lpszString;
125 143                 } while ((*lpszString != _T('\0')) ? !MatchMask(lpszMask, lpszString) : (bMatch = false) != false);
126 144
127 145                 if (*lpszString == _T('\0') && *lpszMask == _T('\0')) return true;
128 146
129 147                 return bMatch;
130 148         }
131 149
132           bool TStringPattern::Matches(const TString& strTextToMatch) const
  150         bool TStringPattern::Matches(const TSmartPath& pathToMatch) const
133 151         {
134                   return MatchMask(m_strPattern.c_str(), strTextToMatch.c_str());
  152                 switch(m_ePatternType)
  153                 {
  154                 case EPatternType::eType_Wildcard:
  155                         return MatchMask(m_strPattern.c_str(), pathToMatch.GetFileName().ToString());
  156                 default:
  157                         throw std::invalid_argument("Unsupported pattern type");
135 158                 }
  159         }
136 160
137 161         void TStringPattern::SetPattern(const TString& strPattern, EPatternType ePatternType)
138 162         {
139 163                 m_ePatternType = ePatternType;
140 164                 m_strPattern = strPattern;
141 165         }
142 166 }