GameScript: Move initialization out of constructor.
[gemrb.git] / gemrb / core / DataStream.cpp
blob5d4fd0e8bb7d2573c42b2fb946785ec153decc84
1 /* GemRB - Infinity Engine Emulator
2 * Copyright (C) 2003 The GemRB Project
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "win32def.h"
22 #include <ctype.h>
23 #include "DataStream.h"
25 static bool EndianSwitch = false;
27 DataStream::DataStream(void)
29 Pos = 0;
30 Encrypted = false;
33 DataStream::~DataStream(void)
37 void DataStream::SetEndianSwitch(int tmp)
39 EndianSwitch = !! tmp;
42 bool DataStream::IsEndianSwitch()
44 return EndianSwitch;
47 /** Returns true if the stream is encrypted */
48 bool DataStream::CheckEncrypted()
50 ieWord two;
51 Seek( 0, GEM_STREAM_START );
52 Read( &two, 2 );
53 if (two == 0xFFFF) {
54 Pos = 0;
55 Encrypted = true;
56 size -= 2;
57 return true;
59 Seek( 0, GEM_STREAM_START );
60 Encrypted = false;
61 return false;
63 /** No descriptions */
64 void DataStream::ReadDecrypted(void* buf, unsigned int size)
66 for (unsigned int i = 0; i < size; i++)
67 ( ( unsigned char * ) buf )[i] ^= GEM_ENCRYPTION_KEY[( Pos + i ) & 63];
70 void DataStream::Rewind()
72 Seek( Encrypted ? 2 : 0, GEM_STREAM_START );
73 Pos = 0;
76 unsigned long DataStream::GetPos() const
78 return Pos;
81 unsigned long DataStream::Size() const
83 return size;
86 unsigned long DataStream::Remains() const
88 return size-Pos;
91 int DataStream::ReadWord(ieWord *dest)
93 int len = Read(dest, 2);
94 if (EndianSwitch) {
95 unsigned char tmp;
96 tmp=((unsigned char *) dest)[0];
97 ((unsigned char *) dest)[0]=((unsigned char *) dest)[1];
98 ((unsigned char *) dest)[1]=tmp;
100 return len;
103 int DataStream::ReadWordSigned(ieWordSigned *dest)
105 int len = Read(dest, 2);
106 if (EndianSwitch) {
107 unsigned char tmp;
108 tmp=((unsigned char *) dest)[0];
109 ((unsigned char *) dest)[0]=((unsigned char *) dest)[1];
110 ((unsigned char *) dest)[1]=tmp;
112 return len;
115 int DataStream::WriteWord(const ieWord *src)
117 int len;
118 if (EndianSwitch) {
119 char tmp[2];
120 tmp[0]=((unsigned char *) src)[1];
121 tmp[1]=((unsigned char *) src)[0];
122 len = Write( tmp, 2 );
124 else {
125 len = Write( src, 2 );
127 return len;
130 int DataStream::ReadDword(ieDword *dest)
132 int len = Read(dest, 4);
133 if (EndianSwitch) {
134 unsigned char tmp;
135 tmp=((unsigned char *) dest)[0];
136 ((unsigned char *) dest)[0]=((unsigned char *) dest)[3];
137 ((unsigned char *) dest)[3]=tmp;
138 tmp=((unsigned char *) dest)[1];
139 ((unsigned char *) dest)[1]=((unsigned char *) dest)[2];
140 ((unsigned char *) dest)[2]=tmp;
142 return len;
145 int DataStream::WriteDword(const ieDword *src)
147 int len;
148 if (EndianSwitch) {
149 char tmp[4];
150 tmp[0]=((unsigned char *) src)[3];
151 tmp[1]=((unsigned char *) src)[2];
152 tmp[2]=((unsigned char *) src)[1];
153 tmp[3]=((unsigned char *) src)[0];
154 len = Write( tmp, 4 );
156 else {
157 len = Write( src, 4 );
159 return len;
162 int DataStream::ReadResRef(ieResRef dest)
164 int len = Read(dest, 8);
165 int i;
166 // lowercase the resref
167 for(i = 0; i < 8; i++) {
168 dest[i] = (char) tolower(dest[i]);
170 // remove trailing spaces
171 for (i = 7; i >= 0; i--) {
172 if (dest[i] == ' ') dest[i] = 0;
173 else break;
175 // null-terminate
176 dest[8] = 0;
177 return len;
180 int DataStream::WriteResRef(const ieResRef src)
182 return Write( src, 8);