~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): Mario Iseli
  • Date: 2006-04-08 18:38:25 UTC
  • mfrom: (1.1.2 upstream) (2.1.1 etch)
  • Revision ID: james.westby@ubuntu.com-20060408183825-vu1jk57rk929derx
* New Maintainer (Closes: #361345)
* New upstream release (Closes: #349725)
* Build-Depend now on libslang2-dev (Closes: #325577)
* Complete rebuild of debian/, upgraded to policy-standards
  3.6.2 and compat-level 5.
* Removed stellarc since stella only reads ~/.stellarc and even
  works without a first config.
* New debian/watch file.

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-1998 by Bradford W. Mott
 
11
// Copyright (c) 1995-2005 by Bradford W. Mott and the Stella team
12
12
//
13
13
// See the file "license" for information on usage and redistribution of
14
14
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
15
15
//
16
 
// $Id: Serializer.cxx,v 1.3 2004/04/04 02:03:15 stephena Exp $
 
16
// $Id: Serializer.cxx,v 1.9 2005/12/29 21:16:28 stephena Exp $
17
17
//============================================================================
18
18
 
19
 
#include <iostream>
20
 
#include <fstream>
21
 
#include <string>
22
 
 
23
19
#include "Serializer.hxx"
24
20
 
25
21
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
26
22
Serializer::Serializer(void)
27
23
{
28
 
  TruePattern = 0xfab1fab2;
29
 
  FalsePattern = 0xbad1bad2;
30
 
 
31
 
  myStream = (ofstream*) 0;
32
24
}
33
25
 
34
26
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
38
30
}
39
31
 
40
32
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
41
 
bool Serializer::open(string& fileName)
 
33
bool Serializer::open(const string& fileName)
42
34
{
43
35
  close();
44
 
  myStream = new ofstream(fileName.c_str(), ios::out | ios::binary);
 
36
  myStream.open(fileName.c_str(), ios::out | ios::binary);
45
37
 
46
 
  return (myStream && myStream->is_open());
 
38
  return isOpen();
47
39
}
48
40
 
49
41
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
50
42
void Serializer::close(void)
51
43
{
52
 
  if(myStream)
53
 
  {
54
 
    if(myStream->is_open())
55
 
      myStream->close();
56
 
 
57
 
    delete myStream;
58
 
    myStream = (ofstream*) 0;
59
 
  }
60
 
}
61
 
 
62
 
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
63
 
void Serializer::putLong(long value)
64
 
{
65
 
  myStream->write(reinterpret_cast<char *> (&value), sizeof (long));
66
 
  if(myStream->bad())
 
44
  myStream.close();
 
45
  myStream.clear();
 
46
}
 
47
 
 
48
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 
49
bool Serializer::isOpen(void)
 
50
{
 
51
  return myStream.is_open();
 
52
}
 
53
 
 
54
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 
55
void Serializer::putInt(int value)
 
56
{
 
57
  unsigned char buf[4];
 
58
  for(int i = 0; i < 4; ++i)
 
59
    buf[i] = (value >> (i<<3)) & 0xff;
 
60
 
 
61
  myStream.write((char*)buf, 4);
 
62
  if(myStream.bad())
67
63
    throw "Serializer: file write failed";
68
64
}
69
65
 
71
67
void Serializer::putString(const string& str)
72
68
{
73
69
  int len = str.length();
74
 
  putLong(len);
75
 
  myStream->write(str.data(), len);
 
70
  putInt(len);
 
71
  myStream.write(str.data(), (streamsize)len);
76
72
 
77
 
  if(myStream->bad())
 
73
  if(myStream.bad())
78
74
    throw "Serializer: file write failed";
79
75
}
80
76
 
81
77
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
82
78
void Serializer::putBool(bool b)
83
79
{
84
 
  long l = b ? TruePattern: FalsePattern;
85
 
  putLong(l);
86
 
 
87
 
  if(myStream->bad ())
88
 
    throw "Serializer: file write failed";
 
80
  putInt(b ? TruePattern: FalsePattern);
89
81
}