Code: Select all
#include <stdio.h>
#include <string>
typedef void* Object;
typedef unsigned char Byte;
typedef signed char SByte;
typedef unsigned short UInt16;
typedef signed short Int16;
typedef unsigned long UInt32;
typedef signed long Int32;
typedef unsigned __int64 UInt64;
typedef signed __int64 Int64;
typedef const char* String;
typedef enum t_SeekOrigin
{
SeekOriginBegin,
SeekOriginCurrent,
SeekOriginEnd,
}SeekOrigin;
class BinaryStream
{
private:
FILE *BinRW;
public:
BinaryStream(String filename)
{
this->Open(filename);
}
~BinaryStream()
{
delete BinRW;
}
FILE* BaseStream()
{
return BinBW;
}
void BaseStream(FILE* stream)
{
if(BinRW)
BinRW = stream;
}
void Open(String filename)
{
if(BinRW)
{
this->Flush();
this->Close();
BinRW = NULL;
}
BinRW = fopen(filename, "r+b");
}
void Close()
{
fclose(BinRW);
}
void Flush()
{
fflush(BinRW);
}
void Seek(Int32 position, SeekOrigin origin)
{
fseek(BinRW, position, origin);
}
Int32 Position()
{
return ftell(BinRW);
}
void Position(long pos)
{
this->Seek(pos, SeekOrigin::SeekOriginBegin);
}
Int32 FileLength()
{
Int32 pos = GetPosition();
this->Seek(0, 2);
int end = GetPosition();
this->Seek(pos);
return end;
}
template<class T> T ReadObjectByValue()
{
T value;
fread(&value, sizeof(T), 1 BinBW);
return value;
}
template<class T> T* ReadObjectByReference()
{
T* value;
fread(value, sizeof(T), 1 BinBW);
return value;
}
Int64 ReadInt64()
{
Int64 value;
fread(&value, sizeof(Int64), 1, BinRW);
return value;
}
UInt64 ReadUInt64()
{
UInt64 value;
fread(&value, sizeof(UInt64), 1, BinRW);
return value;
}
Int32 ReadInt32()
{
Int32 value;
fread(&value, sizeof(Int32), 1, BinRW);
return value;
}
UInt32 ReadUInt32()
{
UInt32 value;
fread(&value, sizeof(Int32), 1, BinRW);
return value;
}
Int16 ReadInt16()
{
Int16 value;
fread(&value, sizeof(Int16), 1, BinRW);
return value;
}
UInt16 ReadUInt16()
{
UInt16 value;
fread(&value, sizeof(UInt16), 1, BinRW);
return value;
}
Byte ReadByte()
{
Byte value;
fread(&value, sizeof(Byte), 1, BinRW);
return value;
}
SByte ReadSByte()
{
SByte value;
fread(&value, sizeof(SByte), 1, BinRW);
return value;
}
char *ReadChars(int size)
{
char* value;
fread(&value, size, 1, BinRW);
return value;
}
template<class T> void WriteObjectByValue(T value)
{
fwrite(&value, sizeof(T), 1 BinBW);
}
template<class T> void WriteObjectByReference(T* value)
{
fwrite(value, sizeof(T), 1 BinBW);
}
void Write(Int64 value)
{
fwrite(&value, sizeof(Int64), 1, BinRW);
}
void Write(UInt64 value)
{
fwrite(&value, sizeof(UInt64), 1, BinRW);
}
void Write(Int32 value)
{
fwrite(&value, size(Int32), 1, BinRW);
}
void Write(UInt32 value)
{
fwrite(&value, sizeof(UInt32), 1, BinRW);
}
void Write(Int16 value)
{
fwrite(&value, sizeof(Int16), 1, BinRW);
}
void Write(UInt16 value)
{
fwrite(&value, sizeof(UInt16), 1, BinRW);
}
void Write(Byte value)
{
fwrite(&value, sizeof(Byte), 1, BinRW);
}
void Write(SByte value)
{
fwrite(&value, sizeof(SByte), 1, BinRW);
}
void Write(String value, int size)
{
fwrite(&value, size, 1, BinRW);
}
void Write(String value)
{
fwrite(&value, strlen(value), 1, BinRW);
}
};