FirteX-高性能全文索引和检索平台API Documentation |
00001 // 00002 // Copyright(C) 2005--2006 Institute of Computing Tech, Chinese Academy of Sciences. 00003 // All rights reserved. 00004 // This file is part of FirteX (www.firtex.org) 00005 // 00006 // Use of the FirteX is subject to the terms of the software license set forth in 00007 // the LICENSE file included with this software, and also available at 00008 // http://www.firtex.org/license.html 00009 // 00010 // Author : 郭瑞杰(GuoRuijie) 00011 // Email : ruijieguo@software.ict.ac.cn,ruijieguo@gmail.com 00012 // Created : 2006/5/20 00013 // 00014 #ifndef _READER_H 00015 #define _READER_H 00016 00017 #include "StdHeader.h" 00018 #include <string> 00019 using namespace std; 00020 00021 namespace firtex 00022 { 00023 namespace utility 00024 { 00025 #define READER_BUFFSIZE 32768*10 00026 class CReader 00027 { 00028 public: 00029 CReader(char* buffer,size_t buffsize); 00030 CReader(size_t buffsize=0); 00031 virtual ~CReader(void); 00032 public: 00038 void read(char* data, size_t length); 00039 00045 char* readWithNoCopy( size_t& length); 00046 00050 virtual void close(); 00051 00055 int64_t getFilePointer(){return m_bufferStart + m_bufferPosition;} 00056 00061 void seek(int64_t pos); 00062 00066 virtual bool isEof(){return ( (m_bufferStart + (int64_t )m_bufferPosition) >= m_length);} 00067 00071 int64_t length(){return m_length;} 00072 00073 /*virtual const char* getReaderName() const = 0; 00074 bool instanceOf(const char* other) 00075 { 00076 const char* t = getReaderName(); 00077 if ( t==other || _tcscmp( t, other )==0 ) 00078 return true; 00079 else 00080 return false; 00081 }*/ 00082 00083 public: 00087 virtual CReader* clone(char* buffer,size_t buffsize) = 0; 00091 virtual CReader* clone() = 0; 00092 protected: 00097 virtual void seekInternal(int64_t position) = 0; 00098 00105 virtual void readInternal(char* b, size_t offset, size_t length) = 0; 00106 00107 protected: 00111 void refill(); 00112 protected: 00113 char* m_buffer; 00114 size_t m_bufferSize; 00115 00116 int64_t m_bufferStart; // position in file of m_buffer 00117 size_t m_bufferLength; // end of valid bytes 00118 size_t m_bufferPosition; // next byte to read 00119 00120 int64_t m_length; // set by subclasses 00121 bool m_bOwnBuff; 00122 }; 00123 00125 // 00126 inline void CReader::refill() 00127 { 00128 int64_t start = m_bufferStart + (int64_t)m_bufferPosition; 00129 int64_t end = start + (int64_t)m_bufferSize; 00130 if (end > m_length) //超过了结尾 00131 end = m_length; 00132 m_bufferLength = (size_t) (end - start); 00133 if (m_bufferLength == 0) 00134 throw CFileIOException("CReader:read past EOF."); 00135 00136 if (m_buffer == NULL) 00137 m_buffer = new char[m_bufferSize]; // allocate m_buffer lazily 00138 readInternal(m_buffer, 0, m_bufferLength); 00139 00140 m_bufferStart = start; 00141 m_bufferPosition = 0; 00142 } 00143 } 00144 } 00145 00146 00147 #endif
http://www.firtex.org http://www.sourceforge.net/projects/firtex