CLucene - a full-featured, c++ search engine
API Documentation
00001 /*------------------------------------------------------------------------------ 00002 * Copyright (C) 2003-2006 Jos van den Oever 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 /* This file is part of Strigi Desktop Search 00008 * 00009 * Copyright (C) 2006 Jos van den Oever <jos@vandenoever.info> 00010 * 00011 * This library is free software; you can redistribute it and/or 00012 * modify it under the terms of the GNU Library General Public 00013 * License as published by the Free Software Foundation; either 00014 * version 2 of the License, or (at your option) any later version. 00015 * 00016 * This library is distributed in the hope that it will be useful, 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00019 * Library General Public License for more details. 00020 * 00021 * You should have received a copy of the GNU Library General Public License 00022 * along with this library; see the file COPYING.LIB. If not, write to 00023 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00024 * Boston, MA 02110-1301, USA. 00025 */ 00026 #ifndef STRINGREADER_H 00027 #define STRINGREADER_H 00028 00035 #include "streambase.h" 00036 00037 namespace jstreams { 00038 00039 template <class T> 00040 class StringReader : public StreamBase<T> { 00041 private: 00042 int64_t markpt; 00043 T* data; 00044 bool dataowner; 00045 StringReader(const StringReader<T>&); 00046 void operator=(const StringReader<T>&); 00047 public: 00048 StringReader(const T* value, int32_t length = -1, bool copy = true); 00049 ~StringReader(); 00050 int32_t read(const T*& start, int32_t min, int32_t max); 00051 int64_t skip(int64_t ntoskip); 00052 int64_t reset(int64_t pos); 00053 }; 00054 00055 typedef StringReader<char> StringInputStream; 00056 00057 template <class T> 00058 StringReader<T>::StringReader(const T* value, int32_t length, bool copy) 00059 : markpt(0), dataowner(copy) { 00060 if (length < 0) { 00061 length = 0; 00062 while (value[length] != '\0') { 00063 length++; 00064 } 00065 } 00066 StreamBase<T>::size = length; 00067 if (copy) { 00068 data = new T[length+1]; 00069 size_t s = (size_t)(length*sizeof(T)); 00070 memcpy(data, value, s); 00071 data[length] = 0; 00072 } else { 00073 // casting away const is ok, because we don't write anyway 00074 data = (T*)value; 00075 } 00076 } 00077 template <class T> 00078 StringReader<T>::~StringReader() { 00079 if (dataowner) { 00080 delete [] data; 00081 } 00082 } 00083 template <class T> 00084 int32_t 00085 StringReader<T>::read(const T*& start, int32_t min, int32_t max) { 00086 int64_t left = StreamBase<T>::size - StreamBase<T>::position; 00087 if (left == 0) { 00088 StreamBase<T>::status = Eof; 00089 return -1; 00090 } 00091 if (min < 0) min = 0; 00092 int32_t nread = (int32_t)((max > left || max < 1) ?left :max); 00093 start = data + StreamBase<T>::position; 00094 StreamBase<T>::position += nread; 00095 if (StreamBase<T>::position == StreamBase<T>::size) { 00096 StreamBase<T>::status = Eof; 00097 } 00098 return nread; 00099 } 00100 template <class T> 00101 int64_t 00102 StringReader<T>::skip(int64_t ntoskip) { 00103 const T* start; 00104 return read(start, ntoskip, ntoskip); 00105 } 00106 template <class T> 00107 int64_t 00108 StringReader<T>::reset(int64_t newpos) { 00109 if (newpos < 0) { 00110 StreamBase<T>::status = Ok; 00111 StreamBase<T>::position = 0; 00112 } else if (newpos < StreamBase<T>::size) { 00113 StreamBase<T>::status = Ok; 00114 StreamBase<T>::position = newpos; 00115 } else { 00116 StreamBase<T>::position = StreamBase<T>::size; 00117 StreamBase<T>::status = Eof; 00118 } 00119 return StreamBase<T>::position; 00120 } 00121 00122 } // end namespace jstreams 00123 00124 #endif