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 // ============================================================================
    19 #include "stdafx.h"
    20 #include "TStringPatternArray.h"
    21 #include "../libstring/TStringArray.h"
    22
    23 using namespace string;
    24
    25 namespace chcore
    26 {
    27         bool TStringPatternArray::MatchesAny(const TString& strTextToMatch) const
    28         {
    29                 for (const TStringPattern& pattern : m_vItems)
    30                 {
    31                         if (pattern.Matches(strTextToMatch))
    32                                 return true;
    33                 }
    34
    35                 return false;
    36         }
    37
    38         bool TStringPatternArray::MatchesAll(const TString& strTextToMatch) const
    39         {
    40                 for (const TStringPattern& pattern : m_vItems)
    41                 {
    42                         if (!pattern.Matches(strTextToMatch))
    43                                 return false;
    44                 }
    45
    46                 return true;
    47         }
    48
    49         void TStringPatternArray::FromString(const TString& strPatterns, TStringPattern::EPatternType eDefaultPatternType)
    50         {
    51                 TStringArray arrPatterns;
    52                 strPatterns.Split(_T("|"), arrPatterns);
    53                 FromStringArray(arrPatterns, eDefaultPatternType);
    54         }
    55
    56         void TStringPatternArray::FromSerializedStringArray(const TStringArray& arrSerializedPatterns)
    57         {
    58                 m_vItems.clear();
    59
    60                 for (size_t stIndex = 0; stIndex < arrSerializedPatterns.GetCount(); ++stIndex)
    61                 {
    62                         m_vItems.push_back(TStringPattern::CreateFromString(arrSerializedPatterns.GetAt(stIndex)));
    63                 }
    64         }
    65
    66         void TStringPatternArray::FromStringArray(const TStringArray& arrPatterns, TStringPattern::EPatternType eDefaultPatternType)
    67         {
    68                 for (size_t stIndex = 0; stIndex < arrPatterns.GetCount(); ++stIndex)
    69                 {
    70                         Add(TStringPattern::CreateFromString(arrPatterns.GetAt(stIndex), eDefaultPatternType));
    71                 }
    72         }
    73
    74         TString TStringPatternArray::ToString() const
    75         {
    76                 TString strMask;
    77                 size_t stCount = GetCount();
    78                 if (stCount > 0)
    79                 {
    80                         strMask = GetAt(0).ToString();
    81                         for (size_t stIndex = 1; stIndex < stCount; stIndex++)
    82                         {
    83                                 strMask += _T("|") + GetAt(stIndex).ToString();
    84                         }
    85                 }
    86
    87                 return strMask;
    88         }
    89
    90         TStringArray TStringPatternArray::ToSerializedStringArray() const
    91         {
    92                 TStringArray arrSerialized;
    93                 for (const TStringPattern& pattern : m_vItems)
    94                 {
    95                         arrSerialized.Add(pattern.ToString());
    96                 }
    97
    98                 return arrSerialized;
    99         }
    100 }