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 INPUTSTREAMBUFFER_H 00027 #define INPUTSTREAMBUFFER_H 00028 00029 #include <cstdlib> 00030 00031 namespace jstreams { 00032 00033 template <class T> 00034 class InputStreamBuffer { 00035 private: 00036 public: 00037 T* start; 00038 int32_t size; 00039 T* readPos; 00040 int32_t avail; 00041 00042 InputStreamBuffer(); 00043 ~InputStreamBuffer(); 00044 void setSize(int32_t size); 00045 int32_t read(const T*& start, int32_t max=0); 00046 00051 int32_t makeSpace(int32_t needed); 00052 }; 00053 00054 template <class T> 00055 InputStreamBuffer<T>::InputStreamBuffer() { 00056 readPos = start = 0; 00057 size = avail = 0; 00058 } 00059 template <class T> 00060 InputStreamBuffer<T>::~InputStreamBuffer() { 00061 free(start); 00062 } 00063 template <class T> 00064 void 00065 InputStreamBuffer<T>::setSize(int32_t size) { 00066 // store pointer information 00067 int32_t offset = (int32_t)(readPos - start); 00068 00069 // allocate memory in the buffer 00070 if ( start == 0 ) 00071 start = (T*)malloc(size*sizeof(T)); 00072 else 00073 start = (T*)realloc(start, size*sizeof(T)); 00074 this->size = size; 00075 00076 // restore pointer information 00077 readPos = start + offset; 00078 } 00079 template <class T> 00080 int32_t 00081 InputStreamBuffer<T>::makeSpace(int32_t needed) { 00082 // determine how much space is available for writing 00083 int32_t space = size - ((int32_t)(readPos - start)) - avail; 00084 if (space >= needed) { 00085 // there's enough space 00086 return space; 00087 } 00088 00089 if (avail) { 00090 if (readPos != start) { 00091 // printf("moving\n"); 00092 // move data to the start of the buffer 00093 memmove(start, readPos, avail*sizeof(T)); 00094 space += (int32_t)(readPos - start); 00095 readPos = start; 00096 } 00097 } else { 00098 // we may start writing at the start of the buffer 00099 readPos = start; 00100 space = size; 00101 } 00102 if (space >= needed) { 00103 // there's enough space now 00104 return space; 00105 } 00106 00107 // still not enough space, we have to allocate more 00108 // printf("resize %i %i %i %i %i\n", avail, needed, space, size + needed - space, size); 00109 setSize(size + needed - space); 00110 return needed; 00111 } 00112 template <class T> 00113 int32_t 00114 InputStreamBuffer<T>::read(const T*& start, int32_t max) { 00115 start = readPos; 00116 if (max <= 0 || max > avail) { 00117 max = avail; 00118 } 00119 readPos += max; 00120 avail -= max; 00121 return max; 00122 } 00123 00124 } // end namespace jstreams 00125 00126 #endif