1 #ifndef _TIANWANG_FILE_H_031104_
2 #define _TIANWANG_FILE_H_031104_
3
4 #include "Tse.h"
5 #include "Url.h"
6 #include "Page.h"
7 #include "FileEngine.h"
8
9 class CTianwangFile : public CFileEngine
10 {
11 public:
12 CTianwangFile(string str);
13 CTianwangFile();
14 virtual ~CTianwangFile();
15
16 inline int GetFileType() { return TIANWANG; }
17
18 virtual bool Write(void *arg);
19 };
20
21 #endif /* _TIANWANG_FILE_H_031104_ */
1 #include "TianwangFile.h"
2
3 extern map<string,string> mapCacheHostLookup;
4
5 CTianwangFile::CTianwangFile()
6 {
7 }
8
9 CTianwangFile::CTianwangFile(string str) : CFileEngine(str)
10 {
11 }
12
13 CTianwangFile::~CTianwangFile()
14 {
15 m_ofsFile.close();
16 }
17
18 bool CTianwangFile::Write(void * arg)
19 {
20 if( !arg || !m_ofsFile ){
21 return false;
22 }
23
24 file_arg *pFile = (file_arg *)arg;
25
26 CUrl *iUrl = pFile->pUrl;
27 CPage *iPage = pFile->pPage;
28
29 char strDownloadTime[128];
30 time_t tDate;
31
32 memset(strDownloadTime, 0, 128);
33 time(&tDate);
34 strftime(strDownloadTime, 128, "%a, %d %b %Y %H:%M:%S GMT", gmtime(&tDate));
35
36 m_ofsFile << "version: 1.0\n";
37 if( iPage->m_sLocation.length() == 0 ){//没有重定向
38 m_ofsFile << "url: " << iUrl->m_sUrl;
39 } else {//重定向了
40 m_ofsFile << "url: " << iPage->m_sLocation;
41 m_ofsFile << "\norigin: " << iUrl->m_sUrl;
42 }
43
44 m_ofsFile << "\ndate: " << strDownloadTime;//记录下下载时间
45
46 //保存IP地址信息
47 if( mapCacheHostLookup.find(iUrl->m_sHost) == mapCacheHostLookup.end() ){
48 m_ofsFile << "\nip: " << iUrl->m_sHost;
49 } else {
50 m_ofsFile << "\nip: " << ( *(mapCacheHostLookup.find(iUrl->m_sHost)) ).second;
51 }
52
53 //保存网页长度
54 m_ofsFile << "\nlength: " << iPage->m_nLenContent + iPage->m_nLenHeader + 1
55 << "\n\n" << iPage->m_sHeader << "\n";
56
57 m_ofsFile.write( iPage->m_sContent.c_str(), iPage->m_nLenContent );
58 m_ofsFile << endl;
59
60 return true;
61 }