64 if (
openFunc(file, QIODevice::WriteOnly | QIODevice::Text)) {
66 qWarning() <<
"Failed to initialize mileage file with default value.";
69 qDebug() <<
"Mileage file created at:" <<
filePath;
71 qWarning() <<
"Failed to create mileage file at:" <<
filePath;
88 if (!
openFunc(file, QIODevice::ReadOnly | QIODevice::Text)) {
89 qWarning() <<
"Failed to open mileage file for reading:" <<
filePath;
97 double mileage = content.toDouble(&ok);
99 qWarning() <<
"Invalid mileage value in file. Defaulting to 0.";
115 if (!
openFunc(file, QIODevice::WriteOnly | QIODevice::Text)) {
116 qWarning() <<
"Failed to open mileage file for writing:" <<
filePath;
120 bool success =
writeFunc(file, QString::number(mileage,
'f', 2));
122 qWarning() <<
"Failed to write mileage data.";
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.
FileExistsFunc existsFunc
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.