-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathValidation.cpp
More file actions
85 lines (56 loc) · 1.81 KB
/
Validation.cpp
File metadata and controls
85 lines (56 loc) · 1.81 KB
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
/*
Validation.cpp - Validation function
Saul bertuccio 5 mar 2017
Released into the public domain.
*/
#include "Validation.h"
boolean Validation::isValidDeviceName(const String &device_name) {
return Validation::parseAlphaNumString(device_name, Device::MAX_NAME_LENGTH);
}
boolean Validation::isValidDeviceKeyName(const String &device_key_name) {
return Validation::parseAlphaNumString(device_key_name, Key::MAX_KEY_NAME_LEN);
}
boolean Validation::isValidDeviceType(const String &device_type) {
for (int i = 0; i < Device::TYPE_NUM; i++) {
if (Device::TYPES[i] == device_type)
return true;
}
return false;
}
boolean Validation::isValidDeviceKeyLen(const String &device_key_len) {
return Validation::parseNumericString(device_key_len, 9);
}
boolean Validation::isValidDeviceKeyCode(const String &device_key_code) {
return Validation::parseNumericString(device_key_code, 9);
}
boolean Validation::isValidDeviceKeyHexCode(const String &device_key_code) {
if (device_key_code.length() > 10)
return false;
if (device_key_code[0] != '0' || device_key_code[1] != 'x')
return false;
for(byte i=2; i< device_key_code.length(); i++) {
char c = device_key_code[i];
if (c >= '0' && c <= '9')
continue;
if (c >= 'A' || c <= 'F')
continue;
if (c >= 'a' || c <= 'f')
continue;
return false;
}
return true;
}
boolean Validation::parseAlphaNumString(const String &s, const int l) {
for(byte i=0; i<s.length(); i++) {
if(!isAlphaNumeric(s.charAt(i)))
return false;
}
return (s.length() > 0 && s.length() <= l);
}
boolean Validation::parseNumericString(const String &s, const int l) {
for(byte i=0; i<s.length(); i++) {
if(!isDigit(s.charAt(i)))
return false;
}
return (s.length() > 0 && s.length() <= l);
}