pattern.cpp
1 //------------------------------------------------------------------------------
2 // pattern.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301 USA
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2021 Braden "Blzut3" Obrzut <admin@maniacsvault.net>
22 //------------------------------------------------------------------------------
23 
24 #include "pattern.h"
25 
26 #include <QDataStream>
27 
28 DClass<Pattern>
29 {
30 public:
31  QString userPattern;
32  Pattern::Syntax syntax = Pattern::RegExp;
33 };
34 
35 DPointered(Pattern)
36 
37 Pattern::Pattern() = default;
38 Pattern::Pattern(const Pattern &pattern) = default;
39 
40 Pattern::Pattern(const QString &pattern, QRegularExpression::PatternOptions options, Syntax syntax)
41  : QRegularExpression()
42 {
43  setUserPattern(pattern, syntax);
44  setPatternOptions(options);
45 }
46 
47 Pattern::Pattern(const QRegularExpression &re)
48  : QRegularExpression(re)
49 {
50  d->userPattern = re.pattern();
51 }
52 
53 Pattern::~Pattern() = default;
54 
55 QString Pattern::userPattern() const
56 {
57  return d->userPattern;
58 }
59 
60 void Pattern::setUserPattern(const QString &pattern, Syntax syntax)
61 {
62  d->userPattern = pattern;
63  d->syntax = syntax;
64 
65  if (syntax == Wildcard)
66  setPattern(wildcardToRegularExpression(pattern));
67  else
68  setPattern(pattern);
69 }
70 
71 QDataStream &operator<<(QDataStream &out, const Pattern &re)
72 {
73  quint8 syntax = re.d->syntax;
74  return out << static_cast<const QRegularExpression &>(re) << re.d->userPattern << syntax;
75 }
76 
77 QDataStream &operator>>(QDataStream &in, Pattern &re)
78 {
79  quint8 syntax;
80  in >> static_cast<QRegularExpression &>(re) >> re.d->userPattern >> syntax;
81  re.d->syntax = static_cast<Pattern::Syntax>(syntax);
82  return in;
83 }