From 6686a5579fd908d6eb061a214e452ebc9172c358 Mon Sep 17 00:00:00 2001 From: Jonathan Young Date: Fri, 9 May 2014 15:17:33 +1000 Subject: [PATCH] Parse q3config.cfg in the home directory when first run to get setting defaults. --- mainwindow.cpp | 111 +++++++++++++++++++++++++++++++++++++++++++++++++ mainwindow.h | 2 + settings.cpp | 10 +++++ settings.h | 3 ++ 4 files changed, 126 insertions(+) diff --git a/mainwindow.cpp b/mainwindow.cpp index 74da1ab..2fbce5a 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -1,5 +1,6 @@ #include #include +#include #ifdef Q_OS_WIN32 #define WIN32_LEAN_AND_MEAN @@ -37,6 +38,16 @@ ioLaunch::ioLaunch(QWidget *parent) : homePath = homeEnv + "/.q3a"; #endif #endif + + QDir homeDir(homePath); + + // Try to parse q3config.cfg to get default settings if this is the first time the program has run. + if (!homePath.isEmpty() && homeDir.exists() && !settings.getHaveRun()) + { + parseQuake3Config(); + } + + settings.setHaveRun(true); } ioLaunch::~ioLaunch() @@ -264,3 +275,103 @@ void ioLaunch::on_sbHeight_valueChanged(int arg1) ioHedited = false; } } + +// Since q3config.cfg is generated it's nice and clean and shouldn't need a full parser. +static QString ParseToken(const QString &s, int &offset) +{ + // Skip whitespace. + while (offset < s.length() && s[offset] == ' ') + { + offset++; + } + + if (offset >= s.length()) + return QString(); + + // Check for quoted token. + bool quoted = s[offset] == '\"'; + + if (quoted) + offset++; + + // Parse token. + int start = offset; + + while (offset < s.length() && ((quoted && s[offset] != '\"') || (!quoted && s[offset] != ' '))) + { + offset++; + } + + // Get token substring. + int end = offset; + + if (quoted && s[offset] == '\"') + { + offset++; + } + + if (end - start <= 0) + return QString(); + + return s.mid(start, end - start); +} + +void ioLaunch::parseQuake3Config() +{ + QFile file(homePath + "/baseq3/q3config.cfg"); + + if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) + return; + + QTextStream stream(&file); + + // These may occur in any order, so process them after parsing the entire file. + QString r_mode, r_customwidth, r_customheight; + + while (!stream.atEnd()) + { + const QString line(stream.readLine()); + + // Skip comments. + if (line.startsWith("//")) + continue; + + int offset = 0; + + if (ParseToken(line, offset) == "seta") + { + const QString cvar(ParseToken(line, offset)); + + if (cvar == "r_mode") + { + r_mode = ParseToken(line, offset); + } + else if (cvar == "r_customwidth") + { + r_customwidth = ParseToken(line, offset); + } + else if (cvar == "r_customheight") + { + r_customheight = ParseToken(line, offset); + } + else if (cvar == "r_fullscreen") + { + // Set fullscreen/windows radio buttons. + const QString value(ParseToken(line, offset)); + + if (value == "0") + ui->rbWin->setChecked(true); + else if (value == "1") + ui->rbFull->setChecked(true); + } + } + } + + // Populate resolution spinboxes. + if (r_mode == "-1" && !r_customwidth.isEmpty() && !r_customheight.isEmpty()) + { + ui->sbWidth->setValue(r_customwidth.toInt()); + ui->sbHeight->setValue(r_customheight.toInt()); + ioWedited = ioHedited = true; + } +} diff --git a/mainwindow.h b/mainwindow.h index 1eacc7d..7b89c44 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -41,6 +41,8 @@ private slots: void on_sbHeight_valueChanged(int arg1); private: + void parseQuake3Config(); + Ui::ioLaunch *ui; QString resOption; QString screenOption; diff --git a/settings.cpp b/settings.cpp index c91f20f..5a85b76 100644 --- a/settings.cpp +++ b/settings.cpp @@ -28,6 +28,16 @@ Settings::Settings() : { } +bool Settings::getHaveRun() const +{ + return settings.value("haveRun").toBool(); +} + +void Settings::setHaveRun(bool value) +{ + settings.setValue("haveRun", value); +} + QString Settings::getQuakePath() const { return settings.value("quakePath").toString(); diff --git a/settings.h b/settings.h index 7a6099c..4e709c8 100644 --- a/settings.h +++ b/settings.h @@ -32,6 +32,9 @@ class Settings public: Settings(); + bool getHaveRun() const; + void setHaveRun(bool value); + QString getQuakePath() const; bool containsQuakePath() const; void setQuakePath(const QString &path);