-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMPAFileStream.cpp
More file actions
168 lines (133 loc) · 5.15 KB
/
MPAFileStream.cpp
File metadata and controls
168 lines (133 loc) · 5.15 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
// GNU LESSER GENERAL PUBLIC LICENSE
// Version 3, 29 June 2007
//
// Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
//
// Everyone is permitted to copy and distribute verbatim copies of this license
// document, but changing it is not allowed.
//
// This version of the GNU Lesser General Public License incorporates the terms
// and conditions of version 3 of the GNU General Public License, supplemented
// by the additional permissions listed below.
#include "stdafx.h"
#include "MPAFileStream.h"
#include "Platform.h"
#include "MPAException.h"
#include "MPAEndOfFileException.h"
// 1KB is initial buffersize
const unsigned CMPAFileStream::INIT_BUFFERSIZE = 1024U;
CMPAFileStream::CMPAFileStream(LPCTSTR file_name, IMPAFileSystem *file_system)
: CMPAStream(file_name), m_dwOffset(0) {
// open with CreateFile (no limitation of 128byte filename length, like in
// mmioOpen)
m_hFile = file_system->Open(file_name, "rb");
if (!m_hFile) {
throw CMPAException{CMPAException::Error::ErrOpenFile, file_name,
_T("CMPAFileStream ctor (fopen)"), GetLastSystemError(),
_T("Unable to open file in read mode.")};
}
Init();
}
CMPAFileStream::CMPAFileStream(LPCTSTR file_name, std::unique_ptr<IMPAFile> &&hFile)
: CMPAStream(file_name), m_hFile(std::move(hFile)), m_dwOffset(0) {
Init();
}
void CMPAFileStream::Init() {
m_dwBufferSize = INIT_BUFFERSIZE;
// fill buffer for first time
m_pBuffer = new unsigned char[m_dwBufferSize];
FillBuffer(m_dwOffset, m_dwBufferSize, false);
}
CMPAFileStream::~CMPAFileStream() {
delete[] m_pBuffer;
}
// set file position
void CMPAFileStream::SetPosition(unsigned offset) const {
const int result = m_hFile->Seek(offset, MPAFileSeekType::kSet);
if (result != 0) {
throw CMPAException{CMPAException::Error::ErrSetPosition, m_szFile,
_T("CMPAFileStream::SetPosition (fseek)"),
GetLastSystemError()};
}
}
unsigned char* CMPAFileStream::ReadBytes(unsigned size, unsigned& offset,
bool move_offset,
bool should_reverse) const {
// enough bytes in buffer, otherwise read from file
if (offset < m_dwOffset || m_dwOffset + m_dwBufferSize < offset + size) {
if (!FillBuffer(offset, size, should_reverse)) {
throw CMPAEndOfFileException{m_szFile};
}
if (move_offset) offset += size;
return m_pBuffer;
}
unsigned char* buffer{m_pBuffer + (offset - m_dwOffset)};
if (move_offset) offset += size;
return buffer;
}
unsigned CMPAFileStream::GetSize() const {
const long start_pos{m_hFile->Tell()};
if (start_pos == -1L) {
throw CMPAException{CMPAException::Error::ErrReadFile, m_szFile,
_T("CMPAFileStream::GetSize (ftell)"),
GetLastSystemError()};
}
if (m_hFile->Seek(0L, MPAFileSeekType::kEnd)) {
(void)m_hFile->Seek(start_pos, MPAFileSeekType::kSet);
throw CMPAException{CMPAException::Error::ErrReadFile, m_szFile,
_T("CMPAFileStream::GetSize (fseek)"),
GetLastSystemError()};
}
// ftell and _ftelli64 return the current file position.
// The value returned by ftell and _ftelli64 may not reflect the physical byte
// offset for streams opened in text mode, because text mode causes carriage
// return-line feed translation.
const long size{m_hFile->Tell()};
if (size == -1L) {
throw CMPAException{CMPAException::Error::ErrReadFile, m_szFile,
_T("CMPAFileStream::GetSize (ftell)"),
GetLastSystemError()};
}
if (m_hFile->Seek(start_pos, MPAFileSeekType::kSet)) {
throw CMPAException{CMPAException::Error::ErrReadFile, m_szFile,
_T("CMPAFileStream::GetSize (fseek)"), GetLastSystemError()};
}
return size;
}
// fills internal buffer, returns false if EOF is reached, otherwise true.
// Throws exceptions
bool CMPAFileStream::FillBuffer(unsigned offset, unsigned size,
bool should_reverse) const {
// calc new buffer size
if (size > m_dwBufferSize) {
m_dwBufferSize = size;
// release old buffer
delete[] m_pBuffer;
// reserve new buffer
m_pBuffer = new unsigned char[m_dwBufferSize];
}
if (should_reverse) {
if (offset + size < m_dwBufferSize)
offset = 0;
else
offset = offset + size - m_dwBufferSize;
}
// read <m_dwBufferSize> bytes from offset <offset>
m_dwBufferSize = Read(m_pBuffer, offset, m_dwBufferSize);
// set new offset
m_dwOffset = offset;
// false if eof.
return m_dwBufferSize >= size;
}
// read from file, return number of bytes read
unsigned CMPAFileStream::Read(void* data, unsigned offset,
unsigned size) const {
// set position first
SetPosition(offset);
const size_t bytes_read = m_hFile->Read(data, size);
if (bytes_read < size && m_hFile->Error())
throw CMPAException{CMPAException::Error::ErrReadFile, m_szFile,
_T("CMPAFileStream::Read (fread)"), GetLastSystemError()};
// Safe as size is unsigned.
return static_cast<unsigned>(bytes_read);
}