summaryrefslogtreecommitdiff
path: root/noncore/apps/zsafe/qsettings.cpp
Unidiff
Diffstat (limited to 'noncore/apps/zsafe/qsettings.cpp') (more/less context) (ignore whitespace changes)
-rwxr-xr-xnoncore/apps/zsafe/qsettings.cpp153
1 files changed, 153 insertions, 0 deletions
diff --git a/noncore/apps/zsafe/qsettings.cpp b/noncore/apps/zsafe/qsettings.cpp
new file mode 100755
index 0000000..62a9947
--- a/dev/null
+++ b/noncore/apps/zsafe/qsettings.cpp
@@ -0,0 +1,153 @@
1/*
2** $Id$
3*/
4
5#include "qsettings.h"
6#include <qstringlist.h>
7#include <stdio.h>
8#include <qfile.h>
9#include <qtextstream.h>
10
11
12QSettings::QSettings(const QString &_fn)
13{
14 // read the prefs from the file
15 fn = _fn;
16
17 QFile f(_fn);
18 if ( f.open(IO_ReadOnly) ) { // file opened successfully
19 QTextStream t( &f ); // use a text stream
20 QString s;
21 while ( !t.eof() ) { // until end of file...
22 s = t.readLine(); // line of text excluding '\n'
23 char buf[256];
24 sprintf (buf, "%s", (const char *) s);
25 int pos = s.find (" = ");
26 QString key = s.left (pos);
27 QString val = s.right (s.length() - pos - 3);
28 writeEntry (key, val);
29
30 sprintf (buf, "%s|%s", (const char *)key, (const char *)val);
31 }
32 f.close();
33 }
34
35
36}
37
38QSettings::~QSettings()
39{
40 // write out the prefs to the file
41 QAsciiDictIterator <QString> it( prefs ); // iterator for dict
42 QFile f(fn);
43 f.open(IO_WriteOnly);
44 QTextStream ts( &f );
45
46 while ( it.current() )
47 {
48 QString *key = new QString(it.currentKey());
49 char buf[256];
50 sprintf (buf, "%s", (const char *) *( it.current()));
51 QString *val = new QString(buf);
52 sprintf (buf, "%s %s", (const char *)*key, (const char *)*val);
53 ts << *key << " = " << *val << endl;
54 ++it;
55 }
56
57 f.close();
58 prefs.clear();
59}
60
61void QSettings::insertSearchPath (System sys, const QString &str)
62{
63 fn = str;
64}
65
66QString QSettings::readEntry (const QString &key, const QString &def)
67{
68
69 QString *s = prefs.find((const char *)key);
70 if (!s)
71 return def;
72 else
73 return *s;
74
75}
76
77int QSettings::readNumEntry (const QString &key, int def)
78{
79 QString *s = prefs[key];
80 if (!s) return def;
81 return s->toInt();
82}
83
84bool QSettings::readBoolEntry (const QString &key, bool def)
85{
86 QString *s = prefs[key];
87 if (!s) return def;
88 if (*s == "1")
89 return true;
90 else
91 return false;
92}
93
94bool QSettings::writeEntry (const QString &key, int val)
95{
96 char buf[64];
97 sprintf (buf, "%d", val);
98 QString *v = new QString(buf);
99 prefs.replace ((const char *)key, v);
100 return true;
101}
102
103bool QSettings::writeEntry (const QString &key, bool val)
104{
105 QString *v;
106 if (val)
107 v = new QString("1");
108 else
109 v = new QString("0");
110 prefs.replace ((const char *)key, v);
111 return true;
112}
113
114bool QSettings::writeEntry (const QString &key, const QString &val)
115{
116 QString *v = new QString (val);
117 prefs.replace ((const char *)key, v);
118 return true;
119}
120
121bool QSettings::writeEntry (const QString &key, const char *val)
122{
123 QString *v = new QString (val);
124 prefs.replace ((const char *)key, v);
125 return true;
126}
127
128bool QSettings::removeEntry (const QString &key)
129{
130 prefs.remove (key);
131 return true;
132}
133
134QStringList QSettings::entryList (const QString &key) const
135{
136 QAsciiDictIterator <QString> it( prefs ); // iterator for dict
137 QStringList list;
138
139 while ( it.current() )
140 {
141 char buf[512];
142 sprintf (buf, "%s", (const char *) *( it.current()));
143 QString *val = new QString(buf);
144 sprintf(buf, "%s -> %s\n", it.currentKey(), (const char *)*val );
145 QString *cat = new QString(it.currentKey());
146 if (cat->contains("-field", FALSE))
147 list.append (it.currentKey());
148 ++it;
149
150 }
151 return list;
152}
153