-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathstraw.h
257 lines (251 loc) · 4.06 KB
/
straw.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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#pragma once
#include "RawFileClass.h"
class Straw {
private:
Straw* ChainTo;
Straw* ChainFrom;
public:
Straw();
virtual ~Straw();
virtual void Get_From(Straw* straw);
virtual int Get(void* source,int slen);
};
class Buffer {
private:
void* BufferPtr;
long Size;
bool IsAllocated;
public:
Buffer(void* buffer,long size);
Buffer(long size) : BufferPtr(0), Size(size), IsAllocated(false)
{
if (size > 0)
{
BufferPtr = new char[size];
IsAllocated = true;
}
}
void *Get_Buffer()
{
return BufferPtr;
}
long Get_Size()
{
return Size;
}
~Buffer();
};
class BufferStraw : public Straw {
private:
Buffer BufferPtr;
int Index;
public:
BufferStraw(void* buffer, int size);
~BufferStraw();
int Get(void* source,int slen);
};
class FileClass;
class FileStraw : public Straw {
private:
FileClass* File;
bool HasOpened;
public:
FileStraw(FileClass&);
~FileStraw();
int Get(void* source,int slen);
};
class CacheStraw : public Straw {
private:
Buffer BufferPtr;
int Index;
int Length;
public:
CacheStraw(int size) : BufferPtr(size), Index(0), Length(0)
{
}
bool Is_Valid()
{
return BufferPtr.Get_Buffer() != 0;
}
~CacheStraw() {}
int Get(void* source,int slen)
{
char *src = (char *)source;
int len = slen;
int ret = 0;
int len2;
if (BufferPtr.Get_Buffer())
{
for (int i = source == 0;!i && len > 0;i = len2 == 0)
{
if (Length > 0 )
{
int sz = len;
if (len > this->Length)
{
sz = Length;
}
memmove(src,(char *)BufferPtr.Get_Buffer() + Index,sz);
len -= sz;
Index += sz;
ret += sz;
Length -= sz;
src += sz;
}
if (!len)
{
break;
}
len2 = Straw::Get(BufferPtr.Get_Buffer(),BufferPtr.Get_Size());
Length = len2;
Index = 0;
}
}
return ret;
}
};
class Pipe
{
private:
Pipe* ChainTo;
Pipe* ChainFrom;
public:
Pipe() : ChainTo(0), ChainFrom(0)
{
}
virtual ~Pipe()
{
if (ChainTo)
{
ChainTo->ChainFrom = ChainFrom;
}
if (ChainFrom)
{
ChainFrom->Put_To(ChainTo);
}
ChainFrom = 0;
ChainTo = 0;
}
virtual int Flush()
{
if (ChainTo)
{
return ChainTo->Flush();
}
else
{
return 0;
}
}
virtual int End()
{
return Flush();
}
virtual void Put_To(Pipe* pipe)
{
if (ChainTo != pipe)
{
if (pipe && pipe->ChainFrom)
{
pipe->ChainFrom->Put_To(0);
pipe->ChainFrom = 0;
}
if (ChainTo)
{
ChainTo->ChainFrom = 0;
ChainTo->Flush();
}
ChainTo = pipe;
if (pipe)
{
pipe->ChainFrom = this;
}
}
}
virtual int Put(const void *source, int length)
{
if (ChainTo)
{
return ChainTo->Put(source, length);
}
return length;
}
};
class BufferPipe : public Pipe
{
private:
Buffer BufferPtr;
int Index;
public:
BufferPipe(void *data,int size) : BufferPtr(data,size), Index(0)
{
}
virtual int Put(const void *source,int length)
{
if (BufferPtr.Get_Buffer() && source && length > 0)
{
int len = length;
int size = BufferPtr.Get_Size();
if (size)
{
len = size - Index;
if (len > length)
{
len = length;
}
}
if (len > 0)
{
memmove((char *)(BufferPtr.Get_Buffer()) + Index,source,len);
}
Index += len;
return len;
}
return 0;
}
};
class FilePipe : public Pipe
{
private:
FileClass* File;
bool HasOpened;
public:
FilePipe(FileClass *file) : File(file), HasOpened(false)
{
}
virtual ~FilePipe()
{
if (File && HasOpened)
{
HasOpened = false;
File->Close();
File = 0;
}
}
virtual int End()
{
int ret = Flush();
if (File && HasOpened)
{
HasOpened = false;
File->Close();
}
return ret;
}
virtual int Put(const void *source,int length)
{
if (File && source && length > 0)
{
if (!File->Is_Open())
{
HasOpened = true;
File->Open(2);
}
return File->Write((void *)source,length);
}
else
{
return 0;
}
}
};