leocad/common/lc_file.h

571 lines
9.5 KiB
C
Raw Normal View History

#pragma once
2011-09-07 23:06:51 +02:00
#include <stdio.h>
#include <string.h>
#include "lc_math.h"
2011-09-07 23:06:51 +02:00
2017-12-02 21:22:04 +01:00
#define LC_FOURCC(ch0, ch1, ch2, ch3) (quint32)((quint32)(quint8)(ch0) | ((quint32)(quint8)(ch1) << 8) | \
2024-12-20 00:54:40 +01:00
((quint32)(quint8)(ch2) << 16) | ((quint32)(quint8)(ch3) << 24))
2013-08-17 00:39:47 +02:00
2012-03-23 00:44:56 +01:00
class lcFile
2011-09-07 23:06:51 +02:00
{
public:
lcFile()
{
}
virtual ~lcFile()
{
}
2011-09-07 23:06:51 +02:00
lcFile(const lcFile&) = delete;
lcFile(lcFile&&) = delete;
lcFile& operator=(const lcFile&) = delete;
lcFile& operator=(lcFile&&) = delete;
2012-03-23 00:44:56 +01:00
virtual long GetPosition() const = 0;
2019-05-18 20:33:27 +02:00
virtual void Seek(qint64 Offset, int From) = 0;
virtual size_t GetLength() const = 0;
2011-09-07 23:06:51 +02:00
2012-03-23 00:44:56 +01:00
virtual void Close() = 0;
virtual char* ReadLine(char* Buffer, size_t BufferSize) = 0;
void WriteLine(const char* Buffer)
{
WriteBuffer(Buffer, strlen(Buffer));
}
2011-09-07 23:06:51 +02:00
2016-02-17 00:11:52 +01:00
virtual size_t ReadBuffer(void* Buffer, size_t Bytes) = 0;
virtual size_t WriteBuffer(const void* Buffer, size_t Bytes) = 0;
2012-03-23 00:44:56 +01:00
2017-12-02 21:22:04 +01:00
quint8 ReadU8()
2012-03-23 00:44:56 +01:00
{
2017-12-02 21:22:04 +01:00
quint8 Value;
2012-03-23 00:44:56 +01:00
Read8(&Value, 1);
return Value;
}
2017-12-02 21:22:04 +01:00
size_t ReadU8(quint8* Buffer, size_t Count)
2012-03-23 00:44:56 +01:00
{
return Read8(Buffer, Count);
}
2017-12-02 21:22:04 +01:00
qint8 ReadS8()
2012-03-23 00:44:56 +01:00
{
2017-12-02 21:22:04 +01:00
qint8 Value;
2012-03-23 00:44:56 +01:00
Read8(&Value, 1);
return Value;
}
2017-12-02 21:22:04 +01:00
size_t ReadS8(qint8* Buffer, size_t Count)
2012-03-23 00:44:56 +01:00
{
return Read8(Buffer, Count);
}
2017-12-02 21:22:04 +01:00
quint16 ReadU16()
2012-03-23 00:44:56 +01:00
{
2017-12-02 21:22:04 +01:00
quint16 Value;
2012-03-23 00:44:56 +01:00
Read16(&Value, 1);
return Value;
}
2017-12-02 21:22:04 +01:00
size_t ReadU16(quint16* Buffer, size_t Count)
2012-03-23 00:44:56 +01:00
{
return Read16(Buffer, Count);
}
2017-12-02 21:22:04 +01:00
qint16 ReadS16()
2012-03-23 00:44:56 +01:00
{
2017-12-02 21:22:04 +01:00
qint16 Value;
2012-03-23 00:44:56 +01:00
Read16(&Value, 1);
return Value;
}
2017-12-02 21:22:04 +01:00
size_t ReadS16(qint16* Buffer, size_t Count)
2012-03-23 00:44:56 +01:00
{
return Read16(Buffer, Count);
}
2017-12-02 21:22:04 +01:00
quint32 ReadU32()
2012-03-23 00:44:56 +01:00
{
2017-12-02 21:22:04 +01:00
quint32 Value;
2012-03-23 00:44:56 +01:00
Read32(&Value, 1);
return Value;
}
2017-12-02 21:22:04 +01:00
size_t ReadU32(quint32* Buffer, size_t Count)
2012-03-23 00:44:56 +01:00
{
return Read32(Buffer, Count);
}
2017-12-02 21:22:04 +01:00
qint32 ReadS32()
2012-03-23 00:44:56 +01:00
{
2017-12-02 21:22:04 +01:00
qint32 Value;
2012-03-23 00:44:56 +01:00
Read32(&Value, 1);
return Value;
}
2017-12-02 21:22:04 +01:00
size_t ReadS32(qint32* Buffer, size_t Count)
2012-03-23 00:44:56 +01:00
{
return Read32(Buffer, Count);
}
2017-12-02 21:22:04 +01:00
quint64 ReadU64()
2012-09-08 00:46:40 +02:00
{
2017-12-02 21:22:04 +01:00
quint64 Value;
2012-09-08 00:46:40 +02:00
Read64(&Value, 1);
return Value;
}
2017-12-02 21:22:04 +01:00
size_t ReadU64(quint64* Buffer, size_t Count)
2012-09-08 00:46:40 +02:00
{
return Read64(Buffer, Count);
}
2017-12-02 21:22:04 +01:00
qint64 ReadS64()
2012-09-08 00:46:40 +02:00
{
2017-12-02 21:22:04 +01:00
qint64 Value;
2012-09-08 00:46:40 +02:00
Read64(&Value, 1);
return Value;
}
2017-12-02 21:22:04 +01:00
size_t ReadS64(qint64* Buffer, size_t Count)
2012-09-08 00:46:40 +02:00
{
return Read64(Buffer, Count);
}
2012-03-23 00:44:56 +01:00
float ReadFloat()
{
float Value;
Read32(&Value, 1);
return Value;
}
size_t ReadFloats(float* Buffer, size_t Count)
{
return Read32(Buffer, Count);
}
double ReadDouble()
{
double Value;
Read64(&Value, 1);
return Value;
}
lcVector3 ReadVector3()
{
lcVector3 Vector;
ReadFloats(Vector, 3);
return Vector;
}
2012-03-23 00:44:56 +01:00
size_t ReadDoubles(double* Buffer, size_t Count)
{
return Read64(Buffer, Count);
}
QString ReadQString()
{
2020-03-23 04:18:52 +01:00
const quint32 Size = ReadU32();
char* Buffer = new char[Size];
ReadBuffer(Buffer, Size);
QString String = QString::fromUtf8(Buffer, Size);
delete[] Buffer;
return String;
}
2017-12-02 21:22:04 +01:00
void WriteU8(const quint8& Value)
2012-03-23 00:44:56 +01:00
{
Write8(&Value, 1);
}
2017-12-02 21:22:04 +01:00
size_t WriteU8(const quint8* Buffer, size_t Count)
2012-03-23 00:44:56 +01:00
{
return Write8(Buffer, Count);
}
2017-12-02 21:22:04 +01:00
void WriteS8(const qint8& Value)
2012-03-23 00:44:56 +01:00
{
Write8(&Value, 1);
}
2017-12-02 21:22:04 +01:00
size_t WriteS8(const qint8* Buffer, size_t Count)
2012-03-23 00:44:56 +01:00
{
return Write8(Buffer, Count);
}
2017-12-02 21:22:04 +01:00
void WriteU16(const quint16& Value)
2012-03-23 00:44:56 +01:00
{
Write16(&Value, 1);
}
2017-12-02 21:22:04 +01:00
size_t WriteU16(const quint16* Buffer, size_t Count)
2012-03-23 00:44:56 +01:00
{
return Write16(Buffer, Count);
}
2017-12-02 21:22:04 +01:00
void WriteS16(const qint16& Value)
2012-03-23 00:44:56 +01:00
{
Write16(&Value, 1);
}
2017-12-02 21:22:04 +01:00
size_t WriteS16(const qint16* Buffer, size_t Count)
2012-03-23 00:44:56 +01:00
{
return Write16(Buffer, Count);
}
2011-09-07 23:06:51 +02:00
2017-12-02 21:22:04 +01:00
void WriteU32(const quint32& Value)
2012-03-23 00:44:56 +01:00
{
Write32(&Value, 1);
}
2017-12-02 21:22:04 +01:00
size_t WriteU32(const quint32* Buffer, size_t Count)
2012-03-23 00:44:56 +01:00
{
return Write32(Buffer, Count);
}
2017-12-02 21:22:04 +01:00
void WriteS32(const qint32& Value)
2012-03-23 00:44:56 +01:00
{
Write32(&Value, 1);
}
2017-12-02 21:22:04 +01:00
size_t WriteS32(const qint32* Buffer, size_t Count)
2012-03-23 00:44:56 +01:00
{
return Write32(Buffer, Count);
}
2017-12-02 21:22:04 +01:00
void WriteU64(const quint64& Value)
2012-09-08 00:46:40 +02:00
{
Write64(&Value, 1);
}
2017-12-02 21:22:04 +01:00
size_t WriteU64(const quint64* Buffer, size_t Count)
2012-09-08 00:46:40 +02:00
{
return Write64(Buffer, Count);
}
2017-12-02 21:22:04 +01:00
void WriteS64(const qint64& Value)
2012-09-08 00:46:40 +02:00
{
Write64(&Value, 1);
}
2017-12-02 21:22:04 +01:00
size_t WriteS64(const qint64* Buffer, size_t Count)
2012-09-08 00:46:40 +02:00
{
return Write64(Buffer, Count);
}
2012-03-23 00:44:56 +01:00
void WriteFloat(const float& Value)
{
Write32(&Value, 1);
}
size_t WriteFloats(const float* Buffer, size_t Count)
{
return Write32(Buffer, Count);
}
void WriteDouble(const double& Value)
{
Write64(&Value, 1);
}
size_t WriteDoubles(const double* Buffer, size_t Count)
{
return Write64(Buffer, Count);
}
2011-09-07 23:06:51 +02:00
void WriteVector3(const lcVector3& Vector)
{
WriteFloats(Vector, 3);
}
void WriteQString(const QString& String)
{
QByteArray Data = String.toUtf8();
WriteU32(Data.size());
WriteBuffer(Data, Data.size());
}
2011-09-07 23:06:51 +02:00
protected:
2012-03-23 00:44:56 +01:00
size_t Read8(void* Buffer, size_t Count)
{
return ReadBuffer(Buffer, Count);
}
size_t Read16(void* Buffer, size_t Count)
{
size_t NumRead;
NumRead = ReadBuffer(Buffer, Count * 2) / 2;
2015-01-31 21:38:53 +01:00
#if Q_BYTE_ORDER == Q_BIG_ENDIAN
2017-12-02 21:22:04 +01:00
quint8 Temp[2];
quint8* Bytes = (quint8*)Buffer;
2012-03-23 00:44:56 +01:00
for (size_t Idx = 0; Idx < NumRead; Idx++)
{
Temp[0] = Bytes[0];
Temp[1] = Bytes[1];
*Bytes++ = Temp[1];
*Bytes++ = Temp[0];
}
#endif
return NumRead;
}
size_t Read32(void* Buffer, size_t Count)
{
size_t NumRead;
NumRead = ReadBuffer(Buffer, Count * 4) / 4;
2015-01-31 21:38:53 +01:00
#if Q_BYTE_ORDER == Q_BIG_ENDIAN
2017-12-02 21:22:04 +01:00
quint8 Temp[4];
quint8* Bytes = (quint8*)Buffer;
2012-03-23 00:44:56 +01:00
for (size_t Idx = 0; Idx < NumRead; Idx++)
{
Temp[0] = Bytes[0];
Temp[1] = Bytes[1];
Temp[2] = Bytes[2];
Temp[3] = Bytes[3];
*Bytes++ = Temp[3];
*Bytes++ = Temp[2];
*Bytes++ = Temp[1];
*Bytes++ = Temp[0];
}
#endif
return NumRead;
}
size_t Read64(void* Buffer, size_t Count)
{
size_t NumRead;
NumRead = ReadBuffer(Buffer, Count * 8) / 8;
2015-01-31 21:38:53 +01:00
#if Q_BYTE_ORDER == Q_BIG_ENDIAN
2017-12-02 21:22:04 +01:00
quint8 Temp[8];
quint8* Bytes = (quint8*)Buffer;
2012-03-23 00:44:56 +01:00
for (size_t Idx = 0; Idx < NumRead; Idx++)
{
Temp[0] = Bytes[0];
Temp[1] = Bytes[1];
Temp[2] = Bytes[2];
Temp[3] = Bytes[3];
Temp[4] = Bytes[4];
Temp[5] = Bytes[5];
Temp[6] = Bytes[6];
Temp[7] = Bytes[7];
*Bytes++ = Temp[7];
*Bytes++ = Temp[6];
*Bytes++ = Temp[5];
*Bytes++ = Temp[4];
*Bytes++ = Temp[3];
*Bytes++ = Temp[2];
*Bytes++ = Temp[1];
*Bytes++ = Temp[0];
}
#endif
return NumRead;
}
size_t Write8(const void* Buffer, size_t Count)
{
return WriteBuffer(Buffer, Count);
}
size_t Write16(const void* Buffer, size_t Count)
{
2015-01-31 21:38:53 +01:00
#if Q_BYTE_ORDER == Q_BIG_ENDIAN
2012-03-23 00:44:56 +01:00
size_t BytesWritten = 0;
2017-12-02 21:22:04 +01:00
quint8 Temp[2];
quint8* Bytes = (quint8*)Buffer;
2012-03-23 00:44:56 +01:00
2012-11-25 22:44:11 +01:00
for (size_t Idx = 0; Idx < Count; Idx++)
2012-03-23 00:44:56 +01:00
{
2012-11-25 22:44:11 +01:00
Temp[1] = *Bytes++;
Temp[0] = *Bytes++;
2012-03-23 00:44:56 +01:00
2012-11-25 22:44:11 +01:00
BytesWritten += WriteBuffer(Temp, 2);
2012-03-23 00:44:56 +01:00
}
return BytesWritten / 2;
#else
return WriteBuffer(Buffer, Count * 2) / 2;
#endif
}
size_t Write32(const void* Buffer, size_t Count)
{
2015-01-31 21:38:53 +01:00
#if Q_BYTE_ORDER == Q_BIG_ENDIAN
2012-03-23 00:44:56 +01:00
size_t BytesWritten = 0;
2017-12-02 21:22:04 +01:00
quint8 Temp[4];
quint8* Bytes = (quint8*)Buffer;
2012-03-23 00:44:56 +01:00
2012-11-25 22:44:11 +01:00
for (size_t Idx = 0; Idx < Count; Idx++)
2012-03-23 00:44:56 +01:00
{
2012-11-25 22:44:11 +01:00
Temp[3] = *Bytes++;
Temp[2] = *Bytes++;
Temp[1] = *Bytes++;
Temp[0] = *Bytes++;
2012-03-23 00:44:56 +01:00
2012-11-25 22:44:11 +01:00
BytesWritten += WriteBuffer(Temp, 4);
2012-03-23 00:44:56 +01:00
}
return BytesWritten / 4;
#else
return WriteBuffer(Buffer, Count * 4);
#endif
}
size_t Write64(const void* Buffer, size_t Count)
{
2015-01-31 21:38:53 +01:00
#if Q_BYTE_ORDER == Q_BIG_ENDIAN
2012-03-23 00:44:56 +01:00
size_t BytesWritten = 0;
2017-12-02 21:22:04 +01:00
quint8 Temp[8];
quint8* Bytes = (quint8*)Buffer;
2012-03-23 00:44:56 +01:00
2012-11-25 22:44:11 +01:00
for (size_t Idx = 0; Idx < Count; Idx++)
2012-03-23 00:44:56 +01:00
{
2012-11-25 22:44:11 +01:00
Temp[7] = *Bytes++;
Temp[6] = *Bytes++;
Temp[5] = *Bytes++;
Temp[4] = *Bytes++;
Temp[3] = *Bytes++;
Temp[2] = *Bytes++;
Temp[1] = *Bytes++;
Temp[0] = *Bytes++;
BytesWritten += WriteBuffer(Temp, 8);
2012-03-23 00:44:56 +01:00
}
return BytesWritten / 8;
#else
return WriteBuffer(Buffer, Count * 8);
#endif
}
2011-09-07 23:06:51 +02:00
};
2012-03-23 00:44:56 +01:00
class lcMemFile : public lcFile
2011-09-07 23:06:51 +02:00
{
public:
2012-03-23 00:44:56 +01:00
lcMemFile();
~lcMemFile();
lcMemFile(const lcMemFile&) = delete;
lcMemFile(lcMemFile&&) = delete;
lcMemFile& operator=(const lcMemFile&) = delete;
lcMemFile& operator=(lcMemFile&&) = delete;
2012-03-23 00:44:56 +01:00
long GetPosition() const override;
2019-05-18 20:33:27 +02:00
void Seek(qint64 Offset, int From) override;
2012-03-23 00:44:56 +01:00
void SetLength(size_t NewLength);
size_t GetLength() const override;
2011-09-07 23:06:51 +02:00
void Close() override;
2011-09-07 23:06:51 +02:00
char* ReadLine(char* Buffer, size_t BufferSize) override;
size_t ReadBuffer(void* Buffer, size_t Bytes) override;
size_t WriteBuffer(const void* Buffer, size_t Bytes) override;
2011-09-07 23:06:51 +02:00
2012-03-23 00:44:56 +01:00
void GrowFile(size_t NewLength);
size_t mGrowBytes;
size_t mPosition;
size_t mBufferSize;
size_t mFileSize;
unsigned char* mBuffer;
2011-09-07 23:06:51 +02:00
};
2012-03-23 00:44:56 +01:00
class lcDiskFile : public lcFile
2011-09-07 23:06:51 +02:00
{
public:
lcDiskFile()
{
}
2012-03-23 00:44:56 +01:00
lcDiskFile(const QString& FileName)
: mFile(FileName)
{
}
~lcDiskFile()
{
Close();
}
lcDiskFile(const lcDiskFile&) = delete;
lcDiskFile(lcDiskFile&&) = delete;
lcDiskFile& operator=(const lcDiskFile&) = delete;
lcDiskFile& operator=(lcDiskFile&&) = delete;
void SetFileName(const QString& FileName)
{
mFile.setFileName(FileName);
}
long GetPosition() const override
{
return mFile.pos();
}
2019-05-18 20:33:27 +02:00
void Seek(qint64 Offset, int From) override
{
switch (From)
{
case SEEK_CUR:
Offset += mFile.pos();
break;
case SEEK_END:
Offset += mFile.size();
break;
}
mFile.seek(Offset);
}
size_t GetLength() const override
{
return mFile.size();
}
void Close() override
{
mFile.close();
}
2011-09-07 23:06:51 +02:00
char* ReadLine(char* Buffer, size_t BufferSize) override
{
2020-03-23 04:18:52 +01:00
const qint64 LineLength = mFile.readLine(Buffer, BufferSize);
return LineLength != -1 ? Buffer : nullptr;
}
2011-09-07 23:06:51 +02:00
size_t ReadBuffer(void* Buffer, size_t Bytes) override
{
return mFile.read((char*)Buffer, Bytes);
}
2012-03-23 00:44:56 +01:00
size_t WriteBuffer(const void* Buffer, size_t Bytes) override
{
return mFile.write((const char*)Buffer, Bytes);
}
2012-03-23 00:44:56 +01:00
bool Open(QIODevice::OpenMode Flags)
{
return mFile.open(Flags);
}
2012-03-23 00:44:56 +01:00
protected:
QFile mFile;
2011-09-07 23:06:51 +02:00
};