TITANIC: Implementing CProjectItem::loadData

This commit is contained in:
Paul Gilbert 2016-02-19 19:45:17 -05:00
parent ad8450209a
commit 8c9a1c2159
4 changed files with 93 additions and 2 deletions

View File

@ -73,8 +73,54 @@ void CProjectItem::clear() {
}
void CProjectItem::loadData(SimpleFile *file) {
CProjectItem *CProjectItem::loadData(SimpleFile *file) {
if (!file->IsClassStart())
return nullptr;
CProjectItem *root = nullptr;
CTreeItem *parent = nullptr;
CTreeItem *item = nullptr;
do {
CString entryString = file->readString();
if (entryString == "ALONG") {
// Move along, nothing needed
} else if (entryString == "UP") {
// Move up
if (parent == nullptr ||
(parent = parent->getParent()) == nullptr)
break;
} else if (entryString == "DOWN") {
// Move down
if (parent == nullptr)
parent = item;
else
parent = parent->getLastChild();
} else {
// Create new class instance
item = dynamic_cast<CTreeItem *>(CSaveableObject::createInstance(entryString));
assert(item);
if (root) {
// Already created root project
item->addUnder(parent);
} else {
// TODO: Validate this is correct
root = dynamic_cast<CProjectItem *>(item);
assert(root);
_filename = root->_filename;
}
// Load the data for the item
item->load(file);
}
file->IsClassStart();
} while (file->IsClassStart());
return root;
}
void CProjectItem::saveData(SimpleFile *file, CTreeItem *item) const {

View File

@ -30,11 +30,13 @@
namespace Titanic {
class CProjectItem : public CFileItem {
private:
CString _filename;
private:
/**
* Load project data from the passed file
*/
void loadData(SimpleFile *file);
CProjectItem *loadData(SimpleFile *file);
/**
* Save project data to the passed file

View File

@ -52,4 +52,32 @@ CTreeItem *CTreeItem::getLastChild() {
return _firstChild->getLastSibling();
}
void CTreeItem::addUnder(CTreeItem *newParent) {
if (newParent->_firstChild)
addSibling(newParent->getLastSibling());
else
setParent(newParent);
}
void CTreeItem::setParent(CTreeItem *newParent) {
_parent = newParent;
_priorSibling = nullptr;
_nextSibling = newParent->_firstChild;
if (newParent->_firstChild)
newParent->_firstChild->_priorSibling = this;
newParent->_firstChild = this;
}
void CTreeItem::addSibling(CTreeItem *item) {
_priorSibling = item->_nextSibling;
_nextSibling = item->_nextSibling;
_parent = item->_parent;
if (item->_nextSibling)
item->_nextSibling->_priorSibling = this;
item->_nextSibling = this;
}
} // End of namespace Titanic

View File

@ -81,6 +81,21 @@ public:
* Get the last child of the item, if any
*/
CTreeItem *getLastChild();
/**
* Adds the item under another tree item
*/
void addUnder(CTreeItem *newParent);
/**
* Sets the parent for the item
*/
void setParent(CTreeItem *newParent);
/**
* Adds the item as a sibling of another item
*/
void addSibling(CTreeItem *item);
};
} // End of namespace Titanic