📄 document.cpp
字号:
else
{
// Everything was normal so just fixup the links
pThis->m_pProgress->SetAborted(FALSE);
pThis->FixupLinks();
}
}
// 关闭日志文件
pThis->m_fileLog.Close();
// Are we doing the TOC??
if(pThis->m_Options.bContents)
{
// 写入文件的尾部
CString strText;
strText = "</UL>\r\n</BODY>\r\n</HTML>\r\n";
pThis->m_fileContents.Write(strText,strText.GetLength());
pThis->m_fileContents.Close();
}
// 输出信息表示已经完成
pThis->m_pProgress->SetActionTitle("********* 处理完毕 *********");
pView->SetSnagging(FALSE);
// 释放内存
pThis->ClearCacheMaps();
// Make sure the "stopping, please wait" message isn't displayed
//pView->EndWait();
// 如果是自动模式,关闭住窗口
if(pThis->m_bAutoMode)
AfxGetMainWnd()->PostMessage(WM_CLOSE);
// 关闭WinInet会话
try
{
pThis->m_Inet.CloseSession();
}
catch(...)
{
}
pThis->m_nLevel = 0;
return(0);
}
//释放资源,即所有文件入口以及文件下载列表
void CSnaggerDoc::ClearCacheMaps()
{
MAP_FILES *pMapEntry;
CString strPage;
POSITION pos;
int i;
// 删除页面列表
pos = m_arrPagesDone.GetStartPosition();
for(i = 0; i < m_arrPagesDone.GetCount(); i++)
{
m_arrPagesDone.GetNextAssoc(pos,strPage,(CObject *&) pMapEntry);
delete pMapEntry;
}
m_arrPagesDone.RemoveAll();
// 删除多媒体页面列表
pos = m_arrMediaDone.GetStartPosition();
for(i = 0; i < m_arrMediaDone.GetCount(); i++)
{
m_arrMediaDone.GetNextAssoc(pos,strPage,(CObject *&) pMapEntry);
delete pMapEntry;
}
m_arrMediaDone.RemoveAll();
}
// 增加一个进入下载页面文件列表的入口
void CSnaggerDoc::SetPageCacheEntry(LPCTSTR lpszPage, LPCTSTR lpszFileName, int nLevel)
{
CString strTempPage = lpszPage;
MAP_FILES *pMapEntry = new MAP_FILES();
// 将网页名称以小写保存
pMapEntry->strFileName = lpszFileName;
pMapEntry->nMaxLevel = nLevel;
strTempPage.MakeLower();
strTempPage = strTempPage.SpanExcluding("#");
// 让所有页面有一个相对路径
CString strExt = CInet::SplitFileName(strTempPage,CInet::EXT);
if(strExt.IsEmpty() && strTempPage.Right(1) != "/")
strTempPage += "/";
// Assimilate it into the collective
m_arrPagesDone.SetAt(strTempPage,(CObject *&) pMapEntry);
TRACE("Pages Done: "+strTempPage+"\n");
// 设定默认名称
if(pMapEntry->strFileName == "index.htm" && strTempPage.Find("index.htm") == -1)
{
// 常用的默认页文件名
char *aszDefPages[4] = { "index.htm", "index.html", "default.htm",
"default.html"};
MAP_FILES *pTempMapEntry;
CString strServer, strObject, strUser, strPassword;
INTERNET_PORT nPort;
DWORD dwServiceType;
AfxParseURLEx(strTempPage,dwServiceType,strServer,strObject,nPort,
strUser,strPassword,
ICU_NO_ENCODE);
// 加入默认问卷爱你们
if(!strServer.IsEmpty())
{
strServer = "http://"+strServer+"/";
for(int i = 0; i < 4; i++)
{
pTempMapEntry = new MAP_FILES();
pTempMapEntry->strFileName = pMapEntry->strFileName;
pTempMapEntry->nMaxLevel = pMapEntry->nMaxLevel;
m_arrPagesDone.SetAt(strServer+aszDefPages[i],(CObject *&) pTempMapEntry);
TRACE("Pages Done: "+CString(aszDefPages[i])+"\n");
}
}
}
}
// 增家一个新的文件列表的入口
void CSnaggerDoc::SetMediaCacheEntry(LPCTSTR lpszMedia, LPCTSTR lpszFileName)
{
CString strTempMedia = lpszMedia;
MAP_FILES *pMapEntry = new MAP_FILES();
// 文件名保存成小写
pMapEntry->strFileName = lpszFileName;
pMapEntry->nMaxLevel = -1;
strTempMedia.MakeLower();
strTempMedia = strTempMedia.SpanExcluding("#");
// Assimilate it into the collective
m_arrMediaDone.SetAt(strTempMedia,(CObject *&) pMapEntry);
}
//开始下载网站内容
void CSnaggerDoc::RecursiveDownload(LPCTSTR lpszURL)
{
// 初始化参数
m_nGottenPageCount = 0;
m_nGottenFileCount = 0;
m_nQueuedPageCount = 0;
m_nTotalBytes = 0;
m_strStartPage = lpszURL;
ClearCacheMaps();
// 初始化参数显示窗口
POSITION pos = GetFirstViewPosition();
CSnaggerView* pView = (CSnaggerView *) GetNextView(pos);
m_pProgress = pView->GetProgress();
m_pProgress->SetAborted(FALSE);
UpdateStatus();
// 启动下载线程
CWinThread *pThread = AfxBeginThread(DownloadThread,this);
}
// Fixs up each of the downloaded pages to allow local browsing. It
// basically goes through the downloaded page list and opens each
// file specified then parses the page and fixes up each of the
// links found.
//
void CSnaggerDoc::FixupLinks()
{
BYTE *pbyBuffer = m_byBuffer;
int nLen;
CFile fileIn, fileOut;
CFileException ex;
MAP_FILES* pMapEntry;
int nIndex;
int nBytesWritten;
int nSectionLength;
int nOffset;
CString strFixupFileName;
CString strPage;
CString strFileName;
CString strTempName = m_strDirectory+"$ssnag$.htm";
BOOL bFixup;
int nFiles = m_arrPagesDone.GetCount();
BOOL bIndexDone = FALSE;
//更新参数显示窗口指示还未处理的文件数量
m_pProgress->SetQueuedFiles(nFiles);
// Iterate through the downloaded page list
//获得下载页列表
POSITION pos = m_arrPagesDone.GetStartPosition();
for(int i = 0; i < m_arrPagesDone.GetCount() && !m_pProgress->IsAborted(); i++)
{
m_arrPagesDone.GetNextAssoc(pos,strPage,(CObject *&) pMapEntry);
if(pMapEntry->strFileName == "index.htm" ||
pMapEntry->strFileName == "index.html")
{
if(bIndexDone)
{
// 更新参数显示窗口
m_pProgress->SetQueuedFiles(--nFiles);
continue;
}
else bIndexDone = TRUE;
}
//显示参数
strFileName = m_strDirectory+pMapEntry->strFileName;
m_pProgress->SetActionTitle("Fixing up links in file: "+pMapEntry->strFileName);
// 打开本地文件
TRACE("Fixing up: "+strFileName+"\n");
if(fileIn.Open(strFileName.SpanExcluding("#"),CFile::modeRead,&ex))
{
pbyBuffer = m_byBuffer;
// 把文件读入内存
nLen = fileIn.Read(pbyBuffer,MAX_INET_BUFFER);
fileIn.Close();
// Parse it (tell the parser to look for only links, i.e. fixup mode)
CSnaggerHtmlParser Parser;
Parser.SetFixupMode(TRUE);
Parser.ResetArrays();
Parser.SetGetMedia(m_Options.bMultimedia);
Parser.SetPageURL(strPage);
Parser.ParseText((char *)pbyBuffer,nLen);
CString strServer, strObject, strUser, strPassword;
INTERNET_PORT nPort;
DWORD dwServiceType;
AfxParseURLEx(strPage,dwServiceType,strServer,strObject,nPort,
strUser,strPassword,
ICU_NO_ENCODE);
CString strOrigHost = "http://"+strServer+"/";
// 打开个临时文件
if(fileOut.Open(strTempName,CFile::modeCreate|CFile::modeWrite,&ex))
{
CString strURL;
nIndex = 0;
nBytesWritten = 0;
nOffset = 0;
// 写入文件并整理(如果有必要)
for(int j = 0; j < Parser.GetFixups().arrURL.GetSize(); j++)
{
strURL = Parser.GetFixups().arrURL.GetAt(j);
// 是否整理完文本文件
if(Parser.GetFixups().arrTextPage.GetAt(j))
{
// 是否要下载这个文件
if(!ShouldGetPage(strURL,pMapEntry))
{
// 获得本地文件
strFixupFileName = pMapEntry->strFileName;
bFixup = TRUE;
}
else
{
AfxParseURLEx(strURL,dwServiceType,strServer,
strObject,nPort,strUser,strPassword,
ICU_NO_ENCODE);
if(!strServer.IsEmpty())
{
strFixupFileName = strURL;
bFixup = TRUE;
}
else bFixup = FALSE;
}
}
else // 多媒体文件整理
{
// 是否下载多媒体文件
if(!ShouldGetMedia(strURL,pMapEntry))
{
// 获取文件名
strFixupFileName = pMapEntry->strFileName;
bFixup = TRUE;
}
else bFixup = FALSE;
}
// 写入数据
nSectionLength = Parser.GetFixups().arrIndex.GetAt(j)-nIndex-nOffset;
try
{
fileOut.Write(pbyBuffer,nSectionLength);
}
catch(...)
{
}
nBytesWritten += nSectionLength;
pbyBuffer += nSectionLength;
nOffset = 0;
// 是否获得整理的入口
if(bFixup)
{
// 写入文件
try
{
fileOut.Write(" \"",2);
fileOut.Write(strFixupFileName,strFixupFileName.GetLength());
}
catch(...)
{
}
// 找到原来文件的开始除
while(*pbyBuffer == ' ')
{
pbyBuffer++;
nBytesWritten++;
nOffset++;
}
// Is the filename in quotes?
if(*pbyBuffer == '"')
{
// 找到最后
do
{
pbyBuffer++;
nBytesWritten++;
nOffset++;
}
while(*pbyBuffer != '"');
pbyBuffer++;
nBytesWritten++;
nOffset++;
}
else
{
// Not in quotes, so just look for the first
// trailing blank
do
{
pbyBuffer++;
nBytesWritten++;
nOffset++;
}
while(*pbyBuffer != ' ');
}
// Write a trailing quote and blank, just to be safe
fileOut.Write("\" ",2);
}
// 保存开始整理的位置
nIndex = Parser.GetFixups().arrIndex.GetAt(j);
}
// 完成整理,然后写入剩余的数据
try
{
if(nLen-nBytesWritten > 0)
fileOut.Write(pbyBuffer,nLen-nBytesWritten);
}
catch(...)
{
}
fileOut.Close();
}
}
// 用整理过的文件替代原始文件
remove(strFileName.SpanExcluding("#"));
rename(strTempName,strFileName.SpanExcluding("#"));
// 更新参数显示串口
m_pProgress->SetQueuedFiles(--nFiles);
}
// 删除临时文件,因为有可能中间用户要硬性停止
remove(strTempName);
}
/////////////////////////////////////////////////////////////////////////////
// CSnaggerDoc diagnostics
#ifdef _DEBUG
// Check validity of object
void CSnaggerDoc::AssertValid() const
{
CDocument::AssertValid();
}
// Dump the object
void CSnaggerDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -