-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.cpp
111 lines (100 loc) · 2.61 KB
/
utils.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mgo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/08 12:32:07 by mgo #+# #+# */
/* Updated: 2022/09/08 12:32:09 by mgo ### ########.fr */
/* */
/* ************************************************************************** */
#include "utils.hpp"
#include <string>
#include <vector>
#include <sstream>
bool isOnlyNums(std::string str)
{
for (std::string::iterator it = str.begin()
; it != str.end()
; ++it)
{
if (isnumber(*it) == false)
return false;
}
return true;
}
bool isValidPort(const std::string& port)
{
return (port.find_first_not_of("0123456789") == std::string::npos);
}
t_port getPortType(int value)
{
if (value >= 0 && value < 1024)
return (WELL_KNOWN_PORT);
else if (value < 49152)
return (REGISTERED_PORT);
else if (value < 65536)
return (DYNAMIC_PORT);
return (INVALID_PORT);
}
bool setPortNumber(const char *str, int *o_value)
{
__int64_t total(0);
t_port retPort(INVALID_PORT);
while (*str)
{
total = total * 10 + *str - '0';
if (total > 2147483647 || total < -2147483648)
return (false);
str++;
}
retPort = getPortType(total);
if (retPort == WELL_KNOWN_PORT || retPort == INVALID_PORT)
return (false);
*o_value = total;
return (true);
}
bool isNotAlnumAndUnderscore(const std::string& str)
{
size_t i = 0;
while (i < str.size())
{
if ((std::isalnum(str[i]) == false) && (str[i] != '_'))
{
return (true);
}
++i;
}
return (false);
}
bool isSpecialCharactor(const std::string& str)
{
size_t i = 0;
while (i < str.size())
{
if (!std::isalnum(str[i]))
return (true);
++i;
}
return (false);
}
bool isValidPwd(const std::string& pwd)
{
if (pwd.length() == 0)
return (false);
if (isSpecialCharactor(pwd))
return (false);
return (true);
}
std::vector<std::string> split(std::string input, char delimiter)
{
std::vector<std::string> answer;
std::stringstream ss(input);
std::string temp;
while (getline(ss, temp, delimiter))
{
answer.push_back(temp);
}
return answer;
}