-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileOperations.cpp
188 lines (154 loc) · 4.94 KB
/
FileOperations.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
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
#pragma once
#include <fstream>
#include <iostream>
#include "User.cpp"
#include "Submission.cpp"
bool Read(User& record, std::fstream& file, const std::streampos& pos)
{
if (!file)
return false;
file.seekg(pos);
file.read(reinterpret_cast<char*>(&record), sizeof(User));
return !file.fail();
}
bool Read(Submission& record, std::fstream& file, const std::streampos& pos)
{
if (!file)
return false;
file.seekg(pos);
file.read(reinterpret_cast<char*>(&record), sizeof(Submission));
return !file.fail();
}
bool Read(Header& record, std::fstream& file, const std::streampos& pos)
{
if (!file)
return false;
file.seekg(pos);
file.read(reinterpret_cast<char*>(&record), sizeof(Header));
return !file.fail();
}
bool Write(const Submission& record, std::fstream& file, const std::streampos& pos)
{
if (!file)
{
return false;
}
file.seekp(pos);
file.write(reinterpret_cast<const char*>(&record), sizeof(Submission));
file.flush();
return !file.fail();
}
bool Write(const User& record, std::fstream& file, const std::streampos& pos)
{
if (!file)
return false;
file.seekp(pos);
file.write(reinterpret_cast<const char*>(&record), sizeof(User));
file.flush();
return !file.fail();
}
bool Write(const Header& record, std::fstream& file, const std::streampos& pos)
{
if (!file)
return false;
file.seekp(pos);
file.write(reinterpret_cast<const char*>(&record), sizeof(Header));
file.flush();
return !file.fail();
}
std::streampos PosByIndex(int32_t index, User& user)
{
return static_cast<std::streamoff>(sizeof(Header) + sizeof(User) * index);
}
std::streampos PosByIndex(int32_t index, Submission& submission)
{
return static_cast<std::streamoff>(sizeof(Header) + sizeof(Submission) * index);
}
std::streampos GetHeaderPos()
{
return static_cast<std::streamoff>(0);
}
int32_t GetTableSize(std::fstream &file)
{
Header header = Header();
Read(header, file, GetHeaderPos());
return header.TableSize;
}
int32_t GetTableCapacity(std::fstream &file)
{
Header header = Header();
Read(header, file, GetHeaderPos());
return header.TableCapacity;
}
int32_t GetTableFirstElement(std::fstream &file)
{
Header header = Header();
Read(header, file, GetHeaderPos());
return header.FirstElement;
}
Header GetHeader(std::fstream &file)
{
Header header = Header();
Read(header, file, GetHeaderPos());
return header;
}
int32_t GetIndexByID(std::fstream &file, int32_t ID, User user)//пошук індекса елемента з певним айді
{ //повертає -1, якщо такого айді нема у файлі та індекс елементу, якщо він присутній у файлі
int32_t CurrentIndex = GetHeader(file).FirstElement; //поточна позиція у файлі
if(CurrentIndex < 0)
{
return -1;
}
for(Read(user, file, PosByIndex(CurrentIndex, user)); ; Read(user, file, PosByIndex(user.NextElement, user))) // проходимося по всіх записах та перевіряємо айдішки
{
if(user.UserID.value == ID) //знайшли айдішку
{
return CurrentIndex;
}
CurrentIndex = user.NextElement;
if(CurrentIndex == -1)
{
break;
}
}
return -1; // не знайшли айдішку (((
}
int32_t GetIndexByID(std::fstream &file, int32_t ID, Submission submission)//пошук індекса елемента з певним айді
{ //повертає -1, якщо такого айді нема у файлі та індекс елементу, якщо він присутній у файлі
int32_t CurrentIndex = GetHeader(file).FirstElement; //поточна позиція у файлі
if(CurrentIndex < 0)
{
return -1;
}
for(Read(submission, file, PosByIndex(CurrentIndex, submission)); ; Read(submission, file, PosByIndex(submission.NextElementInFile, submission))) // проходимося по всіх записах та перевіряємо айдішки
{
if(submission.SubmissionID.value == ID) //знайшли айдішку
{
return CurrentIndex;
}
CurrentIndex = submission.NextElementInFile;
if(CurrentIndex == -1)
{
break;
}
}
return -1; // не знайшли айдішку (((
}
Submission GetSubmissionByIndex(std::fstream &file, int32_t index)
{
Submission submission = Submission();
if(index >= 0)
{
Read(submission, file, PosByIndex(index, submission));
}
return submission;
}
User GetUserByIndex(std::fstream &file, int32_t index)
{
User user = User();
if(index >= 0)
{
Read(user, file, PosByIndex(index, user));
}
return user;
}