최종완료보고 버전
This commit is contained in:
@@ -0,0 +1,130 @@
|
||||
|
||||
#ifndef FILE__H_
|
||||
#define FILE__H_
|
||||
|
||||
#ifdef _WIN32
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
|
||||
class HFile
|
||||
{
|
||||
public:
|
||||
enum
|
||||
{
|
||||
FMODE_CREATE = 0x0001,
|
||||
FMODE_RDONLY = 0x0002,
|
||||
FMODE_WRONLY = 0x0004,
|
||||
FMODE_RW = 0x0008
|
||||
};
|
||||
|
||||
static off_t GetSize(const char * pszFileName)
|
||||
{
|
||||
struct stat statTemp;
|
||||
stat(pszFileName, &statTemp);
|
||||
|
||||
return statTemp.st_size;
|
||||
}
|
||||
|
||||
////
|
||||
|
||||
HFile()
|
||||
{
|
||||
m_fdFile = -1;
|
||||
}
|
||||
|
||||
~HFile()
|
||||
{
|
||||
Close();
|
||||
}
|
||||
|
||||
inline int GetFile();
|
||||
|
||||
inline bool Open(const char * pszFileName, int nMode, bool bIsContinue = false, bool bIsBinary = true);
|
||||
inline void Close();
|
||||
|
||||
inline int Read(void * buffer, unsigned int count);
|
||||
inline int ReadString(char * buffer, unsigned int count);
|
||||
inline int Write(const void * buffer, unsigned int count);
|
||||
|
||||
private:
|
||||
int m_fdFile;
|
||||
};
|
||||
|
||||
inline int HFile::GetFile() { return m_fdFile; }
|
||||
|
||||
inline bool HFile::Open(const char * pszFileName, int nMode, bool bIsContinue, bool bIsBinary)
|
||||
{
|
||||
int oflag = 0;
|
||||
|
||||
if ((nMode & FMODE_CREATE) == FMODE_CREATE)
|
||||
oflag = O_CREAT;
|
||||
|
||||
if ((nMode & FMODE_RDONLY) == FMODE_RDONLY)
|
||||
oflag |= O_RDONLY;
|
||||
else if ((nMode & FMODE_WRONLY) == FMODE_WRONLY)
|
||||
oflag |= O_WRONLY;
|
||||
else
|
||||
oflag |= O_RDWR;
|
||||
|
||||
if (bIsContinue == false && ((nMode & FMODE_CREATE) == FMODE_CREATE))
|
||||
oflag |= O_TRUNC;
|
||||
|
||||
#ifdef _WIN32
|
||||
if (bIsBinary)
|
||||
oflag |= O_BINARY;
|
||||
else
|
||||
oflag |= O_TEXT;
|
||||
|
||||
m_fdFile = open(pszFileName, oflag, S_IREAD | S_IWRITE);
|
||||
#else
|
||||
m_fdFile = open(pszFileName, oflag, S_IREAD | S_IWRITE | S_IRGRP | S_IROTH);
|
||||
#endif
|
||||
|
||||
if (m_fdFile == -1)
|
||||
return false;
|
||||
|
||||
if (bIsContinue && ((nMode & FMODE_WRONLY) == FMODE_WRONLY || (nMode & FMODE_RW) == FMODE_RW))
|
||||
lseek(m_fdFile, 0L, SEEK_END);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
inline void HFile::Close()
|
||||
{
|
||||
if (m_fdFile != -1)
|
||||
{
|
||||
close(m_fdFile);
|
||||
m_fdFile = -1;
|
||||
}
|
||||
}
|
||||
|
||||
inline int HFile::Read(void * buffer, unsigned int count)
|
||||
{
|
||||
if (m_fdFile == -1)
|
||||
return -1;
|
||||
|
||||
return read(m_fdFile, buffer, count);
|
||||
}
|
||||
|
||||
inline int HFile::ReadString(char * buffer, unsigned int count)
|
||||
{
|
||||
if (m_fdFile == -1)
|
||||
return -1;
|
||||
|
||||
int nResult = read(m_fdFile, buffer, count);
|
||||
if (nResult > 0)
|
||||
buffer[nResult] = '\0';
|
||||
|
||||
return nResult;
|
||||
}
|
||||
|
||||
inline int HFile::Write(const void * buffer, unsigned int count)
|
||||
{
|
||||
if (m_fdFile == -1)
|
||||
return -1;
|
||||
|
||||
return write(m_fdFile, buffer, count);
|
||||
}
|
||||
|
||||
#endif // FILE__H_
|
||||
Reference in New Issue
Block a user