Loading...
Searching...
No Matches
resourceFile.h
Go to the documentation of this file.
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//
#ifndef BALL_FORMAT_RESOURCEFILE_H
#define BALL_FORMAT_RESOURCEFILE_H
#ifndef BALL_COMMON_H
# include <BALL/common.h>
#endif
#ifndef BALL_CONCEPT_FORWARDITERATOR_H
# include <BALL/CONCEPT/forwardIterator.h>
#endif
#ifndef BALL_CONCEPT_VISITOR_H
# include <BALL/CONCEPT/visitor.h>
#endif
#ifndef BALL_CONCEPT_AUTODELETABLE_H
# include <BALL/CONCEPT/autoDeletable.h>
#endif
#ifndef BALL_CONCEPT_PROCESSOR_H
# include <BALL/CONCEPT/processor.h>
#endif
#ifndef BALL_SYSTEM_FILE_H
# include <BALL/SYSTEM/file.h>
#endif
namespace BALL
{
class BALL_EXPORT ResourceEntry
: public AutoDeletable
{
public:
BALL_CREATE_DEEP(ResourceEntry)
ResourceEntry();
ResourceEntry(const ResourceEntry& entry, bool deep = true);
ResourceEntry(const String& key, const String& value, ResourceEntry* parent = 0);
virtual ~ResourceEntry();
void clear();
void destroy();
void set(const ResourceEntry& entry, bool deep = true);
const ResourceEntry& operator = (const ResourceEntry &entry);
void get(ResourceEntry& entry, bool deep = true) const;
bool operator == (const ResourceEntry& entry) const;
bool operator != (const ResourceEntry& entry) const;
ResourceEntry& getRoot();
const ResourceEntry& getRoot() const;
ResourceEntry* getParent();
const ResourceEntry* getParent() const;
ResourceEntry* getChild(Position index);
const ResourceEntry* getChild(Position index) const;
ResourceEntry* getEntry(const String& key_path);
const ResourceEntry* getEntry(const String& key_path) const;
const String& getKey() const;
void setValue(const String& value);
String& getValue();
const String& getValue() const;
String getPath() const;
Size countChildren() const;
Size countDescendants() const;
Size getSize() const;
Size getDepth() const;
ResourceEntry* insertChild(const String& key, const String& value, bool replace_value = true);
ResourceEntry* insertSibling(const String& key, const String& value, bool replace_value = true);
ResourceEntry* insertChild(ResourceEntry& entry, bool replace_value = true);
ResourceEntry* insert(const String& key_path, const String& name);
bool mergeChildrenOf(ResourceEntry& entry, bool replace_value = true);
bool removeChild(const String& key, ResourceEntry** removed = 0);
bool removeKey(const String& key_path);
ResourceEntry* findChild(const String& key);
const ResourceEntry* findChild(const String& key) const;
ResourceEntry* findDescendant(const String& key);
const ResourceEntry* findDescendant(const String& key) const;
ResourceEntry* findEntry(const String& key);
const ResourceEntry* findEntry(const String& key) const;
bool hasChild(const String& key) const;
bool isEmpty() const;
bool isParentOf(const ResourceEntry& entry) const;
bool isChildOf(const ResourceEntry& entry) const;
bool isAncestorOf(const ResourceEntry& entry) const;
bool isDescendantOf(const ResourceEntry& entry) const;
bool isRelatedWith(const ResourceEntry& entry) const;
bool isRoot() const;
bool isValid() const;
void dump(std::ostream& s = std::cout, Size depth = 0) const;
void host(Visitor<ResourceEntry>& visitor);
bool apply(UnaryProcessor<ResourceEntry>& processor);
bool applyChildren(UnaryProcessor<ResourceEntry>& processor);
typedef ResourceEntry* IteratorPosition;
class BALL_EXPORT IteratorTraits_
{
public:
BALL_CREATE_DEEP(IteratorTraits_)
IteratorTraits_()
: bound_(0),
position_(0),
stack_index_(new Index[1]),
stack_capacity_(1),
stack_size_(0)
{
}
IteratorTraits_(const ResourceEntry& entry)
: bound_((ResourceEntry *)&entry),
position_(0),
stack_index_(new Index[1]),
stack_capacity_(1),
stack_size_(0)
{
}
IteratorTraits_(const IteratorTraits_& traits, bool /* deep */ = true)
: bound_(traits.bound_),
position_(traits.position_),
stack_index_(new Index[traits.stack_capacity_]),
stack_capacity_(traits.stack_capacity_),
stack_size_(traits.stack_size_)
{
for (Index index = 0; index < (Index)stack_capacity_; ++index)
stack_index_[index] = traits.stack_index_[index];
}
virtual ~IteratorTraits_()
{
delete[] stack_index_;
}
const IteratorTraits_& operator = (const IteratorTraits_ &traits)
{
bound_ = traits.bound_;
position_ = traits.position_;
delete[] stack_index_;
stack_index_ = new Index[traits.stack_capacity_];
stack_capacity_ = traits.stack_capacity_;
stack_size_ = traits.stack_size_;
for (Index index = 0; index < (Index)stack_capacity_; ++index)
{
stack_index_[index] = traits.stack_index_[index];
}
return *this;
}
ResourceEntry* getContainer()
{
return bound_;
}
const ResourceEntry* getContainer() const
{
return bound_;
}
bool isSingular() const
{
return (bound_ == 0);
}
IteratorPosition& getPosition()
{
return position_;
}
const IteratorPosition& getPosition() const
{
return position_;
}
bool operator == (const IteratorTraits_& traits) const
{
return (position_ == traits.position_);
}
bool operator != (const IteratorTraits_& traits) const
{
return (position_ != traits.position_);
}
bool isValid() const
{
return (bound_ != 0 && position_ != 0
&& stack_index_ != 0
&& stack_size_ <= stack_capacity_
&& stack_capacity_ > 0);
}
void invalidate()
{
bound_ = 0;
position_ = 0;
stack_size_ = 0;
}
void toBegin()
{
stack_size_ = 0;
position_ = bound_;
}
bool isBegin() const
{
return (position_ == bound_);
}
void toEnd()
{
position_ = 0;
}
bool isEnd() const
{
return (position_ == 0);
}
ResourceEntry& getData()
{
return *position_;
}
const ResourceEntry& getData() const
{
return *position_;
}
void forward()
{
if (position_->number_children_ > 0)
{
position_ = position_->child_[0];
push(0);
}
else
{
Index next_child = INVALID_Index;
do
{
next_child = pop();
if (next_child == INVALID_Index)
{
position_ = 0;
return;
}
else
{
position_ = position_->parent_;
}
} while(++next_child >= (Index)position_->number_children_);
push(next_child);
position_ = position_->child_[next_child];
}
}
// traits-specific:
Size getDepth() const
{
return stack_size_;
}
private:
void push(Index next_child)
{
if (stack_size_ >= stack_capacity_)
{
Index *new_stack_index = new Index[stack_capacity_ << 1];
for (Index index = 0; index < (Index)stack_capacity_; ++index)
{
new_stack_index[index] = stack_index_[index];
}
delete[] stack_index_;
stack_index_ = new_stack_index;
stack_capacity_ <<= 1;
}
stack_index_[stack_size_] = next_child;
++stack_size_;
}
Index pop()
{
if (stack_size_ == 0)
{
return INVALID_Index;
}
else
{
return stack_index_[--stack_size_];
}
}
ResourceEntry* bound_;
IteratorPosition position_;
Index* stack_index_;
Size stack_capacity_;
Size stack_size_;
};
friend class IteratorTraits_;
typedef ForwardIterator<ResourceEntry, ResourceEntry, ResourceEntry*, IteratorTraits_> Iterator;
typedef ConstForwardIterator<ResourceEntry, ResourceEntry, ResourceEntry*, IteratorTraits_> ConstIterator;
Iterator begin() { return Iterator::begin(*this); }
Iterator end() { return Iterator::end(*this); }
ConstIterator begin() const { return ConstIterator::begin(*this); }
ConstIterator end() const { return ConstIterator::end(*this); }
protected:
virtual ResourceEntry* newEntry
(const String& key, const String& value, ResourceEntry* parent = 0) const;
virtual ResourceEntry** newEntryArray(Size size) const;
virtual void deleteEntry(ResourceEntry* entry) const;
virtual void deleteEntryArray(ResourceEntry** entry_array) const;
bool applyNostart_(UnaryProcessor<ResourceEntry>& processor);
private:
ResourceEntry* clone_(ResourceEntry* parent) const;
bool findGreaterOrEqual_(const String& key, Index& found) const;
String key_;
String value_;
ResourceEntry* parent_;
ResourceEntry** child_;
Size number_children_;
};
class BALL_EXPORT ResourceFile
: public File
{
public:
static char ENTRY_BEGIN;
static char ENTRY_END;
static char SEPARATOR;
typedef ResourceEntry Entry;
ResourceFile();
ResourceFile(const String& name);
virtual ~ResourceFile();
void destroy();
virtual void clear();
/*
*/
bool open(const String& name);
/*
*/
void close();
/*
*/
static void saveAs(const Entry& entry, const String& name);
/*
*/
void saveAs(const String& name);
/*
*/
void save(const Entry& entry);
/*
*/
void save();
/*
*/
Size getSize() const;
/*
*/
Entry& getRoot();
/*
*/
const Entry& getRoot() const;
/*
*/
Entry* getEntry(const String& key_path);
/*
*/
const Entry* getEntry(const String& key_path) const;
/*
*/
String* getValue(const String& key_path);
/*
*/
const String* getValue(const String& key_path) const;
/*
*/
Entry* insert(const String& key_path, const String& name);
/*
*/
bool removeKey(const String& key_path);
bool operator == (const ResourceFile& entry) const;
bool operator != (const ResourceFile& entry) const;
/* Return true if the key exists somewhere in the tree.
*/
bool hasKey(const String& key_path) const;
/* Return true if the entry has no children.
*/
bool isEmpty() const;
bool isValid() const;
void dump(std::ostream& s = std::cout, Size depth = 0) const;
friend std::istream& operator >> (std::istream& s, ResourceFile& resource_file);
void host(Visitor<ResourceFile>& visitor);
bool apply(UnaryProcessor<Entry>& processor);
static bool applyChildren(Entry& entry, UnaryProcessor<Entry>& processor);
typedef Entry::Iterator Iterator;
Iterator begin()
{
return Iterator::begin(root_);
}
Iterator end()
{
return Iterator::end(root_);
}
typedef Entry::ConstIterator ConstIterator;
ConstIterator begin() const
{
return ConstIterator::begin(root_);
}
ConstIterator end() const
{
return ConstIterator::end(root_);
}
private:
const ResourceFile& operator = (const ResourceFile& file);
static void save_(File& file, const Entry* entry, Size& depth);
bool validateSyntax_();
void skipWhitespaces_();
Entry root_;
};
# ifndef BALL_NO_INLINE_FUNCTIONS
# include <BALL/FORMAT/resourceFile.iC>
# endif
} // namespace BALL
#endif // BALL_FORMAT_RESOURCEFILE_H
BALL_EXPORT
#define BALL_EXPORT
Definition COMMON/global.h:50
autoDeletable.h
BALL::AutoDeletable::AutoDeletable
AutoDeletable()
BALL::ConstForwardIterator
Definition forwardIterator.h:26
BALL::ConstForwardIterator< ResourceEntry, ResourceEntry, ResourceEntry *, IteratorTraits_ >::begin
static ConstForwardIterator begin(const ResourceEntry &container)
BALL::ConstForwardIterator< ResourceEntry, ResourceEntry, ResourceEntry *, IteratorTraits_ >::end
static ConstForwardIterator end(const ResourceEntry &container)
BALL::File
Definition file.h:136
BALL::File::File
File()
BALL::ForwardIterator
Definition forwardIterator.h:199
BALL::ForwardIterator< ResourceEntry, ResourceEntry, ResourceEntry *, IteratorTraits_ >::begin
static ForwardIterator begin(const ResourceEntry &container)
BALL::ForwardIterator< ResourceEntry, ResourceEntry, ResourceEntry *, IteratorTraits_ >::end
static ForwardIterator end(const ResourceEntry &container)
BALL::ResourceEntry::IteratorTraits_
Definition resourceFile.h:322
BALL::ResourceEntry::IteratorTraits_::IteratorTraits_
IteratorTraits_(const ResourceEntry &entry)
Definition resourceFile.h:336
BALL::ResourceEntry::IteratorTraits_::isEnd
bool isEnd() const
Definition resourceFile.h:444
BALL::ResourceEntry::IteratorTraits_::getContainer
const ResourceEntry * getContainer() const
Definition resourceFile.h:383
BALL::ResourceEntry::IteratorTraits_::IteratorTraits_
IteratorTraits_(const IteratorTraits_ &traits, bool=true)
Definition resourceFile.h:345
BALL::ResourceEntry::IteratorTraits_::getPosition
const IteratorPosition & getPosition() const
Definition resourceFile.h:398
BALL::ResourceEntry::IteratorTraits_::getData
ResourceEntry & getData()
Definition resourceFile.h:449
BALL::ResourceEntry::IteratorTraits_::toBegin
void toBegin()
Definition resourceFile.h:428
BALL::ResourceEntry::IteratorTraits_::toEnd
void toEnd()
Definition resourceFile.h:439
BALL::ResourceEntry::IteratorTraits_::isValid
bool isValid() const
Definition resourceFile.h:413
BALL::ResourceEntry::IteratorTraits_::getContainer
ResourceEntry * getContainer()
Definition resourceFile.h:378
BALL::ResourceEntry::IteratorTraits_::IteratorTraits_
IteratorTraits_()
Definition resourceFile.h:327
BALL::ResourceEntry::IteratorTraits_::invalidate
void invalidate()
Definition resourceFile.h:421
BALL::ResourceEntry::IteratorTraits_::forward
void forward()
Definition resourceFile.h:459
BALL::ResourceEntry::IteratorTraits_::getPosition
IteratorPosition & getPosition()
Definition resourceFile.h:393
BALL::ResourceEntry::IteratorTraits_::isSingular
bool isSingular() const
Definition resourceFile.h:388
BALL::ResourceEntry::IteratorTraits_::isBegin
bool isBegin() const
Definition resourceFile.h:434
BALL::ResourceEntry::IteratorTraits_::getData
const ResourceEntry & getData() const
Definition resourceFile.h:454
BALL::ResourceEntry::IteratorTraits_::~IteratorTraits_
virtual ~IteratorTraits_()
Definition resourceFile.h:356
BALL::ResourceEntry::IteratorTraits_::getDepth
Size getDepth() const
Definition resourceFile.h:492
BALL::ResourceEntry
Definition resourceFile.h:43
BALL::ResourceEntry::getParent
ResourceEntry * getParent()
BALL::ResourceEntry::ResourceEntry
ResourceEntry()
BALL::ResourceEntry::destroy
void destroy()
BALL::ResourceEntry::newEntry
virtual ResourceEntry * newEntry(const String &key, const String &value, ResourceEntry *parent=0) const
BALL::ResourceEntry::isValid
bool isValid() const
BALL::ResourceEntry::Iterator
ForwardIterator< ResourceEntry, ResourceEntry, ResourceEntry *, IteratorTraits_ > Iterator
Definition resourceFile.h:541
BALL::ResourceEntry::host
void host(Visitor< ResourceEntry > &visitor)
BALL::ResourceEntry::insertSibling
ResourceEntry * insertSibling(const String &key, const String &value, bool replace_value=true)
BALL::ResourceEntry::apply
bool apply(UnaryProcessor< ResourceEntry > &processor)
BALL::ResourceEntry::removeChild
bool removeChild(const String &key, ResourceEntry **removed=0)
BALL::ResourceEntry::isChildOf
bool isChildOf(const ResourceEntry &entry) const
BALL::ResourceEntry::findChild
ResourceEntry * findChild(const String &key)
BALL::ResourceEntry::insert
ResourceEntry * insert(const String &key_path, const String &name)
BALL::ResourceEntry::getPath
String getPath() const
BALL::ResourceEntry::getSize
Size getSize() const
BALL::ResourceEntry::deleteEntry
virtual void deleteEntry(ResourceEntry *entry) const
BALL::ResourceEntry::findDescendant
ResourceEntry * findDescendant(const String &key)
BALL::ResourceEntry::getDepth
Size getDepth() const
BALL::ResourceEntry::removeKey
bool removeKey(const String &key_path)
BALL::ResourceEntry::getKey
const String & getKey() const
BALL::ResourceEntry::applyChildren
bool applyChildren(UnaryProcessor< ResourceEntry > &processor)
BALL::ResourceEntry::isRelatedWith
bool isRelatedWith(const ResourceEntry &entry) const
BALL::ResourceEntry::get
void get(ResourceEntry &entry, bool deep=true) const
BALL::ResourceEntry::insertChild
ResourceEntry * insertChild(const String &key, const String &value, bool replace_value=true)
BALL::ResourceEntry::isParentOf
bool isParentOf(const ResourceEntry &entry) const
BALL::ResourceEntry::isRoot
bool isRoot() const
BALL::ResourceEntry::isEmpty
bool isEmpty() const
BALL::ResourceEntry::end
Iterator end()
Definition resourceFile.h:545
BALL::ResourceEntry::set
void set(const ResourceEntry &entry, bool deep=true)
BALL::ResourceEntry::dump
void dump(std::ostream &s=std::cout, Size depth=0) const
BALL::ResourceEntry::mergeChildrenOf
bool mergeChildrenOf(ResourceEntry &entry, bool replace_value=true)
BALL::ResourceEntry::clear
void clear()
BALL::ResourceEntry::applyNostart_
bool applyNostart_(UnaryProcessor< ResourceEntry > &processor)
BALL::ResourceEntry::getValue
String & getValue()
BALL::ResourceEntry::ConstIterator
ConstForwardIterator< ResourceEntry, ResourceEntry, ResourceEntry *, IteratorTraits_ > ConstIterator
Definition resourceFile.h:542
BALL::ResourceEntry::begin
Iterator begin()
Definition resourceFile.h:544
BALL::ResourceEntry::deleteEntryArray
virtual void deleteEntryArray(ResourceEntry **entry_array) const
BALL::ResourceEntry::hasChild
bool hasChild(const String &key) const
BALL::ResourceEntry::setValue
void setValue(const String &value)
BALL::ResourceEntry::getRoot
ResourceEntry & getRoot()
BALL::ResourceEntry::isAncestorOf
bool isAncestorOf(const ResourceEntry &entry) const
BALL::ResourceEntry::getEntry
ResourceEntry * getEntry(const String &key_path)
BALL::ResourceEntry::begin
ConstIterator begin() const
Definition resourceFile.h:548
BALL::ResourceEntry::countChildren
Size countChildren() const
BALL::ResourceEntry::countDescendants
Size countDescendants() const
BALL::ResourceEntry::IteratorPosition
ResourceEntry * IteratorPosition
Definition resourceFile.h:319
BALL::ResourceEntry::end
ConstIterator end() const
Definition resourceFile.h:549
BALL::ResourceEntry::getChild
ResourceEntry * getChild(Position index)
BALL::ResourceEntry::isDescendantOf
bool isDescendantOf(const ResourceEntry &entry) const
BALL::ResourceEntry::newEntryArray
virtual ResourceEntry ** newEntryArray(Size size) const
BALL::ResourceEntry::findEntry
ResourceEntry * findEntry(const String &key)
BALL::ResourceFile
Definition resourceFile.h:583
BALL::ResourceFile::dump
void dump(std::ostream &s=std::cout, Size depth=0) const
BALL::ResourceFile::getRoot
Entry & getRoot()
BALL::ResourceFile::hasKey
bool hasKey(const String &key_path) const
BALL::ResourceFile::save
void save(const Entry &entry)
BALL::ResourceFile::ResourceFile
ResourceFile()
BALL::ResourceFile::getRoot
const Entry & getRoot() const
BALL::ResourceFile::isValid
bool isValid() const
BALL::ResourceFile::saveAs
static void saveAs(const Entry &entry, const String &name)
BALL::ResourceFile::begin
ConstIterator begin() const
Definition resourceFile.h:788
BALL::ResourceFile::ENTRY_BEGIN
static char ENTRY_BEGIN
Definition resourceFile.h:592
BALL::ResourceFile::apply
bool apply(UnaryProcessor< Entry > &processor)
BALL::ResourceFile::begin
Iterator begin()
Definition resourceFile.h:775
BALL::ResourceFile::Entry
ResourceEntry Entry
Definition resourceFile.h:609
BALL::ResourceFile::end
Iterator end()
Definition resourceFile.h:780
BALL::ResourceFile::getValue
const String * getValue(const String &key_path) const
BALL::ResourceFile::saveAs
void saveAs(const String &name)
BALL::ResourceFile::destroy
void destroy()
BALL::ResourceFile::ENTRY_END
static char ENTRY_END
Definition resourceFile.h:596
BALL::ResourceFile::~ResourceFile
virtual ~ResourceFile()
BALL::ResourceFile::open
bool open(const String &name)
BALL::ResourceFile::getValue
String * getValue(const String &key_path)
BALL::ResourceFile::host
void host(Visitor< ResourceFile > &visitor)
BALL::ResourceFile::applyChildren
static bool applyChildren(Entry &entry, UnaryProcessor< Entry > &processor)
BALL::ResourceFile::ConstIterator
Entry::ConstIterator ConstIterator
Definition resourceFile.h:786
BALL::ResourceFile::clear
virtual void clear()
BALL::ResourceFile::SEPARATOR
static char SEPARATOR
Definition resourceFile.h:600
BALL::ResourceFile::end
ConstIterator end() const
Definition resourceFile.h:793
BALL::ResourceFile::getSize
Size getSize() const
BALL::ResourceFile::getEntry
Entry * getEntry(const String &key_path)
BALL::ResourceFile::removeKey
bool removeKey(const String &key_path)
BALL::ResourceFile::save
void save()
BALL::ResourceFile::close
void close()
BALL::ResourceFile::getEntry
const Entry * getEntry(const String &key_path) const
BALL::ResourceFile::ResourceFile
ResourceFile(const String &name)
BALL::ResourceFile::isEmpty
bool isEmpty() const
BALL::ResourceFile::insert
Entry * insert(const String &key_path, const String &name)
BALL::ResourceFile::Iterator
Entry::Iterator Iterator
Definition resourceFile.h:773
BALL::String
Definition string.h:57
BALL::UnaryProcessor
Definition processor.h:60
BALL::Visitor
Definition visitor.h:23
common.h
file.h
forwardIterator.h
BALL_CREATE_DEEP
#define BALL_CREATE_DEEP(name)
Definition create.h:26
BALL
Definition constants.h:13
BALL::Size
BALL_SIZE_TYPE Size
Definition COMMON/global.h:114
BALL::INVALID_Index
static const Index INVALID_Index
Definition COMMON/global.h:239
BALL::Position
BALL_SIZE_TYPE Position
Definition COMMON/global.h:140
BALL::Index
BALL_INDEX_TYPE Index
Definition COMMON/global.h:105
std
STL namespace.
processor.h
visitor.h