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 STREAMBASE_H 00027 #define STREAMBASE_H 00028 00029 #include <string> 00030 00031 #define INT32MAX 0x7FFFFFFFL 00032 00033 namespace jstreams { 00034 00035 enum StreamStatus { Ok, Eof, Error }; 00036 00048 // java mapping: long=int64, int=int32, byte=uint8_t 00049 template <class T> 00050 class StreamBase { 00051 protected: 00052 int64_t size; 00053 int64_t position; 00054 std::string error; 00055 StreamStatus status; 00056 public: 00057 StreamBase() :size(-1), position(0), status(Ok){ } 00058 virtual ~StreamBase(){} 00063 const char* getError() const { return error.c_str(); } 00064 StreamStatus getStatus() const { return status; } 00069 int64_t getPosition() const { return position; } 00076 int64_t getSize() const { return size; } 00093 virtual int32_t read(const T*& start, int32_t min, int32_t max) = 0; 00099 virtual int64_t skip(int64_t ntoskip); 00109 virtual int64_t reset(int64_t pos) = 0; 00110 int64_t mark(int32_t readlimit) { 00111 int64_t p = getPosition(); 00112 const T* ptr; 00113 read(ptr, readlimit, -1); 00114 return reset(p); 00115 } 00116 }; 00117 #define SKIPSTEP 1024 00118 template <class T> 00119 int64_t 00120 StreamBase<T>::skip(int64_t ntoskip) { 00121 const T *begin; 00122 int32_t nread; 00123 int64_t skipped = 0; 00124 while (ntoskip) { 00125 int32_t step = (int32_t)((ntoskip > SKIPSTEP) ?SKIPSTEP :ntoskip); 00126 nread = read(begin, 1, step); 00127 if (nread < -1 ) { 00128 // an error occurred 00129 return nread; 00130 } else if (nread < 1) { 00131 ntoskip = 0; 00132 } else { 00133 skipped += nread; 00134 ntoskip -= nread; 00135 } 00136 } 00137 return skipped; 00138 } 00139 00140 } // end namespace jstreams 00141 00142 #endif