Mercurial > hg > forks > UniversalIndentGUI
changeset 478:5417f9e5f6e5
Added a new class UiguiIniFileParser which will replace the currently used QSettings for reading the indenter parameters.
git-svn-id: svn://svn.code.sf.net/p/universalindent/code/trunk@718 59b1889a-e5ac-428c-b0c7-476e01d41282
author | thomas_-_s <thomas_-_s@59b1889a-e5ac-428c-b0c7-476e01d41282> |
---|---|
date | Fri, 06 Jun 2008 10:20:44 +0000 |
parents | ce6cd144159d |
children | 4f7041de0d0e |
files | UniversalIndentGUI.pro src/UniversalIndentGUI.vcproj src/main.cpp src/uiguiIniFileParser.cpp src/uiguiIniFileParser.h |
diffstat | 5 files changed, 189 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/UniversalIndentGUI.pro Fri Jun 06 10:18:15 2008 +0000 +++ b/UniversalIndentGUI.pro Fri Jun 06 10:20:44 2008 +0000 @@ -154,6 +154,7 @@ src/indenthandler.h \ src/mainwindow.h \ src/uiguierrormessage.h \ + src/uiguiIniFileParser.h \ src/uiguisettings.h \ src/uiguisettingsdialog.h \ src/updatecheckdialog.h @@ -170,6 +171,7 @@ src/main.cpp \ src/mainwindow.cpp \ src/uiguierrormessage.cpp \ + src/uiguiIniFileParser.cpp \ src/uiguisettings.cpp \ src/uiguisettingsdialog.cpp \ src/updatecheckdialog.cpp
--- a/src/UniversalIndentGUI.vcproj Fri Jun 06 10:18:15 2008 +0000 +++ b/src/UniversalIndentGUI.vcproj Fri Jun 06 10:20:44 2008 +0000 @@ -244,6 +244,10 @@ > </File> <File + RelativePath=".\uiguiIniFileParser.cpp" + > + </File> + <File RelativePath=".\uiguisettings.cpp" > </File> @@ -298,6 +302,10 @@ > </File> <File + RelativePath=".\uiguiIniFileParser.h" + > + </File> + <File RelativePath=".\uiguisettings.h" > </File>
--- a/src/main.cpp Fri Jun 06 10:18:15 2008 +0000 +++ b/src/main.cpp Fri Jun 06 10:20:44 2008 +0000 @@ -20,15 +20,19 @@ #include "mainwindow.h" #include <QApplication> +#include "uiguiIniFileParser.h" /*! - \brief Entry point to UniversalIndentGUI application. Does not evaluate any command line parameters. + /brief Entry point to UniversalIndentGUI application. Does not evaluate any command line parameters. */ int main(int argc, char *argv[]) { QApplication app(argc, argv); QString file2OpenOnStart = ""; + //UiguiIniFileParser iniParser("C:/Dokumente und Einstellungen/ts/Eigene Dateien/Visual Studio 2005/Projects/UiGui/indenters/uigui_astyle.ini"); + //iniParser.childGroups(); + if ( argc > 1 ) { file2OpenOnStart = argv[1]; }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/uiguiIniFileParser.cpp Fri Jun 06 10:20:44 2008 +0000 @@ -0,0 +1,122 @@ +/*************************************************************************** + * Copyright (C) 2006-2008 by Thomas Schweitzer * + * thomas-schweitzer(at)arcor.de * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License version 2.0 as * + * published by the Free Software Foundation. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program in the file LICENSE.GPL; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + +#include "uiguiIniFileParser.h" + +#include <QTextStream> + +UiguiIniFileParser::UiguiIniFileParser(void) +{ + sections.clear(); + keyValueMap.clear(); + iniFileName = ""; +} + + +UiguiIniFileParser::UiguiIniFileParser(const QString &iniFileName) +{ + UiguiIniFileParser::UiguiIniFileParser(); + this->iniFileName = iniFileName; + parseIniFile(); +} + + +UiguiIniFileParser::~UiguiIniFileParser(void) +{ +} + + +/*! + \brief Returns the group/section names in the same order as they occurr in the ini file as QStringList. + */ +QStringList UiguiIniFileParser::childGroups() +{ + QStringList sectionsStringList; + + for( unsigned int i = 0; i < sections.size(); i++ ) { + sectionsStringList << sections[i]; + } + + return sectionsStringList; +} + + +/*! + \brief Returns the value of the defined \a keyName as QVariant. + + The \a keyName is assembled by a section name, a slash and the key name itself. + For example if you wish to access the value of the following setting: + <code>[NiceSection]</br>niceKeyName=2</code> you would have to call + value("NiceSection/niceKeyName"). + */ +QVariant UiguiIniFileParser::value(const QString &keyName) +{ + return keyValueMap.value(keyName, QVariant("error") ); +} + + +/*! + \brief Parses the ini file and stores the key value pairs in the internal vectors \a keys and \a values. + */ +void UiguiIniFileParser::parseIniFile() +{ + QFile iniFile(iniFileName); + + if ( iniFile.open(QFile::ReadOnly) ) { + // Clear the vectors holding the keys and values. + sections.clear(); + keyValueMap.clear(); + + QTextStream iniFileStream( &iniFile ); + QString line; + QString currentSectionName = ""; + QString keyName = ""; + QString keyValueAsString = ""; + + while ( !iniFileStream.atEnd() ) { + line = iniFileStream.readLine().trimmed(); + + // Test if the read line is a section name and if so remeber it. + if ( line.startsWith("[") && line.endsWith("]") ) { + currentSectionName = line.remove(0, 1); + currentSectionName.chop(1); + + // Store the section name. + sections.push_back( currentSectionName ); + } + // Otherwise test whether the line has a assign char + else if ( line.contains("=") ) { + int indexOfFirstAssign = line.indexOf("="); + keyName = line.left(indexOfFirstAssign); + + if ( !keyName.isEmpty() ) { + keyValueAsString = line.remove(0, indexOfFirstAssign+1); + + // Prepend an eventually section name to the key name. + if ( !currentSectionName.isEmpty() ) { + keyName = currentSectionName + "/" + keyName; + } + + // Store the key and value in the map. + keyValueMap.insert(keyName, keyValueAsString ); + } + } + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/uiguiIniFileParser.h Fri Jun 06 10:20:44 2008 +0000 @@ -0,0 +1,52 @@ +/*************************************************************************** + * Copyright (C) 2006-2008 by Thomas Schweitzer * + * thomas-schweitzer(at)arcor.de * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License version 2.0 as * + * published by the Free Software Foundation. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program in the file LICENSE.GPL; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + +#ifndef UIGUIINIFILEPARSER_H +#define UIGUIINIFILEPARSER_H + +#include <QFile> +#include <QString> +#include <QMap> +#include <QVariant> +#include <QStringList> +#include <vector> + +/*! + \class UiguiIniFileParser + \brief This class can be used to parse and access the contents of well formed ini files, but only readable. + */ + +class UiguiIniFileParser +{ +public: + UiguiIniFileParser(void); + UiguiIniFileParser(const QString &iniFileName); + ~UiguiIniFileParser(void); + QVariant value(const QString &keyName); + QStringList childGroups(); + +private: + void parseIniFile(); + + QString iniFileName; + std::vector<QString> sections; + QMap<QString, QVariant> keyValueMap; +}; + +#endif // UIGUIINIFILEPARSER_H