CLucene - a full-featured, c++ search engine
API Documentation
00001 /*------------------------------------------------------------------------------ 00002 * Copyright (C) 2003-2006 Ben van Klinken and the CLucene Team 00003 * 00004 * Distributable under the terms of either the Apache License (Version 2.0) or 00005 * the GNU Lesser General Public License, as specified in the COPYING file. 00006 ------------------------------------------------------------------------------*/ 00007 #ifndef _lucene_util_Reader_ 00008 #define _lucene_util_Reader_ 00009 00010 #if defined(_LUCENE_PRAGMA_ONCE) 00011 # pragma once 00012 #endif 00013 00014 #include "streambase.h" 00015 #include "stringreader.h" 00016 #include "fileinputstream.h" 00017 #include "bufferedstream.h" 00018 00019 CL_NS_DEF(util) 00023 class Reader:LUCENE_BASE { 00024 typedef jstreams::StreamBase<TCHAR> jsReader; 00025 public: 00026 bool deleteReader; 00027 jsReader* reader; 00028 00029 Reader(jsReader* reader, bool deleteReader){ 00030 this->reader = reader; 00031 this->deleteReader = deleteReader; 00032 } 00033 virtual ~Reader(){ 00034 if ( deleteReader ) 00035 delete reader; 00036 reader = NULL; 00037 } 00038 inline int read(){ 00039 const TCHAR*b; 00040 int32_t nread = reader->read(b, 1,1); 00041 if ( nread < -1 ) //if not eof 00042 _CLTHROWA(CL_ERR_IO,reader->getError() ); 00043 else if ( nread == -1 ) 00044 return -1; 00045 else 00046 return b[0]; 00047 } 00051 inline int32_t read(const TCHAR*& start){ 00052 int32_t nread = reader->read(start,1,0); 00053 if ( nread < -1 ) //if not eof 00054 _CLTHROWA(CL_ERR_IO,reader->getError()); 00055 else 00056 return nread; 00057 } 00058 inline int32_t read(const TCHAR*& start, int32_t len){ 00059 int32_t nread = reader->read(start, len, len); 00060 if ( nread < -1 ) //if not eof 00061 _CLTHROWA(CL_ERR_IO,reader->getError()); 00062 else 00063 return nread; 00064 } 00065 inline int64_t skip(int64_t ntoskip){ 00066 int64_t skipped = reader->skip(ntoskip); 00067 if ( skipped < 0 ) 00068 _CLTHROWA(CL_ERR_IO,reader->getError()); 00069 else 00070 return skipped; 00071 } 00072 inline int64_t mark(int32_t readAheadlimit){ 00073 int64_t pos = reader->mark(readAheadlimit); 00074 if ( pos < 0 ) 00075 _CLTHROWA(CL_ERR_IO,reader->getError()); 00076 else 00077 return pos; 00078 } 00079 int64_t reset(int64_t pos){ 00080 int64_t r = reader->reset(pos); 00081 if ( r < 0 ) 00082 _CLTHROWA(CL_ERR_IO,reader->getError()); 00083 else 00084 return r; 00085 } 00086 }; 00087 00089 class StringReader: public Reader{ 00090 public: 00091 StringReader ( const TCHAR* value ); 00092 StringReader ( const TCHAR* value, const int32_t length ); 00093 StringReader ( const TCHAR* value, const int32_t length, bool copyData ); 00094 ~StringReader(); 00095 }; 00096 00101 class SimpleInputStreamReader: public jstreams::BufferedInputStream<TCHAR>{ 00102 int32_t decode(TCHAR* start, int32_t space); 00103 int encoding; 00104 enum{ 00105 ASCII=1, 00106 UTF8=2, 00107 UCS2_LE=3 00108 }; 00109 bool finishedDecoding; 00110 jstreams::StreamBase<char>* input; 00111 int32_t charsLeft; 00112 00113 jstreams::InputStreamBuffer<char> charbuf; 00114 int32_t fillBuffer(TCHAR* start, int32_t space); 00115 public: 00116 SimpleInputStreamReader(jstreams::StreamBase<char> *i, const char* encoding); 00117 ~SimpleInputStreamReader(); 00118 }; 00119 00124 class FileReader: public Reader{ 00125 jstreams::FileInputStream* input; 00126 public: 00127 FileReader ( const char* path, const char* enc, 00128 const int32_t cachelen = 13, 00129 const int32_t cachebuff = 14 ); //todo: optimise these cache values 00130 ~FileReader (); 00131 00132 int32_t read(const TCHAR*& start, int32_t _min, int32_t _max); 00133 int64_t mark(int32_t readlimit); 00134 int64_t reset(int64_t); 00135 }; 00136 00137 CL_NS_END 00138 #endif