-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.cpp
More file actions
72 lines (62 loc) · 1.27 KB
/
file.cpp
File metadata and controls
72 lines (62 loc) · 1.27 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
#include <format>
#include "file.h"
File::File(const std::string& file_id): RoomEntity(file_id), RegisterIntf("F"), m_index(0)
{
}
void File::reset()
{
m_index = 0;
}
bool File::isEOF()
{
return m_index == m_data.size();
}
bool File::isGrabbed()
{
return m_is_grabbed;
}
void File::grab()
{
m_is_grabbed = true;
}
void File::drop()
{
m_is_grabbed = false;
}
void File::write(int value)
{
if(m_index == m_data.size()){
m_data.push_back(value);
} else {
m_data[m_index] = value;
}
m_index++;
}
int File::read() //TODO return std::optional<int>
{
if(m_index < m_data.size()){
m_index++;
return m_data[m_index - 1];
}
return 0;
}
void File::seek(int offset)
{
int new_index = m_index + offset;
if(new_index > m_data.size()){
m_index = static_cast<int>(m_data.size());
}else if(new_index < 0){
m_index = 0;
}
m_index = new_index;
}
std::string File::toString()
{
std::string result = std::format("File ID:{}, Index:{}, isEOF:{}, ", getId(), m_index, isEOF());
result += "Data:[";
for(auto value : m_data){
result += std::format("{},", value);
}
result += "]";
return result;
}