added support for UTF-8
now the UTF-8 is a default charset git-svn-id: svn://ttmath.org/publicrep/winix/trunk@677 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
@@ -8,8 +8,10 @@
|
||||
*/
|
||||
|
||||
#include <cstdlib>
|
||||
#include <wchar.h>
|
||||
#include "confparser.h"
|
||||
#include "misc.h"
|
||||
#include "ezc.h"
|
||||
|
||||
|
||||
|
||||
@@ -26,8 +28,9 @@ ConfParser::ConfParser()
|
||||
split_single = false;
|
||||
skip_empty = false;
|
||||
use_escape_char = true;
|
||||
input_as_utf8 = false;
|
||||
|
||||
default_str = "";
|
||||
default_str = L"";
|
||||
default_int = 0;
|
||||
default_size = 0;
|
||||
default_bool = false;
|
||||
@@ -52,6 +55,9 @@ void ConfParser::UseEscapeChar(bool escape)
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
ConfParser::Status ConfParser::Parse(const char * file_name)
|
||||
{
|
||||
line = 1;
|
||||
@@ -83,6 +89,23 @@ ConfParser::Status ConfParser::Parse(const std::string & file_name)
|
||||
|
||||
|
||||
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
ConfParser::Status ConfParser::ParseFile()
|
||||
{
|
||||
ReadChar();
|
||||
@@ -149,7 +172,7 @@ void ConfParser::AddOption()
|
||||
|
||||
|
||||
|
||||
void ConfParser::DeleteFromTable(const std::string & var)
|
||||
void ConfParser::DeleteFromTable(const std::wstring & var)
|
||||
{
|
||||
Table::iterator i = table.find(var);
|
||||
|
||||
@@ -159,7 +182,7 @@ void ConfParser::DeleteFromTable(const std::string & var)
|
||||
|
||||
|
||||
|
||||
void ConfParser::DeleteFromTableSingle(const std::string & var)
|
||||
void ConfParser::DeleteFromTableSingle(const std::wstring & var)
|
||||
{
|
||||
TableSingle::iterator i = table_single.find(var);
|
||||
|
||||
@@ -303,7 +326,33 @@ return true;
|
||||
}
|
||||
|
||||
|
||||
int ConfParser::ReadChar()
|
||||
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()
|
||||
{
|
||||
lastc = file.get();
|
||||
|
||||
@@ -314,6 +363,15 @@ return lastc;
|
||||
}
|
||||
|
||||
|
||||
int ConfParser::ReadChar()
|
||||
{
|
||||
if( input_as_utf8 )
|
||||
return ReadUTF8Char();
|
||||
|
||||
return ReadASCIIChar();
|
||||
}
|
||||
|
||||
|
||||
bool ConfParser::IsWhite(int c)
|
||||
{
|
||||
// dont use '\n' here
|
||||
@@ -359,9 +417,9 @@ void ConfParser::SkipLine()
|
||||
|
||||
|
||||
|
||||
void ConfParser::Trim(std::string & s)
|
||||
void ConfParser::Trim(std::wstring & s)
|
||||
{
|
||||
std::string::size_type i;
|
||||
std::wstring::size_type i;
|
||||
|
||||
if( s.empty() )
|
||||
return;
|
||||
@@ -378,7 +436,7 @@ std::string::size_type i;
|
||||
|
||||
// deleting white characters at the end
|
||||
if( i != s.size() - 1 )
|
||||
s.erase(i+1, std::string::npos);
|
||||
s.erase(i+1, std::wstring::npos);
|
||||
|
||||
// looking for white characters at the beginning
|
||||
for(i=0 ; i<s.size() && IsWhite(s[i]) ; ++i);
|
||||
@@ -393,20 +451,20 @@ std::string::size_type i;
|
||||
|
||||
|
||||
|
||||
std::string ConfParser::Text(const char * name)
|
||||
std::wstring ConfParser::Text(const wchar_t * name)
|
||||
{
|
||||
return Text(std::string(name), default_str);
|
||||
return Text(std::wstring(name), default_str);
|
||||
}
|
||||
|
||||
|
||||
|
||||
std::string ConfParser::Text(const char * name, const char * def)
|
||||
std::wstring ConfParser::Text(const wchar_t * name, const wchar_t * def)
|
||||
{
|
||||
return Text(std::string(name), std::string(def));
|
||||
return Text(std::wstring(name), std::wstring(def));
|
||||
}
|
||||
|
||||
|
||||
std::string ConfParser::Text(const std::string & name, const std::string & def)
|
||||
std::wstring ConfParser::Text(const std::wstring & name, const std::wstring & def)
|
||||
{
|
||||
TableSingle::iterator i = table_single.find(name);
|
||||
|
||||
@@ -425,27 +483,63 @@ return i->second;
|
||||
|
||||
|
||||
|
||||
int ConfParser::Int(const char * name)
|
||||
std::string ConfParser::AText(const wchar_t * name)
|
||||
{
|
||||
return Int(std::string(name), default_int);
|
||||
std::wstring res = Text(name);
|
||||
std::string ares;
|
||||
|
||||
Ezc::WideToUTF8(res, ares);
|
||||
|
||||
return ares;
|
||||
}
|
||||
|
||||
|
||||
int ConfParser::Int(const char * name, int def)
|
||||
|
||||
std::string ConfParser::AText(const wchar_t * name, const wchar_t * def)
|
||||
{
|
||||
return Int(std::string(name), def);
|
||||
std::wstring res = Text(name, def);
|
||||
std::string ares;
|
||||
|
||||
Ezc::WideToUTF8(res, ares);
|
||||
|
||||
return ares;
|
||||
}
|
||||
|
||||
|
||||
int ConfParser::ToInt(const std::string & value)
|
||||
std::string ConfParser::AText(const std::wstring & name, const std::wstring & def)
|
||||
{
|
||||
long res = (value[0] == '0')? strtol(value.c_str() + 1, 0, 8) : strtol(value.c_str(), 0, 10);
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
int ConfParser::Int(const wchar_t * name, int def)
|
||||
{
|
||||
return Int(std::wstring(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 res;
|
||||
return static_cast<int>(res);
|
||||
}
|
||||
|
||||
|
||||
int ConfParser::Int(const std::string & name, int def)
|
||||
int ConfParser::Int(const std::wstring & name, int def)
|
||||
{
|
||||
TableSingle::iterator i = table_single.find(name);
|
||||
|
||||
@@ -464,28 +558,28 @@ return ToInt(i->second);
|
||||
|
||||
|
||||
|
||||
size_t ConfParser::Size(const char * name)
|
||||
size_t ConfParser::Size(const wchar_t * name)
|
||||
{
|
||||
return Size(std::string(name), default_size);
|
||||
return Size(std::wstring(name), default_size);
|
||||
}
|
||||
|
||||
|
||||
size_t ConfParser::Size(const char * name, size_t def)
|
||||
size_t ConfParser::Size(const wchar_t * name, size_t def)
|
||||
{
|
||||
return Size(std::string(name), def);
|
||||
return Size(std::wstring(name), def);
|
||||
}
|
||||
|
||||
|
||||
|
||||
size_t ConfParser::ToSize(const std::string & value)
|
||||
size_t ConfParser::ToSize(const std::wstring & value)
|
||||
{
|
||||
long res = (value[0] == '0')? strtoul(value.c_str() + 1, 0, 8) : strtoul(value.c_str(), 0, 10);
|
||||
unsigned long res = (value[0] == '0')? wcstoul(value.c_str() + 1, 0, 8) : wcstoul(value.c_str(), 0, 10);
|
||||
|
||||
return (size_t)res;
|
||||
return static_cast<size_t>(res);
|
||||
}
|
||||
|
||||
|
||||
size_t ConfParser::Size(const std::string & name, size_t def)
|
||||
size_t ConfParser::Size(const std::wstring & name, size_t def)
|
||||
{
|
||||
TableSingle::iterator i = table_single.find(name);
|
||||
|
||||
@@ -505,28 +599,28 @@ return ToSize(i->second);
|
||||
|
||||
|
||||
|
||||
bool ConfParser::Bool(const char * name)
|
||||
bool ConfParser::Bool(const wchar_t * name)
|
||||
{
|
||||
return Bool(std::string(name), default_bool);
|
||||
return Bool(std::wstring(name), default_bool);
|
||||
}
|
||||
|
||||
|
||||
bool ConfParser::Bool(const char * name, bool def)
|
||||
bool ConfParser::Bool(const wchar_t * name, bool def)
|
||||
{
|
||||
return Bool(std::string(name), def);
|
||||
return Bool(std::wstring(name), def);
|
||||
}
|
||||
|
||||
|
||||
bool ConfParser::ToBool(const std::string & value)
|
||||
bool ConfParser::ToBool(const std::wstring & value)
|
||||
{
|
||||
return ( EqualNoCase(value.c_str(), "true") ||
|
||||
EqualNoCase(value.c_str(), "yes") ||
|
||||
EqualNoCase(value.c_str(), "1")
|
||||
return ( EqualNoCase(value.c_str(), L"true") ||
|
||||
EqualNoCase(value.c_str(), L"yes") ||
|
||||
EqualNoCase(value.c_str(), L"1")
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
bool ConfParser::Bool(const std::string & name, bool def)
|
||||
bool ConfParser::Bool(const std::wstring & name, bool def)
|
||||
{
|
||||
TableSingle::iterator i = table_single.find(name);
|
||||
|
||||
@@ -544,7 +638,7 @@ return ToBool(i->second);
|
||||
}
|
||||
|
||||
|
||||
void ConfParser::SetDefaultText(const std::string & def)
|
||||
void ConfParser::SetDefaultText(const std::wstring & def)
|
||||
{
|
||||
default_str = def;
|
||||
}
|
||||
@@ -568,13 +662,13 @@ void ConfParser::SetDefaultBool(bool def)
|
||||
|
||||
|
||||
// in lists we don't use default values
|
||||
void ConfParser::ListText(const char * name, std::vector<std::string> & list)
|
||||
void ConfParser::ListText(const wchar_t * name, std::vector<std::wstring> & list)
|
||||
{
|
||||
ListText(std::string(name), list);
|
||||
ListText(std::wstring(name), list);
|
||||
}
|
||||
|
||||
|
||||
void ConfParser::ListText(const std::string & name, std::vector<std::string> & list)
|
||||
void ConfParser::ListText(const std::wstring & name, std::vector<std::wstring> & list)
|
||||
{
|
||||
list.clear();
|
||||
|
||||
@@ -595,3 +689,41 @@ void ConfParser::ListText(const std::string & name, std::vector<std::string> & l
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user