我想做一个单文档RichEdit视图的程序,自行实现RTF压缩功能,有何建议?
我的问题主要集中在重载CRichEditDoc::Serialize(CArchive& ar)函数的存储语句上。
我目前的思路是:
采用CRichEditCtrl::StreamOut()函数。MSDN中流输出的例子如下:
// My callback procedure that reads the rich edit control contents
// from a file.
static DWORD CALLBACK
MyStreamOutCallback(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
{
CFile* pFile = (CFile*) dwCookie;
pFile->Write(pbBuff, cb);
*pcb = cb;
return 0;
}
// The example code.
// The pointer to my rich edit control.
extern CRichEditCtrl* pmyRichEditCtrl;
// The file to store the contents of the rich edit control.
CFile cFile(TEXT("myfile.rtf"), CFile::modeCreate|CFile::modeWrite);
EDITSTREAM es;
es.dwCookie = (DWORD) &cFile;
es.pfnCallback = MyStreamOutCallback;
pmyRichEditCtrl->StreamOut(SF_RTF, es);
我完全可以修改例子中的MyStreamOutCallback函数,在函数里先将pbBuff指向内存里的数据压缩后再保存到文件中。
但MSDN中又说了:
In the EDITSTREAM parameter es, you specify a callback function which fills a buffer with text. This callback function is called repeatedly, until the output stream is exhausted.
就是说,如果你的内容太多,StreamOut函数要多次调用MyStreamOutCallback函数来保存输出流。
这样的话,MyStreamOutCallback函数里pbBuff指向的内存就不一定是连续的了,也就是说,我要保存的文件就可能是分段压缩,这就会降低压缩效率。
当然也有一个方法,就是先开足够大的内存,等完全写入此内存后再做压缩存储。但是问题是不知道到底要开多大的内存,开太大占用资源太多,不是个好办法。
哪位有更好的办法吗?