1
/* This file is part of Strigi Desktop Search
3
* Copyright (C) 2009 Jos van den Oever <jos@vandenoever.info>
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.
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.
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.
20
#include "skippingfileinputstream.h"
22
#include <strigi/strigiconfig.h>
30
using namespace Strigi;
33
SkippingFileInputStream::SkippingFileInputStream(const char* filepath) :p(0) {
38
m_error = "No filename was provided.";
42
FILE* f = fopen(filepath, "rb");
46
SkippingFileInputStream::open(FILE* f, const char* path) {
47
// try to open the file for reading
49
filepath.assign(path);
52
m_error = "Could not read file '";
55
m_error += strerror(errno);
59
// determine file size. if the stream is not seekable, the size will be -1
60
if (fseeko(file, 0, SEEK_END) == -1) {
63
m_size = ftello(file);
64
fseeko(file, 0, SEEK_SET);
65
// if the file has size 0, make sure that it's really empty
66
// this is useful for filesystems like /proc that report files as size 0
67
// for files that do contain content
70
size_t n = fread(dummy, 1, 1, file);
73
fseeko(file, 0, SEEK_SET);
82
SkippingFileInputStream::~SkippingFileInputStream() {
86
m_error = "Could not close file '" + filepath + "'.";
92
SkippingFileInputStream::read(const char*& start, int32_t _min, int32_t _max) {
93
int32_t n = max(_min, _max);
95
buffer = (char*)realloc(buffer, n);
97
int32_t nr = (int32_t)fread(buffer, 1, n, file);
98
m_position = ftell(file);
113
SkippingFileInputStream::skip(int64_t ntoskip) {
114
int64_t oldpos = m_position;
115
if (reset(m_position + ntoskip) == -2) return -2;
116
return m_position - oldpos;
119
SkippingFileInputStream::reset(int64_t pos) {
120
if (m_size >= 0 && pos > m_size) pos = m_size;
121
if (fseek(file, pos, SEEK_SET)) {
125
m_position = ftell(file);
126
m_status = (m_position == m_size) ?Eof :Ok;