File: streambuf.cpp

package info (click to toggle)
odil 0.13.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,476 kB
  • sloc: cpp: 55,982; python: 3,947; javascript: 460; xml: 182; makefile: 99; sh: 36
file content (187 lines) | stat: -rw-r--r-- 4,110 bytes parent folder | download | duplicates (4)
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*************************************************************************
 * odil - Copyright (C) Universite de Strasbourg
 * Distributed under the terms of the CeCILL-B license, as published by
 * the CEA-CNRS-INRIA. Refer to the LICENSE file or to
 * http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
 * for details.
 ************************************************************************/

#include "streambuf.h"

#include <algorithm>
#include <ios>
#include <streambuf>
#include <string>
#include <vector>

#include <pybind11/pybind11.h>

#include "odil/Exception.h"

#include "opaque_types.h"
#include "type_casters.h"

#if PY_MAJOR_VERSION >= 3
    #define FromStringAndSize(v, len) PyBytes_FromStringAndSize(v, len)
#else
    #define IS_PY2
    #define FromStringAndSize(v, len) PyString_FromStringAndSize(v, len)
#endif

namespace odil
{

namespace wrappers
{

namespace python
{

streambuf
::streambuf(pybind11::object object, std::string::size_type buffer_size)
: _object(object),
  _buffer_size(buffer_size), _buffer(), _current(std::string::npos)
{
    this->setg(nullptr, nullptr, nullptr);
    this->setp(nullptr, nullptr);
}

// Python objects do not differentiate between input and output sequence: the
// "which" parameter is unused.
std::streambuf::pos_type
streambuf
::seekoff(
    std::streambuf::off_type off, std::ios_base::seekdir dir,
    std::ios_base::openmode /* which */)
{
    std::streambuf::off_type adjusted_offset;
    if(dir == std::ios_base::cur && this->_current != std::string::npos)
    {
        // We need to adjust the offset since the position of the Python
        // file-like object is at the end of the buffer.
        std::streambuf::off_type const position_offset =
            this->_buffer.size()-this->_current;
        adjusted_offset = off-position_offset;
    }
    else
    {
        // Either
        // - beg or end,
        // - or at the end of buffer (_current == std::string::npos)
        // In those cases, the semantics don't change
        adjusted_offset = off;
    }

    int whence;
    if(dir == std::ios::beg)
    {
        whence = 0;
    }
    else if(dir == std::ios::cur)
    {
        whence = 1;
    }
    else if(dir == std::ios::end)
    {
        whence = 2;
    }
    else
    {
        throw Exception("Invalid direction");
    }

    this->_object.attr("seek")(adjusted_offset, whence);
    this->_update_buffer();

    return (
        pybind11::cast<int>(this->_object.attr("tell")())
        - this->_buffer.size());
}

std::streambuf::pos_type
streambuf
::seekpos(std::streambuf::pos_type pos, std::ios_base::openmode which)
{
    return this->seekoff(pos, std::ios_base::beg, which);
}

int
streambuf
::underflow()
{
    if(this->_current == std::string::npos)
    {
        this->_update_buffer();
    }

    if(this->_current == std::string::npos)
    {
        return std::char_traits<char>::eof();
    }
    else
    {
        auto const character = this->_buffer.at(this->_current);
        return std::char_traits<char>::to_int_type(character);
    }
}

int
streambuf
::uflow()
{
    auto const character = this->underflow();
    if(character != std::char_traits<char>::eof())
    {
        ++this->_current;
        if(this->_current >= this->_buffer.size())
        {
            this->_current = std::string::npos;
        }
    }
    return character;
}

int
streambuf
::overflow(int ch)
{
    if(ch != std::char_traits<char>::eof())
    {
        char const cast_ch(ch);
        pybind11::bytes bytes_ch(&cast_ch, 1);
        this->_object.attr("write")(bytes_ch);
    }
    return ch;
}

void
streambuf
::_update_buffer()
{
    auto data = this->_object.attr("read")(this->_buffer_size);
    if(pybind11::len(data) == 0)
    {
        // EOF
        this->_current = std::string::npos;
    }
    else
    {
        this->_buffer = pybind11::cast<std::string>(data);
        this->_current = 0;
    }
}

}

}

}

void wrap_iostream(pybind11::module & m)
{
    using namespace pybind11;
    using namespace odil::wrappers::python;

    class_<iostream>(m, "iostream")
        .def(init<object>());
}