Serious-Engine/Sources/Engine/Base/Serial.cpp
Ryan C. Gordon 24cb244d43 First attempt to hand-merge Ryan's Linux and Mac OS X port.
This was a _ton_ of changes, made 15 years ago, so there are probably some
problems to work out still.

Among others: Engine/Base/Stream.* was mostly abandoned and will need to be
re-ported.

Still, this is a pretty good start, and probably holds a world record for
lines of changes or something.  :)
2016-03-28 23:46:13 -04:00

150 lines
2.8 KiB
C++

/* Copyright (c) 2002-2012 Croteam Ltd. All rights reserved. */
#include "Engine/StdH.h"
#include <Engine/Base/Serial.h>
#include <Engine/Base/Stream.h>
#include <Engine/Base/CRCTable.h>
/*
* Default constructor.
*/
CSerial::CSerial( void) : ser_ctUsed(0) // not used initially
{
}
/*
* Destructor.
*/
CSerial::~CSerial( void)
{
// must not be used at all
ASSERT(ser_ctUsed == 0); // look at _strLastCleared for possible name
}
/*
* Clear the object.
*/
void CSerial::Clear(void)
{
// mark that you have changed
MarkChanged();
// clear the filename
ser_FileName.Clear();
}
/* Get the description of this object. */
CTString CSerial::GetDescription(void)
{
return "<no description>";
}
/*
* Load from file.
*/
void CSerial::Load_t(const CTFileName fnFileName) // throw char *
{
ASSERT(!IsUsed());
// mark that you have changed
MarkChanged();
// open a stream
CTFileStream istrFile;
istrFile.Open_t(fnFileName);
// read object from stream
Read_t(&istrFile);
// if still here (no exceptions raised)
// remember filename
ser_FileName = fnFileName;
}
/*
* Reload from file.
*/
void CSerial::Reload(void)
{
// mark that you have changed
MarkChanged();
CTFileName fnmOldName = ser_FileName;
Clear();
// try to
//try {
// open a stream
CTFileStream istrFile;
istrFile.Open_t(fnmOldName);
// read object from stream
Read_t(&istrFile);
// if there is some error while reloading
//} catch (char *strError) {
// quit the application with error explanation
//FatalError(TRANS("Cannot reload file '%s':\n%s"), (const char *) (CTString&)fnmOldName, strError);
//}
// if still here (no exceptions raised)
// remember filename
ser_FileName = fnmOldName;
}
/*
* Save to file.
*/
void CSerial::Save_t(const CTFileName fnFileName) // throw char *
{
// open a stream
CTFileStream ostrFile;
ostrFile.Create_t(fnFileName);
// write object to stream
Write_t(&ostrFile);
// if still here (no exceptions raised)
// remember new filename
ser_FileName = fnFileName;
}
/*
* Mark that object is used once more.
*/
void CSerial::MarkUsed(void)
{
// use count must not have dropped below zero
ASSERT(ser_ctUsed>=0);
// increment use count
ser_ctUsed++;
}
/*
* Mark that object is used once less.
*/
void CSerial::MarkUnused(void)
{
// decrement use count
ser_ctUsed--;
// use count must not have dropped below zero
ASSERT(ser_ctUsed>=0);
}
/*
* Check if object is used at least once.
*/
BOOL CSerial::IsUsed(void)
{
// use count must not have dropped below zero
ASSERT(ser_ctUsed>=0);
return ser_ctUsed>0;
}
INDEX CSerial::GetUsedCount(void)
{
return ser_ctUsed;
}
// gather the CRC of the file
void CSerial::AddToCRCTable(void)
{
// add the file to CRC table
CRCT_AddFile_t(ser_FileName);
}