annotate base/ConfigFile.cpp @ 149:3e4c384f518e

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