代码改变世界

CMoReader

2014-02-22 23:54  hongjiumu  阅读(241)  评论(0编辑  收藏  举报
#ifndef __E3GLOGLOADBYFILE_H__ 
#define __E3GLOGLOADBYFILE_H__ 
  
#include "PubCommon\MemoryManager.h" 
#include "PubCommon.h" 
  
//------------------------ 
// 读取内存中的 LOG 文件 
//------------------------ 
class CMoReader 
{ 
public: 
    CMoReader(Win32Tools::CMemoryManager* pMemMgr); 
    virtual ~CMoReader(void); 
public: 
    bool LoadFile(char* pFileMemory); 
    void GetMemLine(CMemLine*& pMemLine); 
    bool IsEof(); 
private: 
    size_t GetBufSize(PCHAR pBuf); 
    bool ReadLine(CMemLine*& pMemLine); // 读取一行数据 
    bool MoveToLineEnd();   // 移动到行尾 
    bool MoveToNextLine();  // 移动到下一行 
private: 
    char*   m_pMemory;      // 文件流的位置 
    char*   m_pReadPos;     // 当前读取的位置 
    int     m_nCount;       // 总计读取的行数 
private: 
    Win32Tools::CMemoryManager* m_pMemMgr; 
}; 
  
#endif 
#include "stdafx.h" 
#include "MoReader.h" 
#include "PubCommon\FileFormatDefine.h" 
  
CMoReader::CMoReader(Win32Tools::CMemoryManager* pMemMgr) 
    : m_nCount(0) 
    , m_pMemMgr(pMemMgr) 
    , m_pMemory(NULL) 
    , m_pReadPos(NULL) 
{ 
} 
  
CMoReader::~CMoReader(void) 
{ 
} 
  
bool CMoReader::LoadFile(char* pFileMemory) 
{ 
    // 加载信息 
      
    if(pFileMemory == NULL) 
        return false; 
    m_pMemory = pFileMemory; 
    m_pReadPos = pFileMemory; 
    return true; 
} 
  
void CMoReader::GetMemLine(CMemLine*& pMemLine) 
{ 
    // 首行数据 
    pMemLine = (CMemLine*)m_pMemMgr->GetMemory(sizeof(CMemLine)); 
    pMemLine = new (pMemLine) CMemLine; 
    CMemLine* pOld = pMemLine; 
  
    while(1) 
    { 
        // 依次追加的行 
        CMemLine* pNew = (CMemLine*)m_pMemMgr->GetMemory(sizeof(CMemLine)); 
        pNew = new (pNew) CMemLine; 
        if(false == ReadLine(pNew)) 
            break; 
        if(*(pNew->m_pLine) == '=') 
        { 
            if(3 <= ++m_nCount)  // 完整对象判断 
            { 
                break; 
            } 
            continue; 
        } 
        if(*(pNew->m_pLine) != '\0' && (NUMBER_ZERO != m_nCount)) 
        { 
            pMemLine->m_pNextLine = pNew; 
            pMemLine = pNew; 
        } 
    } 
    if (3 != m_nCount)                  // 结尾非法对象处理 
    { 
        pOld->m_pNextLine = NULL; 
    } 
    pMemLine = pOld; 
    pMemLine = pMemLine->m_pNextLine; 
    m_nCount = 1; 
} 
  
bool CMoReader::IsEof() 
{ 
    return (*m_pReadPos == FILE_END_CHAR); 
} 
  
size_t CMoReader::GetBufSize(PCHAR pBuf) 
{ 
    size_t iRelt(0); 
    while(*pBuf++ != '\0') 
    { 
        ++iRelt; 
    } 
    return iRelt; 
} 
  
bool CMoReader::MoveToLineEnd() 
{ 
    // 移动到行尾 
  
    if(*m_pReadPos == '\r' || *m_pReadPos == '\0' || *m_pReadPos == '\n' || *m_pReadPos == FILE_END_CHAR) 
        return false; 
  
    do
    { 
        ++m_pReadPos; 
    } 
    while(*m_pReadPos != '\r' && *m_pReadPos != '\0' && *m_pReadPos != '\n' && *m_pReadPos != FILE_END_CHAR); 
    return true; 
} 
  
bool CMoReader::MoveToNextLine() 
{ 
    // 移动到下一行 
  
    MoveToLineEnd(); // 首先移动至行尾 
    if(*m_pReadPos == FILE_END_CHAR) 
        return false; 
  
    while(*m_pReadPos == '\r' || *m_pReadPos == '\n' || *m_pReadPos == '\0') 
    { 
        if(*m_pReadPos == FILE_END_CHAR) 
            return false; 
        ++m_pReadPos; 
    } 
    return true; 
} 
  
bool CMoReader::ReadLine(CMemLine*& pMemLine) 
{ 
    // 读取一行数据 
  
    char* pBegin = m_pReadPos; 
    if(MoveToLineEnd() == false) 
        return false; 
    char* pEnd = m_pReadPos; 
    size_t iSize = pEnd - pBegin; 
    pMemLine->m_pLine = pBegin; 
    pMemLine->m_iLength = iSize; 
    *pEnd = '\0'; 
    return MoveToNextLine(); 
}