]> 4ch.mooo.com Git - 16.git/blob - 16/adplug/libbinio/src/binstr.cpp
wwww~
[16.git] / 16 / adplug / libbinio / src / binstr.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  * binstr.cpp - Binary I/O on standard C strings in memory
17  * Copyright (C) 2003 Simon Peter <dn.tlp@gmx.net>
18  */
19
20 #include "binstr.h"
21
22 /***** binsbase *****/
23
24 binsbase::binsbase(void *str, unsigned long len)
25   : data((Byte *)str), spos((Byte *)str), length(len)
26 {
27 }
28
29 binsbase::~binsbase()
30 {
31 }
32
33 void binsbase::seek(long p, Offset offs)
34 {
35   switch(offs) {
36   case Set: spos = data + p; break;
37   case Add: spos += p; break;
38   case End: spos = data + length - 1 + p; break;
39   }
40
41   // Seek before start of data
42   if(spos < data) {
43     err |= Eof;
44     spos = data;
45     return;
46   }
47
48   // Seek after end of data
49   if(spos - data >= length) {
50     err |= Eof;
51     spos = data + length - 1;
52   }
53 }
54
55 long binsbase::pos()
56 {
57   return (long)(spos - data);
58 }
59
60 /***** binisstream *****/
61
62 binisstream::binisstream(void *str, unsigned long len)
63   : binsbase(str, len)
64 {
65 }
66
67 binisstream::~binisstream()
68 {
69 }
70
71 binisstream::Byte binisstream::getByte()
72 {
73   Byte in = 0;
74
75   if(spos - data >= length)
76     err |= Eof;
77   else {
78     in = *spos;
79     spos++;
80   }
81
82   return in;
83 }
84
85 /***** binosstream *****/
86
87 binosstream::binosstream(void *str, unsigned long len)
88   : binsbase(str, len)
89 {
90 }
91
92 binosstream::~binosstream()
93 {
94 }
95
96 void binosstream::putByte(Byte b)
97 {
98   *spos = b;
99   spos++;
100
101   if(spos - data >= length)
102     spos = data + length - 1;
103 }
104
105 /***** binsstream *****/
106
107 binsstream::binsstream(void *str, unsigned long len)
108   : binsbase(str, len), binisstream(str, len), binosstream(str, len)
109 {
110 }
111
112 binsstream::~binsstream()
113 {
114 }