-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathBufferedFileClass.h
125 lines (124 loc) · 2.18 KB
/
BufferedFileClass.h
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
#pragma once
#include "RawFileClass.h"
class BufferedFileClass : public RawFileClass {
private:
unsigned char* Buffer;
unsigned int BufferSize;
int BufferAvailable;
int BufferOffset;
public:
BufferedFileClass() : Buffer(0), BufferSize(0), BufferAvailable(0), BufferOffset(0)
{
}
~BufferedFileClass()
{
Reset_Buffer();
}
int Read(void* buffer,int size)
{
void *tmp = buffer;
int sz = size;
int result = 0;
if (BufferAvailable > 0)
{
int buf = BufferAvailable;
if (buf > size)
{
buf = size;
}
memcpy(buffer, Buffer + BufferOffset, buf);
BufferAvailable -= buf;
BufferOffset += buf;
sz -= buf;
tmp = (char *)tmp + buf;
result += buf;
}
if (sz)
{
int buf = BufferSize;
if (!buf)
{
buf = 16384;
}
if (sz > buf)
{
return result + RawFileClass::Read(tmp,sz);
}
if (!BufferSize)
{
BufferSize = 16384;
Buffer = new unsigned char[16384];
BufferAvailable = 0;
BufferOffset = 0;
}
if (!Buffer)
{
return 0;
}
if (!BufferAvailable)
{
BufferAvailable = RawFileClass::Read(Buffer,BufferSize);
BufferOffset = 0;
}
if (BufferAvailable > 0)
{
int buf2 = BufferAvailable;
if (buf2 > sz)
{
buf2 = sz;
}
if (!Buffer)
{
return 0;
}
memcpy(tmp,Buffer+BufferOffset,buf2);
BufferAvailable -= buf2;
BufferOffset += buf2;
result += buf2;
}
}
return result;
}
int Seek(int pos,int dir)
{
if (dir != 1 || pos < 0)
{
Reset_Buffer();
}
if (BufferAvailable)
{
int buf = BufferAvailable;
if (buf > pos)
{
buf = pos;
}
BufferAvailable -= buf;
BufferOffset += buf;
return RawFileClass::Seek(pos - buf,dir) - BufferAvailable;
}
else
{
return RawFileClass::Seek(pos,dir);
}
}
int Write(void* buffer,int size)
{
return RawFileClass::Write(buffer,size);
}
void Close()
{
RawFileClass::Close();
Reset_Buffer();
}
void Reset_Buffer()
{
if (Buffer)
{
delete[] Buffer;
Buffer = 0;
BufferSize = 0;
BufferAvailable = 0;
BufferOffset = 0;
}
}
};