Repository navigation
Expand file tree
/
Copy pathFileStorage.cpp
More file actions
212 lines (178 loc) · 7.1 KB
/
Copy pathFileStorage.cpp
File metadata and controls
212 lines (178 loc) · 7.1 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include "FileStorage.h"
#include "validation.h"
#include <fstream>
#include <set>
#include <sstream>
bool stringToInt(const std::string& str, int& outValue) {
try {
size_t pos;
outValue = std::stoi(str, &pos);
return pos == str.length();
} catch (...) {
return false;
}
}
std::vector<Contact> FileStorage::load() {
lastError.clear();
std::vector<Contact> contacts;
std::ifstream file(filename);
if (!file.is_open()) {
lastError = "Cannot open file '" + filename + "' for reading.";
return contacts;
}
std::string line;
int lineNum = 0;
std::set<int> loadedIds;
std::set<std::string> loadedEmails;
std::set<std::string> loadedPhoneNumbers;
while (std::getline(file, line)) {
lineNum++;
if (line.empty()) {
continue;
}
std::stringstream ss(line);
std::string token;
std::vector<std::string> parts;
while (std::getline(ss, token, ';')) {
parts.push_back(token);
}
if (parts.size() < 10) {
lastError = "Line " + std::to_string(lineNum) + ": not enough columns.";
return {};
}
Contact contact;
int id;
if (!stringToInt(parts[0], id)) {
lastError = "Line " + std::to_string(lineNum) + ": ID is not a number.";
return {};
}
if (id <= 0) {
lastError = "Line " + std::to_string(lineNum) + ": ID must be positive.";
return {};
}
if (loadedIds.count(id)) {
lastError = "Line " + std::to_string(lineNum) + ": duplicate ID found: " + std::to_string(id);
return {};
}
loadedIds.insert(id);
contact.setId(id);
std::string surname = validation::trim(parts[1]);
if (!validation::isValidName(surname)) {
lastError = "Line " + std::to_string(lineNum) + ": invalid surname.";
return {};
}
contact.setSurname(surname);
std::string forename = validation::trim(parts[2]);
if (!validation::isValidName(forename)) {
lastError = "Line " + std::to_string(lineNum) + ": invalid forename.";
return {};
}
contact.setForename(forename);
std::string patronymic = validation::trim(parts[3]);
if (!patronymic.empty() && !validation::isValidName(patronymic)) {
lastError = "Line " + std::to_string(lineNum) + ": invalid patronymic.";
return {};
}
contact.setPatronymic(patronymic);
std::string address = validation::trim(parts[4]);
if (!validation::isValidAddress(address)) {
lastError = "Line " + std::to_string(lineNum) + ": invalid address.";
return {};
}
contact.setAddress(address);
int day, month, year;
bool dateOk = true;
dateOk &= stringToInt(parts[5], day);
dateOk &= stringToInt(parts[6], month);
dateOk &= stringToInt(parts[7], year);
if (!dateOk) {
lastError = "Line " + std::to_string(lineNum) + ": birth date must be valid numbers.";
return {};
}
if (!(day == 0 && month == 0 && year == 0)) {
if (!validation::isValidDate(day, month, year)) {
lastError = "Line " + std::to_string(lineNum) + ": invalid birth date.";
return {};
}
}
contact.setBirthDate(Date(day, month, year));
std::string email = validation::trim(parts[8]);
if (!validation::isValidEmail(email)) {
lastError = "Line " + std::to_string(lineNum) + ": invalid email.";
return {};
}
if (!validation::isForenameInEmail(email, forename)) {
lastError = "Line " + std::to_string(lineNum) + ": email does not contain forename.";
return {};
}
std::string normalizedEmail = validation::normalizeEmail(email);
if (loadedEmails.count(normalizedEmail)) {
lastError = "Line " + std::to_string(lineNum) + ": duplicate email found.";
return {};
}
loadedEmails.insert(normalizedEmail);
contact.setEmail(normalizedEmail);
std::stringstream phonesStream(parts[9]);
std::string phonePairStr;
while (std::getline(phonesStream, phonePairStr, '|')) {
size_t colonPos = phonePairStr.find(':');
if (colonPos != std::string::npos) {
std::string type = validation::trim(phonePairStr.substr(0, colonPos));
std::string number = validation::trim(phonePairStr.substr(colonPos + 1));
if (!validation::isValidPhoneType(type)) {
lastError = "Line " + std::to_string(lineNum) + ": invalid phone type.";
return {};
}
if (!validation::isValidPhoneNumber(number)) {
lastError = "Line " + std::to_string(lineNum) + ": invalid phone number.";
return {};
}
std::string normalizedNumber = validation::normalizePhoneNumber(number);
if (loadedPhoneNumbers.count(normalizedNumber)) {
lastError = "Line " + std::to_string(lineNum) + ": duplicate phone number found.";
return {};
}
loadedPhoneNumbers.insert(normalizedNumber);
contact.addPhoneNumber(type, normalizedNumber);
} else {
lastError = "Line " + std::to_string(lineNum) + ": malformed phone entry.";
return {};
}
}
if (contact.getPhoneNumbers().empty()) {
lastError = "Line " + std::to_string(lineNum) + ": contact must have at least one phone number.";
return {};
}
contacts.push_back(contact);
}
file.close();
return contacts;
}
bool FileStorage::save(const std::vector<Contact>& contacts) {
std::ofstream file(filename);
if (!file.is_open()) {
return false;
}
for (const Contact& contact : contacts) {
file << contact.getId() << ";"
<< contact.getSurname() << ";"
<< contact.getForename() << ";"
<< contact.getPatronymic() << ";"
<< contact.getAddress() << ";"
<< contact.getBirthDate().day << ";"
<< contact.getBirthDate().month << ";"
<< contact.getBirthDate().year << ";"
<< contact.getEmail() << ";";
std::stringstream phonesStream;
const std::vector<PhoneNumber>& phones = contact.getPhoneNumbers();
for (size_t i = 0; i < phones.size(); ++i) {
phonesStream << phones[i].type << ":" << phones[i].number;
if (i < phones.size() - 1) {
phonesStream << "|";
}
}
file << phonesStream.str() << std::endl;
}
file.close();
return true;
}