-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckInterface.cpp
More file actions
144 lines (132 loc) · 3.85 KB
/
CheckInterface.cpp
File metadata and controls
144 lines (132 loc) · 3.85 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
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
// CheckInterface.cpp: implementation of the CheckInterface class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "CheckInterface.h"
bool CheckInterface::check(Person& person, const bool _check_repe) const {
unsigned i = 0, j = 0; //common loop variable
string re_write = ""; //rewrite tel and qq(if valid) into good format
vector<string> temp_str;
bool mailFlag = false; //check mailbox's format
//===============================
if (!((strlen(person.name)>0) && (strlen(person.tel)>0) && (strlen(person.addr)>0)) ){
errorMsg = "Name/Tel/Address is null.";
return false;
}
//===============================
if (_check_repe){ //here to use _check_repe only once.
for (i = 0; i < contact_item.size(); i++)
if (strcmp(person.name,&*contact_item[i]->name) == 0){
errorMsg = "Item already exists.";
return false;
}
}
//===============================
for (i = 0; i<strlen(person.tel); i++){
if (!(( (person.tel[i]>='0')&&(person.tel[i]<='9') ) || (person.tel[i] == ','))){
errorMsg = "Unknown character in Tel option.";
return false;
}
}
temp_str = this->part_tq(person, "tel");
re_write = "";
for (i = 0; i<temp_str.size(); i++){
for (j = i + 1; j<temp_str.size();j++){
if ((temp_str.at(i) == temp_str.at(j)) && (temp_str.at(i) != "")){
errorMsg = "Duplicated Tel number(s).";
return false;
}
}
if (temp_str.at(i) != ""){
re_write += temp_str.at(i);
re_write += ",";
}
}
re_write = re_write.substr(0,re_write.length()-1);
strcpy(person.tel,re_write.c_str());
//===============================
if (person.sex != '\0'){
if ((person.sex == 'f') || (person.sex == 'm'))
person.sex -=32;
if ( !( (person.sex == 'F') || (person.sex == 'M') ) ){
errorMsg = "Unknown character in Gender option.";
return false;
}
}
//===============================
if (strcmp(person.mail,"") != 0){
mailFlag = false;
for (i = 0; i<strlen(person.mail); ++i){
if (person.mail[i] == '@'){
if (mailFlag){
errorMsg = "Format of Mailbox fill is incorrect.";
return false;
}
(!mailFlag) && (mailFlag = true);
}
}//for
if (!mailFlag){
errorMsg = "Format of Mailbox fill is incorrect.";
return false;
}
}
//===============================
if (strcmp(person.qq, "") != 0){
re_write = "";
for (i = 0; i<strlen(person.qq); i++)
if (!(( (person.qq[i]>='0')&&(person.qq[i]<='9') ) || (person.qq[i] == ','))){
errorMsg = "Unknown character in QQ option.";
return false;
}
temp_str.clear();
temp_str = this->part_tq(person, "qq");
for (i = 0; i<temp_str.size(); i++){
for (j = i + 1; j<temp_str.size();j++){
if (temp_str.at(i) == temp_str.at(j)){
errorMsg = "Duplicated QQ number(s).";
return false;
}
}
if (temp_str.at(i) != ""){
re_write += temp_str.at(i);
re_write += ",";
}
}
re_write = re_write.substr(0,re_write.length()-1);
strcpy(person.qq,re_write.c_str());
}
return true;
//===============================
//...
}
bool CheckInterface::check_exact(const Person& index, const string info_str) const{
if (strcmp(index.name,info_str.c_str()) == 0)
return true;
vector<string> temp_vec = part_tq(index, "tel");
for (unsigned i = 0; i<temp_vec.size(); i++){
if (strcmp(temp_vec.at(i).c_str(),info_str.c_str()) == 0)
return true;
}
return false;
}
vector<string> CheckInterface::part_tq(const Person& person, const char* const TEL_QQ) const {
vector<string> rtn_vec;
string src_str;
string temp_str = "";
if (strcmp(TEL_QQ,"tel") == 0)
src_str = person.tel;
else if (strcmp(TEL_QQ,"qq") == 0)
src_str = person.qq;
else
return rtn_vec;
for (unsigned i = 0; i<strlen(src_str.c_str()); i++){
if (src_str[i] == ','){
rtn_vec.push_back(temp_str);
temp_str = "";
}
else
temp_str += src_str[i];
}
rtn_vec.push_back(temp_str);
return rtn_vec;
}