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:
Tomasz Sowa 2011-09-26 05:40:26 +00:00
parent 60f0e62c23
commit 89efaa790f
10 changed files with 644 additions and 599 deletions

View File

@ -84,7 +84,7 @@ config.o: ../core/htmlfilter.h ../templates/templates.h
config.o: ../templates/patterncacher.h ../templates/indexpatterns.h
config.o: ../templates/patterns.h ../templates/changepatterns.h
config.o: ../core/sessionmanager.h
confparser.o: confparser.h misc.h item.h requesttypes.h ../../ezc/src/utf8.h
confparser.o: confparser.h ../../ezc/src/utf8.h
crypt.o: crypt.h run.h config.h confparser.h htmlfilter.h user.h
crypt.o: ../../ezc/src/utf8.h misc.h item.h requesttypes.h log.h textstream.h
crypt.o: logmanipulators.h slog.h cur.h request.h error.h

View File

@ -31,6 +31,10 @@ void Config::ShowError()
switch( parser.status )
{
case ConfParser::no_space:
log << log2 << "Config: space not set" << logend;
break;
case ConfParser::ok:
log << log2 << "Config: syntax ok" << logend;
break;
@ -70,8 +74,9 @@ bool Config::ReadConfig(bool errors_to_stdout_, bool stdout_is_closed)
parser.SplitSingle(true);
parser.UTF8(true); // config is always read in UTF-8
ConfParser::Status status = parser.Parse( config_file );
parser.SetSpace(space);
ConfParser::Status status = parser.Parse(config_file);
if( status == ConfParser::ok )
{
@ -184,7 +189,7 @@ void Config::AssignValues(bool stdout_is_closed)
html_filter_wrap_line = Int(L"html_filter_wrap_line", 110);
html_filter_tabs = Size(L"html_filter_tabs", 2);
html_filter_orphans = Bool(L"html_filter_orphans", true);
html_filter_orphans_mode_str = AText(L"html_filter_orphans_mode_str", L"nbsp");
html_filter_orphans_mode_str = Text(L"html_filter_orphans_mode_str", L"nbsp");
locale_dir = Text(L"locale_dir");
locale_dir_default = Text(L"locale_dir_default");
@ -230,7 +235,7 @@ void Config::AssignValues(bool stdout_is_closed)
void Config::SetAdditionalVariables()
{
if( html_filter_orphans_mode_str == "160" )
if( html_filter_orphans_mode_str == L"160" )
html_filter_orphans_mode = HTMLFilter::orphan_160space;
else
html_filter_orphans_mode = HTMLFilter::orphan_nbsp;
@ -297,111 +302,111 @@ void Config::CheckPasswd()
std::wstring & Config::Text(const wchar_t * name)
{
return parser.Text(name);
return space.Text(name);
}
std::wstring & Config::Text(const wchar_t * name, const wchar_t * def)
{
return parser.Text(name, def);
return space.Text(name, def);
}
std::wstring & Config::Text(const std::wstring & name, const wchar_t * def)
{
return parser.Text(name, def);
return space.Text(name, def);
}
std::string & Config::AText(const wchar_t * name)
{
return parser.AText(name);
return space.AText(name);
}
std::string & Config::AText(const wchar_t * name, const wchar_t * def)
std::string & Config::AText(const wchar_t * name, const char * def)
{
return parser.AText(name, def);
return space.AText(name, def);
}
std::string & Config::AText(const std::wstring & name, const wchar_t * def)
std::string & Config::AText(const std::wstring & name, const char * def)
{
return parser.AText(name, def);
return space.AText(name, def);
}
int Config::Int(const wchar_t * name)
{
return parser.Int(name);
return space.Int(name);
}
int Config::Int(const wchar_t * name, int def)
{
return parser.Int(name, def);
return space.Int(name, def);
}
int Config::Int(const std::wstring & name, int def)
{
return parser.Int(name, def);
return space.Int(name, def);
}
size_t Config::Size(const wchar_t * name)
{
return parser.Size(name);
return space.Size(name);
}
size_t Config::Size(const wchar_t * name, size_t def)
{
return parser.Size(name, def);
return space.Size(name, def);
}
size_t Config::Size(const std::wstring & name, size_t def)
{
return parser.Size(name, def);
return space.Size(name, def);
}
bool Config::Bool(const wchar_t * name)
{
return parser.Bool(name);
return space.Bool(name);
}
bool Config::Bool(const wchar_t * name, bool def)
{
return parser.Bool(name, def);
return space.Bool(name, def);
}
bool Config::Bool(const std::wstring & name, bool def)
{
return parser.Bool(name, def);
return space.Bool(name, def);
}
void Config::ListText(const wchar_t * name, std::vector<std::wstring> & list)
bool Config::ListText(const wchar_t * name, std::vector<std::wstring> & list)
{
parser.ListText(name, list);
return space.ListText(name, list);
}
void Config::ListText(const std::wstring & name, std::vector<std::wstring> & list)
bool Config::ListText(const std::wstring & name, std::vector<std::wstring> & list)
{
parser.ListText(name, list);
return space.ListText(name, list);
}
void Config::Print(std::ostream & out)
void Config::Print(std::wostream & out)
{
parser.Print(out);
space.Print(out);
}

View File

@ -188,7 +188,7 @@ public:
// orphans mode
// either: "nbsp" or "160"
// default: "nbsp"
std::string html_filter_orphans_mode_str;
std::wstring html_filter_orphans_mode_str;
HTMLFilter::OrphanMode html_filter_orphans_mode;
// the url of a new empty item (if there is not the subject too)
@ -459,8 +459,8 @@ public:
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);
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);
@ -471,11 +471,11 @@ public:
bool Bool(const wchar_t *);
bool Bool(const wchar_t * name, bool def);
bool Bool(const std::wstring & name, bool def);
void ListText(const wchar_t * name, std::vector<std::wstring> & list);
void ListText(const std::wstring & name, std::vector<std::wstring> & list);
bool ListText(const wchar_t * name, std::vector<std::wstring> & list);
bool ListText(const std::wstring & name, std::vector<std::wstring> & list);
// for debug
void Print(std::ostream & out);
void Print(std::wostream & out);
private:
void ShowError();
@ -484,6 +484,7 @@ private:
void CheckLocale();
void CheckPasswd();
Space space;
ConfParser parser;
std::string default_str;

View File

@ -14,12 +14,399 @@
/*
*
*
* Space
*
*
*/
Space::Space()
{
parent = 0;
}
Space::~Space()
{
Clear();
}
Space::Space(const Space & s)
{
operator=(s);
}
Space & Space::operator=(const Space & s)
{
Clear();
name = s.name;
table_single = s.table_single;
table = s.table;
parent = s.parent;
for(size_t i=0 ; i<s.spaces.size() ; ++i)
{
Space * pspace = new Space(*s.spaces[i]);
pspace->parent = this;
spaces.push_back(pspace);
}
return *this;
}
void Space::Clear()
{
name.clear();
table_single.clear();
table.clear();
for(size_t i=0 ; i<spaces.size() ; ++i)
delete spaces[i];
spaces.clear();
}
std::wstring * Space::GetValue(const wchar_t * name)
{
tmp_name = name;
return GetValue(tmp_name);
}
std::wstring * Space::GetValue(const std::wstring & name)
{
TableSingle::iterator i = table_single.find(name);
if( i != table_single.end() )
{
return &i->second;
}
else
{
Table::iterator t = table.find(name);
if( t == table.end() || t->second.empty() )
{
return 0;
}
else
{
return &t->second[0];
}
}
}
std::wstring & Space::Text(const wchar_t * name)
{
tmp_name = name;
return Text(tmp_name, L"");
}
std::wstring & Space::Text(const wchar_t * name, const wchar_t * def)
{
tmp_name = name;
return Text(tmp_name, def);
}
std::wstring & Space::Text(const std::wstring & name, const wchar_t * def)
{
std::wstring * value = GetValue(name);
if( value )
{
return *value;
}
else
{
tmp_value_text = def;
return tmp_value_text;
}
}
std::string & Space::AText(const wchar_t * name)
{
tmp_name = name;
return AText(tmp_name, "");
}
std::string & Space::AText(const wchar_t * name, const char * def)
{
tmp_name = name;
return AText(tmp_name, def);
}
std::string & Space::AText(const std::wstring & name, const char * def)
{
std::wstring * value = GetValue(name);
if( value )
{
Ezc::WideToUTF8(*value, tmp_value_text_ascii);
return tmp_value_text_ascii;
}
else
{
tmp_value_text_ascii = def;
return tmp_value_text_ascii;
}
}
int Space::Int(const wchar_t * name)
{
tmp_name = name;
return Int(tmp_name, 0);
}
int Space::Int(const wchar_t * name, int def)
{
tmp_name = name;
return Int(tmp_name, def);
}
int Space::ToInt(const std::wstring & value)
{
long res = (value[0] == '0')? wcstol(value.c_str() + 1, 0, 8) : wcstol(value.c_str(), 0, 10);
return static_cast<int>(res);
}
int Space::Int(const std::wstring & name, int def)
{
std::wstring * value = GetValue(name);
if( value )
return ToInt(*value);
return def;
}
size_t Space::Size(const wchar_t * name)
{
tmp_name = name;
return Size(tmp_name, 0);
}
size_t Space::Size(const wchar_t * name, size_t def)
{
tmp_name = name;
return Size(tmp_name, def);
}
size_t Space::ToSize(const std::wstring & value)
{
unsigned long res = (value[0] == '0')? wcstoul(value.c_str() + 1, 0, 8) : wcstoul(value.c_str(), 0, 10);
return static_cast<size_t>(res);
}
size_t Space::Size(const std::wstring & name, size_t def)
{
std::wstring * value = GetValue(name);
if( value )
return ToSize(*value);
return def;
}
bool Space::Bool(const wchar_t * name)
{
tmp_name = name;
return Bool(tmp_name, false);
}
bool Space::Bool(const wchar_t * name, bool def)
{
tmp_name = name;
return Bool(tmp_name, def);
}
bool Space::ToBool(const std::wstring & value)
{
return ( EqualNoCase(value.c_str(), L"true") ||
EqualNoCase(value.c_str(), L"yes") ||
EqualNoCase(value.c_str(), L"1")
);
}
bool Space::Bool(const std::wstring & name, bool def)
{
std::wstring * value = GetValue(name);
if( value )
return ToBool(*value);
return def;
}
// in lists we don't use default values
bool Space::ListText(const wchar_t * name, std::vector<std::wstring> & list)
{
tmp_name = name;
return ListText(tmp_name, list);
}
bool Space::ListText(const std::wstring & name, std::vector<std::wstring> & list)
{
list.clear();
TableSingle::iterator i = table_single.find(name);
if( i != table_single.end() )
{
list.push_back(i->second);
return true;
}
else
{
Table::iterator t = table.find(name);
if( t != table.end() )
{
list = t->second;
return true;
}
}
return false;
}
wchar_t Space::ToSmall(wchar_t c)
{
if( c>='A' && c<='Z' )
c = c - 'A' + 'a';
return c;
}
bool Space::EqualNoCase(const wchar_t * str1, const wchar_t * str2)
{
while( *str1 && *str2 && ToSmall(*str1) == ToSmall(*str2) )
{
++str1;
++str2;
}
if( *str1 == 0 && *str2 == 0 )
return true;
return false;
}
void Space::Print(std::wostream & out)
{
out << "Space name: " << name << std::endl;
out << "table_single: " << std::endl;
TableSingle::iterator i1;
for(i1 = table_single.begin() ; i1 != table_single.end() ; ++i1)
out << i1->first << '=' << i1->second << std::endl;
out << "table: " << std::endl;
Table::iterator i2;
Value::iterator i3;
for(i2 = table.begin() ; i2 != table.end() ; ++i2)
{
out << i2->first << '=';
for(i3 = i2->second.begin() ; i3 != i2->second.end() ; ++i3)
out << *i3 << ',';
out << std::endl;
}
for(size_t i=0 ; i<spaces.size() ; ++i)
spaces[i]->Print(out);
}
/*
*
*
* ConfParser
*
*
*/
ConfParser::ConfParser()
{
root_space = 0;
SetDefault();
}
void ConfParser::SetSpace(Space * pspace)
{
root_space = pspace;
}
void ConfParser::SetSpace(Space & pspace)
{
root_space = &pspace;
}
void ConfParser::SetDefault()
{
// you can change this separators to what you want
@ -34,11 +421,6 @@ void ConfParser::SetDefault()
skip_empty = false;
use_escape_char = true;
input_as_utf8 = false;
default_str = L"";
default_int = 0;
default_size = 0;
default_bool = false;
}
@ -67,23 +449,9 @@ void ConfParser::UTF8(bool utf)
void ConfParser::Clear()
{
space.table.clear();
space.table_single.clear();
spaces.clear();
line = 1;
using_global_space = true;
pchar_ascii = 0;
pchar_unicode = 0;
status = ok;
}
ConfParser::Status ConfParser::Parse(const char * file_name)
{
Clear();
reading_from_file = true;
file.clear();
@ -129,10 +497,10 @@ ConfParser::Status ConfParser::Parse(const std::wstring & file_name)
ConfParser::Status ConfParser::ParseString(const char * str)
{
Clear();
reading_from_file = false;
reading_from_wchar_string = false;
pchar_ascii = str;
pchar_unicode = 0;
Parse();
@ -148,10 +516,10 @@ ConfParser::Status ConfParser::ParseString(const std::string & str)
ConfParser::Status ConfParser::ParseString(const wchar_t * str)
{
Clear();
reading_from_file = false;
reading_from_wchar_string = true;
pchar_unicode = str;
pchar_ascii = 0;
Parse();
@ -167,15 +535,35 @@ ConfParser::Status ConfParser::ParseString(const std::wstring & str)
void ConfParser::Parse()
{
if( !root_space )
{
status = no_space;
return;
}
line = 1;
status = ok;
space = root_space;
ReadChar();
SkipWhiteLines();
ParseLoop();
if( status == ok && space != root_space )
{
// last closing a space characters ')' are missing
status = syntax_error;
}
}
void ConfParser::ParseLoop()
{
while( status == ok && lastc != -1 )
{
if( lastc == list_end )
{
TestListEnd();
SpaceEnds();
}
else
{
@ -183,7 +571,7 @@ void ConfParser::Parse()
if( lastc == list_start )
{
TestListStart();
SpaceStarts();
}
else
if( lastc == separator && !variable.empty() )
@ -202,36 +590,31 @@ void ConfParser::Parse()
}
void ConfParser::TestListEnd()
void ConfParser::SpaceEnds()
{
if( using_global_space )
if( space == root_space )
{
// there cannot be a loose list end character in the global space
status = syntax_error;
}
else
{
using_global_space = true;
space = space->parent;
ReadChar();
}
}
void ConfParser::TestListStart()
void ConfParser::SpaceStarts()
{
if( using_global_space )
{
spaces.insert(spaces.end(), Space());
spaces.back().name = variable;
using_global_space = false;
Space * new_space = new Space();
space->spaces.push_back(new_space);
new_space->parent = space;
new_space->name = variable;
space = new_space;
ReadChar();
}
else
{
// only one additional level of spaces is allowed
status = syntax_error;
}
}
void ConfParser::ReadAddValue()
@ -336,28 +719,6 @@ std::wstring::size_type i;
}
wchar_t ConfParser::ToSmall(wchar_t c)
{
if( c>='A' && c<='Z' )
c = c - 'A' + 'a';
return c;
}
bool ConfParser::EqualNoCase(const wchar_t * str1, const wchar_t * str2)
{
while( *str1 && *str2 && ToSmall(*str1) == ToSmall(*str2) )
{
++str1;
++str2;
}
if( *str1 == 0 && *str2 == 0 )
return true;
return false;
}
@ -370,19 +731,14 @@ void ConfParser::AddOption()
return;
}
Space * ps = &space;
if( !using_global_space && !spaces.empty() )
ps = &spaces.back();
if( split_single && value.size() == 1 )
{
ps->table_single[variable] = value[0];
space->table_single[variable] = value[0];
DeleteFromTable(variable);
}
else
{
ps->table[variable] = value;
space->table[variable] = value;
DeleteFromTableSingle(variable);
}
}
@ -391,30 +747,20 @@ void ConfParser::AddOption()
void ConfParser::DeleteFromTable(const std::wstring & var)
{
Space * ps = &space;
Space::Table::iterator i = space->table.find(var);
if( !using_global_space && !spaces.empty() )
ps = &spaces.back();
Table::iterator i = ps->table.find(var);
if( i != ps->table.end() )
ps->table.erase(i);
if( i != space->table.end() )
space->table.erase(i);
}
void ConfParser::DeleteFromTableSingle(const std::wstring & var)
{
Space * ps = &space;
Space::TableSingle::iterator i = space->table_single.find(var);
if( !using_global_space && !spaces.empty() )
ps = &spaces.back();
TableSingle::iterator i = ps->table_single.find(var);
if( i != ps->table_single.end() )
ps->table_single.erase(i);
if( i != space->table_single.end() )
space->table_single.erase(i);
}
@ -533,7 +879,7 @@ bool ConfParser::ReadValueSimple(bool use_list_delimiter)
if( use_list_delimiter )
list_delimiter1 = list_delimiter;
if( use_list_delimiter || !using_global_space )
if( use_list_delimiter || space != root_space )
list_delimiter2 = list_end;
while( lastc!=-1 && lastc!='\n' && lastc!=commentary &&
@ -673,313 +1019,5 @@ int ConfParser::ReadChar()
bool ConfParser::Space::GetValue(const wchar_t * name, std::wstring & out)
{
tmp_name = name;
return GetValue(tmp_name, out, L"");
}
bool ConfParser::Space::GetValue(const wchar_t * name, std::wstring & out, const wchar_t * def)
{
tmp_name = name;
return GetValue(tmp_name, out, def);
}
bool ConfParser::Space::GetValue(const std::wstring & name, std::wstring & out)
{
return GetValue(name, out, L"");
}
bool ConfParser::Space::GetValue(const std::wstring & name, std::wstring & out, const wchar_t * def)
{
TableSingle::iterator i = table_single.find(name);
if( i != table_single.end() )
{
out = i->second;
return true;
}
else
{
Table::iterator t = table.find(name);
if( t == table.end() || t->second.empty() )
{
out = def;
return false;
}
else
{
out = t->second[0];
return true;
}
}
}
void ConfParser::ToText(const wchar_t * name, std::wstring & out)
{
tmp_name = name;
return ToText(tmp_name, out, default_str.c_str());
}
void ConfParser::ToText(const wchar_t * name, std::wstring & out, const wchar_t * def)
{
tmp_name = name;
return ToText(tmp_name, out, def);
}
void ConfParser::ToText(const std::wstring & name, std::wstring & out, const wchar_t * def)
{
space.GetValue(name, out, def);
}
std::wstring & ConfParser::Text(const wchar_t * name)
{
ToText(name, tmp_value_text);
return tmp_value_text;
}
std::wstring & ConfParser::Text(const wchar_t * name, const wchar_t * def)
{
ToText(name, tmp_value_text, def);
return tmp_value_text;
}
std::wstring & ConfParser::Text(const std::wstring & name, const wchar_t * def)
{
ToText(name, tmp_value_text, def);
return tmp_value_text;
}
std::string & ConfParser::AText(const wchar_t * name)
{
ToText(name, tmp_value_text);
Ezc::WideToUTF8(tmp_value_text, tmp_value_text_ascii);
return tmp_value_text_ascii;
}
std::string & ConfParser::AText(const wchar_t * name, const wchar_t * def)
{
ToText(name, tmp_value_text, def);
Ezc::WideToUTF8(tmp_value_text, tmp_value_text_ascii);
return tmp_value_text_ascii;
}
std::string & ConfParser::AText(const std::wstring & name, const wchar_t * def)
{
ToText(name, tmp_value_text, def);
Ezc::WideToUTF8(tmp_value_text, tmp_value_text_ascii);
return tmp_value_text_ascii;
}
int ConfParser::Int(const wchar_t * name)
{
tmp_name = name;
return Int(tmp_name, default_int);
}
int ConfParser::Int(const wchar_t * name, int def)
{
tmp_name = name;
return Int(tmp_name, def);
}
int ConfParser::ToInt(const std::wstring & value)
{
long res = (value[0] == '0')? wcstol(value.c_str() + 1, 0, 8) : wcstol(value.c_str(), 0, 10);
return static_cast<int>(res);
}
int ConfParser::Int(const std::wstring & name, int def)
{
if( space.GetValue(name, tmp_value_text) )
return ToInt(tmp_value_text);
return def;
}
size_t ConfParser::Size(const wchar_t * name)
{
tmp_name = name;
return Size(tmp_name, default_size);
}
size_t ConfParser::Size(const wchar_t * name, size_t def)
{
tmp_name = name;
return Size(tmp_name, def);
}
size_t ConfParser::ToSize(const std::wstring & value)
{
unsigned long res = (value[0] == '0')? wcstoul(value.c_str() + 1, 0, 8) : wcstoul(value.c_str(), 0, 10);
return static_cast<size_t>(res);
}
size_t ConfParser::Size(const std::wstring & name, size_t def)
{
if( space.GetValue(name, tmp_value_text) )
return ToSize(tmp_value_text);
return def;
}
bool ConfParser::Bool(const wchar_t * name)
{
tmp_name = name;
return Bool(tmp_name, default_bool);
}
bool ConfParser::Bool(const wchar_t * name, bool def)
{
tmp_name = name;
return Bool(tmp_name, def);
}
bool ConfParser::ToBool(const std::wstring & value)
{
return ( EqualNoCase(value.c_str(), L"true") ||
EqualNoCase(value.c_str(), L"yes") ||
EqualNoCase(value.c_str(), L"1")
);
}
bool ConfParser::Bool(const std::wstring & name, bool def)
{
if( space.GetValue(name, tmp_value_text) )
return ToBool(tmp_value_text);
return def;
}
void ConfParser::SetDefaultText(const wchar_t * def)
{
default_str = def;
}
void ConfParser::SetDefaultText(const std::wstring & def)
{
default_str = def;
}
void ConfParser::SetDefaultInt(int def)
{
default_int = def;
}
void ConfParser::SetDefaultSize(size_t def)
{
default_size = def;
}
void ConfParser::SetDefaultBool(bool def)
{
default_bool = def;
}
// in lists we don't use default values
void ConfParser::ListText(const wchar_t * name, std::vector<std::wstring> & list)
{
tmp_name = name;
ListText(tmp_name, list);
}
void ConfParser::ListText(const std::wstring & name, std::vector<std::wstring> & list)
{
list.clear();
ConfParser::TableSingle::iterator i = space.table_single.find(name);
if( i != space.table_single.end() )
{
list.push_back(i->second);
}
else
{
ConfParser::Table::iterator z = space.table.find(name);
if( z != space.table.end() )
list = z->second;
}
}
void ConfParser::Print(std::ostream & out)
{
TableSingle::iterator i1;
for(i1 = space.table_single.begin() ; i1 != space.table_single.end() ; ++i1)
{
Ezc::WideToUTF8(i1->first, out);
out << '=';
Ezc::WideToUTF8(i1->second, out);
out << std::endl;
}
Table::iterator i2;
Value::iterator i3;
for(i2 = space.table.begin() ; i2 != space.table.end() ; ++i2)
{
Ezc::WideToUTF8(i2->first, out);
out << '=';
for(i3 = i2->second.begin() ; i3 != i2->second.end() ; ++i3)
{
Ezc::WideToUTF8(*i3, out);
out << ',';
}
out << std::endl;
}
}

View File

@ -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,132 +359,19 @@ 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;
/*
current space set by SetSpace();
*/
Space * root_space;
private:
/*
a space in which we are now
*/
Space * space;
/*
@ -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);
};

View File

@ -46,11 +46,14 @@ bool GroupInfo::ParseGroups(const std::wstring & str, Groups & groups)
{
groups.Clear();
conf_parser.SetSpace(space);
conf_parser.UTF8(true);
if( conf_parser.ParseString(str) == ConfParser::ok )
{
ConfParser::Table::iterator i;
Space::Table::iterator i;
for(i=conf_parser.space.table.begin() ; i!=conf_parser.space.table.end() ; ++i)
for(i=space.table.begin() ; i!=space.table.end() ; ++i)
{
groups.AddGroup();
log << log1 << "stworzylem nowa grupe" << logend;

View File

@ -65,6 +65,7 @@ private:
typedef std::map<long, GroupsWrapItem> GroupsWrap;
GroupsWrap groups_wrap;
Space space;
ConfParser conf_parser;
std::vector<Item*> config_dir_tab;
Item config_file;

View File

@ -61,6 +61,7 @@ void menu_dir_load_menu(long parent_id)
menu_dir_iq.sel_subject = true;
menu_dir_iq.sel_url = true;
menu_dir_iq.sel_sort_index = true;
menu_dir_iq.WhereFileType(WINIX_ITEM_FILETYPE_NONE);
menu_dir_iq.WhereParentId(parent_id);
// !! directories will not be read with WINIX_ITEM_FILETYPE_NONE

View File

@ -70,6 +70,7 @@ bool read = false;
loc_parser.SplitSingle(true);
loc_parser.UTF8(input_as_utf8);
loc_parser.SetSpace(space);
if( loc_parser.Parse(file_name) == ConfParser::ok )
{
@ -85,16 +86,16 @@ return read;
void Locale::AddLocale(size_t lang)
{
ConfParser::TableSingle::iterator i = loc_parser.space.table_single.begin();
Space::TableSingle::iterator i = space.table_single.begin();
for( ; i != loc_parser.space.table_single.end() ; ++i)
for( ; i != space.table_single.end() ; ++i)
loc_tab[lang][i->first] = i->second;
// lists
ConfParser::Table::iterator i2 = loc_parser.space.table.begin();
Space::Table::iterator i2 = space.table.begin();
for( ; i2 != loc_parser.space.table.end() ; ++i2)
for( ; i2 != space.table.end() ; ++i2)
loc_tab_multi[lang][i2->first] = i2->second;
}
@ -132,9 +133,9 @@ bool read = false;
if( loc_parser.Parse(file_name) == ConfParser::ok )
{
read = true;
CreateSubstVector(subst_url, loc_parser.space.table_single[L"url_original"], loc_parser.space.table_single[L"url_changeto"]);
CreateSubstVector(subst_smalllet, loc_parser.space.table_single[L"smallleters"], loc_parser.space.table_single[L"capitalics"]);
CreateSubstVector(subst_capitallet, loc_parser.space.table_single[L"capitalics"], loc_parser.space.table_single[L"smallleters"]);
CreateSubstVector(subst_url, space.table_single[L"url_original"], space.table_single[L"url_changeto"]);
CreateSubstVector(subst_smalllet, space.table_single[L"smallleters"], space.table_single[L"capitalics"]);
CreateSubstVector(subst_capitallet, space.table_single[L"capitalics"], space.table_single[L"smallleters"]);
log << log3 << "Locale: read characters substitution tables from: " << file_name << logend;
}
@ -275,7 +276,7 @@ bool Locale::IsKey(const std::wstring & key, size_t lang) const
return false;
// looking in the 'lang' language
ConfParser::TableSingle::const_iterator i = loc_tab[lang].find(key);
Space::TableSingle::const_iterator i = loc_tab[lang].find(key);
if( i != loc_tab[lang].end() )
return true;
@ -311,7 +312,7 @@ bool Locale::IsKeyLang(const std::wstring & key, size_t lang) const
return false;
// looking in the 'lang' language
ConfParser::TableSingle::const_iterator i = loc_tab[lang].find(key);
Space::TableSingle::const_iterator i = loc_tab[lang].find(key);
return i != loc_tab[lang].end();
}
@ -346,7 +347,7 @@ const std::wstring & Locale::Get(const std::wstring & key, size_t lang) const
return empty;
// looking in the 'lang' language
ConfParser::TableSingle::const_iterator i = loc_tab[lang].find(key);
Space::TableSingle::const_iterator i = loc_tab[lang].find(key);
if( i != loc_tab[lang].end() )
return i->second;
@ -383,7 +384,7 @@ bool Locale::IsKeyLangList(const std::wstring & key, size_t lang) const
return false;
// looking in the 'lang' language
ConfParser::Table::const_iterator i = loc_tab_multi[lang].find(key);
Space::Table::const_iterator i = loc_tab_multi[lang].find(key);
return i != loc_tab_multi[lang].end();
}
@ -401,7 +402,7 @@ const std::vector<std::wstring> & Locale::GetList(const std::wstring & key, size
return empty_list;
// looking in the 'lang' language
ConfParser::Table::const_iterator i = loc_tab_multi[lang].find(key);
Space::Table::const_iterator i = loc_tab_multi[lang].find(key);
if( i != loc_tab_multi[lang].end() )
return i->second;

View File

@ -122,11 +122,11 @@ private:
// messages vector<via language>
// this table has the same size as locale_files (at least one item)
std::vector<ConfParser::TableSingle> loc_tab;
std::vector<Space::TableSingle> loc_tab;
// messages vector<via language>
// this table has the same size as locale_files (at least one item)
std::vector<ConfParser::Table> loc_tab_multi;
std::vector<Space::Table> loc_tab_multi;
// vectors of characters substitution (sort by 'from')
std::vector<SubstItem> subst_url;
@ -134,6 +134,7 @@ private:
std::vector<SubstItem> subst_capitallet; // changing from capital to small
Space space;
ConfParser loc_parser;
std::string locale_filea;
std::string file_name;