GCC Code Coverage Report


Directory: ./
File: backends/filer_helpers.h
Date: 2023-04-20 22:59:23
Exec Total Coverage
Lines: 30 42 71.4%
Branches: 13 24 54.2%

Line Branch Exec Source
1 /*************************************************************************
2 Copyright © 2020 Roman Gilg <subdiff@gmail.com>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 **************************************************************************/
18 #pragma once
19
20 #include "logging.h"
21
22 #include <QDir>
23 #include <QFile>
24 #include <QFileInfo>
25 #include <QJsonDocument>
26 #include <QVariant>
27 #include <QVariantMap>
28
29 namespace Disman::Filer_helpers
30 {
31
32 352 static bool read_file(QFileInfo const& file_info, QVariantMap& info)
33 {
34 352 QFile file(file_info.filePath());
35
2/2
✓ Branch 1 taken 258 times.
✓ Branch 2 taken 94 times.
352 if (!file.exists()) {
36 258 return false;
37 }
38
39
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 94 times.
94 if (!file.open(QIODevice::ReadOnly)) {
40 qCWarning(DISMAN_BACKEND) << "Failed to open config control file for reading."
41 << file.errorString();
42 return false;
43 }
44
45 94 QJsonDocument parser;
46 94 info = parser.fromJson(file.readAll()).toVariant().toMap();
47 94 return true;
48 352 }
49
50 558 static QFileInfo file_info(std::string const& dir_path, std::string const& hash)
51 {
52 1116 return QFileInfo(QDir(QString::fromStdString(dir_path)),
53 1674 QString::fromStdString(hash + ".json"));
54 }
55
56 146 static bool write_file(QVariantMap const& map, QFileInfo const& file_info)
57 {
58
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 146 times.
146 if (map.isEmpty()) {
59 // Nothing to write. Default control. Remove file if it exists.
60 QFile::remove(file_info.filePath());
61 return true;
62 }
63
1/2
✗ Branch 7 not taken.
✓ Branch 8 taken 146 times.
146 if (!QDir().mkpath(file_info.path())) {
64 // TODO: error message
65 return false;
66 }
67
68 // write updated data to file
69 146 QFile file(file_info.filePath());
70
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 146 times.
146 if (!file.open(QIODevice::WriteOnly)) {
71 qCWarning(DISMAN_BACKEND) << "Failed to open config control file for writing."
72 << file.errorString();
73 return false;
74 }
75 146 file.write(QJsonDocument::fromVariant(map).toJson());
76
2/2
✓ Branch 10 taken 146 times.
✓ Branch 11 taken 146 times.
292 qCDebug(DISMAN_BACKEND) << "Control saved to:" << file.fileName();
77 146 return true;
78 146 }
79
80 template<typename T>
81 9596 static T from_variant(QVariant const& var, T default_value = T())
82 {
83
2/2
✓ Branch 1 taken 3478 times.
✓ Branch 2 taken 1320 times.
9596 if (var.canConvert<double>()) {
84 6956 return var.toDouble();
85
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1320 times.
2640 } else if (var.canConvert<int>()) {
86 return var.toInt();
87
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1320 times.
2640 } else if (var.canConvert<bool>()) {
88 return var.toBool();
89 }
90 2640 return default_value;
91 }
92
93 template<>
94 265 QString from_variant(QVariant const& var, QString default_value)
95 {
96
1/2
✓ Branch 1 taken 265 times.
✗ Branch 2 not taken.
265 if (var.canConvert<QString>()) {
97 265 return var.toString();
98 }
99 return default_value;
100 }
101
102 }
103