LibrePCB Developers Documentation
Loading...
Searching...
No Matches
fileproofname.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_FILEPROOFNAME_H
21#define LIBREPCB_CORE_FILEPROOFNAME_H
22
23/*******************************************************************************
24 * Includes
25 ******************************************************************************/
26#include "../exceptions.h"
27#include "../serialization/sexpression.h"
28#include "../utils/toolbox.h"
29
30#include <type_safe/constrained_type.hpp>
31
32#include <QtCore>
33
34#include <optional>
35
36/*******************************************************************************
37 * Namespace / Forward Declarations
38 ******************************************************************************/
39namespace librepcb {
40
41/*******************************************************************************
42 * Class FileProofName
43 ******************************************************************************/
44
46 template <typename Value, typename Predicate>
47 static constexpr auto verify(Value&& val, const Predicate& p) ->
48 typename std::decay<Value>::type {
49 return p(val) ? std::forward<Value>(val)
50 : (throw RuntimeError(
51 __FILE__, __LINE__,
52 QString(QCoreApplication::translate(
53 "FileProofName", "Invalid name: '%1'"))
54 .arg(val)),
55 std::forward<Value>(val));
56 }
57};
58
60 static const constexpr std::size_t MAX_LEN = 20; // Keep in sync with regex!
61
62 static QRegularExpression regex() noexcept {
63 // The negative lookahead makes sure that any string consisting of only
64 // dots (no matter how many) will be invalid (i.e. ".", ".." etc.).
65 // This is required since those dots have special meaning for file systems
66 // and cannot be used as file names.
67 return QRegularExpression("\\A(?!\\.+\\z)[-a-zA-Z0-9_+().]{1,20}\\z");
68 }
69
70 bool operator()(const QString& value) const noexcept {
71 return regex()
72 .match(value, 0, QRegularExpression::PartialPreferCompleteMatch)
73 .hasMatch();
74 }
75};
76
89 type_safe::constrained_type<QString, FileProofNameConstraint,
91
92inline bool operator==(const FileProofName& lhs, const QString& rhs) noexcept {
93 return (*lhs) == rhs;
94}
95inline bool operator==(const QString& lhs, const FileProofName& rhs) noexcept {
96 return lhs == (*rhs);
97}
98inline bool operator!=(const FileProofName& lhs, const QString& rhs) noexcept {
99 return (*lhs) != rhs;
100}
101inline bool operator!=(const QString& lhs, const FileProofName& rhs) noexcept {
102 return lhs != (*rhs);
103}
104inline QString operator%(const FileProofName& lhs,
105 const QString& rhs) noexcept {
106 return (*lhs) % rhs;
107}
108inline QString operator%(const QString& lhs,
109 const FileProofName& rhs) noexcept {
110 return lhs % (*rhs);
111}
113 const FileProofName& rhs) noexcept {
114 return FileProofName((*lhs) % (*rhs)); // always safe, will not throw
115}
116
117template <>
118inline std::unique_ptr<SExpression> serialize(const FileProofName& obj) {
119 return SExpression::createString(*obj);
120}
121
122template <>
124 return FileProofName(node.getValue()); // can throw
125}
126
127inline QDataStream& operator<<(QDataStream& stream, const FileProofName& obj) {
128 stream << *obj;
129 return stream;
130}
131
132inline QDebug operator<<(QDebug stream, const FileProofName& obj) {
133 stream << QString("FileProofName('%1')").arg(*obj);
134 return stream;
135}
136
137inline std::size_t qHash(const FileProofName& key,
138 std::size_t seed = 0) noexcept {
139 return ::qHash(*key, seed);
140}
141
142inline static QString cleanFileProofName(const QString& userInput) noexcept {
144 userInput, QRegularExpression("[^-a-zA-Z0-9_+().]"), true, false, false,
145 "-", 20);
146 if ((!s.isEmpty()) && (!FileProofNameConstraint()(s))) {
147 s.clear(); // Consists of only dots -> invalid.
148 }
149 return s;
150}
151
152inline static std::optional<FileProofName> parseFileProofName(
153 const QString& name) noexcept {
154 return FileProofNameConstraint()(name) ? FileProofName(name)
155 : std::optional<FileProofName>();
156}
157
158/*******************************************************************************
159 * End of File
160 ******************************************************************************/
161
162} // namespace librepcb
163
164#endif
The RuntimeError class.
Definition exceptions.h:218
The SExpression class.
Definition sexpression.h:69
static std::unique_ptr< SExpression > createString(const QString &string)
Definition sexpression.cpp:407
const QString & getValue() const
Definition sexpression.cpp:71
static QString cleanUserInputString(const QString &input, const QRegularExpression &removeRegex, bool trim=true, bool toLower=false, bool toUpper=false, const QString &spaceReplacement=" ", int maxLength=-1) noexcept
Clean a user input string.
Definition toolbox.cpp:247
Definition occmodel.cpp:77
std::size_t qHash(const AttributeKey &key, std::size_t seed=0) noexcept
Definition attributekey.h:113
bool operator==(const AttributeKey &lhs, const QString &rhs) noexcept
Definition attributekey.h:80
static QString cleanFileProofName(const QString &userInput) noexcept
Definition fileproofname.h:142
std::unique_ptr< SExpression > serialize(const AttributeKey &obj)
Definition attributekey.h:94
bool operator!=(const AttributeKey &lhs, const QString &rhs) noexcept
Definition attributekey.h:86
type_safe::constrained_type< QString, FileProofNameConstraint, FileProofNameVerifier > FileProofName
Definition fileproofname.h:90
static std::optional< FileProofName > parseFileProofName(const QString &name) noexcept
Definition fileproofname.h:152
AttributeKey deserialize(const SExpression &node)
Definition attributekey.h:99
QDataStream & operator<<(QDataStream &stream, const AttributeKey &obj)
Definition attributekey.h:103
QString operator%(const ComponentPrefix &lhs, const QString &rhs) noexcept
Definition componentprefix.h:99
Definition fileproofname.h:59
bool operator()(const QString &value) const noexcept
Definition fileproofname.h:70
static QRegularExpression regex() noexcept
Definition fileproofname.h:62
static const constexpr std::size_t MAX_LEN
Definition fileproofname.h:60
Definition fileproofname.h:45
static constexpr auto verify(Value &&val, const Predicate &p) -> typename std::decay< Value >::type
Definition fileproofname.h:47