2008-12-11 03:46:16 +01:00
|
|
|
/*
|
2010-02-28 01:08:10 +01:00
|
|
|
* This file is a part of Winix
|
2008-12-11 03:46:16 +01:00
|
|
|
* and is not publicly distributed
|
|
|
|
*
|
2011-09-17 00:46:42 +02:00
|
|
|
* Copyright (c) 2008-2011, Tomasz Sowa
|
2008-12-11 03:46:16 +01:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2010-08-12 21:10:12 +02:00
|
|
|
#include <cstdlib>
|
2010-11-21 01:19:17 +01:00
|
|
|
#include <wchar.h>
|
2008-12-11 03:46:16 +01:00
|
|
|
#include "confparser.h"
|
2010-11-23 22:52:25 +01:00
|
|
|
#include "utf8.h"
|
2008-12-11 03:46:16 +01:00
|
|
|
|
2010-07-21 19:24:16 +02:00
|
|
|
|
|
|
|
|
2011-09-26 07:40:26 +02:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-16 23:33:45 +02:00
|
|
|
void Space::PrintLevel(std::wostream & out, int level)
|
2011-09-26 07:40:26 +02:00
|
|
|
{
|
2011-10-16 23:33:45 +02:00
|
|
|
for(int i=0 ; i<level ; ++i)
|
|
|
|
out << ' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Space::Print(std::wostream & out, int level)
|
|
|
|
{
|
|
|
|
PrintLevel(out, level);
|
2011-09-26 07:40:26 +02:00
|
|
|
out << "Space name: " << name << std::endl;
|
|
|
|
|
2011-10-16 23:33:45 +02:00
|
|
|
if( !table_single.empty() )
|
|
|
|
{
|
|
|
|
PrintLevel(out, level);
|
|
|
|
out << "table_single: " << std::endl;
|
|
|
|
TableSingle::iterator i1;
|
2011-09-26 07:40:26 +02:00
|
|
|
|
2011-10-16 23:33:45 +02:00
|
|
|
for(i1 = table_single.begin() ; i1 != table_single.end() ; ++i1)
|
|
|
|
{
|
|
|
|
PrintLevel(out, level);
|
|
|
|
out << i1->first << '=' << i1->second << std::endl;
|
|
|
|
}
|
|
|
|
}
|
2011-09-26 07:40:26 +02:00
|
|
|
|
2011-10-16 23:33:45 +02:00
|
|
|
if( !table.empty() )
|
|
|
|
{
|
|
|
|
PrintLevel(out, level);
|
|
|
|
out << "table: " << std::endl;
|
2011-09-26 07:40:26 +02:00
|
|
|
|
2011-10-16 23:33:45 +02:00
|
|
|
Table::iterator i2;
|
|
|
|
Value::iterator i3;
|
2011-09-26 07:40:26 +02:00
|
|
|
|
2011-10-16 23:33:45 +02:00
|
|
|
for(i2 = table.begin() ; i2 != table.end() ; ++i2)
|
|
|
|
{
|
|
|
|
PrintLevel(out, level);
|
|
|
|
out << i2->first << '=';
|
2011-09-26 07:40:26 +02:00
|
|
|
|
2011-10-16 23:33:45 +02:00
|
|
|
for(i3 = i2->second.begin() ; i3 != i2->second.end() ; ++i3)
|
|
|
|
out << *i3 << ',';
|
2011-09-26 07:40:26 +02:00
|
|
|
|
2011-10-16 23:33:45 +02:00
|
|
|
out << std::endl;
|
|
|
|
}
|
2011-09-26 07:40:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for(size_t i=0 ; i<spaces.size() ; ++i)
|
2011-10-16 23:33:45 +02:00
|
|
|
spaces[i]->Print(out, level+1);
|
2011-09-26 07:40:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* ConfParser
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-12-11 03:46:16 +01:00
|
|
|
ConfParser::ConfParser()
|
2011-09-17 00:46:42 +02:00
|
|
|
{
|
2011-09-26 07:40:26 +02:00
|
|
|
root_space = 0;
|
2011-09-17 00:46:42 +02:00
|
|
|
SetDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-26 07:40:26 +02:00
|
|
|
void ConfParser::SetSpace(Space * pspace)
|
|
|
|
{
|
|
|
|
root_space = pspace;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ConfParser::SetSpace(Space & pspace)
|
|
|
|
{
|
|
|
|
root_space = &pspace;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-17 00:46:42 +02:00
|
|
|
void ConfParser::SetDefault()
|
2008-12-11 03:46:16 +01:00
|
|
|
{
|
2010-07-21 19:24:16 +02:00
|
|
|
// you can change this separators to what you want
|
|
|
|
// you shoud not use only white characters here (as expected by IsWhite() method)
|
|
|
|
// and new line characters ('\n')
|
2011-09-17 00:46:42 +02:00
|
|
|
separator = '=';
|
|
|
|
commentary = '#';
|
|
|
|
list_start = '(';
|
|
|
|
list_end = ')';
|
|
|
|
list_delimiter = ',';
|
|
|
|
split_single = false;
|
|
|
|
skip_empty = false;
|
2010-08-16 16:15:37 +02:00
|
|
|
use_escape_char = true;
|
2011-09-17 00:46:42 +02:00
|
|
|
input_as_utf8 = false;
|
2010-07-21 19:24:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ConfParser::SplitSingle(bool split)
|
|
|
|
{
|
|
|
|
split_single = split;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ConfParser::SkipEmpty(bool skip)
|
|
|
|
{
|
|
|
|
skip_empty = skip;
|
2008-12-11 03:46:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-16 16:15:37 +02:00
|
|
|
void ConfParser::UseEscapeChar(bool escape)
|
|
|
|
{
|
|
|
|
use_escape_char = escape;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-17 00:46:42 +02:00
|
|
|
void ConfParser::UTF8(bool utf)
|
|
|
|
{
|
|
|
|
input_as_utf8 = utf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-21 01:19:17 +01:00
|
|
|
|
|
|
|
|
2008-12-11 03:46:16 +01:00
|
|
|
ConfParser::Status ConfParser::Parse(const char * file_name)
|
|
|
|
{
|
2011-08-26 01:53:49 +02:00
|
|
|
reading_from_file = true;
|
2010-01-28 16:39:01 +01:00
|
|
|
|
|
|
|
file.clear();
|
2008-12-11 03:46:16 +01:00
|
|
|
file.open( file_name );
|
|
|
|
|
2010-07-21 19:24:16 +02:00
|
|
|
if( file )
|
|
|
|
{
|
2011-09-17 00:46:42 +02:00
|
|
|
Parse();
|
2010-07-21 19:24:16 +02:00
|
|
|
file.close();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status = cant_open_file;
|
|
|
|
}
|
2008-12-11 03:46:16 +01:00
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-07-21 19:24:16 +02:00
|
|
|
ConfParser::Status ConfParser::Parse(const std::string & file_name)
|
|
|
|
{
|
|
|
|
return Parse(file_name.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-11-21 01:19:17 +01:00
|
|
|
|
|
|
|
ConfParser::Status ConfParser::Parse(const wchar_t * file_name)
|
|
|
|
{
|
|
|
|
Ezc::WideToUTF8(file_name, afile_name);
|
|
|
|
return Parse(afile_name.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ConfParser::Status ConfParser::Parse(const std::wstring & file_name)
|
|
|
|
{
|
|
|
|
return Parse(file_name.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-08-26 01:53:49 +02:00
|
|
|
ConfParser::Status ConfParser::ParseString(const char * str)
|
|
|
|
{
|
|
|
|
reading_from_file = false;
|
|
|
|
reading_from_wchar_string = false;
|
2011-09-17 00:46:42 +02:00
|
|
|
pchar_ascii = str;
|
2011-09-26 07:40:26 +02:00
|
|
|
pchar_unicode = 0;
|
2011-08-26 01:53:49 +02:00
|
|
|
|
2011-09-17 00:46:42 +02:00
|
|
|
Parse();
|
2011-08-26 01:53:49 +02:00
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ConfParser::Status ConfParser::ParseString(const std::string & str)
|
|
|
|
{
|
|
|
|
return ParseString(str.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ConfParser::Status ConfParser::ParseString(const wchar_t * str)
|
|
|
|
{
|
|
|
|
reading_from_file = false;
|
|
|
|
reading_from_wchar_string = true;
|
2011-09-17 00:46:42 +02:00
|
|
|
pchar_unicode = str;
|
2011-09-26 07:40:26 +02:00
|
|
|
pchar_ascii = 0;
|
2011-08-26 01:53:49 +02:00
|
|
|
|
2011-09-17 00:46:42 +02:00
|
|
|
Parse();
|
2011-08-26 01:53:49 +02:00
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ConfParser::Status ConfParser::ParseString(const std::wstring & str)
|
|
|
|
{
|
|
|
|
return ParseString(str.c_str());
|
|
|
|
}
|
|
|
|
|
2010-11-21 01:19:17 +01:00
|
|
|
|
2011-09-17 00:46:42 +02:00
|
|
|
void ConfParser::Parse()
|
2008-12-11 03:46:16 +01:00
|
|
|
{
|
2011-09-26 07:40:26 +02:00
|
|
|
if( !root_space )
|
|
|
|
{
|
|
|
|
status = no_space;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
line = 1;
|
2011-09-17 00:46:42 +02:00
|
|
|
status = ok;
|
2011-09-26 07:40:26 +02:00
|
|
|
space = root_space;
|
2010-07-21 19:24:16 +02:00
|
|
|
ReadChar();
|
|
|
|
SkipWhiteLines();
|
|
|
|
|
2011-09-26 07:40:26 +02:00
|
|
|
ParseLoop();
|
|
|
|
|
|
|
|
if( status == ok && space != root_space )
|
|
|
|
{
|
|
|
|
// last closing a space characters ')' are missing
|
|
|
|
status = syntax_error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ConfParser::ParseLoop()
|
|
|
|
{
|
2011-09-17 00:46:42 +02:00
|
|
|
while( status == ok && lastc != -1 )
|
2008-12-11 03:46:16 +01:00
|
|
|
{
|
2011-09-17 00:46:42 +02:00
|
|
|
if( lastc == list_end )
|
|
|
|
{
|
2011-09-26 07:40:26 +02:00
|
|
|
SpaceEnds();
|
2011-09-17 00:46:42 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ReadVariable();
|
2010-07-21 19:24:16 +02:00
|
|
|
|
2011-09-17 00:46:42 +02:00
|
|
|
if( lastc == list_start )
|
|
|
|
{
|
2011-09-26 07:40:26 +02:00
|
|
|
SpaceStarts();
|
2011-09-17 00:46:42 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if( lastc == separator && !variable.empty() )
|
|
|
|
{
|
|
|
|
ReadAddValue();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status = syntax_error;
|
|
|
|
}
|
|
|
|
}
|
2010-07-21 19:24:16 +02:00
|
|
|
|
2011-09-17 00:46:42 +02:00
|
|
|
if( status == ok )
|
|
|
|
SkipWhiteLines();
|
|
|
|
}
|
|
|
|
}
|
2010-07-21 19:24:16 +02:00
|
|
|
|
2011-09-17 00:46:42 +02:00
|
|
|
|
2011-09-26 07:40:26 +02:00
|
|
|
void ConfParser::SpaceEnds()
|
2011-09-17 00:46:42 +02:00
|
|
|
{
|
2011-09-26 07:40:26 +02:00
|
|
|
if( space == root_space )
|
2011-09-17 00:46:42 +02:00
|
|
|
{
|
|
|
|
// there cannot be a loose list end character in the global space
|
|
|
|
status = syntax_error;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-09-26 07:40:26 +02:00
|
|
|
space = space->parent;
|
|
|
|
ReadChar();
|
2011-09-17 00:46:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-26 07:40:26 +02:00
|
|
|
void ConfParser::SpaceStarts()
|
|
|
|
{
|
|
|
|
Space * new_space = new Space();
|
|
|
|
space->spaces.push_back(new_space);
|
|
|
|
new_space->parent = space;
|
|
|
|
new_space->name = variable;
|
|
|
|
space = new_space;
|
|
|
|
|
|
|
|
ReadChar();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-17 00:46:42 +02:00
|
|
|
void ConfParser::ReadAddValue()
|
|
|
|
{
|
|
|
|
ReadChar(); // skipping separator '='
|
|
|
|
|
|
|
|
if( ReadValue() )
|
|
|
|
{
|
|
|
|
AddOption();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status = syntax_error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-21 19:24:16 +02:00
|
|
|
|
2008-12-11 03:46:16 +01:00
|
|
|
bool ConfParser::IsVariableChar(int c)
|
|
|
|
{
|
|
|
|
if( (c>='a' && c<='z') ||
|
|
|
|
(c>='A' && c<='Z') ||
|
|
|
|
(c>='0' && c<='9') ||
|
|
|
|
c=='.' || c==',' || c=='_' )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-17 00:46:42 +02:00
|
|
|
bool ConfParser::IsWhite(int c)
|
|
|
|
{
|
|
|
|
// dont use '\n' here
|
|
|
|
// 13 (\r) is at the end of a line in a dos file \r\n
|
|
|
|
// 160 is an unbreakable space
|
|
|
|
if( c==' ' || c=='\t' || c==13 || c==160 )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ConfParser::SkipWhite()
|
|
|
|
{
|
|
|
|
while( IsWhite(lastc) || lastc == commentary )
|
|
|
|
{
|
|
|
|
if( lastc == commentary )
|
|
|
|
SkipLine();
|
|
|
|
else
|
|
|
|
ReadChar();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ConfParser::SkipWhiteLines()
|
|
|
|
{
|
|
|
|
while( IsWhite(lastc) || lastc == commentary || lastc=='\n' )
|
|
|
|
{
|
|
|
|
if( lastc == commentary )
|
|
|
|
SkipLine();
|
|
|
|
else
|
|
|
|
ReadChar();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ConfParser::SkipLine()
|
|
|
|
{
|
|
|
|
while( lastc != -1 && lastc != '\n' )
|
|
|
|
ReadChar();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ConfParser::Trim(std::wstring & s)
|
|
|
|
{
|
|
|
|
std::wstring::size_type i;
|
|
|
|
|
|
|
|
if( s.empty() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
// looking for white characters at the end
|
|
|
|
for(i=s.size()-1 ; i>0 && IsWhite(s[i]) ; --i);
|
|
|
|
|
|
|
|
if( i==0 && IsWhite(s[i]) )
|
|
|
|
{
|
|
|
|
// the whole string has white characters
|
|
|
|
s.clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// deleting white characters at the end
|
|
|
|
if( i != s.size() - 1 )
|
|
|
|
s.erase(i+1, std::wstring::npos);
|
|
|
|
|
|
|
|
// looking for white characters at the beginning
|
|
|
|
for(i=0 ; i<s.size() && IsWhite(s[i]) ; ++i);
|
|
|
|
|
|
|
|
// deleting white characters at the beginning
|
|
|
|
if( i != 0 )
|
|
|
|
s.erase(0, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-07-21 19:24:16 +02:00
|
|
|
|
|
|
|
void ConfParser::AddOption()
|
2008-12-11 03:46:16 +01:00
|
|
|
{
|
2010-07-21 19:24:16 +02:00
|
|
|
if( value.empty() && skip_empty )
|
|
|
|
{
|
|
|
|
DeleteFromTable(variable);
|
|
|
|
DeleteFromTableSingle(variable);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( split_single && value.size() == 1 )
|
|
|
|
{
|
2011-09-26 07:40:26 +02:00
|
|
|
space->table_single[variable] = value[0];
|
2010-07-21 19:24:16 +02:00
|
|
|
DeleteFromTable(variable);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-09-26 07:40:26 +02:00
|
|
|
space->table[variable] = value;
|
2010-07-21 19:24:16 +02:00
|
|
|
DeleteFromTableSingle(variable);
|
|
|
|
}
|
2008-12-11 03:46:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-21 19:24:16 +02:00
|
|
|
|
2010-11-21 01:19:17 +01:00
|
|
|
void ConfParser::DeleteFromTable(const std::wstring & var)
|
2010-07-21 19:24:16 +02:00
|
|
|
{
|
2011-09-26 07:40:26 +02:00
|
|
|
Space::Table::iterator i = space->table.find(var);
|
2011-09-17 00:46:42 +02:00
|
|
|
|
2011-09-26 07:40:26 +02:00
|
|
|
if( i != space->table.end() )
|
|
|
|
space->table.erase(i);
|
2010-07-21 19:24:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-11-21 01:19:17 +01:00
|
|
|
void ConfParser::DeleteFromTableSingle(const std::wstring & var)
|
2010-07-21 19:24:16 +02:00
|
|
|
{
|
2011-09-26 07:40:26 +02:00
|
|
|
Space::TableSingle::iterator i = space->table_single.find(var);
|
2010-07-21 19:24:16 +02:00
|
|
|
|
2011-09-26 07:40:26 +02:00
|
|
|
if( i != space->table_single.end() )
|
|
|
|
space->table_single.erase(i);
|
2010-07-21 19:24:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-09-17 00:46:42 +02:00
|
|
|
void ConfParser::ReadVariable()
|
2008-12-11 03:46:16 +01:00
|
|
|
{
|
|
|
|
variable.clear();
|
|
|
|
SkipWhite();
|
|
|
|
|
|
|
|
while( IsVariableChar(lastc) )
|
|
|
|
{
|
|
|
|
variable += lastc;
|
|
|
|
ReadChar();
|
|
|
|
}
|
|
|
|
|
|
|
|
SkipWhite();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool ConfParser::ReadValue()
|
|
|
|
{
|
|
|
|
value.clear();
|
|
|
|
SkipWhite();
|
|
|
|
|
2010-07-21 19:24:16 +02:00
|
|
|
if( lastc == list_start )
|
|
|
|
return ReadValueList();
|
2008-12-11 03:46:16 +01:00
|
|
|
else
|
2010-07-21 19:24:16 +02:00
|
|
|
return ReadValueNoList();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool ConfParser::ReadValueList()
|
|
|
|
{
|
|
|
|
ReadChar(); // skipping first list character '('
|
|
|
|
SkipWhiteLines(); // lists can be split into several lines
|
|
|
|
|
|
|
|
while( lastc != -1 && lastc != list_end )
|
|
|
|
{
|
|
|
|
if( !ReadValueNoList(true) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if( lastc == list_delimiter )
|
|
|
|
ReadChar();
|
|
|
|
|
|
|
|
SkipWhiteLines();
|
|
|
|
}
|
|
|
|
|
|
|
|
if( lastc != list_end )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ReadChar(); // skipping last list character ')'
|
|
|
|
SkipWhite();
|
|
|
|
|
|
|
|
return true;
|
2008-12-11 03:46:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-07-21 19:24:16 +02:00
|
|
|
bool ConfParser::ReadValueNoList(bool use_list_delimiter)
|
|
|
|
{
|
|
|
|
bool res;
|
|
|
|
|
|
|
|
value_item.clear();
|
|
|
|
|
|
|
|
if( lastc == '"' )
|
|
|
|
{
|
|
|
|
res = ReadValueQuoted(); // quoted value
|
|
|
|
|
|
|
|
if( res )
|
|
|
|
value.push_back(value_item);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
res = ReadValueSimple(use_list_delimiter);
|
|
|
|
|
|
|
|
if( res && !value_item.empty() )
|
|
|
|
value.push_back(value_item);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-11 03:46:16 +01:00
|
|
|
|
|
|
|
bool ConfParser::ReadValueQuoted()
|
|
|
|
{
|
2010-07-21 19:24:16 +02:00
|
|
|
ReadChar(); // skipping the first quote
|
2008-12-11 03:46:16 +01:00
|
|
|
|
2010-07-21 19:24:16 +02:00
|
|
|
while( lastc != '"' && lastc != -1 )
|
2008-12-11 03:46:16 +01:00
|
|
|
{
|
2010-08-16 16:15:37 +02:00
|
|
|
if( use_escape_char && lastc == '\\' )
|
2008-12-11 03:46:16 +01:00
|
|
|
ReadChar();
|
|
|
|
|
2010-07-21 19:24:16 +02:00
|
|
|
value_item += lastc;
|
2008-12-11 03:46:16 +01:00
|
|
|
ReadChar();
|
|
|
|
}
|
|
|
|
|
|
|
|
if( lastc != '"' )
|
|
|
|
return false;
|
|
|
|
|
2010-07-21 19:24:16 +02:00
|
|
|
ReadChar(); // skipping the last quote
|
2008-12-11 03:46:16 +01:00
|
|
|
SkipWhite();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-07-21 19:24:16 +02:00
|
|
|
bool ConfParser::ReadValueSimple(bool use_list_delimiter)
|
|
|
|
{
|
|
|
|
int list_delimiter1 = -1;
|
|
|
|
int list_delimiter2 = -1;
|
2008-12-11 03:46:16 +01:00
|
|
|
|
2010-07-21 19:24:16 +02:00
|
|
|
if( use_list_delimiter )
|
|
|
|
list_delimiter1 = list_delimiter;
|
2011-09-17 00:46:42 +02:00
|
|
|
|
2011-09-26 07:40:26 +02:00
|
|
|
if( use_list_delimiter || space != root_space )
|
2010-07-21 19:24:16 +02:00
|
|
|
list_delimiter2 = list_end;
|
2008-12-11 03:46:16 +01:00
|
|
|
|
2010-07-21 19:24:16 +02:00
|
|
|
while( lastc!=-1 && lastc!='\n' && lastc!=commentary &&
|
|
|
|
lastc!=list_delimiter1 && lastc!=list_delimiter2 )
|
2008-12-11 03:46:16 +01:00
|
|
|
{
|
2010-07-21 19:24:16 +02:00
|
|
|
value_item += lastc;
|
2008-12-11 03:46:16 +01:00
|
|
|
ReadChar();
|
|
|
|
}
|
|
|
|
|
2010-07-21 19:24:16 +02:00
|
|
|
Trim(value_item);
|
2008-12-11 03:46:16 +01:00
|
|
|
SkipWhite();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-21 01:19:17 +01:00
|
|
|
int ConfParser::ReadUTF8Char()
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
bool correct;
|
|
|
|
|
|
|
|
lastc = -1;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
Ezc::UTF8ToInt(file, c, correct);
|
|
|
|
|
|
|
|
if( !file )
|
|
|
|
return lastc;
|
|
|
|
}
|
|
|
|
while( !correct );
|
|
|
|
|
|
|
|
lastc = c;
|
|
|
|
|
|
|
|
if( lastc == '\n' )
|
|
|
|
++line;
|
|
|
|
|
|
|
|
return lastc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int ConfParser::ReadASCIIChar()
|
2008-12-11 03:46:16 +01:00
|
|
|
{
|
|
|
|
lastc = file.get();
|
|
|
|
|
|
|
|
if( lastc == '\n' )
|
|
|
|
++line;
|
|
|
|
|
|
|
|
return lastc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-26 01:53:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
int ConfParser::ReadCharFromWcharString()
|
|
|
|
{
|
|
|
|
if( *pchar_unicode == 0 )
|
|
|
|
lastc = -1;
|
|
|
|
else
|
|
|
|
lastc = *(pchar_unicode++);
|
|
|
|
|
|
|
|
if( lastc == '\n' )
|
|
|
|
++line;
|
|
|
|
|
|
|
|
return lastc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int ConfParser::ReadCharFromUTF8String()
|
2010-11-21 01:19:17 +01:00
|
|
|
{
|
2011-08-26 01:53:49 +02:00
|
|
|
int c;
|
|
|
|
bool correct;
|
|
|
|
|
|
|
|
lastc = -1;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
size_t len = Ezc::UTF8ToInt(pchar_ascii, c, correct);
|
|
|
|
pchar_ascii += len;
|
|
|
|
|
|
|
|
if( *pchar_ascii == 0 )
|
|
|
|
return lastc;
|
|
|
|
}
|
|
|
|
while( !correct );
|
|
|
|
|
|
|
|
lastc = c;
|
|
|
|
|
|
|
|
if( lastc == '\n' )
|
|
|
|
++line;
|
|
|
|
|
|
|
|
return lastc;
|
|
|
|
|
|
|
|
}
|
2010-11-21 01:19:17 +01:00
|
|
|
|
2011-08-26 01:53:49 +02:00
|
|
|
|
|
|
|
int ConfParser::ReadCharFromAsciiString()
|
|
|
|
{
|
|
|
|
if( *pchar_ascii == 0 )
|
|
|
|
lastc = -1;
|
|
|
|
else
|
|
|
|
lastc = *(pchar_ascii++);
|
|
|
|
|
|
|
|
if( lastc == '\n' )
|
|
|
|
++line;
|
|
|
|
|
|
|
|
return lastc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int ConfParser::ReadChar()
|
|
|
|
{
|
|
|
|
if( reading_from_file )
|
|
|
|
{
|
|
|
|
if( input_as_utf8 )
|
|
|
|
return ReadUTF8Char();
|
|
|
|
else
|
|
|
|
return ReadASCIIChar();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( reading_from_wchar_string )
|
|
|
|
{
|
|
|
|
return ReadCharFromWcharString();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( input_as_utf8 )
|
|
|
|
return ReadCharFromUTF8String();
|
|
|
|
else
|
|
|
|
return ReadCharFromAsciiString();
|
|
|
|
}
|
|
|
|
}
|
2010-11-21 01:19:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-11 03:46:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2010-11-21 01:19:17 +01:00
|
|
|
|
|
|
|
|