All pastes #829164 Raw Edit

Anonymous

public text v1 · immutable
#829164 ·published 2007-12-22 23:25 UTC
rendered paste body
#ifndef STRING_TABLE_H
#define STRING_TABLE_H

#include <irrlicht.h>

enum ST_TokenType
{
	STTT_STRING,
	STTT_PARAM,
	STTT_ENDL,
};

class IStringTableToken : public irr::IUnknown
{
public:
	virtual ST_TokenType GetType()	= 0;
};

class TokenString : public IStringTableToken
{
public:
	virtual ST_TokenType GetType()	{ return STTT_STRING; }
	irr::core::stringw mString;
};

class TokenParam : public IStringTableToken
{
public:
	virtual ST_TokenType GetType()	{ return STTT_PARAM; }
	unsigned int mParamId;
};

class TokenEndl : public IStringTableToken
{
public:
	virtual ST_TokenType GetType()	{ return STTT_ENDL; }
};


struct StringTableEntry
{
    StringTableEntry()  {}
    StringTableEntry(const irr::core::stringw &identifier_);
    StringTableEntry(const irr::core::stringw &identifier_, const irr::core::stringw &string_);

	StringTableEntry(const StringTableEntry &orig_);
	~StringTableEntry();

	StringTableEntry& operator=(const StringTableEntry &orig_);

	void Tokenize(const irr::core::stringw &string_);
	irr::core::stringw GetString( const irr::core::array<irr::core::stringw> & params_ );
	irr::core::stringw GetIdentifier() const { return mIdentifier; }

    bool operator <(const StringTableEntry& other_) const
    {
        return mIdentifier < other_.mIdentifier;
    }

private:
	void DropTokens();

    irr::core::stringw mIdentifier;
	irr::core::stringw mOrigString;		// we could get along without it, but it makes debugging easier
	irr::core::array<IStringTableToken*> mTokens;
};

class StringTable : public irr::IUnknown
{
public:
    bool Load(const char* filename_);
//    bool Save(const c8* filename_);   // not implemented

    //! Get the string for identifier_
    //! If identifier_ ain't available return the identifier itself.
    irr::core::stringw Get(const irr::core::stringw &identifier_, bool clearParamsAfterwards_=true);
    irr::core::stringw Get(const char *identifier_, bool clearParamsAfterwards_=true);

	//! strings can have variable parameters. Like "hello %1 %2". 
	//! In this case %1 and %2 will be replaced by the params pushed here.
	//! The first paramter pushed is %1 as %0 will return the identifier itself.
	//! To avoid using params when a % is in the string use a backslash like "\%"
	void PushParam(const irr::core::stringw &param_);

	//! clear manually the parameter stack. Usually you will do that automatically on Get
	void ClearParams();

	//! set the string for the given identifier to value string_
	void Set(const irr::core::stringw &identifier_, const irr::core::stringw &string_);
	//! remove the string for the given identifier_
	void Remove(const irr::core::stringw &identifier_);
    //! add the string for given identifier_ to the stringtable, but without checking if identifier_ exists already
	//! this can cause duplicates, but is also a lot faster than set
    void Add(const irr::core::stringw &identifier_, const irr::core::stringw &string_);

	//! remove duplicated identifiers. One entry for the identifier will be kept, but you have no control which one.
	//! Returns the number of removed duplicates
	int RemoveDuplicates();

	// selftest
	bool UnitTest();

private:
    irr::core::array<StringTableEntry>  mStringTable;
	irr::core::array<irr::core::stringw> mParamStack;
};


#endif // STRING_TABLE_H