UniversalIndentGUI 1.2.0
|
00001 /*************************************************************************** 00002 * Copyright (C) 2006-2012 by Thomas Schweitzer * 00003 * thomas-schweitzer(at)arcor.de * 00004 * * 00005 * This program is free software; you can redistribute it and/or modify * 00006 * it under the terms of the GNU General Public License version 2.0 as * 00007 * published by the Free Software Foundation. * 00008 * * 00009 * This program is distributed in the hope that it will be useful, * 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00012 * GNU General Public License for more details. * 00013 * * 00014 * You should have received a copy of the GNU General Public License * 00015 * along with this program in the file LICENSE.GPL; if not, write to the * 00016 * Free Software Foundation, Inc., * 00017 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 00018 ***************************************************************************/ 00019 00020 #include "UiGuiIniFileParser.h" 00021 00022 #include <QFile> 00023 #include <QStringList> 00024 #include <QVariant> 00025 #include <QTextStream> 00026 00028 00046 UiGuiIniFileParser::UiGuiIniFileParser(void) { 00047 init(); 00048 } 00049 00050 00054 UiGuiIniFileParser::UiGuiIniFileParser(const QString &iniFileName) { 00055 init(); 00056 _iniFileName = iniFileName; 00057 parseIniFile(); 00058 } 00059 00060 00061 void UiGuiIniFileParser::init() { 00062 _sections.clear(); 00063 _keyValueMap.clear(); 00064 _iniFileName = ""; 00065 } 00066 00067 00068 UiGuiIniFileParser::~UiGuiIniFileParser(void) { 00069 } 00070 00071 00075 QStringList UiGuiIniFileParser::childGroups() { 00076 QStringList sectionsStringList; 00077 00078 for( unsigned int i = 0; i < _sections.size(); i++ ) { 00079 sectionsStringList << _sections[i]; 00080 } 00081 00082 return sectionsStringList; 00083 } 00084 00085 00094 QVariant UiGuiIniFileParser::value(const QString &keyName, const QString &defaultValue) { 00095 return _keyValueMap.value( keyName, defaultValue ); 00096 } 00097 00098 00102 void UiGuiIniFileParser::parseIniFile() { 00103 QFile iniFile(_iniFileName); 00104 00105 if ( iniFile.open(QFile::ReadOnly) ) { 00106 // Clear the vectors holding the keys and values. 00107 _sections.clear(); 00108 _keyValueMap.clear(); 00109 00110 QTextStream iniFileStream( &iniFile ); 00111 QString line; 00112 QString currentSectionName = ""; 00113 QString keyName = ""; 00114 QString valueAsString = ""; 00115 00116 while ( !iniFileStream.atEnd() ) { 00117 line = iniFileStream.readLine().trimmed(); 00118 00119 // Test if the read line is a section name and if so remeber it. 00120 if ( line.startsWith("[") && line.endsWith("]") ) { 00121 currentSectionName = line.remove(0, 1); 00122 currentSectionName.chop(1); 00123 00124 // Store the section name. 00125 _sections.push_back( currentSectionName ); 00126 } 00127 // Otherwise test whether the line has a assign char 00128 else if ( line.contains("=") ) { 00129 int indexOfFirstAssign = line.indexOf("="); 00130 keyName = line.left(indexOfFirstAssign); 00131 00132 if ( !keyName.isEmpty() ) { 00133 valueAsString = line.remove(0, indexOfFirstAssign+1); 00134 // Remove any existing double quotes from the value. 00135 if ( valueAsString.startsWith("\"") && valueAsString.endsWith("\"") ) { 00136 valueAsString = valueAsString.remove(0, 1); 00137 valueAsString.chop(1); 00138 } 00139 00140 // Prepend an eventually section name to the key name. 00141 if ( !currentSectionName.isEmpty() ) { 00142 keyName = currentSectionName + "/" + keyName; 00143 } 00144 00145 // Store the key and value in the map. 00146 _keyValueMap.insert(keyName, valueAsString ); 00147 } 00148 } 00149 } 00150 } 00151 }