1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
/* This file is part of Strigi Desktop Search
*
* Copyright (C) 2006 Jos van den Oever <jos@vandenoever.info>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <strigi/subinputstream.h>
#include <strigi/strigiconfig.h>
#include <iostream>
#include <cassert>
using namespace std;
using namespace Strigi;
SubInputStream::SubInputStream(InputStream *i, int64_t length)
: m_offset(i->position()), m_input(i) {
assert(length >= -1);
// fprintf(stderr, "substream m_offset: %li\n", m_offset);
m_size = length;
}
int32_t
SubInputStream::read(const char*& start, int32_t min, int32_t max) {
if (m_size != -1) {
const int64_t left = m_size - m_position;
if (left == 0) {
m_status = Eof;
return -1;
}
// restrict the amount of data that can be read
if (min > left) min = (int32_t)left;
if (max < min || max > left) {
max = (int32_t)left;
}
}
int32_t nread = m_input->read(start, min, max);
assert(max < min || nread <= max);
if (nread < -1) {
fprintf(stderr, "substream too short.\n");
m_status = Error;
m_error = m_input->error();
} else if (nread < min) {
if (m_size == -1) {
m_status = Eof;
if (nread > 0) {
m_position += nread;
m_size = m_position;
}
} else {
// fprintf(stderr, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! nread %i min %i max %i m_size %li\n", nread, min, max, m_size);
// fprintf(stderr, "pos %li parentpos %li\n", m_position, m_input->position());
// fprintf(stderr, "status: %i error: %s\n", m_input->status(), m_input->error());
// we expected data but didn't get enough so that's an error
m_status = Error;
m_error = "Premature end of stream\n";
nread = -2;
}
} else {
m_position += nread;
if (m_position == m_size) {
m_status = Eof;
}
}
return nread;
}
int64_t
SubInputStream::reset(int64_t newpos) {
assert(newpos >= 0);
// fprintf(stderr, "subreset pos: %li newpos: %li m_offset: %li m_size: %li\n",
// m_position, newpos, m_offset, m_size);
m_position = m_input->reset(newpos + m_offset);
if (m_position < m_offset) {
cerr << "########### m_position " << m_position << " newpos " << newpos
<< endl;
m_status = Error;
m_error = m_input->error();
} else {
m_position -= m_offset;
m_status = m_input->status();
}
return m_position;
}
int64_t
SubInputStream::skip(int64_t ntoskip) {
// fprintf(stderr, "subskip pos: %li ntoskip: %li m_offset: %li m_size: %li\n", m_position, ntoskip, m_offset, m_size);
if (m_size == m_position) {
m_status = Eof;
return -1;
}
if (ntoskip == 0) return 0;
if (m_size != -1) {
const int64_t left = m_size - m_position;
// restrict the amount of data that can be skipped
if (ntoskip > left) {
ntoskip = left;
}
}
int64_t skipped = m_input->skip(ntoskip);
if (m_input->status() == Error) {
m_status = Error;
m_error = m_input->error();
} else {
m_position += skipped;
if (m_position == m_size) {
m_status = Eof;
} else if (skipped <= 0) {
m_status = Error;
m_error = "Premature end of stream\n";
skipped = -2;
}
}
return skipped;
}
|