diff -r 7a96bd401351 -r 30b22f7bd009 settings.cpp --- a/settings.cpp Sun Jan 30 12:58:47 2005 +0000 +++ b/settings.cpp Mon Sep 12 19:52:51 2005 +0000 @@ -1,8 +1,145 @@ #include +#include #include "settings.h" -#include "misc.h" using namespace std; + +bool loadStringFromDisk (const QString &fname, QString &s) +{ + s=""; + QFile file ( fname); + if ( !file.open( IO_ReadOnly ) ) return false; + + QTextStream ts( &file ); + ts.setEncoding (QTextStream::UnicodeUTF8); + while ( !ts.atEnd() ) + s+=ts.readLine()+"\n"; + file.close(); + return true; +} + +bool saveStringToDisk (const QString &fname, const QString &s) +{ + QFile file( fname); + + file.setName ( fname); + if ( !file.open( IO_WriteOnly ) ) + { + file.close(); + return false; + } + + // Write it finally, and write in UTF8, no matter what + QTextStream ts( &file ); + ts.setEncoding (QTextStream::UnicodeUTF8); + ts << s; + file.close(); + return true; +} + +///////////////////////////////////////////////////////////////// +// 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 ""; +} + +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=""; + return; + } + itk++; + itv++; + } + + // If no entry exists, append a new one + keylist.append (key); + valuelist.append (value); + } +} + + + ///////////////////////////////////////////////////////////////// // Settings ///////////////////////////////////////////////////////////////// @@ -116,17 +253,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++; - } -} -