strigi/src/streams
dataeventinputstream.cpp
Go to the documentation of this file.00001 /* This file is part of Strigi Desktop Search 00002 * 00003 * Copyright (C) 2007 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 #include "dataeventinputstream.h" 00021 #include <strigi/strigiconfig.h> 00022 #include <iostream> 00023 #include <cassert> 00024 00025 using namespace std; 00026 using namespace Strigi; 00027 00028 DataEventInputStream::DataEventInputStream(InputStream *i, 00029 DataEventHandler& h) :input(i), handler(h) { 00030 assert(input->position() == 0); 00031 m_size = input->size(); 00032 totalread = 0; 00033 m_status = Ok; 00034 finished = false; 00035 } 00036 int32_t 00037 DataEventInputStream::read(const char*& start, int32_t min, int32_t max) { 00038 // fprintf(stderr, "input->position(): %lli\n", input->position()); 00039 int32_t nread = input->read(start, min, max); 00040 // fprintf(stderr, "%p pos: %lli min %i max %i nread %i\n", this, m_position, min, max, nread); 00041 if (nread < -1) { 00042 m_error = input->error(); 00043 m_status = Error; 00044 return -2; 00045 } 00046 if (nread > 0) { 00047 m_position += nread; 00048 if (totalread < m_position) { 00049 int32_t amount = (int32_t)(m_position - totalread); 00050 handler.handleData(start + nread - amount, amount); 00051 totalread = m_position; 00052 } 00053 } 00054 if (nread < min) { 00055 m_status = Eof; 00056 if (m_size == -1) { 00057 // fprintf(stderr, "set m_size: %lli\n", m_position); 00058 m_size = m_position; 00059 } 00060 #ifndef NDEBUG 00061 if (m_size != m_position || m_size != totalread) { 00062 cerr << "m_size: " << m_size << " m_position: " << m_position 00063 << " totalread: " << totalread << " nread: " << nread << endl; 00064 cerr << input->status() << " " << input->error() << endl; 00065 } 00066 #endif 00067 assert(m_size == m_position); 00068 assert(totalread == m_size); 00069 if (!finished) { 00070 finish(); 00071 finished = true; 00072 } 00073 } 00074 return nread; 00075 } 00076 int64_t 00077 DataEventInputStream::skip(int64_t ntoskip) { 00078 //fprintf(stderr, "skipping %lli\n", ntoskip); 00079 // we call the default implementation because it calls 00080 // read() which is required for sending the signals 00081 int64_t skipped = InputStream::skip(ntoskip); 00082 //const char*d; 00083 //int32_t skipped = read(d, ntoskip, ntoskip); 00084 return skipped; 00085 } 00086 int64_t 00087 DataEventInputStream::reset(int64_t np) { 00088 // fprintf(stderr, "DataEventInputStream::reset from %lli to %lli.\n", m_position, np); 00089 if (np > m_position) { 00090 // advance to the new position, using skip ensure we actually read 00091 // the files 00092 skip(np - m_position); 00093 return m_position; 00094 } 00095 // fprintf(stderr, "DataEventInputStream::reset\n"); 00096 int64_t newpos = input->reset(np); 00097 if (newpos < 0) { 00098 m_status = Error; 00099 m_error = input->error(); 00100 } else { 00101 m_status = (newpos == m_size) ?Eof :Ok; 00102 } 00103 // fprintf(stderr, "np %lli newpos %lli status %i\n", np, newpos, m_status); 00104 m_position = newpos; 00105 return newpos; 00106 } 00107 void 00108 DataEventInputStream::finish() { 00109 handler.handleEnd(); 00110 }
KDE 4.4 API Reference