185 lines
3.9 KiB
C++
185 lines
3.9 KiB
C++
//## begin module.cm preserve=no
|
|
|
|
// %X% %Q% %Z% %W%
|
|
|
|
//## end module.cm
|
|
|
|
|
|
|
|
//## begin module.cp preserve=no
|
|
|
|
//## end module.cp
|
|
|
|
|
|
|
|
//## Module: PStream; Pseudo Package body
|
|
|
|
//## Subsystem: Figures
|
|
|
|
//## Source file: PStream.cpp
|
|
|
|
|
|
|
|
//## begin module.additionalIncludes preserve=no
|
|
|
|
//## end module.additionalIncludes
|
|
|
|
|
|
|
|
//## begin module.includes preserve=yes
|
|
|
|
//## end module.includes
|
|
|
|
|
|
|
|
// PStream
|
|
|
|
#include "PStream.h"
|
|
|
|
//## begin module.additionalDeclarations preserve=yes
|
|
|
|
#include "prsstent.h"
|
|
|
|
|
|
|
|
//## end module.additionalDeclarations
|
|
|
|
|
|
|
|
|
|
|
|
// Class PStream
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PStream::PStream()
|
|
|
|
//## begin PStream::PStream%.hasinit preserve=no
|
|
|
|
: saving(false)
|
|
|
|
//## end PStream::PStream%.hasinit
|
|
|
|
//## begin PStream::PStream%.initialization preserve=yes
|
|
|
|
//## end PStream::PStream%.initialization
|
|
|
|
{
|
|
|
|
//## begin PStream::PStream%.body preserve=yes
|
|
|
|
for(int i=0 ; i<RESTORE_SIZE ; i++)
|
|
|
|
restore[i] = NULL;
|
|
|
|
//## end PStream::PStream%.body
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PStream::~PStream()
|
|
|
|
{
|
|
|
|
//## begin PStream::~PStream%.body preserve=yes
|
|
|
|
//## end PStream::~PStream%.body
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//## Other Operations (implementation)
|
|
|
|
void PStream::load (char* filename)
|
|
|
|
{
|
|
|
|
//## begin PStream::load%940866982.body preserve=yes
|
|
|
|
open(filename, ios::in);
|
|
|
|
saving=false;
|
|
|
|
//## end PStream::load%940866982.body
|
|
|
|
}
|
|
|
|
|
|
|
|
void PStream::save (char* filename)
|
|
|
|
{
|
|
|
|
//## begin PStream::save%940866983.body preserve=yes
|
|
|
|
open(filename, ios::out|ios::trunc);
|
|
|
|
saving=true;
|
|
|
|
//## end PStream::save%940866983.body
|
|
|
|
}
|
|
|
|
|
|
|
|
void PStream::close ()
|
|
|
|
{
|
|
|
|
//## begin PStream::close%940866984.body preserve=yes
|
|
|
|
if(isSaving()) {
|
|
|
|
(*this) << 0;
|
|
|
|
fstream::close();
|
|
|
|
}
|
|
|
|
saving=false;
|
|
|
|
//## end PStream::close%940866984.body
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PStream::isSaving ()
|
|
|
|
{
|
|
|
|
//## begin PStream::isSaving%940866985.body preserve=yes
|
|
|
|
return saving;
|
|
|
|
//## end PStream::isSaving%940866985.body
|
|
|
|
}
|
|
|
|
|
|
|
|
void PStream::archive (Persistent*& x)
|
|
|
|
{
|
|
|
|
//## begin PStream::archive%940866986.body preserve=yes
|
|
|
|
|
|
|
|
// Let's save some data...
|
|
|
|
if(saving){
|
|
|
|
|
|
|
|
if(x==0){
|
|
|
|
(*this) << "0" << endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int n = find(x);
|
|
|
|
if(n)
|
|
|
|
(*this) << n << endl;
|
|
|
|
else{
|
|
|
|
n = add(x);
|
|
|
|
(*this) << n << endl << x->name() << endl;
|
|
|
|
x->archive(*this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let's load some data
|
|
|
|
else{
|
|
|
|
int n;
|
|
|
|
(*this) >> n;
|
|
|
|
if(n==0){
|
|
|
|
x=0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
x = find(n);
|
|
|
|
if(x == 0)
|
|
|
|
{
|
|
|
|
Str name;
|
|
|
|
(*this) >> name;
|
|
|
|
|
|
|
|
x = Persistent::clone1(name.getStr());
|
|
|
|
|
|
|
|
if(x==0){
|
|
|
|
clear(ios::failbit);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
add(n, x);
|
|
|
|
x->archive(*this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//## end PStream::archive%940866986.body
|
|
|
|
}
|
|
|
|
|
|
|
|
Persistent* PStream::find (int x) const
|
|
|
|
{
|
|
|
|
//## begin PStream::find%940866987.body preserve=yes
|
|
|
|
if(x>=0 && x<RESTORE_SIZE)
|
|
|
|
return restore[x];
|
|
|
|
else
|
|
|
|
return (Persistent*) 0;
|
|
|
|
//## end PStream::find%940866987.body
|
|
|
|
}
|
|
|
|
|
|
|
|
void PStream::add (int pos, Persistent* data)
|
|
|
|
{
|
|
|
|
//## begin PStream::add%940866988.body preserve=yes
|
|
|
|
restore[pos]=data;
|
|
|
|
//## end PStream::add%940866988.body
|
|
|
|
}
|
|
|
|
|
|
|
|
int PStream::find (const Persistent* p) const
|
|
|
|
{
|
|
|
|
//## begin PStream::find%940866989.body preserve=yes
|
|
|
|
PsMap* tmp = (PsMap*)&psMap;
|
|
|
|
if(tmp->exist((Persistent*)p))
|
|
|
|
return tmp->find((Persistent*) p);
|
|
|
|
return 0;
|
|
|
|
//## end PStream::find%940866989.body
|
|
|
|
}
|
|
|
|
|
|
|
|
int PStream::add (const Persistent* data)
|
|
|
|
{
|
|
|
|
//## begin PStream::add%940866990.body preserve=yes
|
|
|
|
int size=psMap.getSize()+1;
|
|
|
|
PsMap* tmp = (PsMap*)&psMap;
|
|
|
|
tmp->add((Persistent*)data, size);
|
|
|
|
return size;
|
|
|
|
//## end PStream::add%940866990.body
|
|
|
|
}
|
|
|
|
|
|
|
|
// Additional Declarations
|
|
|
|
//## begin PStream.declarations preserve=yes
|
|
|
|
//## end PStream.declarations
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//## begin module.epilog preserve=yes
|
|
|
|
//## end module.epilog
|
|
|