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
|
|
|
|
*
|
2010-02-28 22:33:06 +01:00
|
|
|
* Copyright (c) 2008-2010, 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-08-10 18:12:50 +02:00
|
|
|
#include "misc.h"
|
2010-11-21 01:19:17 +01:00
|
|
|
#include "ezc.h"
|
2008-12-11 03:46:16 +01:00
|
|
|
|
2010-07-21 19:24:16 +02:00
|
|
|
|
|
|
|
|
2008-12-11 03:46:16 +01:00
|
|
|
ConfParser::ConfParser()
|
|
|
|
{
|
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')
|
|
|
|
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;
|
2010-11-21 01:19:17 +01:00
|
|
|
input_as_utf8 = false;
|
2010-08-10 18:12:50 +02:00
|
|
|
|
2010-11-21 01:19:17 +01:00
|
|
|
default_str = L"";
|
2010-08-10 18:12:50 +02:00
|
|
|
default_int = 0;
|
2010-08-12 21:10:12 +02:00
|
|
|
default_size = 0;
|
2010-08-10 18:12:50 +02:00
|
|
|
default_bool = 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-21 01:19:17 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2008-12-11 03:46:16 +01:00
|
|
|
ConfParser::Status ConfParser::Parse(const char * file_name)
|
|
|
|
{
|
2010-01-28 16:39:01 +01:00
|
|
|
line = 1;
|
|
|
|
table.clear();
|
2010-07-21 19:24:16 +02:00
|
|
|
table_single.clear();
|
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 )
|
|
|
|
{
|
|
|
|
status = ParseFile();
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-12-11 03:46:16 +01:00
|
|
|
ConfParser::Status ConfParser::ParseFile()
|
|
|
|
{
|
2010-07-21 19:24:16 +02:00
|
|
|
ReadChar();
|
|
|
|
SkipWhiteLines();
|
|
|
|
|
2008-12-11 03:46:16 +01:00
|
|
|
while( lastc != -1 )
|
|
|
|
{
|
2010-07-21 19:24:16 +02:00
|
|
|
if( !ReadVariable() )
|
|
|
|
return syntax_error;
|
|
|
|
|
|
|
|
if( lastc != separator )
|
|
|
|
return syntax_error;
|
2008-12-11 03:46:16 +01:00
|
|
|
|
2010-07-21 19:24:16 +02:00
|
|
|
if( !ReadValue() )
|
2008-12-11 03:46:16 +01:00
|
|
|
return syntax_error;
|
2010-07-21 19:24:16 +02:00
|
|
|
|
|
|
|
AddOption();
|
|
|
|
SkipWhite();
|
|
|
|
|
|
|
|
if( lastc != -1 && lastc != '\n' )
|
|
|
|
return syntax_error; // some characters have left at the end of an option
|
2008-12-11 03:46:16 +01:00
|
|
|
|
2010-07-21 19:24:16 +02:00
|
|
|
SkipWhiteLines();
|
2008-12-11 03:46:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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 )
|
|
|
|
{
|
|
|
|
table_single[variable] = value[0];
|
|
|
|
DeleteFromTable(variable);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
table[variable] = value;
|
|
|
|
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
|
|
|
{
|
|
|
|
Table::iterator i = table.find(var);
|
|
|
|
|
|
|
|
if( i != table.end() )
|
|
|
|
table.erase(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-11-21 01:19:17 +01:00
|
|
|
void ConfParser::DeleteFromTableSingle(const std::wstring & var)
|
2010-07-21 19:24:16 +02:00
|
|
|
{
|
|
|
|
TableSingle::iterator i = table_single.find(var);
|
|
|
|
|
|
|
|
if( i != table_single.end() )
|
|
|
|
table_single.erase(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-12-11 03:46:16 +01:00
|
|
|
bool ConfParser::ReadVariable()
|
|
|
|
{
|
|
|
|
variable.clear();
|
|
|
|
SkipWhite();
|
|
|
|
|
|
|
|
while( IsVariableChar(lastc) )
|
|
|
|
{
|
|
|
|
variable += lastc;
|
|
|
|
ReadChar();
|
|
|
|
}
|
|
|
|
|
|
|
|
SkipWhite();
|
|
|
|
|
|
|
|
return !variable.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool ConfParser::ReadValue()
|
|
|
|
{
|
|
|
|
value.clear();
|
2010-07-21 19:24:16 +02:00
|
|
|
ReadChar(); // skipping separator '='
|
2008-12-11 03:46:16 +01:00
|
|
|
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;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-21 01:19:17 +01:00
|
|
|
int ConfParser::ReadChar()
|
|
|
|
{
|
|
|
|
if( input_as_utf8 )
|
|
|
|
return ReadUTF8Char();
|
|
|
|
|
|
|
|
return ReadASCIIChar();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-11 03:46:16 +01:00
|
|
|
bool ConfParser::IsWhite(int c)
|
|
|
|
{
|
2010-07-21 19:24:16 +02:00
|
|
|
// dont use '\n' here
|
|
|
|
// 13 (\r) is at the end of a line in a dos file \r\n
|
|
|
|
// 160 is an unbreakable space
|
2010-06-30 20:42:50 +02:00
|
|
|
if( c==' ' || c=='\t' || c==13 || c==160 )
|
2008-12-11 03:46:16 +01:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ConfParser::SkipWhite()
|
|
|
|
{
|
2010-07-21 19:24:16 +02:00
|
|
|
while( IsWhite(lastc) || lastc == commentary )
|
|
|
|
{
|
|
|
|
if( lastc == commentary )
|
|
|
|
SkipLine();
|
|
|
|
else
|
|
|
|
ReadChar();
|
|
|
|
}
|
2008-12-11 03:46:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-21 19:24:16 +02:00
|
|
|
void ConfParser::SkipWhiteLines()
|
|
|
|
{
|
|
|
|
while( IsWhite(lastc) || lastc == commentary || lastc=='\n' )
|
|
|
|
{
|
|
|
|
if( lastc == commentary )
|
|
|
|
SkipLine();
|
|
|
|
else
|
|
|
|
ReadChar();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-11 03:46:16 +01:00
|
|
|
|
|
|
|
void ConfParser::SkipLine()
|
|
|
|
{
|
|
|
|
while( lastc != -1 && lastc != '\n' )
|
|
|
|
ReadChar();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-11-21 01:19:17 +01:00
|
|
|
void ConfParser::Trim(std::wstring & s)
|
2010-01-28 16:39:01 +01:00
|
|
|
{
|
2010-11-21 01:19:17 +01:00
|
|
|
std::wstring::size_type i;
|
2010-01-28 16:39:01 +01:00
|
|
|
|
|
|
|
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 )
|
2010-11-21 01:19:17 +01:00
|
|
|
s.erase(i+1, std::wstring::npos);
|
2010-01-28 16:39:01 +01:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
2008-12-11 03:46:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2010-08-10 18:12:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2010-11-21 01:19:17 +01:00
|
|
|
std::wstring ConfParser::Text(const wchar_t * name)
|
2010-08-10 18:12:50 +02:00
|
|
|
{
|
2010-11-21 01:19:17 +01:00
|
|
|
return Text(std::wstring(name), default_str);
|
2010-08-10 18:12:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-11-21 01:19:17 +01:00
|
|
|
std::wstring ConfParser::Text(const wchar_t * name, const wchar_t * def)
|
2010-08-10 18:12:50 +02:00
|
|
|
{
|
2010-11-21 01:19:17 +01:00
|
|
|
return Text(std::wstring(name), std::wstring(def));
|
2010-08-10 18:12:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-21 01:19:17 +01:00
|
|
|
std::wstring ConfParser::Text(const std::wstring & name, const std::wstring & def)
|
2010-08-10 18:12:50 +02:00
|
|
|
{
|
|
|
|
TableSingle::iterator i = table_single.find(name);
|
|
|
|
|
|
|
|
if( i == table_single.end() )
|
|
|
|
{
|
|
|
|
Table::iterator t = table.find(name);
|
|
|
|
|
|
|
|
if( t == table.end() || t->second.empty() )
|
|
|
|
return def;
|
|
|
|
|
|
|
|
return t->second[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
return i->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-11-21 01:19:17 +01:00
|
|
|
std::string ConfParser::AText(const wchar_t * name)
|
2010-08-10 18:12:50 +02:00
|
|
|
{
|
2010-11-21 01:19:17 +01:00
|
|
|
std::wstring res = Text(name);
|
|
|
|
std::string ares;
|
|
|
|
|
|
|
|
Ezc::WideToUTF8(res, ares);
|
|
|
|
|
|
|
|
return ares;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::string ConfParser::AText(const wchar_t * name, const wchar_t * def)
|
|
|
|
{
|
|
|
|
std::wstring res = Text(name, def);
|
|
|
|
std::string ares;
|
|
|
|
|
|
|
|
Ezc::WideToUTF8(res, ares);
|
|
|
|
|
|
|
|
return ares;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string ConfParser::AText(const std::wstring & name, const std::wstring & def)
|
|
|
|
{
|
|
|
|
std::wstring res = Text(name, def);
|
|
|
|
std::string ares;
|
|
|
|
|
|
|
|
Ezc::WideToUTF8(res, ares);
|
|
|
|
|
|
|
|
return ares;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int ConfParser::Int(const wchar_t * name)
|
|
|
|
{
|
|
|
|
return Int(std::wstring(name), default_int);
|
2010-08-10 18:12:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-21 01:19:17 +01:00
|
|
|
int ConfParser::Int(const wchar_t * name, int def)
|
2010-08-10 18:12:50 +02:00
|
|
|
{
|
2010-11-21 01:19:17 +01:00
|
|
|
return Int(std::wstring(name), def);
|
2010-08-10 18:12:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-21 01:19:17 +01:00
|
|
|
int ConfParser::ToInt(const std::wstring & value)
|
2010-08-10 18:12:50 +02:00
|
|
|
{
|
2010-11-21 01:19:17 +01:00
|
|
|
long res = (value[0] == '0')? wcstol(value.c_str() + 1, 0, 8) : wcstol(value.c_str(), 0, 10);
|
2010-08-10 18:12:50 +02:00
|
|
|
|
2010-11-21 01:19:17 +01:00
|
|
|
return static_cast<int>(res);
|
2010-08-10 18:12:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-21 01:19:17 +01:00
|
|
|
int ConfParser::Int(const std::wstring & name, int def)
|
2010-08-10 18:12:50 +02:00
|
|
|
{
|
|
|
|
TableSingle::iterator i = table_single.find(name);
|
|
|
|
|
|
|
|
if( i == table_single.end() )
|
|
|
|
{
|
|
|
|
Table::iterator t = table.find(name);
|
|
|
|
|
|
|
|
if( t == table.end() || t->second.empty() )
|
|
|
|
return def;
|
|
|
|
|
|
|
|
return ToInt(t->second[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ToInt(i->second);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-12 21:10:12 +02:00
|
|
|
|
2010-11-21 01:19:17 +01:00
|
|
|
size_t ConfParser::Size(const wchar_t * name)
|
2010-08-12 21:10:12 +02:00
|
|
|
{
|
2010-11-21 01:19:17 +01:00
|
|
|
return Size(std::wstring(name), default_size);
|
2010-08-12 21:10:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-21 01:19:17 +01:00
|
|
|
size_t ConfParser::Size(const wchar_t * name, size_t def)
|
2010-08-12 21:10:12 +02:00
|
|
|
{
|
2010-11-21 01:19:17 +01:00
|
|
|
return Size(std::wstring(name), def);
|
2010-08-12 21:10:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-11-21 01:19:17 +01:00
|
|
|
size_t ConfParser::ToSize(const std::wstring & value)
|
2010-08-12 21:10:12 +02:00
|
|
|
{
|
2010-11-21 01:19:17 +01:00
|
|
|
unsigned long res = (value[0] == '0')? wcstoul(value.c_str() + 1, 0, 8) : wcstoul(value.c_str(), 0, 10);
|
2010-08-12 21:10:12 +02:00
|
|
|
|
2010-11-21 01:19:17 +01:00
|
|
|
return static_cast<size_t>(res);
|
2010-08-12 21:10:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-21 01:19:17 +01:00
|
|
|
size_t ConfParser::Size(const std::wstring & name, size_t def)
|
2010-08-12 21:10:12 +02:00
|
|
|
{
|
|
|
|
TableSingle::iterator i = table_single.find(name);
|
|
|
|
|
|
|
|
if( i == table_single.end() )
|
|
|
|
{
|
|
|
|
Table::iterator t = table.find(name);
|
|
|
|
|
|
|
|
if( t == table.end() || t->second.empty() )
|
|
|
|
return def;
|
|
|
|
|
|
|
|
return ToSize(t->second[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ToSize(i->second);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-11-21 01:19:17 +01:00
|
|
|
bool ConfParser::Bool(const wchar_t * name)
|
2010-08-10 18:12:50 +02:00
|
|
|
{
|
2010-11-21 01:19:17 +01:00
|
|
|
return Bool(std::wstring(name), default_bool);
|
2010-08-10 18:12:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-21 01:19:17 +01:00
|
|
|
bool ConfParser::Bool(const wchar_t * name, bool def)
|
2010-08-10 18:12:50 +02:00
|
|
|
{
|
2010-11-21 01:19:17 +01:00
|
|
|
return Bool(std::wstring(name), def);
|
2010-08-10 18:12:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-21 01:19:17 +01:00
|
|
|
bool ConfParser::ToBool(const std::wstring & value)
|
2010-08-10 18:12:50 +02:00
|
|
|
{
|
2010-11-21 01:19:17 +01:00
|
|
|
return ( EqualNoCase(value.c_str(), L"true") ||
|
|
|
|
EqualNoCase(value.c_str(), L"yes") ||
|
|
|
|
EqualNoCase(value.c_str(), L"1")
|
2010-08-10 18:12:50 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-21 01:19:17 +01:00
|
|
|
bool ConfParser::Bool(const std::wstring & name, bool def)
|
2010-08-10 18:12:50 +02:00
|
|
|
{
|
|
|
|
TableSingle::iterator i = table_single.find(name);
|
|
|
|
|
|
|
|
if( i == table_single.end() )
|
|
|
|
{
|
|
|
|
Table::iterator t = table.find(name);
|
|
|
|
|
|
|
|
if( t == table.end() || t->second.empty() )
|
|
|
|
return def;
|
|
|
|
|
|
|
|
return ToBool(t->second[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ToBool(i->second);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-21 01:19:17 +01:00
|
|
|
void ConfParser::SetDefaultText(const std::wstring & def)
|
2010-08-10 18:12:50 +02:00
|
|
|
{
|
|
|
|
default_str = def;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfParser::SetDefaultInt(int def)
|
|
|
|
{
|
|
|
|
default_int = def;
|
|
|
|
}
|
|
|
|
|
2010-08-12 21:10:12 +02:00
|
|
|
void ConfParser::SetDefaultSize(size_t def)
|
|
|
|
{
|
|
|
|
default_size = def;
|
|
|
|
}
|
|
|
|
|
2010-08-10 18:12:50 +02:00
|
|
|
void ConfParser::SetDefaultBool(bool def)
|
|
|
|
{
|
|
|
|
default_bool = def;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// in lists we don't use default values
|
2010-11-21 01:19:17 +01:00
|
|
|
void ConfParser::ListText(const wchar_t * name, std::vector<std::wstring> & list)
|
2010-08-10 18:12:50 +02:00
|
|
|
{
|
2010-11-21 01:19:17 +01:00
|
|
|
ListText(std::wstring(name), list);
|
2010-08-10 18:12:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-21 01:19:17 +01:00
|
|
|
void ConfParser::ListText(const std::wstring & name, std::vector<std::wstring> & list)
|
2010-08-10 18:12:50 +02:00
|
|
|
{
|
|
|
|
list.clear();
|
|
|
|
|
|
|
|
ConfParser::TableSingle::iterator i = table_single.find(name);
|
|
|
|
|
|
|
|
if( i != table_single.end() )
|
|
|
|
{
|
|
|
|
list.push_back(i->second);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ConfParser::Table::iterator z = table.find(name);
|
|
|
|
|
|
|
|
if( z != table.end() )
|
|
|
|
{
|
|
|
|
list = z->second;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-21 01:19:17 +01:00
|
|
|
|
|
|
|
|
|
|
|
void ConfParser::UTF8(bool utf)
|
|
|
|
{
|
|
|
|
input_as_utf8 = utf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ConfParser::Print(std::ostream & out)
|
|
|
|
{
|
|
|
|
TableSingle::iterator i1;
|
|
|
|
|
|
|
|
for(i1 = table_single.begin() ; i1 != 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 = table.begin() ; i2 != 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;
|
|
|
|
}
|
|
|
|
}
|