Last active
May 14, 2018 05:07
-
-
Save carlosolmos/fe7e4c5e25b47164e5c75b4ac32b2697 to your computer and use it in GitHub Desktop.
QT. Read a configuration file into a Hashtable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Read a configuration file with key = value pairs and [sections] into a Hash table. | |
| sample config file: | |
| [SYSTEM] | |
| key1 = value | |
| key2 = value | |
| [NETWORK] | |
| key1 = value | |
| key4 = value | |
| The resulting hash table will contain the following pairs: | |
| SYSTEM_key1 = value | |
| SYSTEM_key2 = value | |
| NETWORK_key1 = value | |
| NETWORK_key4 = value | |
| */ | |
| QHash<QString, QString> Utils::getConfiguration(const QString &source) | |
| { | |
| QHash<QString, QString> config; | |
| QFile inputFile(source); | |
| if (inputFile.open(QIODevice::ReadOnly)) | |
| { | |
| QTextStream in(&inputFile); | |
| QString currentLabel = ""; | |
| QStringList toks; | |
| while (!in.atEnd()) | |
| { | |
| QString line = in.readLine(); | |
| line = line.trimmed(); | |
| if(line.length() > 0){ | |
| if(line.indexOf("[") == 0){ | |
| currentLabel = line.replace(QRegExp("[\\[\\]]"),""); | |
| }else{ | |
| toks = line.split("="); | |
| if(toks.length() == 2){ | |
| QString key = toks[0].trimmed(); | |
| if(currentLabel.length() > 0){ | |
| key = currentLabel + "_" + key; | |
| } | |
| config.insert(key,toks[1].trimmed()); | |
| } | |
| } | |
| } | |
| } | |
| inputFile.close(); | |
| } | |
| return config; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment