分类: LINUX
2011-09-01 14:13:09
//定义了fseek超过2G的文件处理有关的平台无关的接口
//作者:刘成彬
//适合平台:WindowsXP, WindowsCE, Linux, Android
#ifndef _MINI_SUN_PLATFORM_SEEKFILE_H_
#define _MINI_SUN_PLATFORM_SEEKFILE_H_
/////////////
//对Android平台,也认为是linux
#ifdef ANDROID
#define linux ANDROID
#endif
//包含头文件
#ifdef _WIN32
#include
#include
#endif
#ifdef linux
#include
#include
#include
#include
#define _FILE_OFFSET_BITS 64
#endif
//创建文件的参数和返回值
//文件句柄
#ifdef _WIN32
typedef HANDLE RFile;
#endif
#ifdef linux
typedef FILE* RFile;
#endif
//文件名类型
#ifdef _WIN32
typedef LPCSTR PFileNameA;
#endif
#ifdef linux
typedef char* PFileNameA;
#endif
#ifdef _WIN32
typedef LPCWSTR PFileNameW;
#endif
#ifdef linux
typedef wchar_t* PFileNameW;
#endif
//文件打开模式
#ifdef _WIN32
typedef DWORD POpenMode;
#endif
#ifdef linux
typedef char* POpenMode;
#endif
//seek模式
#ifdef _WIN32
typedef DWORD SEEKMODE;
#endif
#ifdef linux
typedef int SEEKMODE;
#endif
//
#ifdef _WIN32
typedef LPCVOID FTPARAM;
#endif
#ifdef linux
typedef void * FTPARAM;
#endif
//文件长度类型
#ifdef _WIN32
typedef DWORD RTFILELEN;
#endif
#ifdef linux
typedef off_t RTFILELEN;
#endif
//读写文件长度类型
#ifdef _WIN32
typedef DWORD RTReadAndWriteLen;
#endif
#ifdef linux
typedef size_t RTReadAndWriteLen;
#endif
//读类型
#ifdef _WIN32
#define pRead GENERIC_READ
#endif
#ifdef linux
#define pRead "rb"
#endif
//写类型
#ifdef _WIN32
#define pWrite (GENERIC_READ|GENERIC_WRITE)
#endif
#ifdef linux
#define pWrite "w "
#endif
//文件定位位置
#ifdef _WIN32
#define pSEEKSET FILE_BEGIN
#endif
#ifdef linux
#define pSEEKSET SEEK_SET
#endif
#ifdef _WIN32
#define pSEEKCUR FILE_CURRENT
#endif
#ifdef linux
#define pSEEKCUR SEEK_CUR
#endif
#ifdef _WIN32
#define pSEEKEND FILE_END
#endif
#ifdef linux
#define pSEEKEND SEEK_END
#endif
//打开文件的返回值
#ifdef _WIN32
#define pIHV INVALID_HANDLE_VALUE
#endif
#ifdef linux
#define pIHV NULL
#endif
//定位位置类型
#ifdef _WIN32
typedef __int64 RTPos;
#endif
#ifdef linux
typedef off_t RTPos;
#endif
//读写方式创建文件
//(RFile)BfopenA(PFileNameA n,POpenMode m,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_FLAG_SEQUENTIAL_SCAN,NULL);
RFile createWCEA(PFileNameA n, POpenMode m);
#if defined WIN32 && (!defined(WINCE))
#define BfopenA(h,n,m) ((h)=::CreateFileA((PFileNameA)n,m,0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL))
#endif
#if defined(WINCE)
#define BfopenA(h,n,m) ((h)=createWCEA(n,m))
#endif
#ifdef linux
#define BfopenA(h,n,m) ((h)=fopen(n,m))
#endif
//(RFile)BfopenW(PFileNameW n,POpenMode m,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_FLAG_SEQUENTIAL_SCAN,NULL);
RFile createfilew(PFileNameW n, const char* m);
#ifdef _WIN32
#define BfopenW(h,n,m) ((h)=::CreateFileW(n,m,0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL))
#endif
#ifdef linux
#define BfopenW(h,n,m) ((h)=createfilew(n,m))
#endif
//读取文件
//Bfread(RFile h,FTPARAM c,RTFILELEN l,RTReadAndWriteLen n);
#ifdef _WIN32
#define Bfread(h,c,l,n) (::ReadFile(h,c,l,&n,NULL))
#endif
#ifdef linux
#define Bfread(h,c,l,n) ((n)=fread(c,l,1,h))
#endif
//写入文件
////Bfwrite(RFile h,FTPARAM c,RTFILELEN l,RTReadAndWriteLen n);
#ifdef _WIN32
#define Bfwrite(h,c,l,n) (::WriteFile(h,c,l,&n,NULL))
#endif
#ifdef linux
#define Bfwrite(h,c,l,n) (n=fwrite(c,l,1,h))
#endif
//定位位置
//Bfseek(RFile h,RTPos o,SEEKMODE m);
#ifdef _WIN32
inline bool Bfseek(RFile h,RTPos o,SEEKMODE m) //针对64位数据 支持>2.1G
{
LONG lMoveHigh = (LONG)(o>>32);
return ! SetFilePointer(h, (LONG)(o&0xffffffff), &lMoveHigh, m);
}
//#define Bfseek(h,o,m) (SetFilePointer(h,o,NULL,m))//针对32位数据 支持<=2.1G
#endif
#ifdef linux
#define Bfseek(h,o,m) (fseeko(h,o,m))
#endif
//返回当前位置
//Bftell(RTPos o,RFile h);
void Bftello(RTPos &p,RFile h);
#ifdef _WIN32
#define Bftell(o,h) (Bftello(o,h))
#endif
#ifdef linux
#define Bftell(o,h) ((o)=ftello(h))
#endif
//获取文件的长度
//BGetFileSize(RTFILELEN l,RFile h);
#ifdef _WIN32
#define BGetFileSize(l,h) (l=GetFileSize(h,NULL))
#endif
#ifdef linux
#define BGetFileSize(l,h) (l= ftello(h))
#endif
//关闭文件句柄
//Bfclose(RFile h);
#ifdef _WIN32
#define Bfclose(h) (CloseHandle(h))
#endif
#ifdef linux
#define Bfclose(h) (fclose(h))
#endif
#endif //_MINI_SUN_PLATFORM_SEEKFILE_H_
/////////////////////////////////////////////////////////////////////////////////////////
//定义了fseek超过2G的文件处理有关的平台无关的接口
//作者:刘成彬
//适合平台:WindowsXP, WindowsCE, Linux, Android
#include "GlobalInside.h"
#include "SeekFile.h"
//创建文件
RFile createfilew(PFileNameW n, const char* m)
{
assert(n);
char chBuffer[MAX_PATH];
UnicodeToChar(n, chBuffer);
return fopen(chBuffer,m);
}
#if define(WINCE)
RFile createWCEA(PFileNameA n, POpenMode m)
{
wchar_t wcTempPath[MAX_PATH];
mbstowcs(wcTempPath,n,MAX_PATH-1);
return (::CreateFile(wcTempPath, m, 0, NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL));
}
#endif
#ifdef _WIN32
//获取文件指针位置
void Bftello(RTPos &p,RFile h)
{
LARGE_INTEGER liPos;
liPos.QuadPart=0;
liPos.LowPart=::SetFilePointer(h, liPos.LowPart,&liPos.HighPart, FILE_CURRENT);
p = liPos.QuadPart;
}
#endif
///////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// MFile.h
// 提供基本文件操作
#include "SeekFile.h"
#pragma once
namespace sde
{
class SDE_API CMFile
{
public:
CMFile();
virtual ~CMFile();
MBOOL Open(W16* pszFileName);
MBOOL Open(S8* pszFileName);
MBOOL Create(W16* pszFileName);
MBOOL Create(S8* pszFileName);
void Close();
U32 Read(void* lpBuf, U32 nLength);
U32 Write(const void* lpBuf, U32 nLength);
U32 ReadFile(U32 nPos, void* lpBuf , U32 nLength);
U32 WriteFile(U32 nPos, void* lpBuf , U32 nLength);
MBOOL SetCursorTo(U32 nPos);
MBOOL GetIsOpen();
U32 GetFilePos();
U32 GetFileLength();
private:
RFile m_pFile;
};
}
//////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// MFile.cpp
#include "GlobalInside.h"
#include "MFile.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
namespace sde
{
CMFile::CMFile()
:m_pFile(NULL)
{
}
CMFile::~CMFile()
{
Close();
}
MBOOL CMFile::Open(W16* pszFileName)
{
if(m_pFile)
Close();
BfopenW(m_pFile,pszFileName,pRead);
if(pIHV == m_pFile)
return FALSE;
return TRUE;
}
MBOOL CMFile::Open(S8* pszFileName)
{
if(m_pFile)
Close();
BfopenA(m_pFile,pszFileName,pRead);
if(pIHV == m_pFile)
return FALSE;
return TRUE;
}
MBOOL CMFile::Create(W16* pszFileName)
{
if(m_pFile)
Close();
BfopenW(m_pFile,pszFileName,pWrite);
if(pIHV == m_pFile)
return FALSE;
return TRUE;
}
MBOOL CMFile::Create(S8* pszFileName)
{
if(m_pFile)
Close();
BfopenA(m_pFile,pszFileName,pWrite);
if(pIHV == m_pFile)
return FALSE;
return TRUE;
}
void CMFile::Close()
{
if(m_pFile)
{
Bfclose(m_pFile);
m_pFile = NULL;
}
}
U32 CMFile::Read(void* lpBuf, U32 nLength)
{
assert(m_pFile && lpBuf);
if (nLength == 0)
return 0;
RTReadAndWriteLen _nLength = nLength;
RTReadAndWriteLen rsize;
Bfread(m_pFile,lpBuf,_nLength,rsize);
return (U32)rsize;
}
U32 CMFile::Write(const void* lpBuf, U32 nLength)
{
assert(m_pFile && lpBuf);
if (nLength == 0)
return 0;
RTReadAndWriteLen _nLength = nLength;
RTReadAndWriteLen rsize;
Bfwrite(m_pFile,lpBuf,_nLength,rsize);
return (U32)rsize;
}
U32 CMFile::ReadFile(U32 nPos, void* lpBuf , U32 nLength)
{
assert(lpBuf && nLength>0);
RTPos _nPos = nPos;
Bfseek(m_pFile , _nPos , pSEEKSET );
return Read( lpBuf , nLength );
}
U32 CMFile::WriteFile(U32 nPos , void* lpBuf , U32 nLength)
{
assert(lpBuf && nLength>0);
RTPos _nPos = nPos;
Bfseek(m_pFile , _nPos , pSEEKSET );
return Write( lpBuf , nLength );
}
MBOOL CMFile::SetCursorTo(U32 nPos)
{
assert(m_pFile);
RTPos _nPos = nPos;
return !Bfseek(m_pFile,_nPos,pSEEKSET);
}
MBOOL CMFile::GetIsOpen()
{
return m_pFile != NULL;
}
U32 CMFile::GetFilePos()
{
assert(m_pFile);
RTPos pos = 0;
Bftell(pos,m_pFile);
return (U32)pos;
}
U32 CMFile::GetFileLength()
{
assert(m_pFile);
RTFILELEN nFileLen;
RTPos nPos = GetFilePos();
Bfseek(m_pFile, 0, SEEK_END);
BGetFileSize(nFileLen,m_pFile);
Bfseek(m_pFile, nPos, SEEK_SET);
return (U32)nFileLen;
}
}