Hotwheels-Cluster 1.2
Creation of Cluster APP for SEA:ME project.
 
Loading...
Searching...
No Matches
MileageFileHandler.cpp
Go to the documentation of this file.
1
17
19#include <QDebug>
20
46
61{
62 if (!existsFunc(filePath)) {
63 QFile file(filePath);
64 if (openFunc(file, QIODevice::WriteOnly | QIODevice::Text)) {
65 if (!writeFunc(file, "0.0")) {
66 qWarning() << "Failed to initialize mileage file with default value.";
67 }
68 file.close();
69 qDebug() << "Mileage file created at:" << filePath;
70 } else {
71 qWarning() << "Failed to create mileage file at:" << filePath;
72 }
73 }
74}
75
86{
87 QFile file(filePath);
88 if (!openFunc(file, QIODevice::ReadOnly | QIODevice::Text)) {
89 qWarning() << "Failed to open mileage file for reading:" << filePath;
90 return 0.0;
91 }
92
93 QString content = readFunc(file);
94 file.close();
95
96 bool ok = false;
97 double mileage = content.toDouble(&ok);
98 if (!ok) {
99 qWarning() << "Invalid mileage value in file. Defaulting to 0.";
100 return 0.0;
101 }
102 return mileage;
103}
104
112void MileageFileHandler::writeMileage(double mileage) const
113{
114 QFile file(filePath);
115 if (!openFunc(file, QIODevice::WriteOnly | QIODevice::Text)) {
116 qWarning() << "Failed to open mileage file for writing:" << filePath;
117 return;
118 }
119
120 bool success = writeFunc(file, QString::number(mileage, 'f', 2));
121 if (!success) {
122 qWarning() << "Failed to write mileage data.";
123 }
124 file.close();
125}
Definition of the MileageFileHandler class.
std::function< bool(const QString &)> FileExistsFunc
std::function< bool(QFile &, const QString &)> FileWriteFunc
std::function< QString(QFile &)> FileReadFunc
std::function< bool(QFile &, QIODevice::OpenMode)> FileOpenFunc
double readMileage() const override
Reads the mileage from the file.
MileageFileHandler(const QString &filePath, FileOpenFunc openFunc=FileController::open, FileReadFunc readFunc=FileController::read, FileWriteFunc writeFunc=FileController::write, FileExistsFunc existsFunc=FileController::exists)
Constructs a MileageFileHandler object with the specified file path and functions.
void ensureFileExists() const
Checks if the file exists and creates it if it does not.
void writeMileage(double mileage) const override
Writes the mileage to the file.