class CStdioFileEx : public CStdioFile
{
public:
BOOL Open(LPCTSTR lpszFileName, UINT nOpenFlags, CFileException* pError = NULL )
{
if(CStdioFile::Open(lpszFileName,nOpenFlags,pError))
{
BYTE head[3];
Read(head,3);
if((head[0]==0xEF && head[1]==0xBB && head[2]==0xBF))
{
_type = 1;
}
else if ((head[0] == 0xff && head[1] == 0xfe) || (head[0] == 0xfe && head[1] == 0xff))
{
_type = 2;
Seek(2,0);
}
else
{
_type = 0;
SeekToBegin();
}
return true;
}
else{
return false;
}
}
BOOL ReadUTF8StringFile(CString& str)
{
ULONGLONG FileSize=str.GetLength();
char* pContent= str.GetBuffer(FileSize);
int n=MultiByteToWideChar(CP_UTF8,0,pContent,FileSize+1,NULL,0);
wchar_t* pWideChar=(wchar_t*)calloc(n+1,sizeof(wchar_t));
MultiByteToWideChar(CP_UTF8,0,pContent,FileSize+1,pWideChar,n);
str=CString(pWideChar);
free(pWideChar);
return true;
}
BOOL ReadString(CString& rString)
{
BOOL _rel;
rString = "";
if(1 == _type)
{
_rel = CStdioFile::ReadString(rString);
if(_rel)
ReadUTF8StringFile(rString);
}
else if(2 == _type)
{
wchar_t _wc[2]={0x00,0x00};
char pchar[3];
_rel = (2 ==Read(_wc,2));
if(_wc[0] != 0x000a)
{
while (true)
{
if(_wc[0] == 0x000a)
break;
int n = WideCharToMultiByte(CP_ACP,0,&_wc[0],-1,NULL, 0, NULL,NULL);
WideCharToMultiByte(CP_ACP,0,&_wc[0],-1,pchar, n, NULL,NULL);
rString += pchar[0];
if(n>2)
rString += pchar[1];
if(Read(&_wc,2) != 2)
break;
}
}
}
else
_rel = CStdioFile::ReadString(rString);
return _rel;
}
protected:
private:
int _type;
};
CStdioFileEx
最新推荐文章于 2023-04-25 11:46:34 发布