Debian Codegraph @

yo/concrete/examples/fistream/fistream.cc

raw
1 #include "fistream.ih"
2
3 //OPEX
4 istream &std::operator>>(istream &str, field const &params)
5 {
6 return static_cast<Fistream *>(&str)->setField(params);
7 }
8 //=
9
10 //CONS1
11 Fistream::Fistream(istream &stream)
12 :
13 istream(stream.rdbuf()),
14 d_streambuf(rdbuf()),
15 d_width(0)
16 {}
17 //=
18 //CONS2
19 Fistream::Fistream(char const *name, ios::openmode mode)
20 :
21 istream(new filebuf()),
22 d_filebuf(static_cast<filebuf *>(rdbuf())),
23 d_streambuf(d_filebuf.get()),
24 d_width(0)
25 {
26 d_filebuf->open(name, mode);
27 }
28 //=
29 //SETFIELD
30 std::istream &Fistream::setField(field const &params)
31 {
32 if (params.d_newWidth) // new field size requested
33 d_width = params.d_width; // set new width
34
35 if (!d_width) // no width?
36 rdbuf(d_streambuf); // return to the old buffer
37 else
38 setBuffer(); // define the extraction buffer
39
40 return *this;
41 }
42 //=
43 //SETBUFFER
44 void Fistream::setBuffer()
45 {
46 char *buffer = new char[d_width + 1];
47
48 rdbuf(d_streambuf); // use istream's buffer to
49 buffer[read(buffer, d_width).gcount()] = 0; // read d_width chars,
50 // terminated by a 0-byte
51 d_iss.str(buffer);
52 delete[] buffer;
53
54 rdbuf(d_iss.rdbuf()); // switch buffers
55 }
56 //=