-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_indexing_utils.cpp
More file actions
147 lines (135 loc) · 3.18 KB
/
data_indexing_utils.cpp
File metadata and controls
147 lines (135 loc) · 3.18 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
#include "data_indexing_utils.h"
#include <iostream>
#include <fstream>
namespace data_indexing {
char statement_delimiter = '*';
char empty_symbol = '&';
char field_end_symbol = '/';
// ФИО - 50 символов разделенных пробелом,
// Дата рождения - ГГММДД - 8 цифр,
// Пол - 'м' или 'ж' 1 символ
// ...*Иванов Иван Иванович&&...&&/19971105/M/...
// length including ['*','next*')
size_t name_length = 50;
size_t statement_length = name_length + 14;
void print_data(const Person& data, const std::string& filename)
{
std::ofstream out;
out.open(filename, std::ios::app);
out << statement_delimiter;
if (data.name.full_name.length() > name_length)
out << data.name.full_name.substr(0, name_length);
else
{
out << data.name.full_name;
out << std::string(name_length - data.name.full_name.length(), empty_symbol);
}
out << field_end_symbol;
out << data.birth_date.to_string() << field_end_symbol;
out << data.gender << field_end_symbol;
out.close();
}
void print_all(const Sequence<Person>& data, const std::string& filename)
{
for (size_t i = 0; i < data.GetCount(); ++i)
{
print_data(data.Get(i), filename);
}
}
Person read_data(std::ifstream in)
{
if (!in.is_open())
{
std::cerr << "Stream is not opened for reading" << std::endl;
exit(1);
}
if (in.peek() != statement_delimiter)
throw std::invalid_argument("read_data: wrong structure of the file");
in.ignore();
Person person;
while (in.peek() != empty_symbol && in.peek() != field_end_symbol)
{
char t;
in >> t;
person.name.full_name += t;
}
while (in.peek() != field_end_symbol)
{
in.ignore();
}
in.ignore();
std::string tmp;
for (int i = 0; i < 4; ++i)
{
char t;
in >> t;
tmp += t;
}
person.birth_date.year = stoi(tmp);
tmp = "";
for (int i = 0; i < 2; ++i)
{
char t;
in >> t;
tmp += t;
}
person.birth_date.month = stoi(tmp);
tmp = "";
for (int i = 0; i < 2; ++i)
{
char t;
in >> t;
tmp += t;
}
person.birth_date.day = stoi(tmp);
in.ignore();
short t;
in >> t;
person.gender = Gender(t);
in.close();
return person;
}
Person read_data_at(const std::string& filename, size_t index)
{
std::ifstream in;
in.open(filename);
if(!in || in.peek() != '*')
{
std::cerr << "Wrong data format";
exit(1);
}
for (size_t i = 0; i < index; ++i)
{
in.ignore(statement_length);
}
if (in.eof()) throw std::invalid_argument("read index out of range");
return read_data(std::move(in));
}
Sequence<Person>* read_data_by_indices(const std::string& filename, Sequence<size_t>* indices)
{
Sequence<Person>* res = new ArraySequence<Person>();
if (indices->GetCount() == 0) return res;
for (size_t i = 1; i < indices->GetCount(); ++i) {
res->Append(read_data_at(filename, indices->Get(0)));
}
return res;
}
Sequence<Person>* read_all(const std::string& filename)
{
Sequence<Person>* res = new ArraySequence<Person>;
size_t i = 0;
Person p;
while (true) {
try
{
p = read_data_at(filename, i);
}
catch(std::invalid_argument& e)
{
return res;
}
res->Append(p);
++i;
}
}
}