-
Notifications
You must be signed in to change notification settings - Fork 26
/
File.cpp
175 lines (143 loc) · 3.14 KB
/
File.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
#include "StdAfx.h"
#include "Common.h"
#include "File.h"
CFile::CFile()
: m_file{INVALID_HANDLE_VALUE}
{
}
CFile::~CFile()
{
Close();
}
HANDLE CFile::Open(LPCTSTR filename, OpenMode mode)
{
if (mode == OpenMode::Read)
{
m_file = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
}
else if (mode == OpenMode::Write)
{
m_file = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
}
return m_file;
}
bool CFile::OpenForRead(LPCTSTR filename)
{
return Open(filename, OpenMode::Read) != INVALID_HANDLE_VALUE;
}
bool CFile::OpenForWrite(LPCTSTR filename)
{
return Open(filename, OpenMode::Write) != INVALID_HANDLE_VALUE;
}
void CFile::Close()
{
if (m_file != INVALID_HANDLE_VALUE)
{
CloseHandle(m_file);
m_file = INVALID_HANDLE_VALUE;
}
}
u32 CFile::Read(void* buffer, u32 size)
{
DWORD read_size;
ReadFile(m_file, buffer, size, &read_size, nullptr);
return static_cast<u32>(read_size);
}
u32 CFile::ReadLine(void* buffer, u32 buffer_size, bool delete_line_code)
{
u8* byte_buffer = static_cast<u8*>(buffer);
const u8* byte_buffer_end = byte_buffer + buffer_size;
u32 total_read_size = 0;
while (true)
{
// Read one byte
const u32 read_size = Read(byte_buffer, 1);
total_read_size += read_size;
if (*byte_buffer == '\n')
{
// Reached newline character
if (delete_line_code)
{
// Remove carriage return character
if (*(byte_buffer - 1) == '\r')
*(byte_buffer - 1) = '\0';
*byte_buffer = '\0';
}
break;
}
if (read_size == 0)
{
// Read until the end of the file (EOF).
break;
}
byte_buffer++;
if (byte_buffer >= byte_buffer_end)
{
// Filled the entire buffer.
break;
}
}
return total_read_size;
}
u32 CFile::Write(const void* buffer, u32 size)
{
DWORD write_size;
WriteFile(m_file, buffer, size, &write_size, nullptr);
return static_cast<u32>(write_size);
}
void CFile::WriteLine(const void* buffer)
{
const u8* byte_buffer = static_cast<const u8*>(buffer);
while (true)
{
if (*byte_buffer == '\0')
{
// Has reached the null termination character
break;
}
// Writing one byte
const u32 write_size = Write(byte_buffer, 1);
if (write_size == 0)
{
// Could not be written
break;
}
if (*byte_buffer == '\n')
{
// Reached newline character
break;
}
byte_buffer++;
}
}
u64 CFile::Seek(s64 offset, u32 seek_mode)
{
LARGE_INTEGER li;
li.QuadPart = offset;
li.LowPart = SetFilePointer(m_file, li.LowPart, &li.HighPart, seek_mode);
if (li.LowPart == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR)
li.QuadPart = -1;
return static_cast<u64>(li.QuadPart);
}
u64 CFile::SeekHed(s64 offset)
{
return Seek(offset, FILE_BEGIN);
}
u64 CFile::SeekEnd(s64 offset)
{
return Seek(-offset, FILE_END);
}
u64 CFile::SeekCur(s64 offset)
{
return Seek(offset, FILE_CURRENT);
}
u64 CFile::GetFilePointer()
{
return Seek(0, FILE_CURRENT);
}
u64 CFile::GetFileSize()
{
LARGE_INTEGER li = {};
li.LowPart = ::GetFileSize(m_file, &reinterpret_cast<DWORD&>(li.HighPart));
return static_cast<u64>(li.QuadPart);
}