LibrePCB Developers Documentation
attributesubstitutor.h
Go to the documentation of this file.
1 /*
2  * LibrePCB - Professional EDA for everyone!
3  * Copyright (C) 2013 LibrePCB Developers, see AUTHORS.md for contributors.
4  * https://librepcb.org/
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef LIBREPCB_CORE_ATTRIBUTESUBSTITUTOR_H
21 #define LIBREPCB_CORE_ATTRIBUTESUBSTITUTOR_H
22 
23 /*******************************************************************************
24  * Includes
25  ******************************************************************************/
26 #include <QtCore>
27 
28 #include <functional>
29 
30 /*******************************************************************************
31  * Namespace / Forward Declarations
32  ******************************************************************************/
33 namespace librepcb {
34 
35 /*******************************************************************************
36  * Class AttributeSubstitutor
37  ******************************************************************************/
38 
57 class AttributeSubstitutor final {
58 public:
59  using LookupFunction = std::function<QString(const QString&)>;
60  using FilterFunction = std::function<QString(const QString&)>;
61 
62  // Constructors / Destructor / Operator Overloadings
63  AttributeSubstitutor() = delete;
64  AttributeSubstitutor(const AttributeSubstitutor& other) = delete;
66  ~AttributeSubstitutor() = delete;
67 
68  // General Methods
69 
84  static QString substitute(QString str, LookupFunction lookup = nullptr,
85  FilterFunction filter = nullptr) noexcept;
86 
87 private: // Methods
104  static bool searchVariablesInText(const QString& text, int startPos, int& pos,
105  int& length, QStringList& keys) noexcept;
106 
107  static void applyFilter(QString& str, int& start, int& end,
108  FilterFunction filter) noexcept;
109 
110  static bool getValueOfKey(const QString& key, QString& value,
111  LookupFunction lookup) noexcept;
112 };
113 
114 /*******************************************************************************
115  * End of File
116  ******************************************************************************/
117 
118 } // namespace librepcb
119 
120 #endif
static bool getValueOfKey(const QString &key, QString &value, LookupFunction lookup) noexcept
Definition: attributesubstitutor.cpp:121
static void applyFilter(QString &str, int &start, int &end, FilterFunction filter) noexcept
Definition: attributesubstitutor.cpp:113
AttributeSubstitutor & operator=(const AttributeSubstitutor &rhs)=delete
Definition: occmodel.cpp:77
std::function< QString(const QString &)> LookupFunction
Definition: attributesubstitutor.h:59
static QString substitute(QString str, LookupFunction lookup=nullptr, FilterFunction filter=nullptr) noexcept
Substitute all attribute keys in a string with their attribute values.
Definition: attributesubstitutor.cpp:38
static bool searchVariablesInText(const QString &text, int startPos, int &pos, int &length, QStringList &keys) noexcept
Search the next variables (e.g. "{{KEY or FALLBACK}}") in a given text.
Definition: attributesubstitutor.cpp:88
The AttributeSubstitutor class substitutes attribute keys in strings with their actual values (e...
Definition: attributesubstitutor.h:57
std::function< QString(const QString &)> FilterFunction
Definition: attributesubstitutor.h:60