summaryrefslogtreecommitdiffstats
path: root/src/common/resources/inputstreambuffer.cpp
blob: cfa06fb8feff98674098d8fcde7733687c3bfe96 (plain)
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
126
127
128
129
// * This file is part of the COLOBOT source code
// * Copyright (C) 2014 Polish Portal of Colobot (PPC)
// *
// * This program is free software: you can redistribute it and/or modify
// * it under the terms of the GNU General Public License as published by
// * the Free Software Foundation, either version 3 of the License, or
// * (at your option) any later version.
// *
// * This program 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 General Public License for more details.
// *
// * You should have received a copy of the GNU General Public License
// * along with this program. If not, see  http://www.gnu.org/licenses/.

#include "common/resources/inputstreambuffer.h"

#include <stdexcept>
#include <sstream>

CInputStreamBuffer::CInputStreamBuffer(size_t buffer_size) : m_buffer_size(buffer_size)
{
    if (buffer_size <= 0)
    {
        throw std::runtime_error("File buffer must be larger then 0 bytes");
    }

    m_buffer = new char[buffer_size];
    m_file = nullptr;
}


CInputStreamBuffer::~CInputStreamBuffer()
{
    close();
    delete m_buffer;
}


void CInputStreamBuffer::open(const std::string &filename)
{
    if (PHYSFS_isInit())
        m_file = PHYSFS_openRead(filename.c_str());
}


void CInputStreamBuffer::close()
{
    if (is_open())
        PHYSFS_close(m_file);
}


bool CInputStreamBuffer::is_open()
{
    return m_file;
}


size_t CInputStreamBuffer::size()
{
    return PHYSFS_fileLength(m_file);
}


std::streambuf::int_type CInputStreamBuffer::underflow()
{
    if (gptr() < egptr())
        return traits_type::to_int_type(*gptr());

    if (PHYSFS_eof(m_file))
        return traits_type::eof();

    PHYSFS_sint64 read_count = PHYSFS_read(m_file, m_buffer, sizeof(char), m_buffer_size);
    if (read_count <= 0)
        return traits_type::eof();

    setg(m_buffer, m_buffer, m_buffer + read_count);

    return traits_type::to_int_type(*gptr());
}


std::streampos CInputStreamBuffer::seekpos(std::streampos sp, std::ios_base::openmode which)
{
    return seekoff(off_type(sp), std::ios_base::beg, which);
}


std::streampos CInputStreamBuffer::seekoff(std::streamoff off, std::ios_base::seekdir way, std::ios_base::openmode which)
{
    /* A bit of explanation:
       We are reading file by m_buffer_size parts so our 3 internal pointers will be
       * eback (not used here) - start of block
       * gptr - position of read cursor in block
       * egtpr - end of block
       off argument is relative to way */

    std::streamoff new_position;

    switch (way)
    {
        case std::ios_base::beg:
            new_position = off;
            break;

        case std::ios_base::cur:
            // tell will give cursor at begining of block so we have to add where in block we currently are
            new_position = off + static_cast<off_type>(PHYSFS_tell(m_file)) - static_cast<off_type> (egptr() - gptr());
            break;

        case std::ios_base::end:
            new_position = off + static_cast<off_type>(PHYSFS_fileLength(m_file));
            break;

        default:
            break;
    }

    if (PHYSFS_seek(m_file, new_position))
    {
        setg(m_buffer, m_buffer, m_buffer); // reset buffer

        return pos_type(new_position);
    }

    return pos_type(off_type(-1));
}