~ubuntu-branches/debian/squeeze/stella/squeeze

« back to all changes in this revision

Viewing changes to src/emucore/Serializer.cxx

  • Committer: Bazaar Package Importer
  • Author(s): Stephen Kitt
  • Date: 2010-07-12 23:49:36 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20100712234936-juawrr3etzhr2qpv
Tags: 3.1.2-1
* New maintainer (closes: #532039).
* New upstream version (closes: #461121):
  - includes launcher (closes: #396058).
* Fix the reference to the X Window System in the description (closes:
  #411815).
* Move to main, DFSG-free ROMs are available (see README.Debian).
* Enhance the package description.
* Drop the libslang2-dev dependency (closes: #560274).
* Remove the Encoding entry from stella.desktop.
* Avoid ignoring errors when cleaning.
* Add ${misc:Depends} to the package dependencies.
* Provide a doc-base file to install the documentation using doc-base.
* Switch to debhelper 7 with a simplified rules file.
* Use autotools-dev to provide updated configuration files.
* Update to Standards-Version 3.9.0:
  - Move to menu section Applications/Emulators.
  - Move the homepage declaration.
* Re-write the manpage.

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
//  SS  SS   tt   ee      ll   ll  aa  aa
9
9
//   SSSS     ttt  eeeee llll llll  aaaaa
10
10
//
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
12
13
//
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.
15
16
//
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
//============================================================================
18
19
 
 
20
#include <fstream>
 
21
#include <sstream>
 
22
 
 
23
#include "FSNode.hxx"
19
24
#include "Serializer.hxx"
20
25
 
21
26
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 
27
Serializer::Serializer(const string& filename, bool readonly)
 
28
  : myStream(NULL),
 
29
    myUseFilestream(true)
 
30
{
 
31
  if(readonly)
 
32
  {
 
33
    FilesystemNode node(filename);
 
34
    if(!node.isDirectory() && node.isReadable())
 
35
    {
 
36
      fstream* str = new fstream(filename.c_str(), ios::in | ios::binary);
 
37
      if(str && str->is_open())
 
38
      {
 
39
        myStream = str;
 
40
        reset();
 
41
      }
 
42
      else
 
43
        delete str;
 
44
    }
 
45
  }
 
46
  else
 
47
  {
 
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
 
54
    // already exists
 
55
    fstream temp(filename.c_str(), ios::out | ios::app);
 
56
    temp.close();
 
57
 
 
58
    fstream* str = new fstream(filename.c_str(), ios::in | ios::out | ios::binary);
 
59
    if(str && str->is_open())
 
60
    {
 
61
      myStream = str;
 
62
      reset();
 
63
    }
 
64
    else
 
65
      delete str;
 
66
  }
 
67
}
 
68
 
 
69
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
22
70
Serializer::Serializer(void)
 
71
  : myStream(NULL),
 
72
    myUseFilestream(false)
23
73
{
 
74
  myStream = new stringstream(ios::in | ios::out | ios::binary);
 
75
  
 
76
  // For some reason, Windows and possibly OSX needs to store something in
 
77
  // the stream before it is used for the first time
 
78
  if(myStream)
 
79
  {
 
80
    putBool(true);
 
81
    reset();
 
82
  }
24
83
}
25
84
 
26
85
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
27
86
Serializer::~Serializer(void)
28
87
{
29
 
  close();
30
 
}
31
 
 
32
 
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
33
 
bool Serializer::open(const string& fileName)
34
 
{
35
 
  close();
36
 
  myStream.open(fileName.c_str(), ios::out | ios::binary);
37
 
 
38
 
  return isOpen();
39
 
}
40
 
 
41
 
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
42
 
void Serializer::close(void)
43
 
{
44
 
  myStream.close();
45
 
  myStream.clear();
46
 
}
47
 
 
48
 
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
49
 
bool Serializer::isOpen(void)
50
 
{
51
 
  return myStream.is_open();
 
88
  if(myStream != NULL)
 
89
  {
 
90
    if(myUseFilestream)
 
91
      ((fstream*)myStream)->close();
 
92
 
 
93
    delete myStream;
 
94
    myStream = NULL;
 
95
  }
 
96
}
 
97
 
 
98
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 
99
bool Serializer::isValid(void)
 
100
{
 
101
  return myStream != NULL;
 
102
}
 
103
 
 
104
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 
105
void Serializer::reset(void)
 
106
{
 
107
  myStream->seekg(ios_base::beg);
 
108
  myStream->seekp(ios_base::beg);
 
109
}
 
110
 
 
111
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 
112
char Serializer::getByte(void)
 
113
{
 
114
  if(myStream->eof())
 
115
    throw "Serializer::getByte() end of file";
 
116
 
 
117
  char buf;
 
118
  myStream->read(&buf, 1);
 
119
 
 
120
  return buf;
 
121
}
 
122
 
 
123
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 
124
int Serializer::getInt(void)
 
125
{
 
126
  if(myStream->eof())
 
127
    throw "Serializer::getInt() end of file";
 
128
 
 
129
  int val = 0;
 
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);
 
134
 
 
135
  return val;
 
136
}
 
137
 
 
138
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 
139
string Serializer::getString(void)
 
140
{
 
141
  int len = getInt();
 
142
  string str;
 
143
  str.resize((string::size_type)len);
 
144
  myStream->read(&str[0], (streamsize)len);
 
145
 
 
146
  if(myStream->bad())
 
147
    throw "Serializer::getString() file read failed";
 
148
 
 
149
  return str;
 
150
}
 
151
 
 
152
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 
153
bool Serializer::getBool(void)
 
154
{
 
155
  bool result = false;
 
156
 
 
157
  char b = getByte();
 
158
  if(b == (char)TruePattern)
 
159
    result = true;
 
160
  else if(b == (char)FalsePattern)
 
161
    result = false;
 
162
  else
 
163
    throw "Serializer::getBool() data corruption";
 
164
 
 
165
  return result;
52
166
}
53
167
 
54
168
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
55
169
void Serializer::putByte(char value)
56
170
{
57
 
  char buf[1];
58
 
  buf[0] = value;
59
 
  myStream.write(buf, 1);
60
 
  if(myStream.bad())
61
 
    throw "Serializer: file write failed";
 
171
  myStream->write(&value, 1);
 
172
  if(myStream->bad())
 
173
    throw "Serializer::putByte() file write failed";
62
174
}
63
175
 
64
176
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
68
180
  for(int i = 0; i < 4; ++i)
69
181
    buf[i] = (value >> (i<<3)) & 0xff;
70
182
 
71
 
  myStream.write((char*)buf, 4);
72
 
  if(myStream.bad())
73
 
    throw "Serializer: file write failed";
 
183
  myStream->write((char*)buf, 4);
 
184
  if(myStream->bad())
 
185
    throw "Serializer::putInt() file write failed";
74
186
}
75
187
 
76
188
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
78
190
{
79
191
  int len = str.length();
80
192
  putInt(len);
81
 
  myStream.write(str.data(), (streamsize)len);
 
193
  myStream->write(str.data(), (streamsize)len);
82
194
 
83
 
  if(myStream.bad())
84
 
    throw "Serializer: file write failed";
 
195
  if(myStream->bad())
 
196
    throw "Serializer::putString() file write failed";
85
197
}
86
198
 
87
199
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -