1
/*------------------------------------------------------------------------------
2
* Copyright (C) 2003-2006 Jos van den Oever
4
* Distributable under the terms of either the Apache License (Version 2.0) or
5
* the GNU Lesser General Public License, as specified in the COPYING file.
6
------------------------------------------------------------------------------*/
7
/* This file is part of Strigi Desktop Search
9
* Copyright (C) 2006 Jos van den Oever <jos@vandenoever.info>
11
* This library is free software; you can redistribute it and/or
12
* modify it under the terms of the GNU Library General Public
13
* License as published by the Free Software Foundation; either
14
* version 2 of the License, or (at your option) any later version.
16
* This library is distributed in the hope that it will be useful,
17
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19
* Library General Public License for more details.
21
* You should have received a copy of the GNU Library General Public License
22
* along with this library; see the file COPYING.LIB. If not, write to
23
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24
* Boston, MA 02110-1301, USA.
26
#ifndef INPUTSTREAMBUFFER_H
27
#define INPUTSTREAMBUFFER_H
34
class InputStreamBuffer {
44
void setSize(int32_t size);
45
int32_t read(const T*& start, int32_t max=0);
48
* This function prepares the buffer for a new write.
49
* returns the number of available places.
51
int32_t makeSpace(int32_t needed);
55
InputStreamBuffer<T>::InputStreamBuffer() {
60
InputStreamBuffer<T>::~InputStreamBuffer() {
65
InputStreamBuffer<T>::setSize(int32_t size) {
66
// store pointer information
67
int32_t offset = (int32_t)(readPos - start);
69
// allocate memory in the buffer
71
start = (T*)malloc(size*sizeof(T));
73
start = (T*)realloc(start, size*sizeof(T));
76
// restore pointer information
77
readPos = start + offset;
81
InputStreamBuffer<T>::makeSpace(int32_t needed) {
82
// determine how much space is available for writing
83
int32_t space = size - ((int32_t)(readPos - start)) - avail;
84
if (space >= needed) {
85
// there's enough space
90
if (readPos != start) {
91
// printf("moving\n");
92
// move data to the start of the buffer
93
memmove(start, readPos, avail*sizeof(T));
94
space += (int32_t)(readPos - start);
98
// we may start writing at the start of the buffer
102
if (space >= needed) {
103
// there's enough space now
107
// still not enough space, we have to allocate more
108
// printf("resize %i %i %i %i %i\n", avail, needed, space, size + needed - space, size);
109
setSize(size + needed - space);
114
InputStreamBuffer<T>::read(const T*& start, int32_t max) {
116
if (max <= 0 || max > avail) {
124
} // end namespace jstreams