~ubuntu-branches/ubuntu/oneiric/strigi/oneiric

« back to all changes in this revision

Viewing changes to libstreams/lib/stringterminatedsubstream.cpp

  • Committer: Package Import Robot
  • Author(s): Felix Geyer
  • Date: 2011-09-24 17:12:15 UTC
  • mfrom: (1.2.6 upstream)
  • mto: This revision was merged to the branch mainline in revision 44.
  • Revision ID: package-import@ubuntu.com-20110924171215-zmbi1f77jntvz65h
Tags: upstream-0.7.6
ImportĀ upstreamĀ versionĀ 0.7.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of Strigi Desktop Search
 
2
 *
 
3
 * Copyright (C) 2006 Jos van den Oever <jos@vandenoever.info>
 
4
 *
 
5
 * This library is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU Library General Public
 
7
 * License as published by the Free Software Foundation; either
 
8
 * version 2 of the License, or (at your option) any later version.
 
9
 *
 
10
 * This library is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
 * Library General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU Library General Public License
 
16
 * along with this library; see the file COPYING.LIB.  If not, write to
 
17
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
18
 * Boston, MA 02110-1301, USA.
 
19
 */
 
20
#include <strigi/stringterminatedsubstream.h>
 
21
#include <strigi/strigiconfig.h>
 
22
#include <strigi/kmpsearcher.h>
 
23
#include <cassert>
 
24
#include <iostream>
 
25
 
 
26
using namespace std;
 
27
using namespace Strigi;
 
28
 
 
29
// TODO add a mechanism that avoid searching for a stop point again after a
 
30
// reset
 
31
 
 
32
class StringTerminatedSubStream::Private {
 
33
public:
 
34
    KmpSearcher m_searcher;
 
35
    const int64_t m_offset;
 
36
    int64_t furthest;
 
37
    InputStream* m_input;
 
38
 
 
39
    Private(InputStream* i, const std::string& terminator)
 
40
            : m_offset(i->position()), furthest(0), m_input(i) {
 
41
        m_searcher.setQuery(terminator);
 
42
    }
 
43
};
 
44
 
 
45
StringTerminatedSubStream::StringTerminatedSubStream(InputStream* i,
 
46
        const std::string& terminator) :p(new Private(i, terminator)) {
 
47
}
 
48
StringTerminatedSubStream::~StringTerminatedSubStream() {
 
49
    delete p;
 
50
}
 
51
int64_t
 
52
StringTerminatedSubStream::offset() const {
 
53
    return p->m_offset;
 
54
}
 
55
int32_t
 
56
StringTerminatedSubStream::read(const char*& start, int32_t min, int32_t max) {
 
57
    if (m_status == Eof) return -1;
 
58
    if (m_status == Error) return -2;
 
59
    int32_t nread;
 
60
 
 
61
    // check if we already read enough
 
62
    const int64_t pos = p->m_input->position();
 
63
    nread = (int32_t)(p->furthest - pos);
 
64
    if (min <= nread) {
 
65
        if (max <= 0 || max > nread) {
 
66
            max = nread;
 
67
        }
 
68
        nread = p->m_input->read(start, min, max);
 
69
        assert(nread >= -1);
 
70
        if (nread > 0) {
 
71
            m_position += nread;
 
72
            if (m_position == m_size) {
 
73
                m_status = Eof;
 
74
            }
 
75
        }
 
76
        return nread;
 
77
    }
 
78
 
 
79
    // convenience parameter
 
80
    int32_t tl = p->m_searcher.queryLength();
 
81
 
 
82
    // increase min and max to accommodate for the length of the terminator
 
83
    int32_t tlmin = min;
 
84
    int32_t tlmax = max;
 
85
    if (tlmin == 0) {
 
86
        tlmin = 1 + tl;
 
87
    } else {
 
88
        tlmin += tl;
 
89
    }
 
90
    if (tlmax > 0 && tlmax < tlmin) tlmax = tlmin;
 
91
 
 
92
    nread = p->m_input->read(start, tlmin, tlmax);
 
93
    if (nread == -1) {
 
94
        m_status = Eof;
 
95
        return nread;
 
96
    }
 
97
    if (nread < -1) {
 
98
        m_status = Error;
 
99
        m_error = p->m_input->error();
 
100
        return nread;
 
101
    }
 
102
 
 
103
    const char* end = p->m_searcher.search(start, nread);
 
104
    if (end) {
 
105
        // the end signature was found
 
106
        nread = (int32_t)(end - start);
 
107
        // signal the end of stream at the next call
 
108
        m_status = Eof;
 
109
        // set input stream to point after the terminator
 
110
        p->m_input->reset(pos + nread + tl);
 
111
    } else if (nread >= tlmin) {
 
112
        // we are not at or near the end and read the required amount
 
113
        // reserve the last bit of buffer for rereading to match the terminator
 
114
        // in the next call
 
115
        nread -= tl;
 
116
        p->furthest = pos + nread;
 
117
        // we rewind, but the pointer 'start' will stay valid nontheless
 
118
        p->m_input->reset(pos + nread);
 
119
    } else if (max != 0 && nread > max) {
 
120
        // we are near the end of the stream but cannot pass all data
 
121
        // at once because the amount read is larger than the amount to pass
 
122
        p->furthest = pos + nread;
 
123
        p->m_input->reset(pos + max);
 
124
        nread = max;
 
125
    } else {
 
126
        // we are at the end of the stream, so no need to rewind
 
127
        // signal the end of stream at the next call
 
128
        m_status = Eof;
 
129
    }
 
130
    if (nread > 0) m_position += nread;
 
131
    if (m_status == Eof) {
 
132
        m_size = m_position;
 
133
    }
 
134
    return nread;
 
135
}
 
136
int64_t
 
137
StringTerminatedSubStream::reset(int64_t newpos) {
 
138
    m_position = p->m_input->reset(newpos + p->m_offset);
 
139
    if (m_position >= p->m_offset) {
 
140
        m_position -= p->m_offset;
 
141
        if (m_position != m_size) m_status = Ok;
 
142
    } else {
 
143
        // the stream is not positioned at a valid m_position
 
144
        m_status = Error;
 
145
        m_position = -1;
 
146
    }
 
147
    return m_position;
 
148
}