forked from ciphrex/mSIGNA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhexvalidator.cpp
45 lines (36 loc) · 1.25 KB
/
hexvalidator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
///////////////////////////////////////////////////////////////////////////////
//
// mSIGNA
//
// hexvalidator.cpp
//
// Copyright (c) 2014 Eric Lombrozo
//
// All Rights Reserved.
#include "hexvalidator.h"
#include <string>
#include <stdexcept>
using namespace std;
HexValidator::HexValidator(unsigned int minLength, unsigned int maxLength, int flags, QObject* parent) :
QValidator(parent), m_minLength(minLength), m_maxLength(maxLength)
{
if (minLength > maxLength) throw runtime_error("HexValidator - invalid length range.");
m_flags = 0;
if (flags & ACCEPT_LOWER) { m_flags |= ACCEPT_LOWER; }
if (flags & ACCEPT_UPPER) { m_flags |= ACCEPT_UPPER; }
if (m_flags == 0) throw std::runtime_error("HexValidator - invalid flags.");
}
QValidator::State HexValidator::validate(QString& input, int& /*pos*/) const
{
if (input.size() > (int)m_maxLength) return Invalid;
if (input.size() < (int)m_minLength) return Intermediate;
string amount = input.toStdString();
for (auto& c: amount)
{
if ((m_flags & ACCEPT_LOWER) && c >= 'a' && c <= 'f') continue;
if ((m_flags & ACCEPT_UPPER) && c >= 'A' && c <= 'F') continue;
if (c >= '0' && c <= '9') continue;
return Invalid;
}
return Acceptable;
}