Added PropertiesSet::merge() method, which merges the given Properties object

into the current PropertiesSet.  This method also accepts a filename specifying
where to save this PropertiesSet to, and a boolean representing whether we want
to actually save the properties on exit.

Cleaned up some variable names, and set some bas initializers in the constructor.


git-svn-id: svn://svn.code.sf.net/p/stella/code/trunk@131 8b62c5a3-ac7e-4cc8-8f21-d9a121418aba
This commit is contained in:
stephena 2002-11-11 02:52:02 +00:00
parent d876ee0766
commit 25bde6512e
2 changed files with 93 additions and 47 deletions

View File

@ -13,26 +13,41 @@
// See the file "license" for information on usage and redistribution of // See the file "license" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES. // this file, and for a DISCLAIMER OF ALL WARRANTIES.
// //
// $Id: PropsSet.cxx,v 1.5 2002-05-09 16:58:04 gunfight Exp $ // $Id: PropsSet.cxx,v 1.6 2002-11-11 02:52:02 stephena Exp $
//============================================================================ //============================================================================
#include <assert.h> #include <assert.h>
#include "Props.hxx" #include "Props.hxx"
#include "PropsSet.hxx" #include "PropsSet.hxx"
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
PropertiesSet::PropertiesSet() PropertiesSet::PropertiesSet()
: myRoot(0),
mySize(0),
myUseMemList(true),
myPropertiesFilename(""),
mySaveOnExit(false)
{ {
root = 0;
mySize = 0;
useMemList = true;
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
PropertiesSet::~PropertiesSet() PropertiesSet::~PropertiesSet()
{ {
deleteNode(root); if(myPropertiesStream.is_open())
proStream.close(); myPropertiesStream.close();
if(myUseMemList && mySaveOnExit && (myPropertiesFilename != ""))
{
ofstream out(myPropertiesFilename.c_str());
if(out.is_open())
{
save(out);
out.close();
}
}
deleteNode(myRoot);
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -40,17 +55,17 @@ void PropertiesSet::getMD5(string md5, Properties &properties)
{ {
bool found = false; bool found = false;
if(useMemList) if(myUseMemList)
{ {
// Make sure tree isn't empty // Make sure tree isn't empty
if(root == 0) if(myRoot == 0)
{ {
properties = defProps; properties = myDefaultProperties;
return; return;
} }
// Else, do a BST search for the node with the given md5 // Else, do a BST search for the node with the given md5
TreeNode *current = root; TreeNode *current = myRoot;
while(current) while(current)
{ {
@ -73,7 +88,7 @@ void PropertiesSet::getMD5(string md5, Properties &properties)
if(found) if(found)
properties = *(current->props); properties = *(current->props);
else else
properties = defProps; properties = myDefaultProperties;
} }
else else
{ {
@ -81,17 +96,17 @@ void PropertiesSet::getMD5(string md5, Properties &properties)
for(;;) for(;;)
{ {
// Make sure the stream is still good or we're done // Make sure the stream is still good or we're done
if(!proStream) if(!myPropertiesStream)
{ {
break; break;
} }
// Get the property list associated with this profile // Get the property list associated with this profile
Properties currentProperties(defProps); Properties currentProperties(myDefaultProperties);
currentProperties.load(proStream); currentProperties.load(myPropertiesStream);
// If the stream is still good then insert the properties // If the stream is still good then insert the properties
if(proStream) if(myPropertiesStream)
{ {
string currentMd5 = currentProperties.get("Cartridge.MD5"); string currentMd5 = currentProperties.get("Cartridge.MD5");
@ -103,14 +118,14 @@ void PropertiesSet::getMD5(string md5, Properties &properties)
} }
} }
properties = defProps; properties = myDefaultProperties;
} }
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void PropertiesSet::insert(const Properties& properties) void PropertiesSet::insert(const Properties& properties)
{ {
insertNode(root, properties); insertNode(myRoot, properties);
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -155,32 +170,31 @@ void PropertiesSet::deleteNode(TreeNode *node)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void PropertiesSet::load(string filename, const Properties* defaults, bool useList) void PropertiesSet::load(string filename, const Properties* defaults, bool useList)
{ {
useMemList = useList; myUseMemList = useList;
defProps = defaults; myDefaultProperties = defaults;
// Cyberstella crashes without this: if(filename == "")
if(filename.length() <= 0) return; return;
// Cybergoth 09.05.02
proStream.open(filename.c_str()); myPropertiesStream.open(filename.c_str(), ios::in);
if(useMemList) if(myUseMemList)
{ {
// Loop reading properties // Loop reading properties
for(;;) for(;;)
{ {
// Make sure the stream is still good or we're done // Make sure the stream is still good or we're done
if(!proStream) if(!myPropertiesStream)
{ {
break; break;
} }
// Get the property list associated with this profile // Get the property list associated with this profile
Properties properties(defProps); Properties properties(myDefaultProperties);
properties.load(proStream); properties.load(myPropertiesStream);
// If the stream is still good then insert the properties // If the stream is still good then insert the properties
if(proStream) if(myPropertiesStream)
{ {
insert(properties); insert(properties);
} }
@ -191,7 +205,7 @@ void PropertiesSet::load(string filename, const Properties* defaults, bool useLi
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void PropertiesSet::save(ostream& out) void PropertiesSet::save(ostream& out)
{ {
saveNode(out, root); saveNode(out, myRoot);
} }
@ -200,9 +214,9 @@ void PropertiesSet::saveNode(ostream& out, TreeNode *node)
{ {
if(node) if(node)
{ {
node->props->save(out);
saveNode(out, node->left); saveNode(out, node->left);
saveNode(out, node->right); saveNode(out, node->right);
node->props->save(out);
} }
} }
@ -211,3 +225,18 @@ uInt32 PropertiesSet::size() const
{ {
return mySize; return mySize;
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool PropertiesSet::merge(Properties& properties, string& filename, bool saveOnExit)
{
myPropertiesFilename = filename;
mySaveOnExit = saveOnExit;
// Can't merge the properties if the PropertiesSet isn't in memory
if(!myUseMemList)
return false;
insert(properties);
return true;
}

View File

@ -13,7 +13,7 @@
// See the file "license" for information on usage and redistribution of // See the file "license" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES. // this file, and for a DISCLAIMER OF ALL WARRANTIES.
// //
// $Id: PropsSet.hxx,v 1.3 2002-01-16 02:14:25 stephena Exp $ // $Id: PropsSet.hxx,v 1.4 2002-11-11 02:52:02 stephena Exp $
//============================================================================ //============================================================================
#ifndef PROPERTIESSET_HXX #ifndef PROPERTIESSET_HXX
@ -84,6 +84,17 @@ class PropertiesSet
*/ */
uInt32 size() const; uInt32 size() const;
/**
Merge the given properties into the collection.
@param properties The properties to merge
@param saveOnExit Whether to sync the PropertiesSet to disk on exit
@param filename Where the PropertiesSet should be saved
@return True on success, false on failure
Failure will occur if the PropertiesSet isn't currently in memory
*/
bool merge(Properties& properties, string& filename, bool saveOnExit = true);
private: private:
struct TreeNode struct TreeNode
@ -126,7 +137,7 @@ class PropertiesSet
void saveNode(ostream& out, TreeNode *node); void saveNode(ostream& out, TreeNode *node);
// The root of the BST // The root of the BST
TreeNode* root; TreeNode* myRoot;
// Property to use as the key // Property to use as the key
string myKey; string myKey;
@ -135,12 +146,18 @@ class PropertiesSet
uInt32 mySize; uInt32 mySize;
// Whether to construct an in-memory list or rescan the file each time // Whether to construct an in-memory list or rescan the file each time
bool useMemList; bool myUseMemList;
// The file stream for the stella.pro file // The file stream for the stella.pro file
ifstream proStream; ifstream myPropertiesStream;
// The default properties set // The default properties set
const Properties* defProps; const Properties* myDefaultProperties;
// The filename where this PropertiesSet should be saved
string myPropertiesFilename;
// Whether or not to actually save the PropertiesSet on exit
bool mySaveOnExit;
}; };
#endif #endif