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