-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBinaryInputStream.cpp
More file actions
153 lines (130 loc) · 3.27 KB
/
Copy pathBinaryInputStream.cpp
File metadata and controls
153 lines (130 loc) · 3.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
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
#include "stdafx.h"
#include "BinaryInputStream.h"
FileInputStream::~FileInputStream()
{
CloseFileStream();
}
bool FileInputStream::OpenFileStream(const std::string& filePath)
{
CloseFileStream();
mFileStream = ::fopen(filePath.c_str(), "rb");
if (mFileStream == nullptr)
return false;
::fseek(mFileStream, 0, SEEK_END);
mFileLength = ::ftell(mFileStream);
::fseek(mFileStream, 0, SEEK_SET);
return true;
}
void FileInputStream::CloseFileStream()
{
if (mFileStream)
{
::fclose(mFileStream);
mFileStream = nullptr;
}
mFileLength = 0;
mFileCursor = 0;
}
long FileInputStream::GetCursorPosition() const
{
cxx_assert(mFileStream);
if (mFileStream)
{
return ::ftell(mFileStream);
}
return 0;
}
bool FileInputStream::SetCursorPosition(long cursorPosition)
{
cxx_assert(mFileStream);
if (mFileStream)
{
return ::fseek(mFileStream, cursorPosition, SEEK_SET) == 0;
}
return false;
}
bool FileInputStream::AdvanceCursorPosition(long advancePosition)
{
cxx_assert(mFileStream);
if (mFileStream)
{
return ::fseek(mFileStream, advancePosition, SEEK_CUR) == 0;
}
return false;
}
long FileInputStream::ReadData(void* dataBuffer, long dataLength)
{
cxx_assert(mFileStream);
cxx_assert(dataBuffer);
if (mFileStream && dataBuffer)
{
return ::fread(dataBuffer, 1, dataLength, mFileStream);
}
return 0;
}
bool FileInputStream::IsEndOfStream() const
{
cxx_assert(mFileStream);
if (mFileStream)
{
return ::feof(mFileStream) != 0;
}
return false;
}
long FileInputStream::GetLength() const
{
return mFileLength;
}
//////////////////////////////////////////////////////////////////////////
MemoryInputStream::~MemoryInputStream()
{
CloseMemoryStream();
}
void MemoryInputStream::OpenMemoryStreamFromBuffer(ByteArray& sourceBuffer)
{
CloseMemoryStream();
mStreamData.swap(sourceBuffer);
mStreamCursor = 0;
mStreamLength = mStreamData.size();
}
void MemoryInputStream::CloseMemoryStream()
{
mStreamData.clear();
mStreamCursor = 0;
mStreamLength = 0;
}
long MemoryInputStream::GetCursorPosition() const
{
return mStreamCursor;
}
bool MemoryInputStream::SetCursorPosition(long cursorPosition)
{
mStreamCursor = std::min(mStreamLength, cursorPosition);
return true;
}
bool MemoryInputStream::AdvanceCursorPosition(long advancePosition)
{
mStreamCursor = std::min(mStreamLength, mStreamCursor + advancePosition);
mStreamCursor = std::max(0L, mStreamCursor);
return true;
}
long MemoryInputStream::ReadData(void* dataBuffer, long dataLength)
{
cxx_assert(dataBuffer);
long cursorPos = std::min(mStreamLength, mStreamCursor + dataLength);
dataLength = (cursorPos - mStreamCursor);
if (dataLength > 0)
{
memcpy(dataBuffer, mStreamData.data() + mStreamCursor, dataLength);
mStreamCursor = cursorPos;
}
return dataLength;
}
bool MemoryInputStream::IsEndOfStream() const
{
return mStreamCursor == mStreamLength;
}
long MemoryInputStream::GetLength() const
{
return mStreamLength;
}