added: functions for dealing with white characters:
bool IsWhite(wchar_t c, bool check_additional_chars, bool treat_new_line_as_white) (checking unicode white characters too) CharType * SkipWhite(CharType * str, bool check_additional_chars = true, bool treat_new_line_as_white = true) IsDigit(wchar_t c, int base, int * digit) added: functions to converting from a string to an integer: unsigned long long Toull(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0) long long Toll(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0) unsigned long Toul(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0) unsigned int Toui(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0) long Tol(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0) int Toi(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0) changed: some work in Space (new Api) now Text() methods returns std::wstring by value (before they were returned by reference) added std::wstring & TextRef() methods added unsigned int UInt(), unsigned long ULong() and LongLong() and ULongLong() GetValue() renamed to GetFirstValue() AText() renamed to TextA() and they return std::string by value now git-svn-id: svn://ttmath.org/publicrep/pikotools/trunk@1066 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
354
space/space.cpp
354
space/space.cpp
@@ -40,7 +40,7 @@
|
||||
#include "space.h"
|
||||
#include "utf8/utf8.h"
|
||||
#include "textstream/textstream.h"
|
||||
|
||||
#include "convert/convert.h"
|
||||
|
||||
|
||||
namespace PT
|
||||
@@ -102,15 +102,15 @@ void Space::Clear()
|
||||
|
||||
|
||||
|
||||
std::wstring * Space::GetValue(const wchar_t * name)
|
||||
std::wstring * Space::GetFirstValue(const wchar_t * name)
|
||||
{
|
||||
tmp_name = name;
|
||||
return GetValue(tmp_name);
|
||||
return GetFirstValue(tmp_name);
|
||||
}
|
||||
|
||||
|
||||
|
||||
std::wstring * Space::GetValue(const std::wstring & name)
|
||||
std::wstring * Space::GetFirstValue(const std::wstring & name)
|
||||
{
|
||||
Table::iterator t = table.find(name);
|
||||
|
||||
@@ -125,8 +125,15 @@ std::wstring * Space::GetValue(const std::wstring & name)
|
||||
}
|
||||
|
||||
|
||||
// CHECK ME
|
||||
const std::wstring * Space::GetFirstValue(const wchar_t * name) const
|
||||
{
|
||||
tmp_name = name;
|
||||
return GetFirstValue(tmp_name);
|
||||
}
|
||||
|
||||
const std::wstring * Space::GetValue(const std::wstring & name) const
|
||||
|
||||
const std::wstring * Space::GetFirstValue(const std::wstring & name) const
|
||||
{
|
||||
Table::const_iterator t = table.find(name);
|
||||
|
||||
@@ -185,7 +192,7 @@ return false;
|
||||
|
||||
|
||||
|
||||
std::wstring & Space::Text(const wchar_t * name)
|
||||
std::wstring Space::Text(const wchar_t * name)
|
||||
{
|
||||
tmp_name = name;
|
||||
return Text(tmp_name, L"");
|
||||
@@ -193,16 +200,16 @@ std::wstring & Space::Text(const wchar_t * name)
|
||||
|
||||
|
||||
|
||||
std::wstring & Space::Text(const wchar_t * name, const wchar_t * def)
|
||||
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 Space::Text(const std::wstring & name, const wchar_t * def)
|
||||
{
|
||||
std::wstring * value = GetValue(name);
|
||||
std::wstring * value = GetFirstValue(name);
|
||||
|
||||
if( value )
|
||||
{
|
||||
@@ -210,54 +217,210 @@ std::wstring & Space::Text(const std::wstring & name, const wchar_t * def)
|
||||
}
|
||||
else
|
||||
{
|
||||
tmp_value_text = def;
|
||||
return tmp_value_text;
|
||||
return std::wstring(def);
|
||||
// tmp_value_text = def;
|
||||
// return tmp_value_text;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
std::string & Space::AText(const wchar_t * name)
|
||||
std::wstring Space::Text(const std::wstring & name, const std::wstring & def)
|
||||
{
|
||||
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);
|
||||
std::wstring * value = GetFirstValue(name);
|
||||
|
||||
if( value )
|
||||
{
|
||||
PT::WideToUTF8(*value, tmp_value_text_ascii);
|
||||
return tmp_value_text_ascii;
|
||||
return *value;
|
||||
}
|
||||
else
|
||||
{
|
||||
tmp_value_text_ascii = def;
|
||||
return tmp_value_text_ascii;
|
||||
return def;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
int Space::Int(const wchar_t * name)
|
||||
std::wstring & Space::TextRef(const wchar_t * name)
|
||||
{
|
||||
tmp_name = name;
|
||||
return Int(tmp_name, 0);
|
||||
return TextRef(tmp_name, L"");
|
||||
}
|
||||
|
||||
|
||||
std::wstring & Space::TextRef(const wchar_t * name, const wchar_t * def)
|
||||
{
|
||||
tmp_name = name;
|
||||
return TextRef(tmp_name, def);
|
||||
}
|
||||
|
||||
std::wstring & Space::TextRef(const std::wstring & name, const wchar_t * def)
|
||||
{
|
||||
Table::iterator t = table.find(name);
|
||||
|
||||
if( t == table.end() )
|
||||
{
|
||||
Value & v = table[name];
|
||||
v.push_back(def);
|
||||
return v[0];
|
||||
}
|
||||
else
|
||||
if( t->second.empty() )
|
||||
{
|
||||
Value & v = t->second;
|
||||
v.push_back(def);
|
||||
return v[0];
|
||||
}
|
||||
else
|
||||
{
|
||||
return t->second[0];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
std::wstring & Space::TextRef(const std::wstring & name, const std::wstring & def)
|
||||
{
|
||||
return TextRef(name, def.c_str());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
std::string Space::TextA(const wchar_t * name)
|
||||
{
|
||||
tmp_name = name;
|
||||
return TextA(tmp_name, "");
|
||||
}
|
||||
|
||||
|
||||
|
||||
std::string Space::TextA(const wchar_t * name, const char * def)
|
||||
{
|
||||
tmp_name = name;
|
||||
return TextA(tmp_name, def);
|
||||
}
|
||||
|
||||
|
||||
std::string Space::TextA(const std::wstring & name, const char * def)
|
||||
{
|
||||
std::wstring * value = GetFirstValue(name);
|
||||
|
||||
if( value )
|
||||
{
|
||||
std::string res;
|
||||
PT::WideToUTF8(*value, res);
|
||||
return res;
|
||||
}
|
||||
else
|
||||
{
|
||||
return def;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
std::string Space::TextA(const std::wstring & name, const std::string & def)
|
||||
{
|
||||
return TextA(name, def.c_str());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
int Space::CheckIntegerBase(const std::wstring & value, const wchar_t ** save_ptr)
|
||||
{
|
||||
const wchar_t * ptr = SkipWhite(value.c_str());
|
||||
int base = 10;
|
||||
|
||||
if( *ptr == '0' )
|
||||
{
|
||||
base = 8;
|
||||
ptr += 1; // we can skip the first zero
|
||||
}
|
||||
else
|
||||
if( *ptr == '-' && *(ptr+1) == '0' )
|
||||
{
|
||||
base = 8;
|
||||
// ptr is pointing to '-', do not increment it here
|
||||
}
|
||||
|
||||
*save_ptr = ptr;
|
||||
return base;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
unsigned int Space::ToUInt(const std::wstring & value)
|
||||
{
|
||||
const wchar_t * ptr;
|
||||
int base = CheckIntegerBase(value, &ptr);
|
||||
return Toui(ptr, base);
|
||||
}
|
||||
|
||||
int Space::ToInt(const std::wstring & value)
|
||||
{
|
||||
const wchar_t * ptr;
|
||||
int base = CheckIntegerBase(value, &ptr);
|
||||
return Toi(ptr, base);
|
||||
}
|
||||
|
||||
|
||||
unsigned long Space::ToULong(const std::wstring & value)
|
||||
{
|
||||
const wchar_t * ptr;
|
||||
int base = CheckIntegerBase(value, &ptr);
|
||||
return Toul(ptr, base);
|
||||
}
|
||||
|
||||
long Space::ToLong(const std::wstring & value)
|
||||
{
|
||||
const wchar_t * ptr;
|
||||
int base = CheckIntegerBase(value, &ptr);
|
||||
return Tol(ptr, base);
|
||||
}
|
||||
|
||||
|
||||
unsigned long long Space::ToULongLong(const std::wstring & value)
|
||||
{
|
||||
const wchar_t * ptr;
|
||||
int base = CheckIntegerBase(value, &ptr);
|
||||
return Toull(ptr, base);
|
||||
}
|
||||
|
||||
long long Space::ToLongLong(const std::wstring & value)
|
||||
{
|
||||
const wchar_t * ptr;
|
||||
int base = CheckIntegerBase(value, &ptr);
|
||||
return Toll(ptr, base);
|
||||
}
|
||||
|
||||
|
||||
size_t Space::ToSize(const std::wstring & value)
|
||||
{
|
||||
if( sizeof(size_t) == sizeof(unsigned int) )
|
||||
return ToUInt(value);
|
||||
else
|
||||
if( sizeof(size_t) == sizeof(unsigned long) )
|
||||
return ToULong(value);
|
||||
else
|
||||
return ToULongLong(value);
|
||||
}
|
||||
|
||||
|
||||
bool Space::ToBool(const std::wstring & value)
|
||||
{
|
||||
// IMPROVE ME add support for trimming white chars?
|
||||
return (EqualNoCase(value.c_str(), L"true") ||
|
||||
EqualNoCase(value.c_str(), L"yes") ||
|
||||
EqualNoCase(value.c_str(), L"1")
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
int Space::Int(const wchar_t * name, int def)
|
||||
{
|
||||
tmp_name = name;
|
||||
@@ -265,18 +428,11 @@ int Space::Int(const wchar_t * name, int def)
|
||||
}
|
||||
|
||||
|
||||
int Space::ToInt(const std::wstring & value)
|
||||
{
|
||||
// !! FIXME what if value is empty?
|
||||
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);
|
||||
std::wstring * value = GetFirstValue(name);
|
||||
|
||||
if( value )
|
||||
return ToInt(*value);
|
||||
@@ -286,15 +442,27 @@ return def;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
long Space::Long(const wchar_t * name)
|
||||
unsigned int Space::UInt(const wchar_t * name, unsigned int def)
|
||||
{
|
||||
tmp_name = name;
|
||||
return Long(tmp_name, 0);
|
||||
return UInt(tmp_name, def);
|
||||
}
|
||||
|
||||
|
||||
unsigned int Space::UInt(const std::wstring & name, unsigned int def)
|
||||
{
|
||||
std::wstring * value = GetFirstValue(name);
|
||||
|
||||
if( value )
|
||||
return ToUInt(*value);
|
||||
|
||||
return def;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
long Space::Long(const wchar_t * name, long def)
|
||||
{
|
||||
tmp_name = name;
|
||||
@@ -302,16 +470,9 @@ long Space::Long(const wchar_t * name, long def)
|
||||
}
|
||||
|
||||
|
||||
long Space::ToLong(const std::wstring & value)
|
||||
{
|
||||
// !! FIXME what if value is empty?
|
||||
return (value[0] == '0')? wcstol(value.c_str() + 1, 0, 8) : wcstol(value.c_str(), 0, 10);
|
||||
}
|
||||
|
||||
|
||||
long Space::Long(const std::wstring & name, long def)
|
||||
{
|
||||
std::wstring * value = GetValue(name);
|
||||
std::wstring * value = GetFirstValue(name);
|
||||
|
||||
if( value )
|
||||
return ToLong(*value);
|
||||
@@ -320,14 +481,61 @@ return def;
|
||||
}
|
||||
|
||||
|
||||
|
||||
size_t Space::Size(const wchar_t * name)
|
||||
unsigned long Space::ULong(const wchar_t * name, unsigned long def)
|
||||
{
|
||||
tmp_name = name;
|
||||
return Size(tmp_name, 0);
|
||||
return ULong(tmp_name, def);
|
||||
}
|
||||
|
||||
|
||||
unsigned long Space::ULong(const std::wstring & name, unsigned long def)
|
||||
{
|
||||
std::wstring * value = GetFirstValue(name);
|
||||
|
||||
if( value )
|
||||
return ToULong(*value);
|
||||
|
||||
return def;
|
||||
}
|
||||
|
||||
|
||||
|
||||
long long Space::LongLong(const wchar_t * name, long long def)
|
||||
{
|
||||
tmp_name = name;
|
||||
return LongLong(tmp_name, def);
|
||||
}
|
||||
|
||||
long long Space::LongLong(const std::wstring & name, long long def)
|
||||
{
|
||||
std::wstring * value = GetFirstValue(name);
|
||||
|
||||
if( value )
|
||||
return ToLongLong(*value);
|
||||
|
||||
return def;
|
||||
}
|
||||
|
||||
unsigned long long Space::ULongLong(const wchar_t * name, unsigned long long def)
|
||||
{
|
||||
tmp_name = name;
|
||||
return ULongLong(tmp_name, def);
|
||||
}
|
||||
|
||||
unsigned long long Space::ULongLong(const std::wstring & name, unsigned long long def)
|
||||
{
|
||||
std::wstring * value = GetFirstValue(name);
|
||||
|
||||
if( value )
|
||||
return ToULongLong(*value);
|
||||
|
||||
return def;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
size_t Space::Size(const wchar_t * name, size_t def)
|
||||
{
|
||||
tmp_name = name;
|
||||
@@ -335,19 +543,9 @@ size_t Space::Size(const wchar_t * name, size_t def)
|
||||
}
|
||||
|
||||
|
||||
|
||||
size_t Space::ToSize(const std::wstring & value)
|
||||
{
|
||||
// !! FIXME what if value is empty?
|
||||
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);
|
||||
std::wstring * value = GetFirstValue(name);
|
||||
|
||||
if( value )
|
||||
return ToSize(*value);
|
||||
@@ -358,13 +556,6 @@ 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;
|
||||
@@ -373,18 +564,9 @@ bool Space::Bool(const wchar_t * name, bool 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);
|
||||
std::wstring * value = GetFirstValue(name);
|
||||
|
||||
if( value )
|
||||
return ToBool(*value);
|
||||
|
Reference in New Issue
Block a user