Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

qsettings.cpp

Go to the documentation of this file.
00001 /*
00002 ** $Id: qsettings.cpp,v 1.1 2004/07/13 10:35:29 llornkcor Exp $
00003 */
00004 
00005 #include "qsettings.h"
00006 #include <stdio.h>
00007 #include <qfile.h>
00008 #include <qtextstream.h>
00009 
00010 
00011 QSettings::QSettings(const QString &_fn)
00012 {
00013                 qWarning("Settings "+_fn);
00014         // read the prefs from the file
00015         fn = _fn;
00016         
00017         QFile f(_fn);
00018     if ( f.open(IO_ReadOnly) ) {    // file opened successfully
00019         QTextStream t( &f );        // use a text stream
00020         QString s;
00021         while ( !t.eof() ) {        // until end of file...
00022             s = t.readLine();       // line of text excluding '\n'
00023                         char buf[256];
00024                         sprintf (buf, "%s", (const char *) s);
00025                         int pos = s.find (" = ");
00026                         QString key = s.left (pos);
00027                         QString val = s.right (s.length() - pos - 3);
00028                         writeEntry (key, val);
00029                         
00030                         sprintf (buf, "%s|%s", (const char *)key, (const char *)val);
00031         }
00032         f.close();
00033     }
00034         
00035 
00036 }
00037 
00038 QSettings::~QSettings()
00039 {
00040         // write out the prefs to the file
00041         QAsciiDictIterator <QString> it( prefs ); // iterator for dict
00042         QFile f(fn);
00043         f.open(IO_WriteOnly);
00044         QTextStream ts( &f );
00045 
00046     while ( it.current() )
00047         {
00048            QString *key = new QString(it.currentKey());
00049            char buf[256];
00050            sprintf       (buf, "%s", (const char *) *( it.current()));
00051            QString *val = new QString(buf);
00052            sprintf (buf, "%s %s", (const char *)*key, (const char *)*val);
00053            ts << *key << " = " << *val << endl;
00054        ++it;
00055         }
00056         
00057         f.close();
00058         prefs.clear();
00059 }
00060 
00061 void QSettings::insertSearchPath (System sys, const QString &str)
00062 {
00063         fn = str;
00064 }
00065 
00066 QString QSettings::readEntry (const QString &key, const QString &def)
00067 {
00068         
00069         QString *s = prefs.find((const char *)key);
00070         if (!s)
00071                 return def;
00072         else 
00073                 return *s;
00074         
00075 }
00076 
00077 int     QSettings::readNumEntry (const QString &key, int def)
00078 {
00079         QString *s = prefs[key];
00080         if (!s) return def;
00081     return s->toInt();
00082 }
00083 
00084 bool    QSettings::readBoolEntry (const QString &key, bool def)
00085 {
00086         QString *s = prefs[key];
00087         if (!s) return def;
00088         if (*s == "1")
00089                 return true;
00090         else
00091                 return false;
00092 }
00093 
00094 bool    QSettings::writeEntry (const QString &key, int val)
00095 {
00096         char buf[64];
00097         sprintf (buf, "%d", val);
00098         QString *v = new QString(buf);
00099     prefs.replace ((const char *)key, v);
00100         return true;
00101 }
00102 
00103 bool    QSettings::writeEntry (const QString &key, bool val)
00104 {
00105         QString *v;
00106         if (val)
00107            v = new QString("1");
00108         else
00109        v = new QString("0");
00110     prefs.replace ((const char *)key, v);
00111         return true;
00112 }
00113 
00114 bool    QSettings::writeEntry (const QString &key, const QString &val)
00115 {
00116         QString *v = new QString (val);
00117         prefs.replace ((const char *)key, v);
00118         return true;
00119 }
00120 
00121 bool    QSettings::writeEntry (const QString &key, const char *val)
00122 {
00123         QString *v = new QString (val);
00124         prefs.replace ((const char *)key, v);
00125         return true;
00126 }
00127 
00128 bool    QSettings::removeEntry (const QString &key)
00129 {
00130         prefs.remove (key);
00131         return true;
00132 }
00133 
00134 QStringList QSettings::entryList (const QString &key) const
00135 {
00136                 qDebug("entryList: "+key);
00137         QStringList list;
00138                 if(!prefs.isEmpty()) {
00139         QAsciiDictIterator <QString> it( prefs ); // iterator for dict
00140  qDebug("ready"); 
00141     while ( it.current() )
00142         {
00143            char buf[512];
00144            sprintf(buf, "%s", (const char *) *( it.current()));
00145            QString *val = new QString(buf);
00146        sprintf(buf, "%s -> %s\n", it.currentKey(), (const char *)*val );
00147            QString *cat = new QString(it.currentKey());
00148        if (cat->contains("-field", FALSE))
00149               list.append (it.currentKey());
00150        ++it;
00151         }
00152                 }
00153                 qWarning("Return here");
00154         return list;
00155 }
00156 

Generated on Sat Nov 5 16:17:14 2005 for OPIE by  doxygen 1.4.2