1.1 --- a/settings.cpp Sun Jan 30 12:58:47 2005 +0000
1.2 +++ b/settings.cpp Mon Sep 12 19:52:51 2005 +0000
1.3 @@ -1,8 +1,145 @@
1.4 #include <iostream>
1.5 +#include <qregexp.h>
1.6 #include "settings.h"
1.7 -#include "misc.h"
1.8
1.9 using namespace std;
1.10 +
1.11 +bool loadStringFromDisk (const QString &fname, QString &s)
1.12 +{
1.13 + s="";
1.14 + QFile file ( fname);
1.15 + if ( !file.open( IO_ReadOnly ) ) return false;
1.16 +
1.17 + QTextStream ts( &file );
1.18 + ts.setEncoding (QTextStream::UnicodeUTF8);
1.19 + while ( !ts.atEnd() )
1.20 + s+=ts.readLine()+"\n";
1.21 + file.close();
1.22 + return true;
1.23 +}
1.24 +
1.25 +bool saveStringToDisk (const QString &fname, const QString &s)
1.26 +{
1.27 + QFile file( fname);
1.28 +
1.29 + file.setName ( fname);
1.30 + if ( !file.open( IO_WriteOnly ) )
1.31 + {
1.32 + file.close();
1.33 + return false;
1.34 + }
1.35 +
1.36 + // Write it finally, and write in UTF8, no matter what
1.37 + QTextStream ts( &file );
1.38 + ts.setEncoding (QTextStream::UnicodeUTF8);
1.39 + ts << s;
1.40 + file.close();
1.41 + return true;
1.42 +}
1.43 +
1.44 +/////////////////////////////////////////////////////////////////
1.45 +// SimpleSettings
1.46 +/////////////////////////////////////////////////////////////////
1.47 +SimpleSettings::SimpleSettings()
1.48 +{
1.49 + clear();
1.50 +}
1.51 +
1.52 +SimpleSettings::~SimpleSettings()
1.53 +{
1.54 +}
1.55 +
1.56 +void SimpleSettings::clear()
1.57 +{
1.58 + keylist.clear();
1.59 + valuelist.clear();
1.60 +}
1.61 +
1.62 +void SimpleSettings::readSettings (const QString &path)
1.63 +{
1.64 + QString s;
1.65 + if (!loadStringFromDisk(path,s))
1.66 + {
1.67 + qWarning ("SimpleSettings::readSettings() Couldn't read "+path);
1.68 + return;
1.69 + }
1.70 + QStringList lines;
1.71 + lines=QStringList::split (QRegExp("\n"),s,false);
1.72 + int i;
1.73 + QStringList::Iterator it=lines.begin();
1.74 + while (it !=lines.end() )
1.75 + {
1.76 + i=(*it).find("=",0);
1.77 + keylist.append((*it).left(i));
1.78 + valuelist.append((*it).right((*it).length()-i-1));
1.79 + it++;
1.80 + }
1.81 +}
1.82 +
1.83 +void SimpleSettings::writeSettings (const QString &path)
1.84 +{
1.85 + QString s;
1.86 + QStringList::Iterator itk=keylist.begin();
1.87 + QStringList::Iterator itv=valuelist.begin();
1.88 +
1.89 + // First search for value in settings saved in map
1.90 + while (itk !=keylist.end() )
1.91 + {
1.92 + s+=*itk+"="+*itv+"\n";
1.93 + itk++;
1.94 + itv++;
1.95 + }
1.96 + if (!saveStringToDisk(path,s))
1.97 + qWarning ("SimpleSettings::writeSettings() Couldn't write "+path);
1.98 +}
1.99 +
1.100 +QString SimpleSettings::readEntry (const QString &key)
1.101 +{
1.102 + QStringList::Iterator itk=keylist.begin();
1.103 + QStringList::Iterator itv=valuelist.begin();
1.104 +
1.105 + // First search for value in settings saved in map
1.106 + while (itk !=keylist.end() )
1.107 + {
1.108 + if (*itk == key)
1.109 + return *itv;
1.110 + itk++;
1.111 + itv++;
1.112 + }
1.113 + qWarning ("SimpleSettings::readEntry() Couldn't find key "+key);
1.114 + return "";
1.115 +}
1.116 +
1.117 +void SimpleSettings::setEntry (const QString &key, const QString &value)
1.118 +{
1.119 + QStringList::Iterator itk=keylist.begin();
1.120 + QStringList::Iterator itv=valuelist.begin();
1.121 +
1.122 + if (!key.isEmpty() )
1.123 + {
1.124 + // Search for existing entry first
1.125 + while (itk !=keylist.end() )
1.126 + {
1.127 + if (*itk == key)
1.128 + {
1.129 + if (!value.isEmpty())
1.130 + *itv=value;
1.131 + else
1.132 + *itv="";
1.133 + return;
1.134 + }
1.135 + itk++;
1.136 + itv++;
1.137 + }
1.138 +
1.139 + // If no entry exists, append a new one
1.140 + keylist.append (key);
1.141 + valuelist.append (value);
1.142 + }
1.143 +}
1.144 +
1.145 +
1.146 +
1.147 /////////////////////////////////////////////////////////////////
1.148 // Settings
1.149 /////////////////////////////////////////////////////////////////
1.150 @@ -116,17 +253,3 @@
1.151 return s;
1.152 }
1.153
1.154 -void Settings::write()
1.155 -{
1.156 - QStringList::Iterator itp=pathlist.begin();
1.157 - QStringList::Iterator itk=keylist.begin();
1.158 - QStringList::Iterator itv=valuelist.begin();
1.159 -
1.160 - while (itp !=pathlist.end() )
1.161 - {
1.162 - itp++;
1.163 - itk++;
1.164 - itv++;
1.165 - }
1.166 -}
1.167 -