forked from jonathanverner/comment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.cpp
112 lines (96 loc) · 3.17 KB
/
config.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/** This file is part of project comment
*
* File: config.cpp
* Created: 2009-02-13
* Author: Jonathan Verner <[email protected]>
* License: GPL v2 or later
*
* Copyright (C) 2010 Jonathan Verner <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include <QtCore/QDir>
#include <QtCore/QFile>
#include <QtCore/QTextStream>
#include <QtCore/QRegExp>
#include <QtCore/QStringList>
#include <QtCore/QDebug>
QString configurator::fName = "";
QHash<QString,QString> configurator::cfg;
bool configurator::haveTeXAndFriends;
configurator& config() {
static configurator *conf = new configurator();
return *conf;
}
bool configurator::haveKey( const QString key ) const {
return cfg.contains( key.toLower().replace(' ','_') );
}
void configurator::removeKey( const QString key ) {
cfg.remove( key.toLower().replace(' ','_') );
}
bool configurator::findTeX() {
if ( cfg["tex"] != "" && QFile::exists( cfg["tex"] ) ) return true;
if ( QFile::exists( "/usr/bin/pdflatex" ) ) {
cfg["tex"] = "/usr/bin/pdflatex";
return true;
}
return false;
}
bool configurator::findGhostScript() {
if ( cfg["gs"] != "" && QFile::exists( cfg["gs"] ) ) return true;
if ( QFile::exists( "/usr/bin/gs" ) ) {
cfg["gs"] = "/usr/bin/gs";
return true;
}
return false;
}
configurator::configurator()
{
fName = QDir::homePath()+"/.comment";
load();
haveTeXAndFriends = findTeX() && findGhostScript();
qDebug() << "initializing configurator";
}
void configurator::load() {
qDebug() << "Loading config from "<<fName << "...";
QFile cfgFile( fName );
if ( ! cfgFile.open( QIODevice::ReadOnly | QIODevice::Text ) )
return;
QTextStream in(&cfgFile);
while( ! in.atEnd() ) {
QString line = in.readLine();
QRegExp exp(" *([^ ][^#= ]*) *= *([^ ][^;]*).*");
if ( ! exp.exactMatch( line ) ) continue;
QStringList list = exp.capturedTexts();
cfg[list[1].toLower().replace(' ','_')]=list[2];
}
cfgFile.close();
}
void configurator::save() {
qDebug() << "Saving config to "<<fName << "...";
QFile cfgFile( fName );
if ( ! cfgFile.open( QIODevice::WriteOnly | QIODevice::Text ) )
return;
QTextStream out(&cfgFile);
foreach( QString key, cfg.keys() ) {
out << key.toLower().replace(' ','_') << " = " << cfg[key] << ";" << endl;
}
cfgFile.close();
}
QString &configurator::operator[] (const QString key) {
return cfg[key.toLower().replace(' ','_')];
}