changed: ConfParser -- now we can have a tree (spaces can have more than one level)
git-svn-id: svn://ttmath.org/publicrep/winix/trunk@768 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
@@ -127,6 +127,132 @@ config syntax:
|
||||
|
||||
|
||||
*/
|
||||
|
||||
|
||||
|
||||
class Space
|
||||
{
|
||||
public:
|
||||
|
||||
Space();
|
||||
~Space();
|
||||
|
||||
Space(const Space & s);
|
||||
Space & operator=(const Space & s);
|
||||
|
||||
void Clear();
|
||||
|
||||
|
||||
// first we are searching in 'table_single' and if there is not
|
||||
// such a 'name' there then we are looking in 'table' (for the first item in the vector)
|
||||
// these methods return true if 'name' was found
|
||||
// in other case they return false and 'out' will be equal 'def'
|
||||
// they can return a null pointer if there is not such a 'name'
|
||||
std::wstring * GetValue(const wchar_t * name);
|
||||
std::wstring * GetValue(const std::wstring & name);
|
||||
|
||||
|
||||
/*
|
||||
those methods are used to extract information from space.table or space.table_single
|
||||
as a parameter they take the name of an option
|
||||
and a default value (if there is no such a parameter),
|
||||
they return appropriate value (either text, int or boolean)
|
||||
(in lists they return the first item if exists)
|
||||
|
||||
when calling Text(...) and AText(...) you should copy the object to whom a reference is returned
|
||||
it will be cleared in a next call to one of these methods (as well to Int() Size() and Bool())
|
||||
|
||||
AText(...) always returns a reference to UTF-8 string
|
||||
*/
|
||||
std::wstring & Text(const wchar_t * name);
|
||||
std::wstring & Text(const wchar_t * name, const wchar_t * def);
|
||||
std::wstring & Text(const std::wstring & name, const wchar_t * def);
|
||||
std::string & AText(const wchar_t * name);
|
||||
std::string & AText(const wchar_t * name, const char * def);
|
||||
std::string & AText(const std::wstring & name, const char * def);
|
||||
int Int(const wchar_t *);
|
||||
int Int(const wchar_t * name, int def);
|
||||
int Int(const std::wstring & name, int def);
|
||||
size_t Size(const wchar_t *);
|
||||
size_t Size(const wchar_t * name, size_t def);
|
||||
size_t Size(const std::wstring & name, size_t def);
|
||||
bool Bool(const wchar_t *);
|
||||
bool Bool(const wchar_t * name, bool def);
|
||||
bool Bool(const std::wstring & name, bool def);
|
||||
|
||||
|
||||
|
||||
/*
|
||||
*
|
||||
*
|
||||
raw access to the parsed values
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
this is the table which represents your config file
|
||||
in the Table map: the first (key) is your 'option' and the second is 'list'
|
||||
*/
|
||||
typedef std::vector<std::wstring> Value;
|
||||
typedef std::map<std::wstring, Value> Table;
|
||||
|
||||
|
||||
|
||||
/*
|
||||
if your config file consists mainly of single forms such as:
|
||||
option = value
|
||||
option2 = value2
|
||||
then you can call SplitSingle(true) for not inserting single values to
|
||||
previous 'table' but instead to 'table_single'
|
||||
table_single as the second parameter takes only std::wstring (instead of the whole std::vector)
|
||||
so you can save a little memory from not using std::vector
|
||||
*/
|
||||
typedef std::map<std::wstring, std::wstring> TableSingle;
|
||||
|
||||
|
||||
std::wstring name; // space name
|
||||
TableSingle table_single; // std::map<std::wstring, std::wstring>
|
||||
Table table; // std::map<std::wstring, std::vector<std::wstring> >
|
||||
|
||||
// childs
|
||||
typedef std::vector<Space*> Spaces;
|
||||
std::vector<Space*> spaces;
|
||||
|
||||
// a parent space
|
||||
// null means a root space
|
||||
Space * parent;
|
||||
|
||||
/*
|
||||
those methods are used to extract lists
|
||||
note: if there is one option in table_single they will return it
|
||||
return true if such an option exists (but value can be an empty list)
|
||||
*/
|
||||
bool ListText(const wchar_t * name, std::vector<std::wstring> & list);
|
||||
bool ListText(const std::wstring & name, std::vector<std::wstring> & list);
|
||||
|
||||
|
||||
/*
|
||||
printing the content
|
||||
(for debug purposes)
|
||||
*/
|
||||
void Print(std::wostream & out);
|
||||
|
||||
private:
|
||||
std::wstring tmp_name;
|
||||
std::wstring tmp_value_text;
|
||||
std::string tmp_value_text_ascii;
|
||||
|
||||
int ToInt(const std::wstring & value);
|
||||
size_t ToSize(const std::wstring & value);
|
||||
bool ToBool(const std::wstring & value);
|
||||
wchar_t ToSmall(wchar_t c);
|
||||
bool EqualNoCase(const wchar_t * str1, const wchar_t * str2);
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
class ConfParser
|
||||
{
|
||||
public:
|
||||
@@ -138,6 +264,13 @@ public:
|
||||
ConfParser();
|
||||
|
||||
|
||||
/*
|
||||
setting the root space
|
||||
*/
|
||||
void SetSpace(Space * pspace);
|
||||
void SetSpace(Space & pspace);
|
||||
|
||||
|
||||
/*
|
||||
setting options of the parser to the default values
|
||||
utf8, split single etc.
|
||||
@@ -145,18 +278,10 @@ public:
|
||||
void SetDefault();
|
||||
|
||||
|
||||
/*
|
||||
clearing
|
||||
this method doesn't call SetDefault() only those parsed values are cleared
|
||||
and the status is set to 'ok'
|
||||
*/
|
||||
void Clear();
|
||||
|
||||
|
||||
/*
|
||||
status of parsing
|
||||
*/
|
||||
enum Status { ok, cant_open_file, syntax_error };
|
||||
enum Status { ok, cant_open_file, syntax_error, no_space };
|
||||
|
||||
|
||||
/*
|
||||
@@ -234,134 +359,21 @@ public:
|
||||
void UTF8(bool utf);
|
||||
|
||||
|
||||
|
||||
/*
|
||||
those methods are used to extract information from space.table or space.table_single
|
||||
as a parameter they take the name of an option
|
||||
and a default value (if there is no such a parameter),
|
||||
they return appropriate value (either text, int or boolean)
|
||||
(in lists they return the first item if exists)
|
||||
|
||||
when calling Text(...) and AText(...) you should copy the object to whom a reference is returned
|
||||
it will be cleared in a next call to one of these methods (as well to Int() Size() and Bool())
|
||||
|
||||
AText(...) always returns a reference to UTF-8 string
|
||||
*/
|
||||
void ToText(const wchar_t * name, std::wstring & out);
|
||||
void ToText(const wchar_t * name, std::wstring & out, const wchar_t * def);
|
||||
void ToText(const std::wstring & name, std::wstring & out, const wchar_t * def);
|
||||
std::wstring & Text(const wchar_t * name);
|
||||
std::wstring & Text(const wchar_t * name, const wchar_t * def);
|
||||
std::wstring & Text(const std::wstring & name, const wchar_t * def);
|
||||
std::string & AText(const wchar_t * name);
|
||||
std::string & AText(const wchar_t * name, const wchar_t * def);
|
||||
std::string & AText(const std::wstring & name, const wchar_t * def);
|
||||
int Int(const wchar_t *);
|
||||
int Int(const wchar_t * name, int def);
|
||||
int Int(const std::wstring & name, int def);
|
||||
size_t Size(const wchar_t *);
|
||||
size_t Size(const wchar_t * name, size_t def);
|
||||
size_t Size(const std::wstring & name, size_t def);
|
||||
bool Bool(const wchar_t *);
|
||||
bool Bool(const wchar_t * name, bool def);
|
||||
bool Bool(const std::wstring & name, bool def);
|
||||
|
||||
|
||||
/*
|
||||
some default values
|
||||
used in Text() Int() or Bool() when you don't explicitly set the default value
|
||||
|
||||
if you don't set it directly then:
|
||||
default text is: "" (empty)
|
||||
default int or size is: 0
|
||||
default bool is: false
|
||||
*/
|
||||
void SetDefaultText(const wchar_t * def);
|
||||
void SetDefaultText(const std::wstring & def);
|
||||
void SetDefaultInt(int def);
|
||||
void SetDefaultSize(size_t def);
|
||||
void SetDefaultBool(bool def);
|
||||
|
||||
|
||||
/*
|
||||
those methods are used to extract lists
|
||||
note: if there is one option in table_single they will return it
|
||||
*/
|
||||
void ListText(const wchar_t * name, std::vector<std::wstring> & list);
|
||||
void ListText(const std::wstring & name, std::vector<std::wstring> & list);
|
||||
|
||||
|
||||
/*
|
||||
printing the content
|
||||
(for debug purposes)
|
||||
*/
|
||||
void Print(std::ostream & out);
|
||||
|
||||
|
||||
/*
|
||||
*
|
||||
*
|
||||
raw access to the parsed values
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
this is the table which represents your config file
|
||||
in the Table map: the first (key) is your 'option' and the second is 'list'
|
||||
*/
|
||||
typedef std::vector<std::wstring> Value;
|
||||
typedef std::map<std::wstring, Value> Table;
|
||||
|
||||
|
||||
|
||||
/*
|
||||
if your config file consists mainly of single forms such as:
|
||||
option = value
|
||||
option2 = value2
|
||||
then you can call SplitSingle(true) for not inserting single values to
|
||||
previous 'table' but instead to 'table_single'
|
||||
table_single as the second parameter takes only std::wstring (instead of the whole std::vector)
|
||||
so you can save a little memory from not using std::vector
|
||||
*/
|
||||
typedef std::map<std::wstring, std::wstring> TableSingle;
|
||||
|
||||
|
||||
|
||||
class Space
|
||||
{
|
||||
public:
|
||||
std::wstring name; // space name
|
||||
TableSingle table_single; // std::map<std::wstring, std::wstring>
|
||||
Table table; // std::map<std::wstring, std::vector<std::wstring> >
|
||||
|
||||
// first we are searching in 'table_single' and if there is not
|
||||
// such a 'name' there then we are looking in 'table' (for the first item in the vector)
|
||||
// these methods return true if 'name' was found
|
||||
// in other case they return false and 'out' will be equal 'def'
|
||||
bool GetValue(const wchar_t * name, std::wstring & out);
|
||||
bool GetValue(const wchar_t * name, std::wstring & out, const wchar_t * def);
|
||||
bool GetValue(const std::wstring & name, std::wstring & out);
|
||||
bool GetValue(const std::wstring & name, std::wstring & out, const wchar_t * def);
|
||||
|
||||
private:
|
||||
std::wstring tmp_name;
|
||||
std::wstring tmp_value;
|
||||
};
|
||||
|
||||
// one global space (without a name)
|
||||
Space space;
|
||||
|
||||
// first namespace
|
||||
typedef std::list<Space> Spaces;
|
||||
Spaces spaces;
|
||||
|
||||
|
||||
|
||||
|
||||
private:
|
||||
|
||||
|
||||
/*
|
||||
current space set by SetSpace();
|
||||
*/
|
||||
Space * root_space;
|
||||
|
||||
|
||||
/*
|
||||
a space in which we are now
|
||||
*/
|
||||
Space * space;
|
||||
|
||||
|
||||
/*
|
||||
true if Parse() method was called
|
||||
false if ParseString() was called
|
||||
@@ -398,7 +410,7 @@ private:
|
||||
/*
|
||||
last read list
|
||||
*/
|
||||
Value value;
|
||||
Space::Value value;
|
||||
|
||||
|
||||
/*
|
||||
@@ -473,30 +485,13 @@ private:
|
||||
*/
|
||||
bool use_escape_char;
|
||||
|
||||
/*
|
||||
if true we are parsing the global space
|
||||
if false we are adding to the last item from spaces tab
|
||||
*/
|
||||
bool using_global_space;
|
||||
|
||||
std::string afile_name;
|
||||
|
||||
std::wstring default_str;
|
||||
int default_int;
|
||||
size_t default_size;
|
||||
bool default_bool;
|
||||
|
||||
std::wstring tmp_name;
|
||||
std::wstring tmp_value_text;
|
||||
std::string tmp_value_text_ascii;
|
||||
|
||||
int ToInt(const std::wstring & value);
|
||||
size_t ToSize(const std::wstring & value);
|
||||
bool ToBool(const std::wstring & value);
|
||||
|
||||
void Parse();
|
||||
void TestListEnd();
|
||||
void TestListStart();
|
||||
void ParseLoop();
|
||||
void SpaceEnds();
|
||||
void SpaceStarts();
|
||||
void ReadAddValue();
|
||||
void AddOption();
|
||||
|
||||
@@ -522,8 +517,7 @@ private:
|
||||
void SkipWhiteLines();
|
||||
void SkipLine();
|
||||
void Trim(std::wstring & s);
|
||||
wchar_t ToSmall(wchar_t c);
|
||||
bool EqualNoCase(const wchar_t * str1, const wchar_t * str2);
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user