-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.cpp
126 lines (110 loc) · 3 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "utils.h"
bool isNumeric(const string &str) {
if (str.empty())
return false;
for (int i = 0; i < str.length(); i++)
if (!isdigit(str[i]))
return false;
return true;
}
string trim(string &str) {
auto str_init = str.find_first_not_of(" \t");
if (str_init == string::npos)
return ""; // String is all whitespace
auto str_end = str.find_last_not_of(" \t");
auto str_range = str_end - str_init + 1;
return str.substr(str_init, str_range);
}
vector<string> strToVect(const string &str, char delim) {
vector<string> result;
string tmp;
// Create stream from string
istringstream ss(str);
// Grab the elements from the stream, separated by delim
while (getline(ss, tmp, delim)) {
result.push_back(trim(tmp));
// Skip whitespace
ss >> ws;
}
return result;
}
pair<float,float> makeCoords(const string str){
pair<float,float> coords;
vector<string> temp;
temp = strToVect(str,',');
coords.first=stof(temp.at(0));
coords.second=stof(temp.at(1));
return coords;
}
//Menu utilities
void line(int size, char ch) {
// Prints a line of specified size with specified character
cout << setfill(ch) << setw(size) << "" << endl << setfill(' ');
}
void cinERR(const string &message) {
cerr << message;
cout << endl;
cin.clear();
}
void getOption(int &dest, const string &message) {
// Tries to get a valid int option from cin to use in a switch-case
string str;
while (true) {
try {
cout << endl << message;
getline(cin, str);
cout << endl;
dest = stoi(str);
break;
}
catch (invalid_argument) {
str = "";
cinERR("ERROR: Invalid entry, try again");
}
}
}
bool inputNIF(int &nif, const string &message) {
string str;
while (true) {
cout << message;
getline(cin, str);
if (validNIF(str)) {
nif = stoi(str);
return true;
} else if (str == "*") {
cout << "Canceled successfully!" << endl;
return false;
}
cinERR("ERROR: Invalid NIF, try again!");
}
}
bool validNIF(string const &nif) {
return !(nif.length() != 9 || !isNumeric(nif));
}
bool validPostcode(string const &postcode){
if(!isNumeric(postcode.substr(0,4)))
return false;
if (postcode.substr(4,1) != "-")
return false;
if (!isNumeric(postcode.substr(5,3)))
return false;
return postcode.size() == 8;
}
bool validLicensePlate(string const &plate){
vector<string> str = strToVect(plate,'-');
if(str.size()!=3) return false;
for(int i=0;i<str.size();i++){
if(str[i].length()!=2) return false;
}
return true;
}
void enterWait(const string &message) {
string str;
cout << message << endl;
getline(cin,str);
}
string removeSpaces(string str)
{
str.erase(remove(str.begin(), str.end(), ' '), str.end());
return str;
}