1 #ifndef _ISAM_FILE_H_031105_ 2 #define _ISAM_FILE_H_031105_ 3 4 #include "FileEngine.h" 5 6 class CIsamFile : public CFileEngine 7 { 8 public: 9 string m_sIndexFileName; 10 FILE *fpDataFile; 11 FILE *fpIdxFile; 12 13 public: 14 CIsamFile(); 15 CIsamFile(string str); 16 CIsamFile(string strData, string strIndex); 17 virtual ~CIsamFile(); 18 19 int GetFileType() { return ISAM; } 20 virtual bool Write(void *arg); 21 bool Open(string strData, string strIndex); 22 virtual void Close(); 23 24 bool Open(string str) { return false; } 25 26 }; 27 28 #endif /* _ISAM_FILE_H_031105_ */
1 /****************************************************************** 2 ** File name: CgIsamFile.cpp 3 ** Copyright (c): 4 ** Function Description: implementation of the CgIsamFile class. 5 ** Author: 6 ** Created time: 7 ** Modified history: 8 ** Version: 9 ** Be careful: 10 ******************************************************************/ 11 12 #include "IsamFile.h" 13 #include "Url.h" 14 #include "Page.h" 15 16 /////////////////////////////////////////////////////////////////// 17 // Construction/Destruction 18 /////////////////////////////////////////////////////////////////// 19 CIsamFile::CIsamFile() 20 { 21 fpDataFile = NULL; 22 fpIdxFile = NULL; 23 } 24 25 CIsamFile::CIsamFile(string str) : CFileEngine(str) 26 { 27 fpDataFile = NULL; 28 fpIdxFile = NULL; 29 } 30 31 CIsamFile::CIsamFile(string strData, string strIndex) 32 { 33 fpDataFile = NULL; 34 fpIdxFile = NULL; 35 36 m_str = strData; 37 m_sIndexFileName = strIndex; 38 39 fpDataFile = fopen(DATA_FILE_NAME.c_str(), "a"); 40 if( fpDataFile == NULL ){ 41 return; 42 } 43 44 fpIdxFile = fopen(INDEX_FILE_NAME.c_str(), "a"); 45 if( fpIdxFile == NULL ){ 46 return; 47 } 48 } 49 50 CIsamFile::~CIsamFile() 51 { 52 } 53 54 bool CIsamFile::Open(string strData, string strIndex) 55 {//打开两个文件,一个保存数据,一个保存索引 56 m_str = strData; 57 m_sIndexFileName = strIndex; 58 59 fpDataFile = fopen(DATA_FILE_NAME.c_str(), "at"); 60 if( fpDataFile == NULL ){ 61 return false; 62 } 63 64 fpIdxFile = fopen(INDEX_FILE_NAME.c_str(), "at"); 65 if( fpIdxFile == NULL ){ 66 return false; 67 } 68 69 return true; 70 } 71 72 void CIsamFile::Close() 73 { 74 fclose(fpIdxFile); 75 fclose(fpDataFile); 76 } 77 78 79 /************************************************************************ 80 * Function name: Write 81 * Input argv: 82 * -- arg: the file_arg handle contain the url & page data 83 * Output argv: 84 * -- 85 * Return: 86 * true: success 87 * false: fail 88 ************************************************************************/ 89 bool CIsamFile::Write(void *arg) 90 { 91 if( !arg || !fpIdxFile || !fpDataFile ){ 92 return false; 93 } 94 95 file_arg *pFile = (file_arg *)arg; 96 97 CUrl *iUrl = pFile->pUrl; 98 CPage *iPage = pFile->pPage; 99 100 const char* url = NULL; 101 const char* buffer = NULL; 102 103 if( iPage->m_sLocation.length() == 0 ){ 104 url = iUrl->m_sUrl.c_str(); 105 } else { 106 url = iPage->m_sLocation.c_str(); 107 } 108 109 buffer = iPage->m_sContent.c_str(); 110 int len = iPage->m_sContent.length(); 111 ///////////////////////////////////////////// 112 113 int offsett = ftell(fpDataFile); 114 fprintf(fpIdxFile, "%10d", offsett); 115 fprintf(fpIdxFile, "%256s\n", url); 116 fflush(fpIdxFile); 117 118 fwrite( buffer, 1, len, fpDataFile); 119 120 //write 25 spaces in the file 121 for(int i=0; i<25; i++){ 122 fputc(0,fpDataFile); 123 } 124 125 //write 3 '1' in the file 126 fputc(1,fpDataFile); 127 fputc(1,fpDataFile); 128 fputc(1,fpDataFile); 129 130 //write [url] in the file 131 fputc(91,fpDataFile); 132 fwrite( url, 1, strlen(url), fpDataFile); 133 fputc(93,fpDataFile); 134 135 for(int i=0; i<25; i++){ 136 fputc(0,fpDataFile); 137 } 138 139 fflush(fpDataFile); 140 141 return true; 142 }