195 lines
4.6 KiB
C++
195 lines
4.6 KiB
C++
#ifndef _FILEIO_FILEIO_HPP_
|
|
#define _FILEIO_FILEIO_HPP_
|
|
#ifndef _COMMON_WINDOWS_HPP_
|
|
#include <common/windows.hpp>
|
|
#endif
|
|
|
|
class FileInfo;
|
|
|
|
extern "C"
|
|
{
|
|
int _cdecl FileOpen(char *pPathFileName,FileInfo *pFileInfo,int access,int share,int open,int attributes);
|
|
int _cdecl FileClose(FileInfo *pFileInfo);
|
|
int _cdecl FileRead(FileInfo *pFileInfo,char *ptrChar);
|
|
int _cdecl FileReadLength(FileInfo *pFileInfo,char *ptrChar,int length);
|
|
int _cdecl FileReadLine(FileInfo *pFileInfo,char *ptrChar);
|
|
int _cdecl FileWriteLine(FileInfo *pFileInfo,char *ptrChar);
|
|
int _cdecl FileWrite(FileInfo *pFileInfo,char *ptrByte);
|
|
int _cdecl FileWriteLength(FileInfo *pFileInfo,char *ptrByte,int length);
|
|
int _cdecl FileFlush(FileInfo *pFileInfo);
|
|
int _cdecl FileSize(FileInfo *pFileInfo);
|
|
int _cdecl FileSeek(FileInfo *pFileInfo,int offset,int whence);
|
|
}
|
|
|
|
class FileInfo
|
|
{
|
|
public:
|
|
FileInfo(void);
|
|
~FileInfo(); // cannot be virtual
|
|
enum{MaxLength=0x800}; // we're using a 2k cache here on reads and writes
|
|
BYTE mBuffer[MaxLength];
|
|
HANDLE mhFileHandle;
|
|
DWORD mBufferIndex;
|
|
DWORD mlpBufferPointer;
|
|
};
|
|
|
|
inline
|
|
FileInfo::FileInfo(void)
|
|
: mhFileHandle(INVALID_HANDLE_VALUE), mBufferIndex(0), mlpBufferPointer(0)
|
|
{
|
|
}
|
|
|
|
inline
|
|
FileInfo::~FileInfo()
|
|
{
|
|
}
|
|
|
|
class FileIO
|
|
{
|
|
public:
|
|
enum Whence{SeekCurr=FILE_CURRENT,SeekBeg=FILE_BEGIN,SeekEnd=FILE_END};
|
|
enum Open{CreateNew=CREATE_NEW,CreateAlways=CREATE_ALWAYS,OpenExisting=OPEN_EXISTING,
|
|
OpenAlways=OPEN_ALWAYS,TruncateExisting=TRUNCATE_EXISTING};
|
|
enum Share{FileShareRead=FILE_SHARE_READ,FileShareWrite=FILE_SHARE_WRITE,FileShareDelete=FILE_SHARE_DELETE};
|
|
enum Access{GenericRead=GENERIC_READ,GenericWrite=GENERIC_WRITE,
|
|
GenericExecute=GENERIC_EXECUTE,GenericAll=GENERIC_ALL};
|
|
enum Attributes{ReadOnly=FILE_ATTRIBUTE_READONLY,Hidden=FILE_ATTRIBUTE_HIDDEN,
|
|
System=FILE_ATTRIBUTE_SYSTEM,Directory=FILE_ATTRIBUTE_DIRECTORY,
|
|
Archive=FILE_ATTRIBUTE_ARCHIVE,Normal=FILE_ATTRIBUTE_NORMAL,
|
|
Temporary=FILE_ATTRIBUTE_TEMPORARY,Compressed=FILE_ATTRIBUTE_COMPRESSED,
|
|
Offline=FILE_ATTRIBUTE_OFFLINE};
|
|
FileIO(void);
|
|
virtual ~FileIO();
|
|
int open(char *pPathFileName,Access access=GenericRead,Share share=FileShareRead,Open open=OpenExisting,Attributes attributes=Archive);
|
|
int open(const String &strPathFileName,Access access=GenericRead,Share share=FileShareRead,Open open=OpenExisting,Attributes attributes=Archive);
|
|
int close(void);
|
|
int size(void);
|
|
int read(char *ptrByte);
|
|
int read(void *ptrByte,int length);
|
|
int write(char *ptrByte);
|
|
int write(void *ptrByte,int length);
|
|
int write(int intData);
|
|
int readLine(char *strLine);
|
|
int writeLine(char *strByte);
|
|
int flush(void);
|
|
int seek(int offset,Whence whence);
|
|
int tell(void);
|
|
int rewind(void);
|
|
BOOL isOkay(void)const;
|
|
private:
|
|
FileInfo mFileInfo;
|
|
};
|
|
|
|
inline
|
|
FileIO::FileIO(void)
|
|
{
|
|
}
|
|
|
|
inline
|
|
FileIO::~FileIO()
|
|
{
|
|
close();
|
|
}
|
|
|
|
inline
|
|
int FileIO::open(char *pPathFileName,Access access,Share share,Open open,Attributes attributes)
|
|
{
|
|
close();
|
|
return !(INVALID_HANDLE_VALUE==(HANDLE)::FileOpen(pPathFileName,&mFileInfo,access,share,open,attributes));
|
|
}
|
|
|
|
inline
|
|
int FileIO::open(const String &strPathFileName,Access access,Share share,Open open,Attributes attributes)
|
|
{
|
|
String strTempFileName(strPathFileName);
|
|
close();
|
|
return !(INVALID_HANDLE_VALUE==(HANDLE)::FileOpen((char*)strTempFileName,&mFileInfo,access,share,open,attributes));
|
|
}
|
|
|
|
inline
|
|
int FileIO::close(void)
|
|
{
|
|
flush();
|
|
return ::FileClose(&mFileInfo);
|
|
}
|
|
|
|
inline
|
|
int FileIO::size(void)
|
|
{
|
|
return ::FileSize(&mFileInfo);
|
|
}
|
|
|
|
inline
|
|
int FileIO::read(char *ptrByte)
|
|
{
|
|
return ::FileRead(&mFileInfo,ptrByte);
|
|
}
|
|
|
|
inline
|
|
int FileIO::read(void *ptrByte,int length)
|
|
{
|
|
return ::FileReadLength(&mFileInfo,(char*)ptrByte,length);
|
|
}
|
|
|
|
inline
|
|
int FileIO::write(char *ptrByte)
|
|
{
|
|
return ::FileWrite(&mFileInfo,ptrByte);
|
|
}
|
|
|
|
inline
|
|
int FileIO::write(int intData)
|
|
{
|
|
return write((char*)&intData,sizeof(intData));
|
|
}
|
|
|
|
inline
|
|
int FileIO::write(void *ptrByte,int length)
|
|
{
|
|
return ::FileWriteLength(&mFileInfo,(char*)ptrByte,length);
|
|
}
|
|
|
|
inline
|
|
int FileIO::readLine(char *strLine)
|
|
{
|
|
return ::FileReadLine(&mFileInfo,strLine);
|
|
}
|
|
|
|
inline
|
|
int FileIO::writeLine(char *strByte)
|
|
{
|
|
return ::FileWriteLine(&mFileInfo,strByte);
|
|
}
|
|
|
|
inline
|
|
int FileIO::flush(void)
|
|
{
|
|
return ::FileFlush(&mFileInfo);
|
|
}
|
|
|
|
inline
|
|
int FileIO::seek(int offset,Whence whence)
|
|
{
|
|
return ::FileSeek(&mFileInfo,offset,whence);
|
|
}
|
|
|
|
inline
|
|
int FileIO::tell(void)
|
|
{
|
|
if(!isOkay())return 0;
|
|
flush();
|
|
return seek(0,FileIO::SeekCurr);
|
|
}
|
|
|
|
inline
|
|
int FileIO::rewind(void)
|
|
{
|
|
return seek(0L,SeekBeg);
|
|
}
|
|
|
|
inline
|
|
BOOL FileIO::isOkay(void)const
|
|
{
|
|
return !(INVALID_HANDLE_VALUE==mFileInfo.mhFileHandle);
|
|
}
|
|
#endif |