]> 4ch.mooo.com Git - 16.git/blob - 16/adplug/libbinio/src/binwrap.cpp
wwww~
[16.git] / 16 / adplug / libbinio / src / binwrap.cpp
1 /*
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.
6  * 
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.
11  * 
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
15  *
16  * binwrap.cpp - Binary I/O wrapper, using standard iostreams library
17  * Copyright (C) 2002, 2003 Simon Peter <dn.tlp@gmx.net>
18  */
19
20 #include "binwrap.h"
21
22 #if BINIO_ENABLE_IOSTREAM
23
24 /***** biniwstream *****/
25
26 biniwstream::biniwstream(istream *istr)
27   : in(istr)
28 {
29 }
30
31 biniwstream::~biniwstream()
32 {
33 }
34
35 void biniwstream::seek(long pos, Offset offs)
36 {
37   if(!in) { err = NotOpen; return; }
38
39   switch(offs) {
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;
43   }
44 }
45
46 biniwstream::Byte biniwstream::getByte()
47 {
48   if(!in) { err = NotOpen; return 0; }
49
50   int i = in->get();
51   if(i == EOF) err |= Eof;
52   return (Byte)i;
53 }
54
55 long biniwstream::pos()
56 {
57   if(!in) { err = NotOpen; return 0; }
58   return (long)in->tellg();
59 }
60
61 /***** binowstream *****/
62
63 binowstream::binowstream(ostream *ostr)
64   : out(ostr)
65 {
66 }
67
68 binowstream::~binowstream()
69 {
70 }
71
72 void binowstream::seek(long pos, Offset offs)
73 {
74   if(!out) { err = NotOpen; return; }
75
76   switch(offs) {
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;
80   }
81 }
82
83 void binowstream::putByte(binio::Byte b)
84 {
85   if(!out) { err = NotOpen; return; }
86   out->put((char)b);
87 }
88
89 long binowstream::pos()
90 {
91   if(!out) { err = NotOpen; return 0; }
92   return (long)out->tellp();
93 }
94
95 /***** binwstream *****/
96
97 binwstream::binwstream(iostream *str)
98   : biniwstream(str), binowstream(str), io(str)
99 {
100 }
101
102 binwstream::~binwstream()
103 {
104 }
105
106 void binwstream::seek(long pos, Offset offs)
107 {
108   biniwstream::seek(pos, offs);
109   binowstream::seek(pos, offs);
110 }
111
112 long binwstream::pos()
113 {
114   if(!io) { err = NotOpen; return 0; }
115   return (long)io->tellg();
116 }
117
118 binwstream::Byte binwstream::getByte()
119 {
120   Byte in = biniwstream::getByte();
121   binowstream::seek(biniwstream::pos(), Set);   // sync stream position
122   return in;
123 }
124
125 void binwstream::putByte(Byte b)
126 {
127   binowstream::putByte(b);
128   biniwstream::seek(binowstream::pos(), Set);   // sync stream position
129 }
130
131 #endif