12#ifndef TT_INCLUDE_RAWFILECLASS_H
13#define TT_INCLUDE_RAWFILECLASS_H
18#include "engine_math.h"
21class RawFileClass :
public FileClass {
31 bool Is_Hash_Checked()
37 return BiasLength != -1;
42 Handle = INVALID_HANDLE_VALUE;
49 RawFileClass(
const char* filename)
52 Handle = INVALID_HANDLE_VALUE;
59 int Transfer_Block_Size()
72 const char *File_Name()
76 const char *Set_Name(
const char* name)
103 if (Filename.Is_Empty())
107 if (!Is_Available(0))
111 if (!DeleteFileA(Filename))
113 Error(GetLastError(),0,Filename);
118 bool Is_Available(
int _handle)
120 if (Filename.Is_Empty())
133 int attr = GetFileAttributesA(Filename);
134 return attr != INVALID_FILE_ATTRIBUTES && !(attr & FILE_ATTRIBUTE_DIRECTORY);
138 if (Handle != INVALID_HANDLE_VALUE)
144 int Open(
const char* name,
int mode)
152 if (Filename.Is_Empty())
160 Handle = CreateFileA(Filename,GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
163 Handle = CreateFileA(Filename,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,0);
166 Handle = CreateFileA(Filename,GENERIC_READ|GENERIC_WRITE,0,0,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
171 if ((BiasStart) && (BiasLength != -1))
175 if (Handle != INVALID_HANDLE_VALUE)
183 int RawFileClass::Read(
void* buffer,
int bytesToRead)
190 bytesRead = Read(buffer, bytesToRead);
196 if (BiasLength != -1)
198 int maxBytesToRead = BiasLength - Tell();
199 if (bytesToRead > maxBytesToRead)
200 bytesToRead = maxBytesToRead;
203 if (!ReadFile(Handle, buffer, bytesToRead, &bytesRead, NULL))
204 Error(GetLastError(), 1, Filename);
211 int Seek(
int pos,
int dir)
213 if (BiasLength != -1)
217 case 1: pos += Raw_Seek(0, 1) - BiasStart;
break;
218 case 2: pos += BiasLength;
break;
221 int result = Raw_Seek(BiasStart + clamp(pos, 0, BiasLength), 0);
225 TT_ASSERT(result <= BiasLength);
229 return Raw_Seek(pos, dir);
233 int Raw_Seek(
int pos,
int dir)
236 Error(9, 0, Filename);
238 int seek = SetFilePointer(Handle, pos, 0, dir);
240 Error(GetLastError(), 0, Filename);
248 if (BiasLength != -1)
263 size = GetFileSize(Handle,0);
265 Error(GetLastError(), 0, Filename);
272 int Write(
void* buffer,
int size)
274 DWORD bytesWritten = 0;
279 bytesWritten = Write(buffer, size);
285 if (!WriteFile(Handle, buffer, size, &bytesWritten, 0))
286 Error(GetLastError(), 0, Filename);
288 if (BiasLength != -1)
290 if (Raw_Seek(0, 1) > BiasStart + BiasLength)
292 BiasLength = Raw_Seek(0, 1) - BiasStart;
304 if (!CloseHandle(Handle))
305 Error(GetLastError(), 0, Filename);
307 Handle = INVALID_HANDLE_VALUE;
312 unsigned long Get_Date_Time()
314 BY_HANDLE_FILE_INFORMATION info;
315 unsigned short dostime;
316 unsigned short dosdate;
317 if (GetFileInformationByHandle(Handle,&info))
319 FileTimeToDosDateTime(&info.ftLastWriteTime, &dosdate, &dostime);
320 return dosdate << 0x10 | dostime;
324 bool Set_Date_Time(
unsigned long datetime)
326 BY_HANDLE_FILE_INFORMATION info;
330 if (GetFileInformationByHandle(Handle,&info))
332 if (DosDateTimeToFileTime((WORD)(datetime >> 0x10),(WORD)datetime,&filetime))
334 if (SetFileTime(Handle,&info.ftCreationTime,&filetime,&filetime))
343 void Error(
int a,
int b,
const char *c)
346 HANDLE Get_File_Handle()
350 void Bias(
int start,
int length)
354 TT_ASSERT(length == -1);
364 if (length < BiasLength)
372 virtual void Attach(HANDLE handle,
int rights)
382 virtual void Detach()
385 Handle = INVALID_HANDLE_VALUE;
393class TextFileClass :
public RawFileClass {
401 bool Read_Line(StringClass &str)
405 memset(buf,0,
sizeof(buf));
409 int sz = Read(buf,63);
413 for (
int i = 0;i < sz;i++)
426 strtrim(str.Peek_Buffer());
429 bool Write_Line(StringClass
const &str)
431 int len = str.Get_Length();
432 if (Write((
void *)str.Peek_Buffer(),len) == len)
434 return Write(
"\r\n",2) == 2;