Mercurial > hg > svcore
comparison data/fileio/ConfigFile.cpp @ 148:1a42221a1522
* Reorganising code base. This revision will not compile.
author | Chris Cannam |
---|---|
date | Mon, 31 Jul 2006 11:49:58 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
147:3a13b0d4934e | 148:1a42221a1522 |
---|---|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ | |
2 | |
3 /* | |
4 Sonic Visualiser | |
5 An audio file viewer and annotation editor. | |
6 Centre for Digital Music, Queen Mary, University of London. | |
7 This file copyright 2006 Chris Cannam. | |
8 | |
9 This program is free software; you can redistribute it and/or | |
10 modify it under the terms of the GNU General Public License as | |
11 published by the Free Software Foundation; either version 2 of the | |
12 License, or (at your option) any later version. See the file | |
13 COPYING included with this distribution for more information. | |
14 */ | |
15 | |
16 #include "ConfigFile.h" | |
17 | |
18 #include "base/Exceptions.h" | |
19 | |
20 #include <iostream> | |
21 | |
22 #include <QFile> | |
23 #include <QMutexLocker> | |
24 #include <QTextStream> | |
25 #include <QStringList> | |
26 | |
27 ConfigFile::ConfigFile(QString filename) : | |
28 m_filename(filename), | |
29 m_loaded(false), | |
30 m_modified(false) | |
31 { | |
32 } | |
33 | |
34 ConfigFile::~ConfigFile() | |
35 { | |
36 try { | |
37 commit(); | |
38 } catch (FileOperationFailed f) { | |
39 std::cerr << "WARNING: ConfigFile::~ConfigFile: Commit failed for " | |
40 << m_filename.toStdString() << std::endl; | |
41 } | |
42 } | |
43 | |
44 QString | |
45 ConfigFile::get(QString key, QString deft) | |
46 { | |
47 if (!m_loaded) load(); | |
48 | |
49 QMutexLocker locker(&m_mutex); | |
50 | |
51 if (m_data.find(key) == m_data.end()) return deft; | |
52 return m_data[key]; | |
53 } | |
54 | |
55 int | |
56 ConfigFile::getInt(QString key, int deft) | |
57 { | |
58 return get(key, QString("%1").arg(deft)).toInt(); | |
59 } | |
60 | |
61 bool | |
62 ConfigFile::getBool(QString key, bool deft) | |
63 { | |
64 QString value = get(key, deft ? "true" : "false").trimmed().toLower(); | |
65 return (value == "true" || value == "yes" || value == "on" || value == "1"); | |
66 } | |
67 | |
68 float | |
69 ConfigFile::getFloat(QString key, float deft) | |
70 { | |
71 return get(key, QString("%1").arg(deft)).toFloat(); | |
72 } | |
73 | |
74 QStringList | |
75 ConfigFile::getStringList(QString key) | |
76 { | |
77 return get(key).split('|'); | |
78 } | |
79 | |
80 void | |
81 ConfigFile::set(QString key, QString value) | |
82 { | |
83 if (!m_loaded) load(); | |
84 | |
85 QMutexLocker locker(&m_mutex); | |
86 | |
87 m_data[key] = value; | |
88 | |
89 m_modified = true; | |
90 } | |
91 | |
92 void | |
93 ConfigFile::set(QString key, int value) | |
94 { | |
95 set(key, QString("%1").arg(value)); | |
96 } | |
97 | |
98 void | |
99 ConfigFile::set(QString key, bool value) | |
100 { | |
101 set(key, value ? QString("true") : QString("false")); | |
102 } | |
103 | |
104 void | |
105 ConfigFile::set(QString key, float value) | |
106 { | |
107 set(key, QString("%1").arg(value)); | |
108 } | |
109 | |
110 void | |
111 ConfigFile::set(QString key, const QStringList &values) | |
112 { | |
113 set(key, values.join("|")); | |
114 } | |
115 | |
116 void | |
117 ConfigFile::commit() | |
118 { | |
119 QMutexLocker locker(&m_mutex); | |
120 | |
121 if (!m_modified) return; | |
122 | |
123 // Really we should write to another file and then move to the | |
124 // intended target, but I don't think we're all that particular | |
125 // about reliability here at the moment | |
126 | |
127 QFile file(m_filename); | |
128 | |
129 if (!file.open(QFile::WriteOnly | QFile::Text)) { | |
130 throw FileOperationFailed(m_filename, "open for writing"); | |
131 } | |
132 | |
133 QTextStream out(&file); | |
134 | |
135 for (DataMap::const_iterator i = m_data.begin(); i != m_data.end(); ++i) { | |
136 out << i->first << "=" << i->second << endl; | |
137 } | |
138 | |
139 m_modified = false; | |
140 } | |
141 | |
142 bool | |
143 ConfigFile::load() | |
144 { | |
145 QMutexLocker locker(&m_mutex); | |
146 | |
147 if (m_loaded) return true; | |
148 | |
149 QFile file(m_filename); | |
150 | |
151 if (!file.open(QFile::ReadOnly | QFile::Text)) { | |
152 return false; | |
153 } | |
154 | |
155 QTextStream in(&file); | |
156 | |
157 m_data.clear(); | |
158 | |
159 while (!in.atEnd()) { | |
160 | |
161 QString line = in.readLine(2048); | |
162 QString key = line.section('=', 0, 0); | |
163 QString value = line.section('=', 1, -1); | |
164 if (key == "") continue; | |
165 | |
166 m_data[key] = value; | |
167 } | |
168 | |
169 m_loaded = true; | |
170 m_modified = false; | |
171 return true; | |
172 } | |
173 | |
174 void | |
175 ConfigFile::reset() | |
176 { | |
177 QMutexLocker locker(&m_mutex); | |
178 m_loaded = false; | |
179 m_modified = false; | |
180 } | |
181 |