8
8
// SS SS tt ee ll ll aa aa
9
9
// SSSS ttt eeeee llll llll aaaaa
11
// Copyright (c) 1995-2008 by Bradford W. Mott and the Stella team
11
// Copyright (c) 1995-2010 by Bradford W. Mott, Stephen Anthony
12
// and the Stella Team
13
// See the file "license" for information on usage and redistribution of
14
// See the file "License.txt" for information on usage and redistribution of
14
15
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
16
// $Id: Serializer.cxx,v 1.13 2008/02/06 13:45:22 stephena Exp $
17
// $Id: Serializer.cxx 2001 2010-04-10 21:37:23Z stephena $
17
18
//============================================================================
19
24
#include "Serializer.hxx"
21
26
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
27
Serializer::Serializer(const string& filename, bool readonly)
33
FilesystemNode node(filename);
34
if(!node.isDirectory() && node.isReadable())
36
fstream* str = new fstream(filename.c_str(), ios::in | ios::binary);
37
if(str && str->is_open())
48
// When using fstreams, we need to manually create the file first
49
// if we want to use it in read/write mode, since it won't be created
50
// if it doesn't already exist
51
// However, if it *does* exist, we don't want to overwrite it
52
// So we open in write and append mode - the write creates the file
53
// when necessary, and the append doesn't delete any data if it
55
fstream temp(filename.c_str(), ios::out | ios::app);
58
fstream* str = new fstream(filename.c_str(), ios::in | ios::out | ios::binary);
59
if(str && str->is_open())
69
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
22
70
Serializer::Serializer(void)
72
myUseFilestream(false)
74
myStream = new stringstream(ios::in | ios::out | ios::binary);
76
// For some reason, Windows and possibly OSX needs to store something in
77
// the stream before it is used for the first time
26
85
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
27
86
Serializer::~Serializer(void)
32
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
33
bool Serializer::open(const string& fileName)
36
myStream.open(fileName.c_str(), ios::out | ios::binary);
41
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
42
void Serializer::close(void)
48
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
49
bool Serializer::isOpen(void)
51
return myStream.is_open();
91
((fstream*)myStream)->close();
98
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
99
bool Serializer::isValid(void)
101
return myStream != NULL;
104
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
105
void Serializer::reset(void)
107
myStream->seekg(ios_base::beg);
108
myStream->seekp(ios_base::beg);
111
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
112
char Serializer::getByte(void)
115
throw "Serializer::getByte() end of file";
118
myStream->read(&buf, 1);
123
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
124
int Serializer::getInt(void)
127
throw "Serializer::getInt() end of file";
130
unsigned char buf[4];
131
myStream->read((char*)buf, 4);
132
for(int i = 0; i < 4; ++i)
133
val += (int)(buf[i]) << (i<<3);
138
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
139
string Serializer::getString(void)
143
str.resize((string::size_type)len);
144
myStream->read(&str[0], (streamsize)len);
147
throw "Serializer::getString() file read failed";
152
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
153
bool Serializer::getBool(void)
158
if(b == (char)TruePattern)
160
else if(b == (char)FalsePattern)
163
throw "Serializer::getBool() data corruption";
54
168
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
55
169
void Serializer::putByte(char value)
59
myStream.write(buf, 1);
61
throw "Serializer: file write failed";
171
myStream->write(&value, 1);
173
throw "Serializer::putByte() file write failed";
64
176
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -