-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidator.cpp
163 lines (114 loc) · 3.21 KB
/
validator.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include "validator.hpp"
#include <iostream>
#include <fstream>
#include <vector>
std::string Validator::strToLower( std::string str)
{
std::string converted;
for(int i = 0; i < str.size(); ++i)
converted += (std::tolower(str[i]));
return converted;
}
bool Validator::isFile( std::string fileName)
{
bool b = false;
std::ifstream f(fileName);
if(f) b = true;
f.close();
return b;
}
bool Validator::validateNumb( std::string numb)
{
std::regex regular("\\D");
std::cmatch res;
return !std::regex_search(numb.c_str(), res, regular);
}
bool Validator::validateNameAndSubject( std::string name)
{
bool b = false;
// std::regex regular("\\^[a-zA-Z]([a-zA-Z-])*[a-zA-Z]\\$");
std::regex regular("[^a-zA-Z-]");
std::cmatch res;
b = !std::regex_search(name.c_str(), res, regular);
if(!b) return b;
if(!(b && ((int)name[0] != 45) && ((int)name[name.size()-1] != 45)))
b = false;
return b;
}
bool Validator::validatePwd( std::string numb)
{
std::regex regular("[^a-zA-Z0-9]");
std::cmatch res;
return !std::regex_search(numb.c_str(), res, regular);
}
bool Validator::validateTitle( std::string name)
{
bool b = false;
std::regex regular("[^ 0-9a-zA-Z-@.]");
std::cmatch res;
std::cmatch res1;
std::cmatch res2;
// std::cmatch res3;
// std::cmatch res4;
// std::cmatch res5;
b = !std::regex_search(name.c_str(), res, regular);
if(!b) return b;
std::regex regularDigital("([a-zA-Z])+\\d([a-zA-Z])+");
b = !std::regex_search(name.c_str(), res, regularDigital);
if(!b) return b;
std::regex regularDot("\\.[a-zA-Z]");
b = !std::regex_search(name.c_str(), res, regularDot);
if(!b) return b;
std::regex regularStrEnd("[a-zA-Z0-9]");
std::string s1 = "";
s1.push_back(name[0]);
std::string s2 = "";
s2.push_back(name[name.size()-1]);
if(!(b && (std::regex_search(s1.c_str(), res1, regularStrEnd)) && (std::regex_search(s2.c_str(), res2, regularStrEnd))))
b = false;
return b;
}
bool Validator::input(std::string& field, std::string stop, bool (Validator::*func)(std::string))
{
while(! (this->*func)(field) )
{
std::cout << "Wrong Data (input " + stop + " to stop)\n";
std::cin >> field;
if(field == stop)
return false;
}
return true;
}
bool Validator::input(std::string& field, std::string stop, bool (Validator::*func)(std::string), int&c )
{
while(! (this->*func)(field) || field == "0")
{
std::cout << "Wrong Data (input " + stop + " to stop)\n";
std::cin >> field;
if(field == stop)
return false;
}
c = std::stoi(field, nullptr, 10);
return true;
}
int Validator::inputMinMax(std::string text, int min, int max)
{
while(true)
{
std::string c = "";
std::cout << text;
std::cin >> c;
if(!this->validateNumb(c))
{
std::cout << "\nIncorrect\n";
continue;
}
int numb = std::stoi(c, nullptr, 10);
if( (numb < min) || (numb > max))
{
std::cout << "\nIncorrect\n";
continue;
}
return numb;
}
}