2 * This library is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2.1 of the License, or (at your option) any later version.
7 * This library is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with this library; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 * binwrap.cpp - Binary I/O wrapper, using standard iostreams library
17 * Copyright (C) 2002, 2003 Simon Peter <dn.tlp@gmx.net>
22 #if BINIO_ENABLE_IOSTREAM
24 /***** biniwstream *****/
26 biniwstream::biniwstream(istream *istr)
31 biniwstream::~biniwstream()
35 void biniwstream::seek(long pos, Offset offs)
37 if(!in) { err = NotOpen; return; }
40 case Set: in->seekg(pos, ios::beg); break;
41 case Add: in->seekg(pos, ios::cur); break;
42 case End: in->seekg(pos, ios::end); break;
46 biniwstream::Byte biniwstream::getByte()
48 if(!in) { err = NotOpen; return 0; }
51 if(i == EOF) err |= Eof;
55 long biniwstream::pos()
57 if(!in) { err = NotOpen; return 0; }
58 return (long)in->tellg();
61 /***** binowstream *****/
63 binowstream::binowstream(ostream *ostr)
68 binowstream::~binowstream()
72 void binowstream::seek(long pos, Offset offs)
74 if(!out) { err = NotOpen; return; }
77 case Set: out->seekp(pos, ios::beg); break;
78 case Add: out->seekp(pos, ios::cur); break;
79 case End: out->seekp(pos, ios::end); break;
83 void binowstream::putByte(binio::Byte b)
85 if(!out) { err = NotOpen; return; }
89 long binowstream::pos()
91 if(!out) { err = NotOpen; return 0; }
92 return (long)out->tellp();
95 /***** binwstream *****/
97 binwstream::binwstream(iostream *str)
98 : biniwstream(str), binowstream(str), io(str)
102 binwstream::~binwstream()
106 void binwstream::seek(long pos, Offset offs)
108 biniwstream::seek(pos, offs);
109 binowstream::seek(pos, offs);
112 long binwstream::pos()
114 if(!io) { err = NotOpen; return 0; }
115 return (long)io->tellg();
118 binwstream::Byte binwstream::getByte()
120 Byte in = biniwstream::getByte();
121 binowstream::seek(biniwstream::pos(), Set); // sync stream position
125 void binwstream::putByte(Byte b)
127 binowstream::putByte(b);
128 biniwstream::seek(binowstream::pos(), Set); // sync stream position