strigi/src/streams
streambase.h
Go to the documentation of this file.00001 /* This file is part of Strigi Desktop Search 00002 * 00003 * Copyright (C) 2006 Jos van den Oever <jos@vandenoever.info> 00004 * 00005 * This library is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU Library General Public 00007 * License as published by the Free Software Foundation; either 00008 * version 2 of the License, or (at your option) any later version. 00009 * 00010 * This library is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 * Library General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU Library General Public License 00016 * along with this library; see the file COPYING.LIB. If not, write to 00017 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 * Boston, MA 02110-1301, USA. 00019 */ 00020 #ifndef STRIGI_STREAMBASE_H 00021 #define STRIGI_STREAMBASE_H 00022 00023 #include <stdio.h> 00024 #include <string> 00025 #include <strigi/strigiconfig.h> 00026 00027 #define INT32MAX 0x7FFFFFFFL 00028 00029 namespace Strigi { 00030 00032 enum StreamStatus { 00033 Ok , 00034 Eof , 00035 Error 00036 }; 00037 00038 // java mapping: long=int64, int=int32, byte=uint8_t 00052 class STREAMS_EXPORT StreamBaseBase { //krazy:exclude=dpointer 00053 protected: 00055 int64_t m_size; 00057 int64_t m_position; 00062 std::string m_error; 00064 StreamStatus m_status; 00065 public: 00069 StreamBaseBase() :m_size(-1), m_position(0), m_status(Ok) {} 00073 virtual ~StreamBaseBase() {} 00078 const char* error() const { return m_error.c_str(); } 00082 StreamStatus status() const { return m_status; } 00087 int64_t position() const { return m_position; } 00098 int64_t size() const { return m_size; } 00099 }; 00100 00113 template <class T> 00114 class StreamBase : public StreamBaseBase { 00115 public: 00116 StreamBase() { } 00117 virtual ~StreamBase(){} 00151 virtual int32_t read(const T*& start, int32_t min, int32_t max) = 0; 00165 virtual int64_t skip(int64_t ntoskip); 00191 virtual int64_t reset(int64_t pos) = 0; 00192 }; 00193 00194 00196 typedef StreamBase<char> InputStream; 00197 00199 typedef StreamBase<wchar_t> Reader; 00200 00201 00202 template <class T> 00203 int64_t 00204 StreamBase<T>::skip(int64_t ntoskip) { 00205 const T* begin; 00206 int32_t nread; 00207 int64_t skipped = 0; 00208 while (ntoskip > 0) { 00209 // make sure we do not overflow uint32_t 00210 int32_t maxstep = (int32_t)((ntoskip > 10000000) 00211 ?10000000 :ntoskip); 00212 // the default implementation is to simply read the data that we want 00213 // to skip 00214 nread = read(begin, 1, maxstep); 00215 if (nread < -1 ) { 00216 // an error occurred 00217 return nread; 00218 } else if (nread < 1) { 00219 // the end of the stream was encountered 00220 ntoskip = 0; 00221 } else { 00222 skipped += nread; 00223 ntoskip -= nread; 00224 } 00225 } 00226 return skipped; 00227 } 00228 00229 } // end namespace Strigi 00230 00231 #endif
KDE 4.4 API Reference