129 lines
3.2 KiB
C++
Executable File
129 lines
3.2 KiB
C++
Executable File
#ifndef _COMMON_FILEIO_HPP_
|
|
#define _COMMON_FILEIO_HPP_
|
|
#ifndef _COMMON_WINDOWS_HPP_
|
|
#include <common/windows.hpp>
|
|
#endif
|
|
#ifndef _COMMON_STRING_HPP_
|
|
#include <common/string.hpp>
|
|
#endif
|
|
#ifndef _COMMON_STDIO_HPP_
|
|
#include <common/stdio.hpp>
|
|
#endif
|
|
#ifndef _COMMON_INTEL_HPP_
|
|
#include <common/intel.hpp>
|
|
#endif
|
|
|
|
#pragma warning( disable : 26812)
|
|
|
|
// Optimized for fast reading of files.
|
|
|
|
class FileIO
|
|
{
|
|
public:
|
|
enum Mode{ReadOnly,ReadWrite};
|
|
enum ByteOrder{BigEndian,LittleEndian};
|
|
enum SeekFrom{SeekCurrent=SEEK_CUR,SeekEnd=SEEK_END,SeekBeginning=SEEK_SET};
|
|
enum CreationFlags{Create,CreateAlways,None};
|
|
FileIO(void);
|
|
FileIO(String pathFileName,ByteOrder byteOrder=LittleEndian,Mode openMode=ReadOnly,CreationFlags creationFlags=None);
|
|
virtual ~FileIO();
|
|
bool open(String pathFileName,ByteOrder byteOrder=LittleEndian,Mode openMode=ReadOnly,CreationFlags creationFlags=None);
|
|
bool close(void);
|
|
bool rewind(void);
|
|
bool peek(BYTE &value);
|
|
bool read(BYTE &value);
|
|
bool read(WORD &value);
|
|
bool read(DWORD &value);
|
|
size_t read(char *lpBuffer,DWORD lengthData);
|
|
size_t read(BYTE *lpBuffer,DWORD lengthData);
|
|
size_t read(char *lpBuffer,DWORD lengthData,int stopChar);
|
|
bool eof(void)const;
|
|
bool setBufferLength(DWORD length);
|
|
bool write(BYTE value);
|
|
bool write(WORD value);
|
|
bool write(DWORD value);
|
|
bool write(char *lpBuffer,DWORD lengthData);
|
|
FileIO &operator++(void);
|
|
FileIO &operator--(void);
|
|
bool seek(LONG seekOffset,SeekFrom seekFrom);
|
|
BOOL readLine(String &lineString);
|
|
bool writeLine(const String &strLine);
|
|
bool isOkay(void)const;
|
|
DWORD tell(void)const;
|
|
ByteOrder getByteOrder(void)const;
|
|
void setByteOrder(ByteOrder byteOrder);
|
|
DWORD size(void);
|
|
private:
|
|
enum {CarriageReturn=0x0D,LineFeed=0x0A,NullChar=0x00};
|
|
enum {BufferLength=65536}; // This is for the file stream
|
|
enum {InitialBlockLength=65536,BlockIncrement=65536}; // This is for readLine
|
|
FILE *mlpFilePointer;
|
|
ByteOrder mByteOrder;
|
|
DWORD adaptiveBlockLength=InitialBlockLength; // This is for readLine
|
|
};
|
|
|
|
inline
|
|
bool FileIO::isOkay(void)const
|
|
{
|
|
return (mlpFilePointer?true:false);
|
|
}
|
|
|
|
inline
|
|
DWORD FileIO::tell(void)const
|
|
{
|
|
if(!isOkay())return false;
|
|
return ::ftell(mlpFilePointer);
|
|
}
|
|
|
|
inline
|
|
bool FileIO::eof(void)const
|
|
{
|
|
if(!isOkay())return true;
|
|
return ::feof(mlpFilePointer);
|
|
}
|
|
|
|
inline
|
|
bool FileIO::read(BYTE &value)
|
|
{
|
|
if(!isOkay())return false;
|
|
return (sizeof(value)==::fread(&value,1,1,mlpFilePointer)?true:false);
|
|
}
|
|
|
|
inline
|
|
size_t FileIO::read(BYTE *lpBuffer,DWORD lengthData)
|
|
{
|
|
return read((char*)lpBuffer,lengthData);
|
|
}
|
|
|
|
inline
|
|
bool FileIO::write(BYTE value)
|
|
{
|
|
if(!isOkay())return false;
|
|
if(1!=::fwrite(&value,1,1,mlpFilePointer))return false;
|
|
return true;
|
|
}
|
|
|
|
inline
|
|
FileIO::ByteOrder FileIO::getByteOrder(void)const
|
|
{
|
|
return mByteOrder;
|
|
}
|
|
|
|
inline
|
|
void FileIO::setByteOrder(ByteOrder byteOrder)
|
|
{
|
|
mByteOrder=byteOrder;
|
|
}
|
|
|
|
inline
|
|
DWORD FileIO::size(void)
|
|
{
|
|
DWORD currentPosition = tell();
|
|
seek(0,FileIO::SeekFrom::SeekEnd);
|
|
DWORD lastPosition = tell();
|
|
seek(currentPosition, FileIO::SeekFrom::SeekBeginning);
|
|
return lastPosition-currentPosition;
|
|
}
|
|
|
|
#endif
|