File: sfstream.cpp

package info (click to toggle)
libsdsl 2.1.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 3,980 kB
  • sloc: cpp: 42,286; makefile: 1,171; ansic: 318; sh: 201; python: 27
file content (290 lines) | stat: -rw-r--r-- 6,988 bytes parent folder | download | duplicates (18)
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include "sdsl/sfstream.hpp"
#include "sdsl/util.hpp"
#include <iostream>

namespace sdsl
{

//  IMPLEMENTATION OF OSFSTREAM

osfstream::osfstream() : std::ostream(nullptr)
{
    this->init(m_streambuf);
}

osfstream::osfstream(const std::string& file, std::ios_base::openmode mode) : std::ostream(nullptr)
{
    this->init(m_streambuf);
    open(file, mode);
}

osfstream::buf_ptr_type
osfstream::open(const std::string& file, std::ios_base::openmode mode)
{
    delete m_streambuf;
    m_streambuf = nullptr;
    m_file = file;
    std::streambuf* success = nullptr;
    if (is_ram_file(file)) {
        m_streambuf = new ram_filebuf();
        success = ((ram_filebuf*)m_streambuf)->open(m_file, mode);
    } else {
        m_streambuf = new std::filebuf();
        success = ((std::filebuf*)m_streambuf)->open(m_file, mode);
    }
    if (success) {
        this->clear();
    } else {
        this->setstate(std::ios_base::failbit);
        delete m_streambuf;
        m_streambuf = nullptr;
    }
    this->rdbuf(m_streambuf);
    return m_streambuf;
}

bool
osfstream::is_open()
{
    if (nullptr == m_streambuf)
        return false;
    if (is_ram_file(m_file)) {
        return ((ram_filebuf*)m_streambuf)->is_open();
    } else {
        return ((std::filebuf*)m_streambuf)->is_open();
    }
}

void
osfstream::close()
{
    bool fail = false;
    if (nullptr == m_streambuf) {
        fail = true;
    } else {
        if (is_ram_file(m_file)) {
            fail = !((ram_filebuf*)m_streambuf)->close();
        } else {
            fail = !((std::filebuf*)m_streambuf)->close();
        }
    }
    if (fail) this->setstate(std::ios::failbit);
}

osfstream::~osfstream()
{
    delete m_streambuf; // streambuf closes the file on destruction
}

osfstream::operator voidptr()const
{
    return m_streambuf;
}

osfstream&
osfstream::seekp(pos_type pos)
{
    ios_base::iostate err = std::ios_base::iostate(std::ios_base::goodbit);
    try {
        if (!this->fail()) {
            pos_type p = 0;
            if (is_ram_file(m_file)) {
                p = ((ram_filebuf*)m_streambuf)->pubseekpos(pos, std::ios_base::out);
            } else {
                p = ((std::filebuf*)m_streambuf)->pubseekpos(pos, std::ios_base::out);
            }
            if (p == pos_type(off_type(-1))) {
                err |= ios_base::failbit;
                this->setstate(err);
            }
        }
    } catch (...) {
        if (err) {
            this->setstate(err);
        }
    }
    return *this;
}


osfstream&
osfstream::seekp(off_type off, std::ios_base::seekdir way)
{
    ios_base::iostate err = std::ios_base::iostate(ios_base::goodbit);
    try {
        if (!this->fail()) {
            pos_type p = 0;
            if (is_ram_file(m_file)) {
                p = ((ram_filebuf*)m_streambuf)->pubseekoff(off, way, std::ios_base::out);

            } else {
                p = ((std::filebuf*)m_streambuf)->pubseekoff(off, way, std::ios_base::out);
            }
            if (p == pos_type(off_type(-1))) {
                err |= ios_base::failbit;
                this->setstate(err);
            }
        }
    } catch (...) {
        if (err) {
            this->setstate(err);
        }
    }
    return *this;
}



//  IMPLEMENTATION OF ISFSTREAM

isfstream::isfstream() : std::istream(nullptr)
{
    this->init(m_streambuf);
}

isfstream::isfstream(const std::string& file, std::ios_base::openmode mode) : std::istream(nullptr)
{
    this->init(m_streambuf);
    open(file, mode);
}

isfstream::buf_ptr_type
isfstream::open(const std::string& file, std::ios_base::openmode mode)
{
    delete m_streambuf;
    m_streambuf = nullptr;
    m_file = file;
    std::streambuf* success = nullptr;
    if (is_ram_file(file)) {
        m_streambuf = new ram_filebuf();
        success = ((ram_filebuf*)m_streambuf)->open(m_file, mode);
    } else {
        m_streambuf = new std::filebuf();
        success = ((std::filebuf*)m_streambuf)->open(m_file, mode);
    }
    if (success) {
        this->clear();
    } else {
        this->setstate(std::ios_base::failbit);
        delete m_streambuf;
        m_streambuf = nullptr;
    }
    this->rdbuf(m_streambuf);
    return m_streambuf;
}

bool
isfstream::is_open()
{
    if (nullptr == m_streambuf)
        return false;
    if (is_ram_file(m_file)) {
        return ((ram_filebuf*)m_streambuf)->is_open();
    } else {
        return ((std::filebuf*)m_streambuf)->is_open();
    }
}

void
isfstream::close()
{
    bool fail = false;
    if (nullptr == m_streambuf) {
        fail = true;
    } else {
        if (is_ram_file(m_file)) {
            fail = !((ram_filebuf*)m_streambuf)->close();
        } else {
            fail = !((std::filebuf*)m_streambuf)->close();
        }
    }
    if (fail) this->setstate(std::ios::failbit);
}

isfstream&
isfstream::seekg(pos_type pos)
{
    ios_base::iostate err = std::ios_base::iostate(std::ios_base::goodbit);
    try {
        if (!this->fail()) {
            pos_type p = 0;
            if (is_ram_file(m_file)) {
                p = ((ram_filebuf*)m_streambuf)->pubseekpos(pos, std::ios_base::in);

            } else {
                p = ((std::filebuf*)m_streambuf)->pubseekpos(pos, std::ios_base::in);
            }
            if (p == pos_type(off_type(-1))) {
                err |= ios_base::failbit;
            }
        }
    } catch (...) {
        if (err) {
            this->setstate(err);
        }
    }
    return *this;
}


isfstream&
isfstream::seekg(off_type off, std::ios_base::seekdir way)
{
    ios_base::iostate err = std::ios_base::iostate(ios_base::goodbit);
    try {
        if (!this->fail()) {
            pos_type p = 0;
            if (is_ram_file(m_file)) {
                p = ((ram_filebuf*)m_streambuf)->pubseekoff(off, way, std::ios_base::in);

            } else {
                p = ((std::filebuf*)m_streambuf)->pubseekoff(off, way, std::ios_base::in);
            }
            if (p == pos_type(off_type(-1))) {
                err |= ios_base::failbit;
            }
        }
    } catch (...) {
        if (err) {
            this->setstate(err);
        }
    }
    return *this;
}

std::streampos
isfstream::tellg()
{
    ios_base::iostate err = std::ios_base::iostate(ios_base::goodbit);
    pos_type p = pos_type(off_type(-1));
    try {
        if (!this->fail()) {
            if (is_ram_file(m_file)) {
                p = ((ram_filebuf*)m_streambuf)->pubseekoff(0, std::ios_base::cur);

            } else {
                p = ((std::filebuf*)m_streambuf)->pubseekoff(0, std::ios_base::cur);
            }
            if (p == pos_type(off_type(-1))) {
                err |= ios_base::failbit;
            }
        }
    } catch (...) {
        if (err) {
            this->setstate(err);
        }
    }
    return p;
}

isfstream::~isfstream()
{
    delete m_streambuf;
}

isfstream::operator voidptr()const
{
    return m_streambuf; // streambuf closes the file on destruction
}

}// end namespace sdsl