6 #include "validatorfilesize_p.h"
19 :
ValidatorRule(*new ValidatorFileSizePrivate(field, option, min, max, messages, defValKey))
37 bool decimalPointFound =
false;
41 bool byteSignFound =
false;
42 ValidatorFileSizePrivate::StartsWith startsWith{ValidatorFileSizePrivate::StartsWith::NotSet};
44 for (
const QChar &ch : str) {
46 const char16_t uc = ch.toUpper().unicode();
47 if (((uc >= ValidatorRulePrivate::ascii_0) && (uc <= ValidatorRulePrivate::ascii_9)) ||
48 (ch == decimalPoint)) {
49 if (startsWith == ValidatorFileSizePrivate::StartsWith::NotSet) {
50 startsWith = ValidatorFileSizePrivate::StartsWith::DigitPart;
52 if (ch == decimalPoint) {
53 if (decimalPointFound) {
54 qCDebug(C_VALIDATOR).nospace()
55 <<
"ValidatorFileSize: Validation failed for " <<
value <<
": "
56 <<
"two decimal seperators in a row";
60 decimalPointFound =
true;
64 (startsWith == ValidatorFileSizePrivate::StartsWith::DigitPart)) ||
66 (startsWith == ValidatorFileSizePrivate::StartsWith::SymbolPart))) {
69 qCDebug(C_VALIDATOR).nospace() <<
"ValidatorFileSize: Validation failed for "
70 <<
value <<
": " <<
"symbol inside digit part";
74 }
else if ((uc != ValidatorRulePrivate::asciiTab) &&
75 (uc != ValidatorRulePrivate::asciiSpace)) {
77 if (startsWith == ValidatorFileSizePrivate::StartsWith::NotSet) {
78 startsWith = ValidatorFileSizePrivate::StartsWith::SymbolPart;
81 (startsWith == ValidatorFileSizePrivate::StartsWith::SymbolPart)) ||
83 (startsWith == ValidatorFileSizePrivate::StartsWith::DigitPart))) {
85 case ValidatorFileSizePrivate::ascii_K:
89 qCDebug(C_VALIDATOR).nospace()
90 <<
"ValdatorFileSize: Validation failed for " <<
value <<
": "
91 <<
"unit symbol K already found";
97 case ValidatorFileSizePrivate::ascii_M:
101 qCDebug(C_VALIDATOR).nospace()
102 <<
"ValdatorFileSize: Validation failed for " <<
value <<
": "
103 <<
"unit symbol M already found";
109 case ValidatorFileSizePrivate::ascii_G:
111 if (multiplier > 0) {
113 qCDebug(C_VALIDATOR).nospace()
114 <<
"ValdatorFileSize: Validation failed for " <<
value <<
": "
115 <<
"unit symbol G already found";
121 case ValidatorFileSizePrivate::ascii_T:
123 if (multiplier > 0) {
125 qCDebug(C_VALIDATOR).nospace()
126 <<
"ValdatorFileSize: Validation failed for " <<
value <<
": "
127 <<
"unit symbol T already found";
133 case ValidatorFileSizePrivate::ascii_P:
135 if (multiplier > 0) {
137 qCDebug(C_VALIDATOR).nospace()
138 <<
"ValdatorFileSize: Validation failed for " <<
value <<
": "
139 <<
"unit symbol P already found";
145 case ValidatorFileSizePrivate::ascii_E:
147 if (multiplier > 0) {
149 qCDebug(C_VALIDATOR).nospace()
150 <<
"ValdatorFileSize: Validation failed for " <<
value <<
": "
151 <<
"unit symbol E already found";
157 case ValidatorRulePrivate::ascii_Z:
159 if (multiplier > 0) {
161 qCDebug(C_VALIDATOR).nospace()
162 <<
"ValdatorFileSize: Validation failed for " <<
value <<
": "
163 <<
"unit symbol Z already found";
169 case ValidatorFileSizePrivate::ascii_Y:
171 if (multiplier > 0) {
173 qCDebug(C_VALIDATOR).nospace()
174 <<
"ValdatorFileSize: Validation failed for " <<
value <<
": "
175 <<
"unit symbol Y already found";
181 case ValidatorFileSizePrivate::ascii_I:
183 if ((multiplier == 0) || binary) {
185 qCDebug(C_VALIDATOR).nospace()
186 <<
"ValdatorFileSize: Validation failed for " <<
value <<
": "
187 <<
"binary indicator I already found or no unit symbol given "
194 case ValidatorFileSizePrivate::ascii_B:
198 qCDebug(C_VALIDATOR).nospace()
199 <<
"ValdatorFileSize: Validation failed for " <<
value <<
": "
200 <<
"byte symbol B already found";
202 byteSignFound =
true;
206 case ValidatorRulePrivate::asciiTab:
207 case ValidatorRulePrivate::asciiSpace:
211 qCDebug(C_VALIDATOR).nospace()
212 <<
"ValdatorFileSize: Validation failed for " <<
value <<
": "
213 <<
"invalid character in symbol part";
238 double size = locale.
toDouble(digitPart, &ok);
242 if (multiplier > 0) {
244 binary ? std::exp2(multiplier * 10) : std::pow(10.0, multiplier * 3);
247 if ((min >= 1.0) && (size < min)) {
250 if ((max >= 1.0) && (size > max)) {
253 if (valid && fileSize) {
275 if (d->min.isValid()) {
276 min = d->extractDouble(c, params, d->min, &ok);
278 qCWarning(C_VALIDATOR).noquote()
279 <<
debugString(c) <<
"Invalid minimum size comparison data";
281 c,
static_cast<int>(ValidatorRulePrivate::ErrorType::InvalidMin));
285 if (ok && d->max.isValid()) {
286 max = d->extractDouble(c, params, d->max, &ok);
288 qCWarning(C_VALIDATOR).noquote()
289 <<
debugString(c) <<
"Invalid maximum size comparison data";
291 c,
static_cast<int>(ValidatorRulePrivate::ErrorType::InvalidMax));
298 if (size <
static_cast<double>(std::numeric_limits<qulonglong>::max())) {
299 result.
value.
setValue<qulonglong>(
static_cast<qulonglong
>(size + 0.5));
305 qCWarning(C_VALIDATOR).noquote()
306 <<
debugString(c) << v <<
"is not a valid data size string";
322 if (d->min.isValid() || d->max.isValid()) {
325 return c->
qtTrId(
"cutelyst-valfilesize-genvalerr-minmax");
329 return c->
qtTrId(
"cutelyst-valfilesize-genvalerr-minmax-label").
arg(_label);
334 return c->
qtTrId(
"cutelyst-valfilesize-genvalerr");
337 return c->
qtTrId(
"cutelyst-valfilesize-genvalerr-label").
arg(_label);
346 const auto errorType =
static_cast<ValidatorRulePrivate::ErrorType
>(errorData.
toInt());
350 case ValidatorRulePrivate::ErrorType::InvalidMin:
352 return c->
qtTrId(
"cutelyst-valfilesize-genvaldataerr-min");
353 case ValidatorRulePrivate::ErrorType::InvalidMax:
355 return c->
qtTrId(
"cutelyst-valfilesize-genvaldataerr-max");
356 case ValidatorRulePrivate::ErrorType::InvalidType:
363 case ValidatorRulePrivate::ErrorType::InvalidMin:
365 return c->
qtTrId(
"cutelyst-valfilesize-genvaldataerr-min-label").
arg(_label);
366 case ValidatorRulePrivate::ErrorType::InvalidMax:
368 return c->
qtTrId(
"cutelyst-valfilesize-genvaldataerr-max-label").
arg(_label);
369 case ValidatorRulePrivate::ErrorType::InvalidType:
376 #if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
378 Q_UNREACHABLE_RETURN({});
388 ? QStringLiteral(
"^\\d+[%1]?\\d*\\s*[KkMmGgTt]?[Ii]?[Bb]?")
390 : QStringLiteral(
"[KkMmGgTt]?[Ii]?[Bb]?\\s*\\d+[%1]?\\d*")
QLocale locale() const noexcept
void setStash(const QString &key, const QVariant &value)
QString qtTrId(const char *id, int n=-1) const
Checks if the input field contains a valid file size string like 1.5 GB.
~ValidatorFileSize() override
Deconstructs the file size validator.
QString genericValidationDataError(Context *c, const QVariant &errorData) const override
Option
Options for ValidatorFileSize.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
static void inputPattern(Context *c, const QString &stashKey=QStringLiteral("fileSizePattern"))
ValidatorFileSize(const QString &field, Option option=NoOption, const QVariant &min=QVariant(), const QVariant &max=QVariant(), const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Base class for all validator rules.
QString validationError(Context *c, const QVariant &errorData={}) const
QString label(Context *c) const
QString validationDataError(Context *c, const QVariant &errorData={}) const
void defaultValue(Context *c, ValidatorReturnType *result) const
QString value(const ParamsMultiMap ¶ms) const
QString debugString(Context *c) const
static bool validate(const QString &value, double min=-1, double max=-1, Option option=NoOption, const QLocale &locale=QLocale(), double *fileSize=nullptr)
Returns true if value is a valid file size string.
The Cutelyst namespace holds all public Cutelyst API.
QString decimalPoint() const const
Qt::LayoutDirection textDirection() const const
double toDouble(QStringView s, bool *ok) const const
QString & append(QChar ch)
QString arg(Args &&... args) const const
bool isEmpty() const const
QString simplified() const const
void setValue(QVariant &&value)
int toInt(bool *ok) const const
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.