changed: directory name: confparser -> space
git-svn-id: svn://ttmath.org/publicrep/pikotools/trunk@401 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
644
space/space.cpp
Executable file
644
space/space.cpp
Executable file
@@ -0,0 +1,644 @@
|
||||
/*
|
||||
* This file is a part of PikoTools
|
||||
* and is distributed under the (new) BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008-2012, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name Tomasz Sowa nor the names of contributors to this
|
||||
* project may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
||||
* THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
//#include <cstdlib>
|
||||
#include <wchar.h>
|
||||
#include "space.h"
|
||||
#include "utf8/utf8.h"
|
||||
|
||||
|
||||
|
||||
namespace PT
|
||||
{
|
||||
|
||||
|
||||
|
||||
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 )
|
||||
{
|
||||
PT::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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
long Space::Long(const wchar_t * name)
|
||||
{
|
||||
tmp_name = name;
|
||||
return Long(tmp_name, 0);
|
||||
}
|
||||
|
||||
|
||||
long Space::Long(const wchar_t * name, long def)
|
||||
{
|
||||
tmp_name = name;
|
||||
return Long(tmp_name, def);
|
||||
}
|
||||
|
||||
|
||||
long Space::ToLong(const std::wstring & value)
|
||||
{
|
||||
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);
|
||||
|
||||
if( value )
|
||||
return ToLong(*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;
|
||||
}
|
||||
|
||||
|
||||
std::wstring & Space::FindAdd(const wchar_t * name)
|
||||
{
|
||||
tmp_name = name;
|
||||
std::wstring * value = GetValue(tmp_name);
|
||||
|
||||
if( !value )
|
||||
{
|
||||
value = &table_single[tmp_name];
|
||||
table.erase(tmp_name);
|
||||
}
|
||||
|
||||
return *value;
|
||||
}
|
||||
|
||||
|
||||
std::wstring & Space::FindAdd(const std::wstring & name)
|
||||
{
|
||||
std::wstring * value = GetValue(name);
|
||||
|
||||
if( !value )
|
||||
{
|
||||
value = &table_single[name];
|
||||
table.erase(name);
|
||||
}
|
||||
|
||||
return *value;
|
||||
}
|
||||
|
||||
|
||||
std::wstring & Space::Add(const wchar_t * name, bool value)
|
||||
{
|
||||
if( value )
|
||||
return Add(name, L"true");
|
||||
else
|
||||
return Add(name, L"false");
|
||||
}
|
||||
|
||||
|
||||
std::wstring & Space::Add(const std::wstring & name, bool value)
|
||||
{
|
||||
return Add(name.c_str(), value);
|
||||
}
|
||||
|
||||
|
||||
std::wstring & Space::Add(const wchar_t * name, int value)
|
||||
{
|
||||
wchar_t value_str[50];
|
||||
|
||||
#if defined _WIN32 || defined _WIN64
|
||||
swprintf(value_str, L"%d", value);
|
||||
#else
|
||||
swprintf(value_str, sizeof(value_str)/sizeof(wchar_t), L"%d", value);
|
||||
#endif
|
||||
|
||||
return Add(name, value_str);
|
||||
}
|
||||
|
||||
|
||||
std::wstring & Space::Add(const std::wstring & name, int value)
|
||||
{
|
||||
return Add(name.c_str(), value);
|
||||
}
|
||||
|
||||
|
||||
std::wstring & Space::Add(const wchar_t * name, long value)
|
||||
{
|
||||
wchar_t value_str[50];
|
||||
|
||||
#if defined _WIN32 || defined _WIN64
|
||||
swprintf(value_str, L"%ld", value);
|
||||
#else
|
||||
swprintf(value_str, sizeof(value_str)/sizeof(wchar_t), L"%ld", value);
|
||||
#endif
|
||||
|
||||
return Add(name, value_str);
|
||||
}
|
||||
|
||||
|
||||
std::wstring & Space::Add(const std::wstring & name, long value)
|
||||
{
|
||||
return Add(name.c_str(), value);
|
||||
}
|
||||
|
||||
|
||||
std::wstring & Space::Add(const wchar_t * name, const wchar_t * value)
|
||||
{
|
||||
tmp_name = name;
|
||||
std::wstring & val = table_single[tmp_name];
|
||||
val = value;
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
|
||||
std::wstring & Space::Add(const wchar_t * name, const std::wstring & value)
|
||||
{
|
||||
tmp_name = name;
|
||||
std::wstring & val = table_single[tmp_name];
|
||||
val = value;
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
|
||||
std::wstring & Space::Add(const std::wstring & name, const std::wstring & value)
|
||||
{
|
||||
std::wstring & val = table_single[name];
|
||||
val = value;
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Space::Remove(const wchar_t * name)
|
||||
{
|
||||
tmp_name = name;
|
||||
|
||||
table_single.erase(tmp_name);
|
||||
table.erase(tmp_name);
|
||||
}
|
||||
|
||||
|
||||
void Space::Remove(const std::wstring & name)
|
||||
{
|
||||
table_single.erase(name);
|
||||
table.erase(name);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Space & Space::AddSpace(const wchar_t * name)
|
||||
{
|
||||
spaces.push_back(new Space());
|
||||
spaces.back()->name = name;
|
||||
spaces.back()->parent = this;
|
||||
|
||||
return *spaces.back();
|
||||
}
|
||||
|
||||
|
||||
Space & Space::AddSpace(const std::wstring & name)
|
||||
{
|
||||
spaces.push_back(new Space());
|
||||
spaces.back()->name = name;
|
||||
spaces.back()->parent = this;
|
||||
|
||||
return *spaces.back();
|
||||
}
|
||||
|
||||
|
||||
Space * Space::FindSpace(const wchar_t * name)
|
||||
{
|
||||
for(size_t i=0 ; i<spaces.size() ; ++i)
|
||||
{
|
||||
if( spaces[i]->name == name )
|
||||
return spaces[i];
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Space * Space::FindSpace(const std::wstring & name)
|
||||
{
|
||||
for(size_t i=0 ; i<spaces.size() ; ++i)
|
||||
{
|
||||
if( spaces[i]->name == name )
|
||||
return spaces[i];
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Space & Space::FindAddSpace(const wchar_t * name)
|
||||
{
|
||||
Space * space = FindSpace(name);
|
||||
|
||||
if( space )
|
||||
return *space;
|
||||
|
||||
return AddSpace(name);
|
||||
}
|
||||
|
||||
|
||||
Space & Space::FindAddSpace(const std::wstring & name)
|
||||
{
|
||||
Space * space = FindSpace(name);
|
||||
|
||||
if( space )
|
||||
return *space;
|
||||
|
||||
return AddSpace(name);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Space::RemoveSpace(const wchar_t * name)
|
||||
{
|
||||
for(size_t i=0 ; i<spaces.size() ; )
|
||||
{
|
||||
if( spaces[i]->name == name )
|
||||
RemoveSpace(i);
|
||||
else
|
||||
++i;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Space::RemoveSpace(const std::wstring & name)
|
||||
{
|
||||
for(size_t i=0 ; i<spaces.size() ; )
|
||||
{
|
||||
if( spaces[i]->name == name )
|
||||
RemoveSpace(i);
|
||||
else
|
||||
++i;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Space::RemoveSpace(size_t child_index)
|
||||
{
|
||||
if( child_index < spaces.size() )
|
||||
{
|
||||
delete spaces[child_index];
|
||||
spaces.erase(spaces.begin() + child_index);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
|
||||
} // namespace
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user