AutoCAD 3DMAX C语言 Pro/E UG JAVA编程 PHP编程 Maya动画 Matlab应用 Android
Photoshop Word Excel flash VB编程 VC编程 Coreldraw SolidWorks A Designer Unity3D
 首页 > VC编程

FMD开发文集 -- CArchive原理

51自学网 2015-08-30 http://www.wanshiok.com

MFC 提供CArchive类实现数据的缓冲区读写,同时定义了类对象的存储与读取方案。 以下对CArchvie 的内部实现作分析。

1.概述
2.内部数据
3.基本数据读写
4.缓冲区的更新
5.指定长度数据段落的读写
6.字符串的读写
7.CObject派生对象的读写

一.概述

CArchive使用了缓冲区,即一段内存空间作为临时数据存储地,对CArchive的读写都先依次排列到此缓冲区,当缓冲区满或用户要求时,将此段整理后的数据读写到指定的存储煤质。
当建立CArchive对象时,应指定其模式是用于缓冲区读,还是用于缓冲区写。
可以这样理解,CArchive对象相当于铁路的货运练调度站,零散的货物被收集,当总量到达火车运量的时候,由火车装运走。
当接到火车的货物时,则货物由被分散到各自的货主。与货运不同的是,交货、取货是按时间循序执行的,而不是凭票据。因此必须保证送货的和取货的货主按同样的循序去存或取。
对于大型的货物,则是拆散成火车单位,运走,取货时,依次取各部分,组装成原物。

二.内部数据
缓冲区指针 BYTE* m_lpBufStart,指向缓冲区,这个缓冲区有可能是底层CFile(如派生类CMemFile)对象提供的,但一般是CArchive自己建立的。
缓冲区尾部指针 BYTE* m_lpBufMax;
缓冲区当前位置指针 BYTE* m_lpBufCur;
初始化时,如果是读模式,当前位置在尾部,如果是写模式,当前位置在头部:

m_lpBufCur = (IsLoading()) ? m_lpBufMax : m_lpBufStart;
三.基本数据读写
对于基本的数据类型,例如字节、双字等,可以直接使用">>"、"<<"符号进行读出、写入。

//操作符定义捕:

//插入操作
CArchive& operator<<(BYTE by);
CArchive& operator<<(WORD w);
CArchive& operator<<(LONG l);
CArchive& operator<<(DWORD dw);
CArchive& operator<<(float f);
CArchive& operator<<(double d);
CArchive& operator<<(int i);
CArchive& operator<<(short w);
CArchive& operator<<(char ch);
CArchive& operator<<(unsigned u);

//提取操作
CArchive& operator>>(BYTE& by);
CArchive& operator>>(WORD& w);
CArchive& operator>>(DWORD& dw);
CArchive& operator>>(LONG& l);
CArchive& operator>>(float& f);
CArchive& operator>>(double& d);

CArchive& operator>>(int& i);
CArchive& operator>>(short& w);
CArchive& operator>>(char& ch);
CArchive& operator>>(unsigned& u);
下面以双字为例,分析原码
双字的插入(写)

CArchive& CArchive::operator<<(DWORD dw)
{
if (m_lpBufCur + sizeof(DWORD) > m_lpBufMax) //缓冲区空间不够
Flush(); //缓冲区内容提交到实际存储煤质。

if (!(m_nMode & bNoByteSwap))
_AfxByteSwap(dw, m_lpBufCur); //处理字节顺序
else
*(DWORD*)m_lpBufCur = dw;   //添入缓冲区

m_lpBufCur += sizeof(DWORD);   //移动当前指针
return *this;
}

双字的提取(读) CArchive& CArchive::operator>>(DWORD& dw)
{
if (m_lpBufCur + sizeof(DWORD) > m_lpBufMax) //缓冲区要读完了
FillBuffer(sizeof(DWORD) - (UINT)(m_lpBufMax - m_lpBufCur)); //重新读入内容到缓冲区

dw = *(DWORD*)m_lpBufCur; //读取双字
m_lpBufCur += sizeof(DWORD); //移动当前位置指针

if (!(m_nMode & bNoByteSwap))
_AfxByteSwap(dw, (BYTE*)&dw); //处理字节顺序
return *this;
}

四.缓冲区的更新

以上操作中,当缓冲区将插入满或缓冲区将提取空时,都将对缓冲区进行更新处理。

缓冲区将插入满时调用Flush(); void CArchive::Flush()
{
ASSERT_VALID(m_pFile);
ASSERT(m_bDirectBuffer || m_lpBufStart != NULL);
ASSERT(m_bDirectBuffer || m_lpBufCur != NULL);
ASSERT(m_lpBufStart == NULL ||
AfxIsValidAddress(m_lpBufStart, m_lpBufMax - m_lpBufStart, IsStoring()));
ASSERT(m_lpBufCur == NULL ||
AfxIsValidAddress(m_lpBufCur, m_lpBufMax - m_lpBufCur, IsStoring()));

if (IsLoading())
{
// unget the characters in the buffer, seek back unused amount
if (m_lpBufMax != m_lpBufCur)
m_pFile-> Seek(-(m_lpBufMax - m_lpBufCur), CFile::current);
m_lpBufCur = m_lpBufMax;  // 指向尾
}
else  //写模式
{
if (!m_bDirectBuffer)
{
// 内容写入到文件
if (m_lpBufCur != m_lpBufStart)
m_pFile-> Write(m_lpBufStart, m_lpBufCur - m_lpBufStart);
}
else
{
//如果是直接针对内存区域的的(例如CMemFile中) (只需移动相关指针,指向新的一块内存)
if (m_lpBufCur != m_lpBufStart)
m_pFile-> GetBufferPtr(CFile::bufferCommit, m_lpBufCur - m_lpBufStart);
// get next buffer
VERIFY(m_pFile-> GetBufferPtr(CFile::bufferWrite, m_nBufSize,
(void**)&m_lpBufStart, (void**)&m_lpBufMax) == (UINT)m_nBufSize);
ASSERT((UINT)m_nBufSize == (UINT)(m_lpBufMax - m_lpBufStart));
}
m_lpBufCur = m_lpBufStart; //指向缓冲区首
}
}
缓冲区将提取空,会调用FillBuffer。 nBytesNeeded为当前剩余部分上尚有用的字节void CArchive::FillBuffer(UINT nBytesNeeded)
{
ASSERT_VALID(m_pFile);
ASSERT(IsLoading());
ASSERT(m_bDirectBuffer || m_lpBufStart != NULL);
ASSERT(m_bDirectBuffer || m_lpBufCur != NULL);
ASSERT(nBytesNeeded > 0);
ASSERT(nBytesNeeded <= (UINT)m_nBufSize);
ASSERT(m_lpBufStart == NULL ||
AfxIsValidAddress(m_lpBufStart, m_lpBufMax - m_lpBufStart, FALSE));
ASSERT(m_lpBufCur == NULL ||
AfxIsValidAddress(m_lpBufCur, m_lpBufMax - m_lpBufCur, FALSE));

UINT nUnused = m_lpBufMax - m_lpBufCur;
ULONG nTotalNeeded = ((ULONG)nBytesNeeded) + nUnused;

// 从文件中读取
if (!m_bDirectBuffer)
{
ASSERT(m_lpBufCur != NULL);
ASSERT(m_lpBufStart != NULL);
ASSERT(m_lpBufMax != NULL);

if (m_lpBufCur > m_lpBufStart)
{
//保留剩余的尚未处理的部分,将它们移动到头
if ((int)nUnused > 0)
{
memmove(m_lpBufStart, m_lpBufCur, nUnused);
m_lpBufCur = m_lpBufStart;
m_lpBufMax = m_lpBufStart + nUnused;
}

// read to satisfy nBytesNeeded or nLeft if possible
UINT nRead = nUnused;
UINT nLeft = m_nBufSize-nUnused;
UINT nBytes;
BYTE* lpTemp = m_lpBufStart + nUnused;
do
{
nBytes = m_pFile-> Read(lpTemp, nLeft);
lpTemp = lpTemp + nBytes;
nRead += nBytes;
nLeft -= nBytes;
}
while (nBytes > 0 && nLeft > 0 && nRead < nBytesNeeded);

m_lpBufCur = m_lpBufStart;
m_lpBufMax = m_lpBufStart + nRead;
}
}
else
{
// 如果是针对内存区域(CMemFile),移动相关指针,指向新的一块内存
if (nUnused != 0)
m_pFile-> Seek(-(LONG)nUnused, CFile::current);
UINT nActual = m_pFile-> GetBufferPtr(CFile::bufferRead, m_nBufSize,
(void**)&m_lpBufStart, (void**)&m_lpBufMax);
ASSERT(nActual == (UINT)(m_lpBufMax - m_lpBufStart));
m_lpBufCur = m_lpBufStart;
}

// not enough data to fill request?
if ((ULONG)(m_lpBufMax - m_lpBufCur) < nTotalNeeded)
AfxThrowArchiveException(CArchiveException::endOfFile);
}

<

 

 

 
说明
:本教程来源互联网或网友上传或出版商,仅为学习研究或媒体推广,wanshiok.com不保证资料的完整性。
上一篇:用VC&nbsp;6.0实现串行通信的三种方法  下一篇:解析C语言中的sizeof