Browse Source

Use plString in plConfigInfo and friends

Michael Hansen 12 years ago
parent
commit
12536a918e
  1. 283
      Sources/Plasma/PubUtilLib/plContainer/plConfigInfo.cpp
  2. 197
      Sources/Plasma/PubUtilLib/plContainer/plConfigInfo.h
  3. 82
      Sources/Plasma/PubUtilLib/plContainer/plConfigInfoLogging.cpp
  4. 132
      Sources/Plasma/PubUtilLib/plContainer/plKeysAndValues.cpp
  5. 40
      Sources/Plasma/PubUtilLib/plContainer/plKeysAndValues.h
  6. 2
      Sources/Plasma/PubUtilLib/plNetClient/plNetClientMgrSend.cpp

283
Sources/Plasma/PubUtilLib/plContainer/plConfigInfo.cpp

@ -48,9 +48,9 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com
#include <stdarg.h> #include <stdarg.h>
#include <sstream> #include <sstream>
const std::string& plConfigInfo::GlobalSection() const plString& plConfigInfo::GlobalSection()
{ {
static std::string section("global"); static plString section("global");
return section; return section;
} }
@ -74,32 +74,32 @@ void plConfigInfo::Clear()
fSections.clear(); fSections.clear();
} }
void plConfigInfo::RemoveSection(const std::string & section) void plConfigInfo::RemoveSection(const plString & section)
{ {
fSections.erase(section.c_str()); fSections.erase(section);
} }
void plConfigInfo::RemoveKey(const std::string & section, const std::string & key) void plConfigInfo::RemoveKey(const plString & section, const plString & key)
{ {
Sections::iterator si = fSections.find(section.c_str()); Sections::iterator si = fSections.find(section);
if (si != fSections.end()) if (si != fSections.end())
fSections[section.c_str()].RemoveKey(key); fSections[section].RemoveKey(key);
} }
bool plConfigInfo::HasSection(const std::string & section) const bool plConfigInfo::HasSection(const plString & section) const
{ {
return fSections.find(section.c_str())!=fSections.end(); return fSections.find(section)!=fSections.end();
} }
bool plConfigInfo::HasKey(const std::string & section, const std::string & key) bool plConfigInfo::HasKey(const plString & section, const plString & key)
{ {
Sections::iterator si = fSections.find(section.c_str()); Sections::iterator si = fSections.find(section);
if (si == fSections.end()) if (si == fSections.end())
return false; return false;
return (si->second.HasKey(key)); return (si->second.HasKey(key));
} }
bool plConfigInfo::HasKeyAny(const std::string & key) bool plConfigInfo::HasKeyAny(const plString & key)
{ {
for (Sections::iterator si=fSections.begin(); si!=fSections.end(); ++si) for (Sections::iterator si=fSections.begin(); si!=fSections.end(); ++si)
{ {
@ -109,12 +109,12 @@ bool plConfigInfo::HasKeyAny(const std::string & key)
return false; return false;
} }
bool plConfigInfo::HasKeyIn(const std::string & key, const char * section1, ...) bool plConfigInfo::HasKeyIn(const plString & key, const char * section1, ...)
{ {
const char * section = section1; const char * section = section1;
va_list va; va_list va;
va_start(va,section1); va_start(va,section1);
std::vector<std::string> sections; std::vector<plString> sections;
while (section) while (section)
{ {
sections.push_back( section ); sections.push_back( section );
@ -124,114 +124,105 @@ bool plConfigInfo::HasKeyIn(const std::string & key, const char * section1, ...)
return HasKeyIn( key, sections ); return HasKeyIn( key, sections );
} }
bool plConfigInfo::HasKeyIn(const std::string & key, const std::vector<std::string> & sections ) bool plConfigInfo::HasKeyIn(const plString & key, const std::vector<plString> & sections )
{ {
for ( int i=0; i<sections.size(); i++ ) for ( int i=0; i<sections.size(); i++ )
{ {
const char * section = sections[i].c_str(); if (HasSection(sections[i]))
if (HasSection(section))
{ {
if (fSections[section].HasKey(key)) if (fSections[sections[i]].HasKey(key))
return true; return true;
} }
} }
return false; return false;
} }
bool plConfigInfo::KeyHasValue(const std::string & section, const std::string & key, const std::string & value) bool plConfigInfo::KeyHasValue(const plString & section, const plString & key, const plString & value)
{ {
Sections::iterator si = fSections.find(section.c_str()); Sections::iterator si = fSections.find(section);
if (si == fSections.end()) if (si == fSections.end())
return false; return false;
return si->second.KeyHasValue(key,value); return si->second.KeyHasValue(key,value);
} }
bool plConfigInfo::KeyHasValue(const std::string & section, const std::string & key, int value) bool plConfigInfo::KeyHasValue(const plString & section, const plString & key, int value)
{ {
Sections::iterator si = fSections.find(section.c_str()); Sections::iterator si = fSections.find(section);
if (si == fSections.end()) if (si == fSections.end())
return false; return false;
return si->second.KeyHasValue(key,value); return si->second.KeyHasValue(key,value);
} }
bool plConfigInfo::KeyHasValue(const std::string & section, const std::string & key, double value) bool plConfigInfo::KeyHasValue(const plString & section, const plString & key, double value)
{ {
Sections::iterator si = fSections.find(section.c_str()); Sections::iterator si = fSections.find(section);
if (si == fSections.end()) if (si == fSections.end())
return false; return false;
return si->second.KeyHasValue(key,value); return si->second.KeyHasValue(key,value);
} }
bool plConfigInfo::AddValue(const std::string & section, const std::string & key, const std::string & value, KAddValueMode mode) bool plConfigInfo::AddValue(const plString & section, const plString & key, const plString & value, KAddValueMode mode)
{ {
fSections[section.c_str()].AddValue(key,value,mode); return fSections[section].AddValue(key,value,mode);
return true;
} }
bool plConfigInfo::AddValue(const std::string & section, const std::string & key, int value, KAddValueMode mode) bool plConfigInfo::AddValue(const plString & section, const plString & key, int value, KAddValueMode mode)
{ {
char buf[20]; return fSections[section].AddValue(key,value,mode);
sprintf(buf, "%d", value);
std::string v(buf);
return AddValue(section,key,v,mode);
} }
bool plConfigInfo::AddValue(const std::string & section, const std::string & key, double value, KAddValueMode mode) bool plConfigInfo::AddValue(const plString & section, const plString & key, double value, KAddValueMode mode)
{ {
char buf[30]; return fSections[section].AddValue(key,value,mode);
sprintf(buf, "%f", value);
std::string v(buf);
return AddValue(section,key,v,mode);
} }
bool plConfigInfo::AddValues(const std::string & section, const std::string & key, const std::vector<std::string> & values, KAddValueMode mode) bool plConfigInfo::AddValues(const plString & section, const plString & key, const std::vector<plString> & values, KAddValueMode mode)
{ {
return fSections[section.c_str()].AddValues(key,values); return fSections[section].AddValues(key,values);
} }
plKeysAndValues plConfigInfo::GetSection(const std::string & section, bool & found) plKeysAndValues plConfigInfo::GetSection(const plString & section, bool & found)
{ {
found = HasSection(section); found = HasSection(section);
if (found) if (found)
return fSections[section.c_str()]; return fSections[section];
else else
return plKeysAndValues(); // empty return plKeysAndValues(); // empty
} }
std::vector<std::string> plConfigInfo::GetSectionNames() std::vector<plString> plConfigInfo::GetSectionNames()
{ {
std::vector<std::string> results; std::vector<plString> results;
for (Sections::const_iterator ii=fSections.begin(); ii!=fSections.end(); ++ii) for (Sections::const_iterator ii=fSections.begin(); ii!=fSections.end(); ++ii)
results.push_back(ii->first.c_str()); results.push_back(ii->first);
return results; return results;
} }
std::string plConfigInfo::GetValue(const std::string & section, const std::string & key, const std::string & defval, bool * outFound) const plString plConfigInfo::GetValue(const plString & section, const plString & key, const plString & defval, bool * outFound) const
{ {
return fSections[section.c_str()].GetValue(key,defval,outFound); return fSections[section].GetValue(key,defval,outFound);
} }
int plConfigInfo::GetValue(const std::string & section, const std::string & key, int defval, bool * outFound) const int plConfigInfo::GetValue(const plString & section, const plString & key, int defval, bool * outFound) const
{ {
return fSections[section.c_str()].GetValue(key,defval,outFound); return fSections[section].GetValue(key,defval,outFound);
} }
double plConfigInfo::GetValue(const std::string & section, const std::string & key, double defval, bool * outFound) const double plConfigInfo::GetValue(const plString & section, const plString & key, double defval, bool * outFound) const
{ {
return fSections[section.c_str()].GetValue(key,defval,outFound); return fSections[section].GetValue(key,defval,outFound);
} }
std::vector<std::string> plConfigInfo::GetAllValues(const std::string & section, const std::string & key) const std::vector<plString> plConfigInfo::GetAllValues(const plString & section, const plString & key) const
{ {
Sections::iterator si = fSections.find(section.c_str()); Sections::iterator si = fSections.find(section);
if (si != fSections.end()) if (si != fSections.end())
return si->second.GetAllValues(key); return si->second.GetAllValues(key);
std::vector<std::string> empty; return std::vector<plString>();
return empty;
} }
std::string plConfigInfo::GetValueAny(const std::string & key, const std::string & defval, bool * outFound) const plString plConfigInfo::GetValueAny(const plString & key, const plString & defval, bool * outFound) const
{ {
if (outFound) *outFound=false; if (outFound) *outFound=false;
for (Sections::iterator si=fSections.begin(); si!=fSections.end(); ++si) for (Sections::iterator si=fSections.begin(); si!=fSections.end(); ++si)
@ -240,7 +231,7 @@ std::string plConfigInfo::GetValueAny(const std::string & key, const std::string
return defval; return defval;
} }
int plConfigInfo::GetValueAny(const std::string & key, int defval, bool * outFound) const int plConfigInfo::GetValueAny(const plString & key, int defval, bool * outFound) const
{ {
if (outFound) *outFound=false; if (outFound) *outFound=false;
for (Sections::iterator si=fSections.begin(); si!=fSections.end(); ++si) for (Sections::iterator si=fSections.begin(); si!=fSections.end(); ++si)
@ -249,7 +240,7 @@ int plConfigInfo::GetValueAny(const std::string & key, int defval, bool * outFou
return defval; return defval;
} }
double plConfigInfo::GetValueAny(const std::string & key, double defval, bool * outFound) const double plConfigInfo::GetValueAny(const plString & key, double defval, bool * outFound) const
{ {
if (outFound) *outFound=false; if (outFound) *outFound=false;
for (Sections::iterator si=fSections.begin(); si!=fSections.end(); ++si) for (Sections::iterator si=fSections.begin(); si!=fSections.end(); ++si)
@ -258,17 +249,16 @@ double plConfigInfo::GetValueAny(const std::string & key, double defval, bool *
return defval; return defval;
} }
std::vector<std::string> plConfigInfo::GetAllValuesAny(const std::string & key) const std::vector<plString> plConfigInfo::GetAllValuesAny(const plString & key) const
{ {
for (Sections::iterator si=fSections.begin(); si!=fSections.end(); ++si) for (Sections::iterator si=fSections.begin(); si!=fSections.end(); ++si)
if (si->second.HasKey(key)) if (si->second.HasKey(key))
return si->second.GetAllValues(key); return si->second.GetAllValues(key);
std::vector<std::string> empty; return std::vector<plString>();
return empty;
} }
std::string plConfigInfo::GetValueIn(const std::string & key, const std::string & defval, bool * outFound, const char * section1, ...) const plString plConfigInfo::GetValueIn(const plString & key, const plString & defval, bool * outFound, const char * section1, ...) const
{ {
if (outFound) *outFound=false; if (outFound) *outFound=false;
const char * section = section1; const char * section = section1;
@ -288,14 +278,14 @@ std::string plConfigInfo::GetValueIn(const std::string & key, const std::string
return defval; return defval;
} }
std::string plConfigInfo::GetValueIn(const std::string & key, const std::string & defval, bool * outFound, const std::vector<std::string> & sections ) const plString plConfigInfo::GetValueIn(const plString & key, const plString & defval, bool * outFound, const std::vector<plString> & sections ) const
{ {
if (outFound) *outFound=false; if (outFound) *outFound=false;
for ( int i=0; i<sections.size(); i++ ) for ( int i=0; i<sections.size(); i++ )
{ {
if (HasSection(sections[i])) if (HasSection(sections[i]))
{ {
plKeysAndValues & kv = fSections[sections[i].c_str()]; plKeysAndValues & kv = fSections[sections[i]];
if (kv.HasKey(key)) if (kv.HasKey(key))
return kv.GetValue(key,defval,outFound); return kv.GetValue(key,defval,outFound);
} }
@ -303,7 +293,7 @@ std::string plConfigInfo::GetValueIn(const std::string & key, const std::string
return defval; return defval;
} }
int plConfigInfo::GetValueIn(const std::string & key, int defval, bool * outFound, const char * section1, ...) const int plConfigInfo::GetValueIn(const plString & key, int defval, bool * outFound, const char * section1, ...) const
{ {
if (outFound) *outFound=false; if (outFound) *outFound=false;
const char * section = section1; const char * section = section1;
@ -323,14 +313,14 @@ int plConfigInfo::GetValueIn(const std::string & key, int defval, bool * outFoun
return defval; return defval;
} }
int plConfigInfo::GetValueIn(const std::string & key, int defval, bool * outFound, const std::vector<std::string> & sections ) const int plConfigInfo::GetValueIn(const plString & key, int defval, bool * outFound, const std::vector<plString> & sections ) const
{ {
if (outFound) *outFound=false; if (outFound) *outFound=false;
for ( int i=0; i<sections.size(); i++ ) for ( int i=0; i<sections.size(); i++ )
{ {
if (HasSection(sections[i])) if (HasSection(sections[i]))
{ {
plKeysAndValues & kv = fSections[sections[i].c_str()]; plKeysAndValues & kv = fSections[sections[i]];
if (kv.HasKey(key)) if (kv.HasKey(key))
return kv.GetValue(key,defval,outFound); return kv.GetValue(key,defval,outFound);
} }
@ -338,7 +328,7 @@ int plConfigInfo::GetValueIn(const std::string & key, int defval, bool * outFoun
return defval; return defval;
} }
double plConfigInfo::GetValueIn(const std::string & key, double defval, bool * outFound, const char * section1, ...) const double plConfigInfo::GetValueIn(const plString & key, double defval, bool * outFound, const char * section1, ...) const
{ {
if (outFound) *outFound=false; if (outFound) *outFound=false;
const char * section = section1; const char * section = section1;
@ -358,14 +348,14 @@ double plConfigInfo::GetValueIn(const std::string & key, double defval, bool * o
return defval; return defval;
} }
double plConfigInfo::GetValueIn(const std::string & key, double defval, bool * outFound, const std::vector<std::string> & sections ) const double plConfigInfo::GetValueIn(const plString & key, double defval, bool * outFound, const std::vector<plString> & sections ) const
{ {
if (outFound) *outFound=false; if (outFound) *outFound=false;
for ( int i=0; i<sections.size(); i++ ) for ( int i=0; i<sections.size(); i++ )
{ {
if (HasSection(sections[i])) if (HasSection(sections[i]))
{ {
plKeysAndValues & kv = fSections[sections[i].c_str()]; plKeysAndValues & kv = fSections[sections[i]];
if (kv.HasKey(key)) if (kv.HasKey(key))
return kv.GetValue(key,defval,outFound); return kv.GetValue(key,defval,outFound);
} }
@ -373,12 +363,12 @@ double plConfigInfo::GetValueIn(const std::string & key, double defval, bool * o
return defval; return defval;
} }
std::vector<std::string> plConfigInfo::GetAllValuesIn(const std::string & key, const char * section1, ...) std::vector<plString> plConfigInfo::GetAllValuesIn(const plString & key, const char * section1, ...)
{ {
const char * section = section1; const char * section = section1;
va_list sections; va_list sections;
va_start(sections,section1); va_start(sections,section1);
std::vector<std::string> result; std::vector<plString> result;
while (section) while (section)
{ {
if (HasSection(section)) if (HasSection(section))
@ -386,7 +376,7 @@ std::vector<std::string> plConfigInfo::GetAllValuesIn(const std::string & key, c
plKeysAndValues & kv = fSections[section]; plKeysAndValues & kv = fSections[section];
if (kv.HasKey(key)) if (kv.HasKey(key))
{ {
std::vector<std::string> values = kv.GetAllValues(key); std::vector<plString> values = kv.GetAllValues(key);
result.insert(result.end(),values.begin(),values.end()); result.insert(result.end(),values.begin(),values.end());
} }
} }
@ -403,7 +393,7 @@ bool plConfigInfo::GetSectionIterators(Sections::const_iterator & iter, Sections
return true; return true;
} }
bool plConfigInfo::GetKeyIterators(const xtl::istring & section, Keys::const_iterator & iter, Keys::const_iterator & end) const bool plConfigInfo::GetKeyIterators(const plString & section, Keys::const_iterator & iter, Keys::const_iterator & end) const
{ {
Sections::const_iterator si = fSections.find(section); Sections::const_iterator si = fSections.find(section);
if (si==fSections.end()) if (si==fSections.end())
@ -411,7 +401,7 @@ bool plConfigInfo::GetKeyIterators(const xtl::istring & section, Keys::const_ite
return fSections[section].GetKeyIterators(iter, end); return fSections[section].GetKeyIterators(iter, end);
} }
bool plConfigInfo::GetValueIterators(const xtl::istring & section, const xtl::istring & key, Values::const_iterator & iter, Values::const_iterator & end) const bool plConfigInfo::GetValueIterators(const plString & section, const plString & key, Values::const_iterator & iter, Values::const_iterator & end) const
{ {
Sections::const_iterator si = fSections.find(section); Sections::const_iterator si = fSections.find(section);
if (si==fSections.end()) if (si==fSections.end())
@ -432,63 +422,62 @@ bool plConfigInfo::WriteTo(plConfigSource * src)
//////////////////////////////////////////////// ////////////////////////////////////////////////
void plConfigSource::SplitAt(std::string & key, std::string & value, char splitter, std::string & in) void plConfigSource::SplitAt(plString & key, plString & value, char splitter, plString & in)
{ {
if(in.length() == 0) if (in.IsEmpty())
return; return;
int t = in.find(splitter); int t = in.Find(splitter);
if(t == std::string::npos) if (t < 0)
{ {
key = in; key = in;
return; return;
} }
key.assign(in.substr(0,t)); key = in.Left(t);
value.assign(in.substr(t+1,in.size()-t-1)); value = in.Substr(t+1,in.GetSize()-t-1);
} }
bool plConfigSource::ReadString(const std::string & in) bool plConfigSource::ReadString(const plString & in)
{ {
std::string work = in; plString work = in.Trim();
xtl::trim(work);
// comment // comment
if (work[0] == '#') if (work.CharAt(0) == '#')
return true; return true;
// comment // comment
if (work[0] == ';') if (work.CharAt(0) == ';')
return true; return true;
// section // section
if (work[0] == '[') if (work.CharAt(0) == '[')
{ {
int close = work.find_first_of("]"); int close = work.Find("]");
if(close == std::string::npos) if (close < 0)
return false; return false;
fCurrSection = work.substr(1,close-1); fCurrSection = work.Substr(1, close-1);
fEffectiveSection = fCurrSection; fEffectiveSection = fCurrSection;
return true; return true;
} }
// key=value // key=value
std::string key, value; plString key, value;
SplitAt(key, value, '=', work); SplitAt(key, value, '=', work);
// dot notation makes section change for this key=value only. // dot notation makes section change for this key=value only.
int t = key.find('.'); int t = key.Find('.');
if (t>0 && t<key.size()-1) if (t>0 && t<key.GetSize()-1)
{ {
fEffectiveSection.assign(key.substr(0,t)); fEffectiveSection = key.Left(t);
key.assign(key.substr(t+1)); key = key.Substr(t+1);
} }
bool ret=ReadPair(key, value); bool ret=ReadPair(key, value);
fEffectiveSection = fCurrSection; fEffectiveSection = fCurrSection;
if(ret && strcmp("LoadIni",key.c_str()) == 0) if(ret && key.Compare("LoadIni") == 0)
{ {
ret = ReadSubSource( value.c_str() ); ret = ReadSubSource( value.c_str() );
} }
@ -496,15 +485,14 @@ bool plConfigSource::ReadString(const std::string & in)
return ret; return ret;
} }
bool plConfigSource::ReadPair(std::string & key, std::string & value) bool plConfigSource::ReadPair(plString & key, plString & value)
{ {
hsAssert(fConfigInfo, "plConfigSource::ProcessPair: fConfigInfo not set."); hsAssert(fConfigInfo, "plConfigSource::ProcessPair: fConfigInfo not set.");
xtl::trim(key); key = key.Trim();
xtl::trim(value); value = value.Trim().Trim("\"'");
xtl::trim(value,"\"'");
if (key.size() == 0) if (key.IsEmpty())
return true; return true;
return fConfigInfo->AddValue(fEffectiveSection, key, value, fAddMode); return fConfigInfo->AddValue(fEffectiveSection, key, value, fAddMode);
@ -542,7 +530,7 @@ bool plConfigSource::WriteOutOf(plConfigInfo & configInfo)
plCmdLineConfigSource::plCmdLineConfigSource(int argc, char ** argv, const char * mySection) plCmdLineConfigSource::plCmdLineConfigSource(int argc, char ** argv, const char * mySection)
: fArgc(argc) : fArgc(argc)
, fArgv(argv) , fArgv(argv)
, fMySection(mySection?mySection:"") , fMySection(mySection)
{} {}
@ -560,7 +548,7 @@ bool plCmdLineConfigSource::ReadInto(plConfigInfo & configInfo, KAddValueMode mo
if(argc < 1) if(argc < 1)
return true; return true;
fConfigInfo->AddValue(fEffectiveSection.c_str(), "ARGV0", *argv, fAddMode); fConfigInfo->AddValue(fEffectiveSection, "ARGV0", *argv, fAddMode);
argc--; argc--;
argv++; argv++;
@ -582,7 +570,7 @@ bool plCmdLineConfigSource::ReadInto(plConfigInfo & configInfo, KAddValueMode mo
plEnvConfigSource::plEnvConfigSource(char ** envp, const char * mySection) plEnvConfigSource::plEnvConfigSource(char ** envp, const char * mySection)
: fEnvp(envp) : fEnvp(envp)
, fMySection(mySection?mySection:"") , fMySection(mySection)
{} {}
@ -617,7 +605,7 @@ bool plIniConfigSource::ReadInto(plConfigInfo & configInfo, KAddValueMode mode)
fCurrSection = plConfigInfo::GlobalSection(); fCurrSection = plConfigInfo::GlobalSection();
fEffectiveSection = fCurrSection; fEffectiveSection = fCurrSection;
if(fFileName.size() < 2) if(fFileName.GetSize() < 2)
return false; return false;
@ -760,33 +748,34 @@ bool plIniStreamConfigSource::WriteOutOf(plConfigInfo & configInfo)
///////////////////////////////////////////////// /////////////////////////////////////////////////
plIniSectionConfigSource::plIniSectionConfigSource(const char * iniFileName, std::vector<std::string> & sections) plIniSectionConfigSource::plIniSectionConfigSource(const char * iniFileName, std::vector<plString> & sections)
: plIniConfigSource(iniFileName) : plIniConfigSource(iniFileName)
{ {
for (int i=0; i<sections.size(); i++) for (int i=0; i<sections.size(); i++)
fSections.push_back(sections[i].c_str()); fSections.push_back(sections[i]);
} }
bool plIniSectionConfigSource::ReadPair(std::string & key, std::string & value) bool plIniSectionConfigSource::ReadPair(plString & key, plString & value)
{ {
hsAssert(fConfigInfo, "plConfigSource::ProcessPair: fConfigInfo not set."); hsAssert(fConfigInfo, "plConfigSource::ProcessPair: fConfigInfo not set.");
// the current section must be in list of sections. // the current section must be in list of sections.
std::vector<xtl::istring>::iterator ii = std::find(fSections.begin(), fSections.end(), fCurrSection.c_str()); Sections::iterator ii = std::find_if(fSections.begin(), fSections.end(),
[this](const plString &v) { return v.CompareI(fCurrSection) == 0; }
);
if (ii==fSections.end()) if (ii==fSections.end())
return true; return true;
xtl::trim(key); key = key.Trim();
xtl::trim(value); value = value.Trim().Trim("\"'");
xtl::trim(value,"\"'");
if (key.size() == 0) if (key.IsEmpty())
return true; return true;
if (key == "section") if (key.CompareI("section") == 0)
fSections.push_back(value.c_str()); fSections.push_back(value);
return fConfigInfo->AddValue(fEffectiveSection, key, value, fAddMode); return fConfigInfo->AddValue(fEffectiveSection, key, value, fAddMode);
} }
@ -794,10 +783,7 @@ bool plIniSectionConfigSource::ReadPair(std::string & key, std::string & value)
bool plIniSectionConfigSource::ReadSubSource( const char * name ) bool plIniSectionConfigSource::ReadSubSource( const char * name )
{ {
std::vector<std::string> sections; plIniSectionConfigSource src(name, fSections);
for ( int i=0; i<fSections.size(); i++ )
sections.push_back( fSections[i].c_str() );
plIniSectionConfigSource src(name, sections);
return fConfigInfo->ReadFrom(&src); return fConfigInfo->ReadFrom(&src);
} }
@ -809,21 +795,20 @@ plIniNoSectionsConfigSource::plIniNoSectionsConfigSource(const char * filename)
fEffectiveSection = fCurrSection = ""; fEffectiveSection = fCurrSection = "";
} }
bool plIniNoSectionsConfigSource::ReadString(const std::string & in) bool plIniNoSectionsConfigSource::ReadString(const plString & in)
{ {
std::string work = in; plString work = in.Trim();
xtl::trim(work);
// ignore comments // ignore comments
if (work[0]=='#' || work[0]==';') if (work.CharAt(0)=='#' || work.CharAt(0)==';')
return true; return true;
// ignore sections // ignore sections
if (work[0] == '[') if (work.CharAt(0) == '[')
return true; return true;
// parse key value // parse key value
std::string key, value; plString key, value;
SplitAt(key, value, '=', work); SplitAt(key, value, '=', work);
return ReadPair(key, value); return ReadPair(key, value);
@ -834,7 +819,7 @@ bool plIniNoSectionsConfigSource::ReadInto(plConfigInfo & configInfo, KAddValueM
if (!plConfigSource::ReadInto(configInfo, mode)) if (!plConfigSource::ReadInto(configInfo, mode))
return false; return false;
if(fFileName.size() < 2) if (fFileName.GetSize() < 2)
return false; return false;
std::ifstream file; std::ifstream file;
@ -944,11 +929,11 @@ void plConfigValueBase::ConfigRead(plConfigInfo * opts)
{ {
if (fReadEvaluate()) if (fReadEvaluate())
{ {
std::string value; plString value;
bool found; bool found;
value = opts->GetValue(GetConfigGroup(),GetConfigName(),"",&found); value = opts->GetValue(GetConfigGroup(),GetConfigName(),"",&found);
if (found) if (found)
SetValue(fReadModify(value).c_str()); SetValue(fReadModify(value));
} }
} }
@ -960,12 +945,12 @@ void plConfigValueBase::ConfigWrite(plConfigInfo * opts)
} }
} }
void plConfigValueBase::SetValue(const char * value) void plConfigValueBase::SetValue(const plString & value)
{ {
ISetValue(fSetModify(value).c_str()); ISetValue(fSetModify(value));
} }
std::string plConfigValueBase::GetValue() const plString plConfigValueBase::GetValue() const
{ {
return fGetModify(IGetValue()); return fGetModify(IGetValue());
} }
@ -1030,7 +1015,7 @@ bool plConfigGroup::Write(plConfigSource * src)
void plConfigGroup::AddItem(plConfigValueBase * item, const char * name) void plConfigGroup::AddItem(plConfigValueBase * item, const char * name)
{ {
item->SetConfigGroup(fGroupName.c_str()); item->SetConfigGroup(fGroupName);
if (name) if (name)
item->SetConfigName(name); item->SetConfigName(name);
fItems.push_back(item); fItems.push_back(item);
@ -1071,29 +1056,28 @@ void plConfigAggregateValue::AddItems(
if (item7) AddItem(item7); if (item7) AddItem(item7);
} }
void plConfigAggregateValue::ISetValue(const char * value) void plConfigAggregateValue::ISetValue(const plString & value)
{ {
std::string work = value; plString work = value.Trim();
int p=0,i=0; int p=0,i=0;
do do
{ {
xtl::trim(work); p = work.Find(" ");
p = work.find(" "); fItems[i]->SetValue(work.Left(p));
fItems[i]->SetValue(work.substr(0,p).c_str()); work = work.Substr(p).TrimLeft();
work.erase(0,p);
i++; i++;
} while (i<fItems.size() && p!=std::string::npos); } while (i<fItems.size() && p>=0);
} }
std::string plConfigAggregateValue::IGetValue() const plString plConfigAggregateValue::IGetValue() const
{ {
std::string value; plStringStream value;
for (int i=0; i<fItems.size(); i++) for (int i=0; i<fItems.size(); i++)
{ {
value.append(fItems[i]->GetValue()); value << fItems[i]->GetValue();
value.append(" "); value << ' ';
} }
return xtl::trim(value); return value.GetString().Trim();
} }
void plConfigAggregateValue::AddItem(plConfigValueBase * item) void plConfigAggregateValue::AddItem(plConfigValueBase * item)
@ -1103,7 +1087,7 @@ void plConfigAggregateValue::AddItem(plConfigValueBase * item)
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
plWWWAuthenticateConfigSource::plWWWAuthenticateConfigSource(const std::string& auth) plWWWAuthenticateConfigSource::plWWWAuthenticateConfigSource(const plString& auth)
: fAuth(auth) : fAuth(auth)
{ {
fEffectiveSection = fCurrSection = ""; fEffectiveSection = fCurrSection = "";
@ -1119,21 +1103,20 @@ bool plWWWAuthenticateConfigSource::ReadInto(plConfigInfo & configInfo, KAddValu
unsigned int i = 0; unsigned int i = 0;
while (i < fAuth.size()) while (i < fAuth.GetSize())
{ {
bool inQuote = false; bool inQuote = false;
unsigned int begin = i,end; unsigned int begin = i,end;
while (i < fAuth.size() while (i < fAuth.GetSize()
&& ((fAuth[i] != ',' && !inQuote) || inQuote)) && ((fAuth.CharAt(i) != ',' && !inQuote) || inQuote))
{ {
if (fAuth[i] == '"') if (fAuth.CharAt(i) == '"')
inQuote = ! inQuote; inQuote = ! inQuote;
i++; i++;
} }
end = i; end = i;
std::string buf; plString buf = fAuth.Substr(begin, end-begin);
buf.assign(fAuth,begin,end-begin);
if(!ReadString(buf.c_str())) if(!ReadString(buf.c_str()))
{ {
// TODO log warning here // TODO log warning here

197
Sources/Plasma/PubUtilLib/plContainer/plConfigInfo.h

@ -48,8 +48,7 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com
///////////////////////////////////////////////// /////////////////////////////////////////////////
typedef std::vector<std::string> plStringList; typedef std::vector<plString> plStringList;
typedef std::vector<std::wstring> plWStringList;
///////////////////////////////////////////////// /////////////////////////////////////////////////
@ -61,10 +60,10 @@ public:
Keys; Keys;
typedef plKeysAndValues::Values typedef plKeysAndValues::Values
Values; Values;
typedef std::map<xtl::istring, plKeysAndValues> typedef std::map<plString, plKeysAndValues, plString::less_i>
Sections; Sections;
static const std::string& GlobalSection(); static const plString& GlobalSection();
private: private:
mutable Sections fSections; mutable Sections fSections;
@ -79,54 +78,54 @@ public:
// remove all sections // remove all sections
void Clear(); void Clear();
// remove section // remove section
void RemoveSection(const std::string & section); void RemoveSection(const plString & section);
// remove key from section // remove key from section
void RemoveKey(const std::string & section, const std::string & key); void RemoveKey(const plString & section, const plString & key);
// QUERY // QUERY
// does this section exist? // does this section exist?
bool HasSection(const std::string & section) const; bool HasSection(const plString & section) const;
// does the given section contain this key? // does the given section contain this key?
bool HasKey(const std::string & section, const std::string & key); bool HasKey(const plString & section, const plString & key);
// does any section contain this key? // does any section contain this key?
bool HasKeyAny(const std::string & key); bool HasKeyAny(const plString & key);
// does any of the given sections contain this key? // does any of the given sections contain this key?
bool HasKeyIn(const std::string & key, const char * section1, ... /*, nil*/); bool HasKeyIn(const plString & key, const char * section1, ... /*, nil*/);
bool HasKeyIn(const std::string & key, const std::vector<std::string> & sections ); bool HasKeyIn(const plString & key, const std::vector<plString> & sections );
// does key in section have this value? // does key in section have this value?
bool KeyHasValue(const std::string & section, const std::string & key, const std::string & value); bool KeyHasValue(const plString & section, const plString & key, const plString & value);
bool KeyHasValue(const std::string & section, const std::string & key, int value); bool KeyHasValue(const plString & section, const plString & key, int value);
bool KeyHasValue(const std::string & section, const std::string & key, double value); bool KeyHasValue(const plString & section, const plString & key, double value);
// ADD // ADD
// add key=value to the section // add key=value to the section
bool AddValue(const std::string & section, const std::string & key, const std::string & value, KAddValueMode mode=kAlwaysAdd); bool AddValue(const plString & section, const plString & key, const plString & value, KAddValueMode mode=kAlwaysAdd);
bool AddValue(const std::string & section, const std::string & key, int value, KAddValueMode mode=kAlwaysAdd); bool AddValue(const plString & section, const plString & key, int value, KAddValueMode mode=kAlwaysAdd);
bool AddValue(const std::string & section, const std::string & key, double value, KAddValueMode mode=kAlwaysAdd); bool AddValue(const plString & section, const plString & key, double value, KAddValueMode mode=kAlwaysAdd);
bool AddValues(const std::string & section, const std::string & key, const std::vector<std::string> & values, KAddValueMode mode=kAlwaysAdd); bool AddValues(const plString & section, const plString & key, const std::vector<plString> & values, KAddValueMode mode=kAlwaysAdd);
// GET // GET
plKeysAndValues GetSection(const std::string & section, bool & found); plKeysAndValues GetSection(const plString & section, bool & found);
std::vector<std::string> GetSectionNames(); std::vector<plString> GetSectionNames();
// get value for key from given section // get value for key from given section
std::string GetValue(const std::string & section, const std::string & key, const std::string & defval="", bool * outFound=nil) const; plString GetValue(const plString & section, const plString & key, const plString & defval="", bool * outFound=nil) const;
int GetValue(const std::string & section, const std::string & key, int defval, bool * outFound=nil) const; int GetValue(const plString & section, const plString & key, int defval, bool * outFound=nil) const;
double GetValue(const std::string & section, const std::string & key, double defval, bool * outFound=nil) const; double GetValue(const plString & section, const plString & key, double defval, bool * outFound=nil) const;
std::vector<std::string> GetAllValues(const std::string & section, const std::string & key) const; std::vector<plString> GetAllValues(const plString & section, const plString & key) const;
// get value for key from any section // get value for key from any section
std::string GetValueAny(const std::string & key, const std::string & defval="", bool * outFound=nil) const; plString GetValueAny(const plString & key, const plString & defval="", bool * outFound=nil) const;
int GetValueAny(const std::string & key, int defval, bool * outFound=nil) const; int GetValueAny(const plString & key, int defval, bool * outFound=nil) const;
double GetValueAny(const std::string & key, double defval, bool * outFound=nil) const; double GetValueAny(const plString & key, double defval, bool * outFound=nil) const;
std::vector<std::string> GetAllValuesAny(const std::string & key) const; std::vector<plString> GetAllValuesAny(const plString & key) const;
// get value for key from one of the given sections // get value for key from one of the given sections
std::string GetValueIn(const std::string & key, const std::string & defval, bool * outFound, const char * section1, ... /*, nil*/) const; plString GetValueIn(const plString & key, const plString & defval, bool * outFound, const char * section1, ... /*, nil*/) const;
std::string GetValueIn(const std::string & key, const std::string & defval, bool * outFound, const std::vector<std::string> & sections ) const; plString GetValueIn(const plString & key, const plString & defval, bool * outFound, const std::vector<plString> & sections ) const;
int GetValueIn(const std::string & key, int defval, bool * outFound, const char * section1, ... /*, nil*/) const; int GetValueIn(const plString & key, int defval, bool * outFound, const char * section1, ... /*, nil*/) const;
int GetValueIn(const std::string & key, int defval, bool * outFound, const std::vector<std::string> & sections ) const; int GetValueIn(const plString & key, int defval, bool * outFound, const std::vector<plString> & sections ) const;
double GetValueIn(const std::string & key, double defval, bool * outFound, const char * section1, ... /*, nil*/) const; double GetValueIn(const plString & key, double defval, bool * outFound, const char * section1, ... /*, nil*/) const;
double GetValueIn(const std::string & key, double defval, bool * outFound, const std::vector<std::string> & sections ) const; double GetValueIn(const plString & key, double defval, bool * outFound, const std::vector<plString> & sections ) const;
std::vector<std::string> GetAllValuesIn(const std::string & key, const char * section1, ... /*, nil*/); std::vector<plString> GetAllValuesIn(const plString & key, const char * section1, ... /*, nil*/);
// ITERATORS // ITERATORS
bool GetSectionIterators(Sections::const_iterator & iter, Sections::const_iterator & end) const; bool GetSectionIterators(Sections::const_iterator & iter, Sections::const_iterator & end) const;
bool GetKeyIterators(const xtl::istring & section, Keys::const_iterator & iter, Keys::const_iterator & end) const; bool GetKeyIterators(const plString & section, Keys::const_iterator & iter, Keys::const_iterator & end) const;
bool GetValueIterators(const xtl::istring & section, const xtl::istring & key, Values::const_iterator & iter, Values::const_iterator & end) const; bool GetValueIterators(const plString & section, const plString & key, Values::const_iterator & iter, Values::const_iterator & end) const;
// CONFIG SOURCE // CONFIG SOURCE
virtual bool ReadFrom(plConfigSource * src, KAddValueMode mode=kAlwaysAdd); virtual bool ReadFrom(plConfigSource * src, KAddValueMode mode=kAlwaysAdd);
virtual bool WriteTo(plConfigSource * src); virtual bool WriteTo(plConfigSource * src);
@ -145,33 +144,33 @@ public:
plConfigInfo* GetConfigInfo() { return &fConfigInfo; } plConfigInfo* GetConfigInfo() { return &fConfigInfo; }
plConfigInfo* GetConfigInfoLog() { return &fLog; } plConfigInfo* GetConfigInfoLog() { return &fLog; }
bool GetValue(std::string& retval, const std::string & section, const std::string & key, const std::string & desc, const std::string& defval = ""); bool GetValue(plString& retval, const plString & section, const plString & key, const plString & desc, const plString& defval = "");
bool GetValue(int& retval, const std::string & section, const std::string & key, const std::string & desc, int defval); bool GetValue(int& retval, const plString & section, const plString & key, const plString & desc, int defval);
bool GetValue(bool& retval, const std::string & section, const std::string & key, const std::string & desc, bool defval); bool GetValue(bool& retval, const plString & section, const plString & key, const plString & desc, bool defval);
bool GetValue(float& retval, const std::string & section, const std::string & key, const std::string & desc, float defval); bool GetValue(float& retval, const plString & section, const plString & key, const plString & desc, float defval);
bool GetValue(double& retval, const std::string & section, const std::string & key, const std::string & desc, double defval); bool GetValue(double& retval, const plString & section, const plString & key, const plString & desc, double defval);
bool GetAllValues(std::vector<std::string>& values, const std::string & section, const std::string & key, const std::string & desc); bool GetAllValues(std::vector<plString>& values, const plString & section, const plString & key, const plString & desc);
#if USE_MULT_SECTIONS #if USE_MULT_SECTIONS
// get value for key from any section // get value for key from any section
bool GetValueAny(std::string& retval, const std::string & key, const std::string & desc, const std::string & defval); bool GetValueAny(plString& retval, const plString & key, const plString & desc, const plString & defval);
bool GetValueAny(int &retval, const std::string & key, const std::string & desc, int defval); bool GetValueAny(int &retval, const plString & key, const plString & desc, int defval);
bool GetValueAny(bool& retval, const std::string & key, const std::string & desc, bool defval); bool GetValueAny(bool& retval, const plString & key, const plString & desc, bool defval);
bool GetValueAny(float& retval, const std::string & key, const std::string & desc, float defval); bool GetValueAny(float& retval, const plString & key, const plString & desc, float defval);
bool GetValueAny(double& retval, const std::string & key, const std::string & desc, double defval); bool GetValueAny(double& retval, const plString & key, const plString & desc, double defval);
bool GetAllValuesAny(std::vector<std::string>& values, const std::string & key, const std::string & desc); bool GetAllValuesAny(std::vector<plString>& values, const plString & key, const plString & desc);
// get value for key from one of the given sections // get value for key from one of the given sections
bool GetValueIn(std::string& retval, const std::string & key, const std::string & desc, const std::string & defval, const char * section1, ... /*, nil*/); bool GetValueIn(plString& retval, const plString & key, const plString & desc, const plString & defval, const char * section1, ... /*, nil*/);
bool GetValueIn(std::string& retval, const std::string & key, const std::string & desc, const std::string & defval, std::vector<std::string> & sections ); bool GetValueIn(plString& retval, const plString & key, const plString & desc, const plString & defval, std::vector<plString> & sections );
bool GetValueIn(int& retval, const std::string & key, const std::string & desc, int defval, const char * section1, ... /*, nil*/); bool GetValueIn(int& retval, const plString & key, const plString & desc, int defval, const char * section1, ... /*, nil*/);
bool GetValueIn(int& retval, const std::string & key, const std::string & desc, int defval, std::vector<std::string> & sections ); bool GetValueIn(int& retval, const plString & key, const plString & desc, int defval, std::vector<plString> & sections );
bool GetValueIn(bool& retval, const std::string & key, const std::string & desc, bool defval, const char * section1, ... /*, nil*/); bool GetValueIn(bool& retval, const plString & key, const plString & desc, bool defval, const char * section1, ... /*, nil*/);
bool GetValueIn(bool& retval, const std::string & key, const std::string & desc, bool defval, std::vector<std::string> & sections ); bool GetValueIn(bool& retval, const plString & key, const plString & desc, bool defval, std::vector<plString> & sections );
bool GetValueIn(float& retval, const std::string & key, const std::string & desc, double defval, const char * section1, ... /*, nil*/); bool GetValueIn(float& retval, const plString & key, const plString & desc, double defval, const char * section1, ... /*, nil*/);
bool GetValueIn(float& retval, const std::string & key, const std::string & desc, double defval, std::vector<std::string> & sections ); bool GetValueIn(float& retval, const plString & key, const plString & desc, double defval, std::vector<plString> & sections );
bool GetValueIn(double& retval, const std::string & key, const std::string & desc, double defval, const char * section1, ... /*, nil*/); bool GetValueIn(double& retval, const plString & key, const plString & desc, double defval, const char * section1, ... /*, nil*/);
bool GetValueIn(double& retval, const std::string & key, const std::string & desc, double defval, std::vector<std::string> & sections ); bool GetValueIn(double& retval, const plString & key, const plString & desc, double defval, std::vector<plString> & sections );
#endif #endif
}; };
@ -180,14 +179,14 @@ public:
class plConfigSource class plConfigSource
{ {
protected: protected:
std::string fCurrSection; // used in parsing plString fCurrSection; // used in parsing
std::string fEffectiveSection; // used in parsing plString fEffectiveSection; // used in parsing
KAddValueMode fAddMode; // used in parsing KAddValueMode fAddMode; // used in parsing
plConfigInfo * fConfigInfo; plConfigInfo * fConfigInfo;
void SplitAt(std::string & key, std::string & value, char splitter, std::string & in); void SplitAt(plString & key, plString & value, char splitter, plString & in);
virtual bool ReadString(const std::string & in); virtual bool ReadString(const plString & in);
virtual bool ReadPair(std::string & key, std::string & value); virtual bool ReadPair(plString & key, plString & value);
virtual bool ReadList(char ** list); virtual bool ReadList(char ** list);
virtual bool ReadSubSource( const char * name ) { return true; } virtual bool ReadSubSource( const char * name ) { return true; }
@ -209,7 +208,7 @@ class plCmdLineConfigSource : public plConfigSource
{ {
int fArgc; int fArgc;
char ** fArgv; char ** fArgv;
std::string fMySection; plString fMySection;
protected: protected:
bool ReadInto(plConfigInfo & configInfo, KAddValueMode mode=kAlwaysAdd); bool ReadInto(plConfigInfo & configInfo, KAddValueMode mode=kAlwaysAdd);
public: public:
@ -222,7 +221,7 @@ public:
class plEnvConfigSource : public plConfigSource class plEnvConfigSource : public plConfigSource
{ {
char ** fEnvp; char ** fEnvp;
std::string fMySection; plString fMySection;
protected: protected:
bool ReadInto(plConfigInfo & configInfo, KAddValueMode mode=kAlwaysAdd); bool ReadInto(plConfigInfo & configInfo, KAddValueMode mode=kAlwaysAdd);
public: public:
@ -235,7 +234,7 @@ public:
class plIniConfigSource : public plConfigSource class plIniConfigSource : public plConfigSource
{ {
protected: protected:
std::string fFileName; plString fFileName;
bool ReadInto(plConfigInfo & configInfo, KAddValueMode mode=kAlwaysAdd); bool ReadInto(plConfigInfo & configInfo, KAddValueMode mode=kAlwaysAdd);
bool WriteOutOf(plConfigInfo & configInfo); bool WriteOutOf(plConfigInfo & configInfo);
public: public:
@ -262,9 +261,9 @@ public:
// data in an unnamed section, or more accurately, in a section named "". // data in an unnamed section, or more accurately, in a section named "".
class plIniNoSectionsConfigSource : public plConfigSource class plIniNoSectionsConfigSource : public plConfigSource
{ {
std::string fFileName; plString fFileName;
protected: protected:
bool ReadString(const std::string & in); bool ReadString(const plString & in);
bool ReadInto(plConfigInfo & configInfo, KAddValueMode mode=kAlwaysAdd); bool ReadInto(plConfigInfo & configInfo, KAddValueMode mode=kAlwaysAdd);
bool WriteOutOf(plConfigInfo & configInfo); bool WriteOutOf(plConfigInfo & configInfo);
public: public:
@ -276,14 +275,14 @@ public:
// an ini file reader that only reads specified sections // an ini file reader that only reads specified sections
class plIniSectionConfigSource : public plIniConfigSource class plIniSectionConfigSource : public plIniConfigSource
{ {
typedef std::vector<xtl::istring> typedef std::vector<plString>
Sections; Sections;
protected: protected:
Sections fSections; Sections fSections;
bool ReadPair(std::string & key, std::string & value); bool ReadPair(plString & key, plString & value);
bool ReadSubSource( const char * name ); bool ReadSubSource( const char * name );
public: public:
plIniSectionConfigSource(const char * iniFileName, std::vector<std::string> & sections); plIniSectionConfigSource(const char * iniFileName, std::vector<plString> & sections);
}; };
///////////////////////////////////////////////// /////////////////////////////////////////////////
@ -304,7 +303,7 @@ public:
class plDebugConfigSource : public plConfigSource class plDebugConfigSource : public plConfigSource
{ {
protected: protected:
std::string fFileName; plString fFileName;
bool WriteOutOf(plConfigInfo & configInfo); bool WriteOutOf(plConfigInfo & configInfo);
public: public:
plDebugConfigSource(){} plDebugConfigSource(){}
@ -314,11 +313,11 @@ public:
class plWWWAuthenticateConfigSource : public plConfigSource class plWWWAuthenticateConfigSource : public plConfigSource
{ {
const std::string& fAuth; const plString& fAuth;
protected: protected:
bool ReadInto(plConfigInfo & configInfo, KAddValueMode mode=kAlwaysAdd); bool ReadInto(plConfigInfo & configInfo, KAddValueMode mode=kAlwaysAdd);
public: public:
plWWWAuthenticateConfigSource(const std::string& auth); plWWWAuthenticateConfigSource(const plString& auth);
}; };
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
@ -363,18 +362,18 @@ struct plEvaluate
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
typedef std::string (plClass::*TModify)(const std::string & value); typedef plString (plClass::*TModify)(const plString & value);
struct plModify struct plModify
{ {
plClass * fTarget; plClass * fTarget;
std::string (plClass::*fModify)(const std::string & value); plString (plClass::*fModify)(const plString & value);
plModify( plClass * target=nil, TModify modify=nil ) plModify( plClass * target=nil, TModify modify=nil )
: fTarget(target) : fTarget(target)
, fModify(modify) , fModify(modify)
{} {}
std::string operator()(const std::string & value) { return (fTarget)?(fTarget->*fModify)(value):value;} plString operator()(const plString & value) { return (fTarget)?(fTarget->*fModify)(value):value;}
std::string operator()(const std::string & value) const { return (fTarget)?(fTarget->*fModify)(value):value;} plString operator()(const plString & value) const { return (fTarget)?(fTarget->*fModify)(value):value;}
}; };
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
@ -382,8 +381,8 @@ struct plModify
class plConfigValueBase class plConfigValueBase
{ {
public: public:
std::string fConfigName; plString fConfigName;
std::string fConfigGroup; plString fConfigGroup;
plEvaluate fReadEvaluate; // returns true if we want to read this value from options plEvaluate fReadEvaluate; // returns true if we want to read this value from options
plEvaluate fWriteEvaluate; // returns true if we want to write this value to options plEvaluate fWriteEvaluate; // returns true if we want to write this value to options
plModify fReadModify; // may modify the value being read from options plModify fReadModify; // may modify the value being read from options
@ -394,18 +393,18 @@ public:
: fConfigName(configName) : fConfigName(configName)
, fConfigGroup(configGroup) , fConfigGroup(configGroup)
{} {}
void SetConfigName(const char * name) { fConfigName=(name)?name:"";} void SetConfigName(const plString & name) { fConfigName=name;}
std::string GetConfigName() const { return fConfigName;} plString GetConfigName() const { return fConfigName;}
void SetConfigGroup(const char * group) { fConfigGroup=group;} void SetConfigGroup(const plString & group) { fConfigGroup=group;}
std::string GetConfigGroup() const { return fConfigGroup;} plString GetConfigGroup() const { return fConfigGroup;}
bool HasConfigName() { return fConfigName.length()>0;} bool HasConfigName() { return !fConfigName.IsEmpty();}
bool HasConfigGroup() { return fConfigGroup.length()>0;} bool HasConfigGroup() { return !fConfigGroup.IsEmpty();}
virtual void ConfigRead(plConfigInfo * opts); virtual void ConfigRead(plConfigInfo * opts);
virtual void ConfigWrite(plConfigInfo * opts); virtual void ConfigWrite(plConfigInfo * opts);
void SetValue(const char * value); void SetValue(const plString & value);
std::string GetValue() const; plString GetValue() const;
virtual void ISetValue(const char * value) = 0; virtual void ISetValue(const plString & value) = 0;
virtual std::string IGetValue() const = 0; virtual plString IGetValue() const = 0;
void SetReadEvaluate(plClass * targetObj, TEvaluate evalFunc); void SetReadEvaluate(plClass * targetObj, TEvaluate evalFunc);
void SetWriteEvaluate(plClass * targetObj, TEvaluate evalFunc); void SetWriteEvaluate(plClass * targetObj, TEvaluate evalFunc);
@ -424,9 +423,9 @@ public:
plConfigValue( const char * configName="", const char * configGroup="" ) plConfigValue( const char * configName="", const char * configGroup="" )
: plConfigValueBase(configName, configGroup) : plConfigValueBase(configName, configGroup)
{} {}
std::string fConfigValue; plString fConfigValue;
void ISetValue(const char * value) { fConfigValue=value;} void ISetValue(const plString & value) { fConfigValue=value;}
std::string IGetValue() const { return fConfigValue;} plString IGetValue() const { return fConfigValue;}
}; };
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
@ -451,8 +450,8 @@ public:
plConfigValueBase * item5=nil, plConfigValueBase * item5=nil,
plConfigValueBase * item6=nil, plConfigValueBase * item6=nil,
plConfigValueBase * item7=nil); plConfigValueBase * item7=nil);
void ISetValue(const char * value); void ISetValue(const plString & value);
std::string IGetValue() const; plString IGetValue() const;
void AddItem(plConfigValueBase * item); void AddItem(plConfigValueBase * item);
void AddItems( void AddItems(
plConfigValueBase * item1=nil, plConfigValueBase * item1=nil,
@ -474,8 +473,8 @@ public:
: fConfigurable(item) : fConfigurable(item)
{} {}
void Set(plConfigValueBase * item) { fConfigurable=item;} void Set(plConfigValueBase * item) { fConfigurable=item;}
void ISetValue(const char * value) { fConfigurable->ISetValue(value);} void ISetValue(const plString & value) { fConfigurable->ISetValue(value);}
std::string IGetValue() const { return fConfigurable->IGetValue();} plString IGetValue() const { return fConfigurable->IGetValue();}
}; };
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
@ -484,7 +483,7 @@ class plConfigGroup
{ {
public: public:
plConfigInfo fOpts; plConfigInfo fOpts;
std::string fGroupName; plString fGroupName;
std::vector<plConfigValueBase*> fItems; std::vector<plConfigValueBase*> fItems;
plConfigGroup(const char * groupName=""); plConfigGroup(const char * groupName="");
bool Read(plConfigSource * src); bool Read(plConfigSource * src);

82
Sources/Plasma/PubUtilLib/plContainer/plConfigInfoLogging.cpp

@ -49,10 +49,9 @@ plConfigInfoLogging::~plConfigInfoLogging()
{ {
} }
bool plConfigInfoLogging::GetValue(std::string& retval, const std::string & section, const std::string & key, const std::string & desc, const std::string& defval) bool plConfigInfoLogging::GetValue(plString& retval, const plString & section, const plString & key, const plString & desc, const plString& defval)
{ {
std::string descwdef; plString descwdef = plString::Format("%s # %s",defval.c_str(),desc.c_str());
xtl::format(descwdef,"%s # %s",defval.c_str(),desc.c_str());
fLog.AddValue(section,key,descwdef,kReplaceIfExists); fLog.AddValue(section,key,descwdef,kReplaceIfExists);
bool found; bool found;
@ -60,10 +59,9 @@ bool plConfigInfoLogging::GetValue(std::string& retval, const std::string & sect
return found; return found;
} }
bool plConfigInfoLogging::GetValue(int& retval, const std::string & section, const std::string & key, const std::string & desc, int defval) bool plConfigInfoLogging::GetValue(int& retval, const plString & section, const plString & key, const plString & desc, int defval)
{ {
std::string descwdef; plString descwdef = plString::Format("%d # %s",defval,desc.c_str());
xtl::format(descwdef,"%d # %s",defval,desc.c_str());
fLog.AddValue(section,key,descwdef,kReplaceIfExists); fLog.AddValue(section,key,descwdef,kReplaceIfExists);
bool found; bool found;
@ -71,10 +69,9 @@ bool plConfigInfoLogging::GetValue(int& retval, const std::string & section, con
return found; return found;
} }
bool plConfigInfoLogging::GetValue(bool& retval, const std::string & section, const std::string & key, const std::string & desc, bool defval) bool plConfigInfoLogging::GetValue(bool& retval, const plString & section, const plString & key, const plString & desc, bool defval)
{ {
std::string descwdef; plString descwdef = plString::Format("%d # %s",defval,desc.c_str());
xtl::format(descwdef,"%d # %s",defval,desc.c_str());
fLog.AddValue(section,key,descwdef,kReplaceIfExists); fLog.AddValue(section,key,descwdef,kReplaceIfExists);
bool found; bool found;
@ -82,10 +79,9 @@ bool plConfigInfoLogging::GetValue(bool& retval, const std::string & section, co
return found; return found;
} }
bool plConfigInfoLogging::GetValue(float& retval, const std::string & section, const std::string & key, const std::string & desc, float defval) bool plConfigInfoLogging::GetValue(float& retval, const plString & section, const plString & key, const plString & desc, float defval)
{ {
std::string descwdef; plString descwdef = plString::Format("%f # %s",defval,desc.c_str());
xtl::format(descwdef,"%f # %s",defval,desc.c_str());
fLog.AddValue(section,key,descwdef,kReplaceIfExists); fLog.AddValue(section,key,descwdef,kReplaceIfExists);
bool found; bool found;
@ -94,10 +90,9 @@ bool plConfigInfoLogging::GetValue(float& retval, const std::string & section, c
return found; return found;
} }
bool plConfigInfoLogging::GetValue(double& retval, const std::string & section, const std::string & key, const std::string & desc, double defval) bool plConfigInfoLogging::GetValue(double& retval, const plString & section, const plString & key, const plString & desc, double defval)
{ {
std::string descwdef; plString descwdef = plString::Format("%f # %s",defval,desc.c_str());
xtl::format(descwdef,"%f # %s",defval,desc.c_str());
fLog.AddValue(section,key,descwdef,kReplaceIfExists); fLog.AddValue(section,key,descwdef,kReplaceIfExists);
bool found; bool found;
@ -105,10 +100,9 @@ bool plConfigInfoLogging::GetValue(double& retval, const std::string & section,
return found; return found;
} }
bool plConfigInfoLogging::GetAllValues(std::vector<std::string>& values, const std::string & section, const std::string & key, const std::string & desc) bool plConfigInfoLogging::GetAllValues(std::vector<plString>& values, const plString & section, const plString & key, const plString & desc)
{ {
std::string descwdef; plString descwdef = plString::Format("%s # %s","\"Multiple Entries\"",desc.c_str());
xtl::format(descwdef,"%s # %s","\"Multiple Entries\"",desc.c_str());
fLog.AddValue(section,key,descwdef,kReplaceIfExists); fLog.AddValue(section,key,descwdef,kReplaceIfExists);
values = fConfigInfo.GetAllValues(section,key); values = fConfigInfo.GetAllValues(section,key);
@ -117,7 +111,7 @@ bool plConfigInfoLogging::GetAllValues(std::vector<std::string>& values, const s
#if USE_MULT_SECTIONS #if USE_MULT_SECTIONS
bool plConfigInfoLogging::GetValueAny(std::string& retval, const std::string & key, const std::string & desc, const std::string & defval) bool plConfigInfoLogging::GetValueAny(plString& retval, const plString & key, const plString & desc, const plString & defval)
{ {
fLog.AddValue("ANY SECTION",key,desc,kReplaceIfExists); fLog.AddValue("ANY SECTION",key,desc,kReplaceIfExists);
@ -126,7 +120,7 @@ bool plConfigInfoLogging::GetValueAny(std::string& retval, const std::string & k
return found; return found;
} }
bool plConfigInfoLogging::GetValueAny(int &retval, const std::string & key, const std::string & desc, int defval) bool plConfigInfoLogging::GetValueAny(int &retval, const plString & key, const plString & desc, int defval)
{ {
fLog.AddValue("ANY SECTION",key,desc,kReplaceIfExists); fLog.AddValue("ANY SECTION",key,desc,kReplaceIfExists);
@ -135,7 +129,7 @@ bool plConfigInfoLogging::GetValueAny(int &retval, const std::string & key, cons
return found; return found;
} }
bool plConfigInfoLogging::GetValueAny(bool &retval, const std::string & key, const std::string & desc, bool defval) bool plConfigInfoLogging::GetValueAny(bool &retval, const plString & key, const plString & desc, bool defval)
{ {
fLog.AddValue("ANY SECTION",key,desc,kReplaceIfExists); fLog.AddValue("ANY SECTION",key,desc,kReplaceIfExists);
@ -144,7 +138,7 @@ bool plConfigInfoLogging::GetValueAny(bool &retval, const std::string & key, con
return found; return found;
} }
bool plConfigInfoLogging::GetValueAny(float& retval, const std::string & key, const std::string & desc, float defval) bool plConfigInfoLogging::GetValueAny(float& retval, const plString & key, const plString & desc, float defval)
{ {
fLog.AddValue("ANY SECTION",key,desc,kReplaceIfExists); fLog.AddValue("ANY SECTION",key,desc,kReplaceIfExists);
@ -153,7 +147,7 @@ bool plConfigInfoLogging::GetValueAny(float& retval, const std::string & key, co
return found; return found;
} }
bool plConfigInfoLogging::GetValueAny(double& retval, const std::string & key, const std::string & desc, double defval) bool plConfigInfoLogging::GetValueAny(double& retval, const plString & key, const plString & desc, double defval)
{ {
fLog.AddValue("ANY SECTION",key,desc,kReplaceIfExists); fLog.AddValue("ANY SECTION",key,desc,kReplaceIfExists);
@ -162,7 +156,7 @@ bool plConfigInfoLogging::GetValueAny(double& retval, const std::string & key, c
return found; return found;
} }
bool plConfigInfoLogging::GetAllValuesAny(std::vector<std::string>& values, const std::string & key, const std::string & desc) bool plConfigInfoLogging::GetAllValuesAny(std::vector<plString>& values, const plString & key, const plString & desc)
{ {
fLog.AddValue("ANY SECTION",key,desc,kReplaceIfExists); fLog.AddValue("ANY SECTION",key,desc,kReplaceIfExists);
@ -170,12 +164,12 @@ bool plConfigInfoLogging::GetAllValuesAny(std::vector<std::string>& values, cons
return values.size() != 0; return values.size() != 0;
} }
bool plConfigInfoLogging::GetValueIn(std::string& retval, const std::string & key, const std::string & desc, const std::string & defval, const char * section1, ... /*, nil*/) bool plConfigInfoLogging::GetValueIn(plString& retval, const plString & key, const plString & desc, const plString & defval, const char * section1, ... /*, nil*/)
{ {
const char * section = section1; const char * section = section1;
va_list va; va_list va;
va_start(va,section1); va_start(va,section1);
std::vector<std::string> sections; std::vector<plString> sections;
while (section) while (section)
{ {
sections.push_back( section ); sections.push_back( section );
@ -186,9 +180,9 @@ bool plConfigInfoLogging::GetValueIn(std::string& retval, const std::string & ke
return GetValueIn(retval,key,desc,defval,sections); return GetValueIn(retval,key,desc,defval,sections);
} }
bool plConfigInfoLogging::GetValueIn(std::string& retval, const std::string & key, const std::string & desc, const std::string & defval, std::vector<std::string> & sections ) bool plConfigInfoLogging::GetValueIn(plString& retval, const plString & key, const plString & desc, const plString & defval, std::vector<plString> & sections )
{ {
std::vector<std::string>::iterator si = sections.begin(); std::vector<plString>::iterator si = sections.begin();
while (si != sections.end()) while (si != sections.end())
{ {
fLog.AddValue(*si,key,desc,kReplaceIfExists); fLog.AddValue(*si,key,desc,kReplaceIfExists);
@ -200,12 +194,12 @@ bool plConfigInfoLogging::GetValueIn(std::string& retval, const std::string & ke
return found; return found;
} }
bool plConfigInfoLogging::GetValueIn(int& retval, const std::string & key, const std::string & desc, int defval, const char * section1, ... /*, nil*/) bool plConfigInfoLogging::GetValueIn(int& retval, const plString & key, const plString & desc, int defval, const char * section1, ... /*, nil*/)
{ {
const char * section = section1; const char * section = section1;
va_list va; va_list va;
va_start(va,section1); va_start(va,section1);
std::vector<std::string> sections; std::vector<plString> sections;
while (section) while (section)
{ {
sections.push_back( section ); sections.push_back( section );
@ -216,9 +210,9 @@ bool plConfigInfoLogging::GetValueIn(int& retval, const std::string & key, cons
return GetValueIn(retval,key,desc,defval,sections); return GetValueIn(retval,key,desc,defval,sections);
} }
bool plConfigInfoLogging::GetValueIn(int& retval, const std::string & key, const std::string & desc, int defval, std::vector<std::string> & sections ) bool plConfigInfoLogging::GetValueIn(int& retval, const plString & key, const plString & desc, int defval, std::vector<plString> & sections )
{ {
std::vector<std::string>::iterator si = sections.begin(); std::vector<plString>::iterator si = sections.begin();
while (si != sections.end()) while (si != sections.end())
{ {
fLog.AddValue(*si,key,desc,kReplaceIfExists); fLog.AddValue(*si,key,desc,kReplaceIfExists);
@ -230,12 +224,12 @@ bool plConfigInfoLogging::GetValueIn(int& retval, const std::string & key, cons
return found; return found;
} }
bool plConfigInfoLogging::GetValueIn(bool& retval, const std::string & key, const std::string & desc, bool defval, const char * section1, ... /*, nil*/) bool plConfigInfoLogging::GetValueIn(bool& retval, const plString & key, const plString & desc, bool defval, const char * section1, ... /*, nil*/)
{ {
const char * section = section1; const char * section = section1;
va_list va; va_list va;
va_start(va,section1); va_start(va,section1);
std::vector<std::string> sections; std::vector<plString> sections;
while (section) while (section)
{ {
sections.push_back( section ); sections.push_back( section );
@ -246,9 +240,9 @@ bool plConfigInfoLogging::GetValueIn(bool& retval, const std::string & key, con
return GetValueIn(retval,key,desc,defval,sections); return GetValueIn(retval,key,desc,defval,sections);
} }
bool plConfigInfoLogging::GetValueIn(bool& retval, const std::string & key, const std::string & desc, bool defval, std::vector<std::string> & sections ) bool plConfigInfoLogging::GetValueIn(bool& retval, const plString & key, const plString & desc, bool defval, std::vector<plString> & sections )
{ {
std::vector<std::string>::iterator si = sections.begin(); std::vector<plString>::iterator si = sections.begin();
while (si != sections.end()) while (si != sections.end())
{ {
fLog.AddValue(*si,key,desc,kReplaceIfExists); fLog.AddValue(*si,key,desc,kReplaceIfExists);
@ -260,12 +254,12 @@ bool plConfigInfoLogging::GetValueIn(bool& retval, const std::string & key, con
return found; return found;
} }
bool plConfigInfoLogging::GetValueIn(float& retval, const std::string & key, const std::string & desc, double defval, const char * section1, ... /*, nil*/) bool plConfigInfoLogging::GetValueIn(float& retval, const plString & key, const plString & desc, double defval, const char * section1, ... /*, nil*/)
{ {
const char * section = section1; const char * section = section1;
va_list va; va_list va;
va_start(va,section1); va_start(va,section1);
std::vector<std::string> sections; std::vector<plString> sections;
while (section) while (section)
{ {
sections.push_back( section ); sections.push_back( section );
@ -276,9 +270,9 @@ bool plConfigInfoLogging::GetValueIn(float& retval, const std::string & key, co
return GetValueIn(retval,key,desc,defval,sections); return GetValueIn(retval,key,desc,defval,sections);
} }
bool plConfigInfoLogging::GetValueIn(float& retval, const std::string & key, const std::string & desc, double defval, std::vector<std::string> & sections ) bool plConfigInfoLogging::GetValueIn(float& retval, const plString & key, const plString & desc, double defval, std::vector<plString> & sections )
{ {
std::vector<std::string>::iterator si = sections.begin(); std::vector<plString>::iterator si = sections.begin();
while (si != sections.end()) while (si != sections.end())
{ {
fLog.AddValue(*si,key,desc,kReplaceIfExists); fLog.AddValue(*si,key,desc,kReplaceIfExists);
@ -290,12 +284,12 @@ bool plConfigInfoLogging::GetValueIn(float& retval, const std::string & key, co
return found; return found;
} }
bool plConfigInfoLogging::GetValueIn(double& retval, const std::string & key, const std::string & desc, double defval, const char * section1, ... /*, nil*/) bool plConfigInfoLogging::GetValueIn(double& retval, const plString & key, const plString & desc, double defval, const char * section1, ... /*, nil*/)
{ {
const char * section = section1; const char * section = section1;
va_list va; va_list va;
va_start(va,section1); va_start(va,section1);
std::vector<std::string> sections; std::vector<plString> sections;
while (section) while (section)
{ {
sections.push_back( section ); sections.push_back( section );
@ -306,9 +300,9 @@ bool plConfigInfoLogging::GetValueIn(double& retval, const std::string & key, c
return GetValueIn(retval,key,desc,defval,sections); return GetValueIn(retval,key,desc,defval,sections);
} }
bool plConfigInfoLogging::GetValueIn(double& retval, const std::string & key, const std::string & desc, double defval, std::vector<std::string> & sections ) bool plConfigInfoLogging::GetValueIn(double& retval, const plString & key, const plString & desc, double defval, std::vector<plString> & sections )
{ {
std::vector<std::string>::iterator si = sections.begin(); std::vector<plString>::iterator si = sections.begin();
while (si != sections.end()) while (si != sections.end())
{ {
fLog.AddValue(*si,key,desc,kReplaceIfExists); fLog.AddValue(*si,key,desc,kReplaceIfExists);

132
Sources/Plasma/PubUtilLib/plContainer/plKeysAndValues.cpp

@ -65,41 +65,37 @@ void plKeysAndValues::Clear()
fKeys.clear(); fKeys.clear();
} }
void plKeysAndValues::RemoveKey(const std::string & key) void plKeysAndValues::RemoveKey(const plString & key)
{ {
fKeys.erase(key.c_str()); fKeys.erase(key);
} }
bool plKeysAndValues::HasKey(const std::string & key) const bool plKeysAndValues::HasKey(const plString & key) const
{ {
return (fKeys.find(key.c_str()) != fKeys.end()); return (fKeys.find(key) != fKeys.end());
} }
bool plKeysAndValues::KeyHasValue(const std::string & key, const std::string & value) bool plKeysAndValues::KeyHasValue(const plString & key, const plString & value)
{ {
Keys::const_iterator ki = fKeys.find(key.c_str()); Keys::const_iterator ki = fKeys.find(key);
if (ki==fKeys.end()) if (ki==fKeys.end())
return false; return false;
return std::find(ki->second.begin(),ki->second.end(), value.c_str()) != ki->second.end(); return std::find_if(ki->second.begin(), ki->second.end(),
[&value](const plString &v) { return v.CompareI(value) == 0; }
) != ki->second.end();
} }
bool plKeysAndValues::KeyHasValue(const std::string & key, int value) bool plKeysAndValues::KeyHasValue(const plString & key, int value)
{ {
char buf[20]; return KeyHasValue(key, plString::Format("%d", value));
sprintf(buf, "%d", value);
std::string v(buf);
return KeyHasValue(key, v);
} }
bool plKeysAndValues::KeyHasValue(const std::string & key, double value) bool plKeysAndValues::KeyHasValue(const plString & key, double value)
{ {
char buf[30]; return KeyHasValue(key, plString::Format("%f", value));
sprintf(buf, "%f", value);
std::string v(buf);
return KeyHasValue(key, v);
} }
bool plKeysAndValues::AddValue(const std::string & key, const std::string & value, KAddValueMode mode) bool plKeysAndValues::AddValue(const plString & key, const plString & value, KAddValueMode mode)
{ {
switch (mode) switch (mode)
{ {
@ -114,97 +110,75 @@ bool plKeysAndValues::AddValue(const std::string & key, const std::string & valu
default: default:
break; break;
} }
fKeys[key.c_str()].push_front(value.c_str()); fKeys[key].push_front(value);
return true; return true;
} }
bool plKeysAndValues::AddValue(const std::string & key, int value, KAddValueMode mode) bool plKeysAndValues::AddValue(const plString & key, int value, KAddValueMode mode)
{ {
char buf[20]; return AddValue(key, plString::Format("%d", value), mode);
sprintf(buf, "%d", value);
std::string v(buf);
return AddValue(key,v,mode);
} }
bool plKeysAndValues::AddValue(const std::string & key, double value, KAddValueMode mode) bool plKeysAndValues::AddValue(const plString & key, double value, KAddValueMode mode)
{ {
char buf[30]; return AddValue(key, plString::Format("%f", value), mode);
sprintf(buf, "%f", value);
std::string v(buf);
return AddValue(key,v,mode);
} }
bool plKeysAndValues::AddValues(const std::string & key, const std::vector<std::string> & values, KAddValueMode mode) bool plKeysAndValues::AddValues(const plString & key, const std::vector<plString> & values, KAddValueMode mode)
{ {
for (int i=0; i<values.size(); i++) for (int i=0; i<values.size(); i++)
AddValue(key,values[i],mode); AddValue(key,values[i],mode);
return true; return true;
} }
bool plKeysAndValues::SetValue(const std::string & key, const std::string & value) bool plKeysAndValues::SetValue(const plString & key, const plString & value)
{ {
fKeys[key.c_str()].clear(); fKeys[key].clear();
return AddValue(key,value); return AddValue(key,value);
} }
bool plKeysAndValues::SetValue(const std::string & key, int value) bool plKeysAndValues::SetValue(const plString & key, int value)
{ {
char buf[20]; return SetValue(key, plString::Format("%d", value));
sprintf(buf, "%d", value);
std::string v(buf);
return SetValue(key, v);
} }
bool plKeysAndValues::SetValue(const std::string & key, double value) bool plKeysAndValues::SetValue(const plString & key, double value)
{ {
char buf[30]; return SetValue(key, plString::Format("%f", value));
sprintf(buf, "%f", value);
std::string v(buf);
return SetValue(key, v);
} }
std::string plKeysAndValues::GetValue(const std::string & key, const std::string & defval, bool * outFound) const plString plKeysAndValues::GetValue(const plString & key, const plString & defval, bool * outFound) const
{ {
Keys::const_iterator ki = fKeys.find(key.c_str()); Keys::const_iterator ki = fKeys.find(key);
if (outFound) if (outFound)
*outFound = (ki!=fKeys.end()); *outFound = (ki!=fKeys.end());
if(ki != fKeys.end()) if(ki != fKeys.end())
return ki->second.front().c_str(); return ki->second.front();
// fKeys[key.c_str()].push_front(defval.c_str()); // fKeys[key].push_front(defval);
return defval; return defval;
} }
uint32_t plKeysAndValues::GetValue(const std::string & key, uint32_t defval, bool * outFound) const uint32_t plKeysAndValues::GetValue(const plString & key, uint32_t defval, bool * outFound) const
{ {
char buf[20]; return strtoul(GetValue(key, plString::Format("%ul", defval), outFound).c_str(), nil, 0);
sprintf(buf, "%ul", defval);
std::string v(buf);
return strtoul(GetValue(key,v,outFound).c_str(), nil, 0);
} }
int plKeysAndValues::GetValue(const std::string & key, int defval, bool * outFound) const int plKeysAndValues::GetValue(const plString & key, int defval, bool * outFound) const
{ {
char buf[20]; return atol(GetValue(key, plString::Format("%d", defval), outFound).c_str());
sprintf(buf, "%d", defval);
std::string v(buf);
return atol(GetValue(key,v,outFound).c_str());
} }
double plKeysAndValues::GetValue(const std::string & key, double defval, bool * outFound) const double plKeysAndValues::GetValue(const plString & key, double defval, bool * outFound) const
{ {
char buf[30]; return atof(GetValue(key, plString::Format("%f", defval), outFound).c_str());
sprintf(buf, "%f", defval);
std::string v(buf);
return atof(GetValue(key,v,outFound).c_str());
} }
std::vector<std::string> plKeysAndValues::GetAllValues(const std::string & key) std::vector<plString> plKeysAndValues::GetAllValues(const plString & key)
{ {
std::vector<std::string> result; std::vector<plString> result;
xtl::istring xkey = key.c_str();
if (HasKey(key)) if (HasKey(key))
for (Values::const_iterator vi=fKeys[xkey].begin(); vi!=fKeys[xkey].end(); ++vi) for (Values::const_iterator vi=fKeys[key].begin(); vi!=fKeys[key].end(); ++vi)
result.push_back(vi->c_str()); result.push_back(*vi);
return result; return result;
} }
@ -215,7 +189,7 @@ bool plKeysAndValues::GetKeyIterators(Keys::const_iterator & iter, Keys::const_i
return true; return true;
} }
bool plKeysAndValues::GetValueIterators(const xtl::istring & key, Values::const_iterator & iter, Values::const_iterator & end) const bool plKeysAndValues::GetValueIterators(const plString & key, Values::const_iterator & iter, Values::const_iterator & end) const
{ {
Keys::const_iterator ki = fKeys.find(key); Keys::const_iterator ki = fKeys.find(key);
if(ki != fKeys.end()) if(ki != fKeys.end())
@ -235,19 +209,19 @@ void plKeysAndValues::Read(hsStream * s)
{ {
uint16_t strlen; uint16_t strlen;
s->ReadLE(&strlen); s->ReadLE(&strlen);
std::string key; plStringBuffer<char> key;
key.assign(strlen+1,'\0'); char* kdata = key.CreateWritableBuffer(strlen);
s->Read(strlen,(void*)key.data()); s->Read(strlen,(void*)kdata);
key.resize(strlen); kdata[strlen] = 0;
uint16_t nvalues; uint16_t nvalues;
s->ReadLE(&nvalues); s->ReadLE(&nvalues);
for (int vi=0; vi<nvalues; vi++) for (int vi=0; vi<nvalues; vi++)
{ {
s->ReadLE(&strlen); s->ReadLE(&strlen);
std::string value; plStringBuffer<char> value;
value.assign(strlen+1,'\0'); char* vdata = value.CreateWritableBuffer(strlen);
s->Read(strlen,(void*)value.data()); s->Read(strlen,(void*)vdata);
value.resize(strlen); vdata[strlen] = 0;
// for now, only single value for key on stream is allowed. // for now, only single value for key on stream is allowed.
SetValue(key,value); SetValue(key,value);
} }
@ -264,8 +238,8 @@ void plKeysAndValues::Write(hsStream * s)
for (;ki!=ke;++ki) for (;ki!=ke;++ki)
{ {
// write key string // write key string
s->WriteLE((uint16_t)ki->first.size()); s->WriteLE((uint16_t)ki->first.GetSize());
s->Write(ki->first.size(),ki->first.c_str()); s->Write(ki->first.GetSize(),ki->first.c_str());
// write nvalues for this key // write nvalues for this key
s->WriteLE((uint16_t)ki->second.size()); s->WriteLE((uint16_t)ki->second.size());
// iterate through values for this key // iterate through values for this key
@ -274,8 +248,8 @@ void plKeysAndValues::Write(hsStream * s)
for (;vi!=ve;++vi) for (;vi!=ve;++vi)
{ {
// write value string // write value string
s->WriteLE((uint16_t)vi->size()); s->WriteLE((uint16_t)vi->GetSize());
s->Write(vi->size(),vi->c_str()); s->Write(vi->GetSize(),vi->c_str());
} }
} }
} }

40
Sources/Plasma/PubUtilLib/plContainer/plKeysAndValues.h

@ -62,9 +62,9 @@ enum KAddValueMode
class plKeysAndValues : public hsStreamable class plKeysAndValues : public hsStreamable
{ {
public: public:
typedef std::list<xtl::istring> typedef std::list<plString>
Values; Values;
typedef std::map<xtl::istring, Values> typedef std::map<plString, Values, plString::less_i>
Keys; Keys;
private: private:
@ -79,31 +79,31 @@ public:
plKeysAndValues & operator =(const plKeysAndValues & src); plKeysAndValues & operator =(const plKeysAndValues & src);
// clear // clear
void Clear(); void Clear();
void RemoveKey(const std::string & key); void RemoveKey(const plString & key);
// query // query
bool HasKey(const std::string & key) const; bool HasKey(const plString & key) const;
bool KeyHasValue(const std::string & key, const std::string & value); bool KeyHasValue(const plString & key, const plString & value);
bool KeyHasValue(const std::string & key, int value); bool KeyHasValue(const plString & key, int value);
bool KeyHasValue(const std::string & key, double value); bool KeyHasValue(const plString & key, double value);
// add // add
bool AddValue(const std::string & key, const std::string & value, KAddValueMode mode=kAlwaysAdd); bool AddValue(const plString & key, const plString & value, KAddValueMode mode=kAlwaysAdd);
bool AddValue(const std::string & key, int value, KAddValueMode mode=kAlwaysAdd); bool AddValue(const plString & key, int value, KAddValueMode mode=kAlwaysAdd);
bool AddValue(const std::string & key, double value, KAddValueMode mode=kAlwaysAdd); bool AddValue(const plString & key, double value, KAddValueMode mode=kAlwaysAdd);
bool AddValues(const std::string & key, const std::vector<std::string> & values, KAddValueMode mode=kAlwaysAdd); bool AddValues(const plString & key, const std::vector<plString> & values, KAddValueMode mode=kAlwaysAdd);
// set (clear and add) // set (clear and add)
bool SetValue(const std::string & key, const std::string & value); bool SetValue(const plString & key, const plString & value);
bool SetValue(const std::string & key, int value); bool SetValue(const plString & key, int value);
bool SetValue(const std::string & key, double value); bool SetValue(const plString & key, double value);
// get single value // get single value
std::string GetValue(const std::string & key, const std::string & defval="", bool * outFound=nil) const; plString GetValue(const plString & key, const plString & defval="", bool * outFound=nil) const;
uint32_t GetValue(const std::string & key, uint32_t defval, bool * outFound=nil) const; uint32_t GetValue(const plString & key, uint32_t defval, bool * outFound=nil) const;
int GetValue(const std::string & key, int defval, bool * outFound=nil) const; int GetValue(const plString & key, int defval, bool * outFound=nil) const;
double GetValue(const std::string & key, double defval, bool * outFound=nil) const; double GetValue(const plString & key, double defval, bool * outFound=nil) const;
std::vector<std::string> GetAllValues(const std::string & key); std::vector<plString> GetAllValues(const plString & key);
// key iterator // key iterator
bool GetKeyIterators(Keys::const_iterator & iter, Keys::const_iterator & end) const; bool GetKeyIterators(Keys::const_iterator & iter, Keys::const_iterator & end) const;
// value iterator (use for getting all values for key) // value iterator (use for getting all values for key)
bool GetValueIterators(const xtl::istring & key, Values::const_iterator & iter, Values::const_iterator & end) const; bool GetValueIterators(const plString & key, Values::const_iterator & iter, Values::const_iterator & end) const;
// streamable // streamable
void Read(hsStream * s); void Read(hsStream * s);
void Write(hsStream * s); void Write(hsStream * s);

2
Sources/Plasma/PubUtilLib/plNetClient/plNetClientMgrSend.cpp

@ -159,7 +159,7 @@ void plNetClientMgr::ISendCCRPetition(plCCRPetitionMsg* petMsg)
char buffy[20]; char buffy[20];
sprintf( buffy, "%lu", GetPlayerID() ); sprintf( buffy, "%lu", GetPlayerID() );
info.AddValue( "Petition", "PlayerID", buffy ); info.AddValue( "Petition", "PlayerID", buffy );
info.AddValue( "Petition", "PlayerName", GetPlayerName().c_str() ); info.AddValue( "Petition", "PlayerName", GetPlayerName() );
// write config info formatted like an ini file to a buffer // write config info formatted like an ini file to a buffer
hsRAMStream ram; hsRAMStream ram;

Loading…
Cancel
Save