CStdioFileEx

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;
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值