Tiberian Technologies Scripts Reference Revision: 9000
Loading...
Searching...
No Matches
RawFileClass.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_RAWFILECLASS_H
13#define TT_INCLUDE_RAWFILECLASS_H
14
15
16
17#include "engine_io.h"
18#include "engine_math.h"
19
20
21class RawFileClass : public FileClass {
22private:
23 int Rights; // 0004
24 int BiasStart; // 0008
25 int BiasLength; // 000C
26 void* Handle; // 0010
27 StringClass Filename; // 0014
28 unsigned short Date; // 0018
29 unsigned short Time; // 001C
30public:
31 bool Is_Hash_Checked()
32 {
33 return Is_Biased();
34 }
35 bool Is_Biased()
36 {
37 return BiasLength != -1;
38 }
39 RawFileClass()
40 {
41 BiasLength = -1;
42 Handle = INVALID_HANDLE_VALUE;
43 Rights = 0;
44 BiasStart = 0;
45 Date = 0;
46 Time = 0;
47 Filename = "";
48 }
49 RawFileClass(const char* filename)
50 {
51 BiasLength = -1;
52 Handle = INVALID_HANDLE_VALUE;
53 Rights = 0;
54 BiasStart = 0;
55 Date = 0;
56 Time = 0;
57 Filename = filename;
58 }
59 int Transfer_Block_Size()
60 {
61 return -11;
62 }
63 void Reset()
64 {
65 Close();
66 Filename = "";
67 }
68 ~RawFileClass()
69 {
70 Reset();
71 }
72 const char *File_Name()
73 {
74 return Filename;
75 }
76 const char *Set_Name(const char* name)
77 {
78 Reset();
79 if (name)
80 {
81 Bias(0,-1);
82 Filename = name;
83 }
84 return 0;
85 }
86 bool Create()
87 {
88 Close();
89 if (!Open(2))
90 {
91 return false;
92 }
93 if (BiasLength != -1)
94 {
95 Seek(0,0);
96 }
97 Close();
98 return true;
99 }
100 bool Delete()
101 {
102 Close();
103 if (Filename.Is_Empty())
104 {
105 Error(2,0,0);
106 }
107 if (!Is_Available(0))
108 {
109 return false;
110 }
111 if (!DeleteFileA(Filename))
112 {
113 Error(GetLastError(),0,Filename);
114 return false;
115 }
116 return true;
117 }
118 bool Is_Available(int _handle)
119 {
120 if (Filename.Is_Empty())
121 return false;
122
123 if (Is_Open())
124 return true;
125
126 if (_handle)
127 {
128 Open(1);
129 Close();
130 return true;
131 }
132
133 int attr = GetFileAttributesA(Filename);
134 return attr != INVALID_FILE_ATTRIBUTES && !(attr & FILE_ATTRIBUTE_DIRECTORY);
135 }
136 bool Is_Open()
137 {
138 if (Handle != INVALID_HANDLE_VALUE)
139 {
140 return true;
141 }
142 return false;
143 }
144 int Open(const char* name, int mode)
145 {
146 Set_Name(name);
147 return Open(mode);
148 }
149 int Open(int mode)
150 {
151 Close();
152 if (Filename.Is_Empty())
153 {
154 Error(2,0,0);
155 }
156 Rights = mode;
157 switch (Rights)
158 {
159 case 2:
160 Handle = CreateFileA(Filename,GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
161 break;
162 case 1:
163 Handle = CreateFileA(Filename,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,0);
164 break;
165 case 0:
166 Handle = CreateFileA(Filename,GENERIC_READ|GENERIC_WRITE,0,0,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
167 break;
168 default:
169 errno = EINVAL;
170 }
171 if ((BiasStart) && (BiasLength != -1))
172 {
173 Seek(0,0);
174 }
175 if (Handle != INVALID_HANDLE_VALUE)
176 {
177 return true;
178 }
179 return false;
180 }
181
182
183 int RawFileClass::Read(void* buffer, int bytesToRead)
184 {
185 DWORD bytesRead = 0;
186 if (!Is_Open())
187 {
188 if (Open(1))
189 {
190 bytesRead = Read(buffer, bytesToRead);
191 Close();
192 }
193 }
194 else
195 {
196 if (BiasLength != -1)
197 {
198 int maxBytesToRead = BiasLength - Tell();
199 if (bytesToRead > maxBytesToRead)
200 bytesToRead = maxBytesToRead;
201 }
202
203 if (!ReadFile(Handle, buffer, bytesToRead, &bytesRead, NULL))
204 Error(GetLastError(), 1, Filename);
205 }
206
207 return bytesRead;
208 }
209
210
211 int Seek(int pos, int dir)
212 {
213 if (BiasLength != -1)
214 {
215 switch (dir)
216 {
217 case 1: pos += Raw_Seek(0, 1) - BiasStart; break;
218 case 2: pos += BiasLength; break;
219 }
220
221 int result = Raw_Seek(BiasStart + clamp(pos, 0, BiasLength), 0);
222 if (result != -1)
223 result -= BiasStart;
224
225 TT_ASSERT(result <= BiasLength);
226 return result;
227 }
228 else
229 return Raw_Seek(pos, dir);
230 }
231
232
233 int Raw_Seek(int pos, int dir)
234 {
235 if (!Is_Open())
236 Error(9, 0, Filename);
237
238 int seek = SetFilePointer(Handle, pos, 0, dir);
239 if (seek == -1)
240 Error(GetLastError(), 0, Filename);
241
242 return seek;
243 }
244
245
246 int Size()
247 {
248 if (BiasLength != -1)
249 return BiasLength;
250 else
251 {
252 int size = -1;
253 if (!Is_Open())
254 {
255 if (Open(1))
256 {
257 size = Size();
258 Close();
259 }
260 }
261 else
262 {
263 size = GetFileSize(Handle,0);
264 if (size == -1)
265 Error(GetLastError(), 0, Filename);
266 }
267 return size;
268 }
269 }
270
271
272 int Write(void* buffer, int size)
273 {
274 DWORD bytesWritten = 0;
275 if (!Is_Open())
276 {
277 if (Open(2))
278 {
279 bytesWritten = Write(buffer, size);
280 Close();
281 }
282 }
283 else
284 {
285 if (!WriteFile(Handle, buffer, size, &bytesWritten, 0))
286 Error(GetLastError(), 0, Filename);
287
288 if (BiasLength != -1)
289 {
290 if (Raw_Seek(0, 1) > BiasStart + BiasLength)
291 {
292 BiasLength = Raw_Seek(0, 1) - BiasStart;
293 }
294 }
295 }
296 return bytesWritten;
297 }
298
299
300 void Close()
301 {
302 if (Is_Open())
303 {
304 if (!CloseHandle(Handle))
305 Error(GetLastError(), 0, Filename);
306
307 Handle = INVALID_HANDLE_VALUE;
308 }
309 }
310
311
312 unsigned long Get_Date_Time()
313 {
314 BY_HANDLE_FILE_INFORMATION info;
315 unsigned short dostime;
316 unsigned short dosdate;
317 if (GetFileInformationByHandle(Handle,&info))
318 {
319 FileTimeToDosDateTime(&info.ftLastWriteTime, &dosdate, &dostime);
320 return dosdate << 0x10 | dostime;
321 }
322 return 0;
323 }
324 bool Set_Date_Time(unsigned long datetime)
325 {
326 BY_HANDLE_FILE_INFORMATION info;
327 FILETIME filetime;
328 if (Is_Open())
329 {
330 if (GetFileInformationByHandle(Handle,&info))
331 {
332 if (DosDateTimeToFileTime((WORD)(datetime >> 0x10),(WORD)datetime,&filetime))
333 {
334 if (SetFileTime(Handle,&info.ftCreationTime,&filetime,&filetime))
335 {
336 return true;
337 }
338 }
339 }
340 }
341 return false;
342 }
343 void Error(int a, int b, const char *c)
344 {
345 }
346 HANDLE Get_File_Handle()
347 {
348 return Handle;
349 }
350 void Bias(int start, int length)
351 {
352 if (start == 0)
353 {
354 TT_ASSERT(length == -1);
355 BiasStart = 0;
356 BiasLength = -1;
357 }
358 else
359 {
360 BiasLength = Size();
361 BiasStart += start;
362 if (length != -1)
363 {
364 if (length < BiasLength)
365 BiasLength = length;
366
367 if (BiasLength < 0)
368 BiasLength = 0;
369 }
370 }
371 }
372 virtual void Attach(HANDLE handle,int rights)
373 {
374 Reset();
375 Handle = handle;
376 Rights = rights;
377 BiasStart = 0;
378 BiasLength = -1;
379 Date = 0;
380 Time = 0;
381 }
382 virtual void Detach()
383 {
384 Rights = 0;
385 Handle = INVALID_HANDLE_VALUE;
386 BiasStart = 0;
387 BiasLength = -1;
388 Date = 0;
389 Time = 0;
390 }
391}; // 0020
392
393class TextFileClass : public RawFileClass {
394public:
395 TextFileClass()
396 {
397 }
398 ~TextFileClass()
399 {
400 }
401 bool Read_Line(StringClass &str)
402 {
403 str = "";
404 char buf[64];
405 memset(buf,0,sizeof(buf));
406 bool b = false;
407 do
408 {
409 int sz = Read(buf,63);
410 b = (sz == 63);
411 if (sz > 0)
412 {
413 for (int i = 0;i < sz;i++)
414 {
415 if (buf[i] == '\n')
416 {
417 buf[i + 1] = 0;
418 Seek(i - sz + 1,1);
419 b = false;
420 break;
421 }
422 }
423 str += buf;
424 }
425 } while (b);
426 strtrim(str.Peek_Buffer());
427 return false;
428 }
429 bool Write_Line(StringClass const &str)
430 {
431 int len = str.Get_Length();
432 if (Write((void *)str.Peek_Buffer(),len) == len)
433 {
434 return Write("\r\n",2) == 2;
435 }
436 return false;
437 }
438};
439
440#endif