summaryrefslogtreecommitdiff
path: root/vendor/jsoncons-0.99.2/jsoncons/ovectorstream.hpp
blob: e19f5085bca6ead184044f6e7f4a676076bd7595 (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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Copyright 2016 Daniel Parker
// Distributed under the Boost license, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

// See https://github.com/danielaparker/jsoncons for latest version

#ifndef JSONCONS_OVECTORSTREAM_HPP
#define JSONCONS_OVECTORSTREAM_HPP

#include <ios>
#include <ostream>
#include <string>
#include <cstddef>
#include <vector>
#include "jsoncons/jsoncons_config.hpp"

namespace jsoncons {

template< 
    class CharT, 
    class Traits = std::char_traits<CharT>
> class basic_ovectorstream;

template<class CharT, class CharTraits>
class basic_ovectorbuf
    : public std::basic_streambuf<CharT, CharTraits>
{
private:
    std::ios_base::openmode mode_;
    std::vector<CharT> buf_;

public:
    typedef CharT                                         char_type;
    typedef typename CharTraits::int_type                 int_type;
    typedef typename CharTraits::pos_type                 pos_type;
    typedef typename CharTraits::off_type                 off_type;
    typedef CharTraits                                    traits_type;
    typedef std::basic_streambuf<char_type, traits_type>  base_streambuf;

public:

    explicit basic_ovectorbuf(std::size_t length) JSONCONS_NOEXCEPT
        : base_streambuf(), 
          mode_(std::ios_base::out | std::ios_base::binary), 
          buf_(length)
    {  
        // Set write position to beginning of buffer.
        this->setp(buf_.data(), buf_.data() + buf_.size());
    }

    virtual ~basic_ovectorbuf() {}

    const CharT* data() const
    {
        return buf_.data();
    }

protected:
    int_type underflow() override
    {
        return this->gptr() != this->egptr() ?
               CharTraits::to_int_type(*this->gptr()) : CharTraits::eof();
    }

    int_type pbackfail(int_type c = CharTraits::eof()) override
    {
        if (this->gptr() != this->eback())
        {
            if (!CharTraits::eq_int_type(c, CharTraits::eof()))
            {
                if (CharTraits::eq(CharTraits::to_char_type(c), this->gptr()[-1]))
                {
                    this->gbump(-1);
                    return c;
                }
                this->gbump(-1);
                *this->gptr() = c;
                return c;
            } 
            else
            {
                this->gbump(-1);
                return CharTraits::not_eof(c);
            }
        } 
        else 
        {
            return CharTraits::eof();
        }
    }

    int_type overflow(int_type c = CharTraits::eof()) override
    {
        if (!CharTraits::eq_int_type(c, CharTraits::eof()))
        {
            size_t pos = buf_.size();
            buf_.resize(pos*2);
            this->setp(buf_.data(), buf_.data() + buf_.size());
            this->pubseekpos(pos, std::ios_base::out);
            *this->pptr() = CharTraits::to_char_type(c);
            this->pbump(1);
            this->pubsync();
            return c;
        } 
        else  
        {
            return CharTraits::not_eof(c);
        }
    }

    pos_type seekoff(off_type off, std::ios_base::seekdir dir,
                     std::ios_base::openmode mode = std::ios_base::out) override
    {
        (void)mode; // Always out

        std::streamoff newoff;
        switch (dir)
        {
        case std::ios_base::beg:
            newoff = 0;
            break;
        case std::ios_base::end:
            newoff = static_cast<std::streamoff>(buf_.size());
            break;
        case std::ios_base::cur:
            newoff = static_cast<std::streamoff>(this->pptr() - this->pbase());
            break;
        default:
            return pos_type(off_type(-1));
        }

        off += newoff;

        std::ptrdiff_t n = this->epptr() - this->pbase();

        if (off < 0 || off > n) return pos_type(off_type(-1));
        else
        {
            this->setp(this->pbase(), this->pbase() + n);
            this->pbump(static_cast<int>(off));
        }

        return pos_type(off);
    }

    pos_type seekoff_beg(off_type off)
    {
        std::ptrdiff_t n = this->epptr() - this->pbase();

        if (off < 0 || off > n) 
        {
            return pos_type(off_type(-1));
        }
        else
        {
            this->setp(this->pbase(), this->pbase() + n);
            this->pbump(static_cast<int>(off));
        }

        return pos_type(off);
    }

    pos_type seekpos(pos_type pos, std::ios_base::openmode mode
                     = std::ios_base::out) override
    {  
        (void)mode; // Always out

        return seekoff_beg(pos - pos_type(off_type(0)));
    }
};

template<class CharT, class CharTraits>
class basic_ovectorstream :
    private basic_ovectorbuf<CharT, CharTraits>,
    public std::basic_ostream<CharT, CharTraits>
{
public:
    typedef typename std::basic_ios
        <CharT, CharTraits>::char_type          char_type;
    typedef typename std::basic_ios<char_type, CharTraits>::int_type     int_type;
    typedef typename std::basic_ios<char_type, CharTraits>::pos_type     pos_type;
    typedef typename std::basic_ios<char_type, CharTraits>::off_type     off_type;
    typedef typename std::basic_ios<char_type, CharTraits>::traits_type  traits_type;

private:
    typedef basic_ovectorbuf<CharT, CharTraits>         base_ouputbuf;
    typedef std::basic_ios<char_type, CharTraits>      base_ios;
    typedef std::basic_ostream<char_type, CharTraits>  base_streambuf;
    base_ouputbuf& get_buf() {return *this;}
    const base_ouputbuf& get_buf() const {return *this;}

public:
    basic_ovectorstream(std::size_t length) JSONCONS_NOEXCEPT
        : base_ouputbuf(length),  
          base_streambuf(&get_buf())
    {}

    ~basic_ovectorstream() {}

public:

    size_t length()
    {
        return this->pptr() - this->pbase();
    }

    void set_locale(const std::locale& loc)
    {
        std::locale result = std::basic_ostream<CharT, CharTraits>::imbue(loc);
        this->pubimbue(loc);
    }

    void reset()
    {
        this->clear();
        this->seekp(0, std::ios::beg);
    }

    const CharT* data() const
    {
        return get_buf().data();
    }
};

} 

#endif