SIde-Channel Analysis toolKit (SICAK)
Software toolkit for side-channel analysis
configloader.hpp
Go to the documentation of this file.
1 /*
2 * SICAK - SIde-Channel Analysis toolKit
3 * Copyright (C) 2018 Petr Socha, FIT, CTU in Prague
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18 
30 #ifndef CONFIGLOADER_HPP
31 #define CONFIGLOADER_HPP
32 
33 #include <QCommandLineParser>
34 #include <QJsonObject>
35 #include <QJsonDocument>
36 
43 class ConfigLoader {
44 
45 public:
46 
48  ConfigLoader(const QCommandLineParser & parser): m_parser(parser) {
49 
50  const QStringList positionalArguments = m_parser.positionalArguments();
51 
52  foreach (const QString &str, positionalArguments) {
53 
54  QFile file;
55  file.setFileName(str);
56  if(file.open(QIODevice::ReadOnly | QIODevice::Text)){
57  QString configStr = file.readAll();
58  QJsonDocument doc = QJsonDocument::fromJson(configStr.toUtf8());
59  m_config_files.push_back(doc.object());
60  }
61 
62  }
63 
64  }
65 
67  QString getParam(const QCommandLineOption & option) const {
68 
69  if(m_parser.isSet(option))
70  return m_parser.value(option);
71 
72  QStringList names = option.names();
73 
74  foreach (const QString &name, names) {
75 
76  if(name.length() == 1) continue;
77 
78  foreach (const QJsonObject &obj, m_config_files) {
79 
80  if(obj.contains(name) && obj.value(name).isString())
81  return obj.value(name).toString();
82 
83  }
84 
85  }
86 
87  return "";
88 
89  }
90 
92  bool isSet(const QCommandLineOption & option) const {
93 
94  if(m_parser.isSet(option))
95  return true;
96 
97  QStringList names = option.names();
98 
99  foreach (const QString &name, names) {
100 
101  if(name.length() == 1) continue;
102 
103  foreach (const QJsonObject &obj, m_config_files) {
104  if(obj.contains(name) && obj.value(name).isString())
105  return true;
106  }
107 
108  }
109 
110  return false;
111 
112  }
113 
114 protected:
115 
116  const QCommandLineParser & m_parser;
117  QList<QJsonObject> m_config_files;
118 
119 };
120 
121 #endif /* CONFIGLOADER_HPP */
A QT based command line options loader/parser.
Definition: configloader.hpp:43
ConfigLoader(const QCommandLineParser &parser)
Constructs a ConfigLoader with given QCommandLineParser.
Definition: configloader.hpp:48
bool isSet(const QCommandLineOption &option) const
Returns true when parameter is set, either on command line, on in a json config file.
Definition: configloader.hpp:92
QString getParam(const QCommandLineOption &option) const
Returns a string parameter value, command-line has priority over json config file.
Definition: configloader.hpp:67