File: mmapfileinputstream.cpp

package info (click to toggle)
strigi 0.7.8-1.2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 5,556 kB
  • ctags: 6,064
  • sloc: cpp: 41,963; ansic: 1,199; perl: 483; java: 367; python: 345; xml: 177; sh: 150; makefile: 27
file content (106 lines) | stat: -rw-r--r-- 3,042 bytes parent folder | download | duplicates (2)
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
/* This file is part of Strigi Desktop Search
 *
 * Copyright (C) 2009 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 "mmapfileinputstream.h"
#include <config.h>
#include <strigi/strigiconfig.h>
#include <fcntl.h>
#include <cerrno>
#include <cstring>
#include <sys/mman.h>
#include <sys/stat.h>
#include <algorithm>

using namespace Strigi;
using namespace std;

MMapFileInputStream::MMapFileInputStream(const char* filepath) {
    int fd = ::open(filepath, O_RDONLY);
    struct stat sb;
    if (fd == -1 || fstat(fd, &sb) == -1) {
        // handle error
        buffer = 0;
        m_error = "Could not read file '";
        m_error += filepath;
        m_error += "': ";
        m_error += strerror(errno);
        m_status = Error;
        close(fd);
        return;
    }
    m_size = sb.st_size;
    if (m_size > 0) {
        buffer = (const char*)mmap(NULL, m_size, PROT_READ, MAP_PRIVATE, fd, 0);
    } else {
        buffer = 0;
    }
    close(fd);
    if (buffer == (const char*)MAP_FAILED) {
        // handle error
        buffer = 0;
        m_error = "Could not read file '";
        m_error += filepath;
        m_error += "': ";
        m_error += strerror(errno);
        m_status = Error;
        return;
    }
}
MMapFileInputStream::~MMapFileInputStream() {
    if (buffer) {
        munmap((void*)buffer, m_size);
    }
}
int32_t
MMapFileInputStream::read(const char*& start, int32_t _min, int32_t _max) {
    if (m_status == Error) return -2;
    if (m_status == Eof) return -1;
    int32_t n = max(_min, _max);
    if (n >= m_size - m_position) {
        m_status = Eof;
        n = (int32_t)(m_size - m_position);
    }
    start = buffer + m_position;
    m_position += n;
    return n;
}
int64_t
MMapFileInputStream::skip(int64_t ntoskip) {
    if (ntoskip >= m_size - m_position) {
        ntoskip = m_size - m_position;
        m_status = Eof;
    }
    m_position += ntoskip;
    return ntoskip;
}
int64_t
MMapFileInputStream::reset(int64_t pos) {
    if (m_status == Error) return -2;
    if (pos >= m_size) {
        m_status = Eof;
        m_position = m_size;
    } else if (pos < 0) {
        m_position = 0;
        m_status = Ok;
    } else {
        m_position = pos;
        m_status = Ok;
    }
    return m_position;
}