Tiberian Technologies Scripts Reference Revision: 9000
Loading...
Searching...
No Matches
straw.h
1/* Renegade Scripts.dll
2 Copyright 2013 Tiberian Technologies
3
4 This file is part of the Renegade scripts.dll
5 The Renegade scripts.dll is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License as published by the Free
7 Software Foundation; either version 2, or (at your option) any later
8 version. See the file COPYING for more details.
9 In addition, an exemption is given to allow Run Time Dynamic Linking of this code with any closed source module that does not contain code covered by this licence.
10 Only the source code to the module(s) containing the licenced code has to be released.
11*/
12#ifndef TT_INCLUDE__STRAW_H
13#define TT_INCLUDE__STRAW_H
14
15class SCRIPTS_API Straw {
16private:
17 Straw* ChainTo;
18 Straw* ChainFrom;
19public:
20 Straw();
21 virtual ~Straw();
22 virtual void Get_From(Straw* straw);
23 virtual int Get(void* source,int slen);
24};
25class SCRIPTS_API Buffer {
26private:
27 void* BufferPtr;
28 long Size;
29 bool IsAllocated;
30public:
31 Buffer(void* buffer,long size);
32 Buffer(long size) : BufferPtr(0), Size(size), IsAllocated(false)
33 {
34 if (size > 0)
35 {
36 BufferPtr = new char[size];
37 IsAllocated = true;
38 }
39 }
40 void *Get_Buffer()
41 {
42 return BufferPtr;
43 }
44 long Get_Size()
45 {
46 return Size;
47 }
48 ~Buffer();
49};
50
51class SCRIPTS_API BufferStraw : public Straw {
52private:
53 Buffer BufferPtr;
54 int Index;
55public:
56 BufferStraw(void* buffer, int size);
57 ~BufferStraw();
58 int Get(void* source,int slen);
59};
60
61class FileClass;
62class SCRIPTS_API FileStraw : public Straw {
63private:
64 FileClass* File;
65 bool HasOpened;
66public:
67 FileStraw(FileClass&);
68 ~FileStraw();
69 int Get(void* source,int slen);
70};
71
72class SCRIPTS_API CacheStraw : public Straw {
73private:
74 Buffer BufferPtr;
75 int Index;
76 int Length;
77public:
78 CacheStraw(int size) : BufferPtr(size), Index(0), Length(0)
79 {
80 }
81 bool Is_Valid()
82 {
83 return BufferPtr.Get_Buffer() != 0;
84 }
85 ~CacheStraw() {}
86 int Get(void* source,int slen)
87 {
88 char *src = (char *)source;
89 int len = slen;
90 int ret = 0;
91 int len2;
92 if (BufferPtr.Get_Buffer())
93 {
94 for (int i = source == 0;!i && len > 0;i = len2 == 0)
95 {
96 if (Length > 0 )
97 {
98 int sz = len;
99 if (len > this->Length)
100 {
101 sz = Length;
102 }
103 memmove(src,(char *)BufferPtr.Get_Buffer() + Index,sz);
104 len -= sz;
105 Index += sz;
106 ret += sz;
107 Length -= sz;
108 src += sz;
109 }
110 if (!len)
111 {
112 break;
113 }
114 len2 = Straw::Get(BufferPtr.Get_Buffer(),BufferPtr.Get_Size());
115 Length = len2;
116 Index = 0;
117 }
118 }
119 return ret;
120 }
121};
122
123class SCRIPTS_API Pipe
124{
125private:
126 Pipe* ChainTo;
127 Pipe* ChainFrom;
128public:
129 Pipe() : ChainTo(0), ChainFrom(0)
130 {
131 }
132 virtual ~Pipe()
133 {
134 if (ChainTo)
135 {
136 ChainTo->ChainFrom = ChainFrom;
137 }
138 if (ChainFrom)
139 {
140 ChainFrom->Put_To(ChainTo);
141 }
142 ChainFrom = 0;
143 ChainTo = 0;
144 }
145 virtual int Flush()
146 {
147 if (ChainTo)
148 {
149 return ChainTo->Flush();
150 }
151 else
152 {
153 return 0;
154 }
155 }
156 virtual int End()
157 {
158 return Flush();
159 }
160 virtual void Put_To(Pipe* pipe)
161 {
162 if (ChainTo != pipe)
163 {
164 if (pipe && pipe->ChainFrom)
165 {
166 pipe->ChainFrom->Put_To(0);
167 pipe->ChainFrom = 0;
168 }
169 if (ChainTo)
170 {
171 ChainTo->ChainFrom = 0;
172 ChainTo->Flush();
173 }
174 ChainTo = pipe;
175 if (pipe)
176 {
177 pipe->ChainFrom = this;
178 }
179 }
180 }
181 virtual int Put(const void *source,int length)
182 {
183 if (ChainTo)
184 {
185 return ChainTo->Put(source,length);
186 }
187 return length;
188 }
189};
190
191class BufferPipe : public Pipe
192{
193private:
194 Buffer BufferPtr;
195 int Index;
196public:
197 BufferPipe(void *data,int size) : BufferPtr(data,size), Index(0)
198 {
199 }
200 virtual int Put(const void *source,int length)
201 {
202 if (BufferPtr.Get_Buffer() && source && length > 0)
203 {
204 int len = length;
205 int size = BufferPtr.Get_Size();
206 if (size)
207 {
208 len = size - Index;
209 if (len > length)
210 {
211 len = length;
212 }
213 }
214 if (len > 0)
215 {
216 memmove((char *)(BufferPtr.Get_Buffer()) + Index,source,len);
217 }
218 return len;
219 }
220 return 0;
221 }
222};
223
224class FilePipe : public Pipe
225{
226private:
227 FileClass* File;
228 bool HasOpened;
229public:
230 FilePipe(FileClass *file) : File(file), HasOpened(false)
231 {
232 }
233 virtual ~FilePipe()
234 {
235 if (File && HasOpened)
236 {
237 HasOpened = false;
238 File->Close();
239 File = 0;
240 }
241 }
242 virtual int End()
243 {
244 int ret = Flush();
245 if (File && HasOpened)
246 {
247 HasOpened = false;
248 File->Close();
249 }
250 return ret;
251 }
252 virtual int Put(const void *source,int length)
253 {
254 if (File && source && length > 0)
255 {
256 if (!File->Is_Open())
257 {
258 HasOpened = true;
259 File->Open(2);
260 }
261 return File->Write((void *)source,length);
262 }
263 else
264 {
265 return 0;
266 }
267 }
268};
269#endif