diff -r 7a96bd401351 -r e3a85616dbc5 settings.cpp --- a/settings.cpp Sun Jan 30 12:58:47 2005 +0000 +++ b/settings.cpp Wed Jul 22 11:47:03 2009 +0000 @@ -1,8 +1,153 @@ #include +#include #include "settings.h" -#include "misc.h" +#include "file.h" -using namespace std; +///////////////////////////////////////////////////////////////// +// SimpleSettings +///////////////////////////////////////////////////////////////// +SimpleSettings::SimpleSettings() +{ + clear(); +} + +SimpleSettings::~SimpleSettings() +{ +} + +void SimpleSettings::clear() +{ + keylist.clear(); + valuelist.clear(); +} + +void SimpleSettings::readSettings (const QString &path) +{ + QString s; + if (!loadStringFromDisk(path,s)) + { + qWarning ("SimpleSettings::readSettings() Couldn't read "+path); + return; + } + QStringList lines; + lines=QStringList::split (QRegExp("\n"),s,false); + int i; + QStringList::Iterator it=lines.begin(); + while (it !=lines.end() ) + { + i=(*it).find("=",0); + keylist.append((*it).left(i)); + valuelist.append((*it).right((*it).length()-i-1)); + it++; + } +} + +void SimpleSettings::writeSettings (const QString &path) +{ + QString s; + QStringList::Iterator itk=keylist.begin(); + QStringList::Iterator itv=valuelist.begin(); + + // First search for value in settings saved in map + while (itk !=keylist.end() ) + { + s+=*itk+"="+*itv+"\n"; + itk++; + itv++; + } + if (!saveStringToDisk(path,s)) + qWarning ("SimpleSettings::writeSettings() Couldn't write "+path); +} + +/* +QString SimpleSettings::readEntry (const QString &key) +{ + QStringList::Iterator itk=keylist.begin(); + QStringList::Iterator itv=valuelist.begin(); + + // First search for value in settings saved in map + while (itk !=keylist.end() ) + { + if (*itk == key) + return *itv; + itk++; + itv++; + } + qWarning ("SimpleSettings::readEntry() Couldn't find key "+key); + return ""; +} +*/ + +QString SimpleSettings::readEntry (const QString &key, const QString &def) +{ + QStringList::Iterator itk=keylist.begin(); + QStringList::Iterator itv=valuelist.begin(); + + // First search for value in settings saved in map + while (itk !=keylist.end() ) + { + if (*itk == key) + return *itv; + itk++; + itv++; + } + return def; +} + +int SimpleSettings::readNumEntry (const QString &key, const int &def) +{ + QStringList::Iterator itk=keylist.begin(); + QStringList::Iterator itv=valuelist.begin(); + + // First search for value in settings saved in map + while (itk !=keylist.end() ) + { + if (*itk == key) + { + bool ok; + int i=(*itv).toInt(&ok,10); + if (ok) + return i; + else + return def; + } + itk++; + itv++; + } + return def; +} + +void SimpleSettings::setEntry (const QString &key, const QString &value) +{ + QStringList::Iterator itk=keylist.begin(); + QStringList::Iterator itv=valuelist.begin(); + + if (!key.isEmpty() ) + { + // Search for existing entry first + while (itk !=keylist.end() ) + { + if (*itk == key) + { + if (!value.isEmpty()) + *itv=value; + else + *itv=""; + *itv=value; + return; + } + itk++; + itv++; + } + + // If no entry exists, append a new one + keylist.append (key); + valuelist.append (value); + } +} + + + ///////////////////////////////////////////////////////////////// // Settings ///////////////////////////////////////////////////////////////// @@ -11,6 +156,11 @@ clear(); } +Settings::Settings(const QString & organization, const QString & application ):QSettings (organization,application) +{ + clear(); +} + Settings::~Settings() { } @@ -93,7 +243,7 @@ } } -QString Settings::getXMLData (const QString &fpath) +QString Settings::getDataXML (const QString &fpath) { QString s; QStringList::Iterator itp=pathlist.begin(); @@ -116,17 +266,3 @@ return s; } -void Settings::write() -{ - QStringList::Iterator itp=pathlist.begin(); - QStringList::Iterator itk=keylist.begin(); - QStringList::Iterator itv=valuelist.begin(); - - while (itp !=pathlist.end() ) - { - itp++; - itk++; - itv++; - } -} -