LCOV - code coverage report
Current view: top level - mileage - MileageFileHandler.cpp (source / functions) Hit Total Coverage
Test: filtered.info Lines: 31 37 83.8 %
Date: 2025-07-25 11:48:17 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /*!
       2             :  * @file MileageFileHandler.cpp
       3             :  * @brief Implementation of the MileageFileHandler class.
       4             :  * @version 0.1
       5             :  * @date 2025-01-31
       6             :  * @details This file contains the implementation of the MileageFileHandler
       7             :  * class, which is used to read and write the mileage to a file.
       8             :  * @note This class is used to read and write the mileage to a file.
       9             :  * @author Félix LE BIHAN (@Fle-bihh)
      10             :  * @author Tiago Pereira (@t-pereira06)
      11             :  * @author Ricardo Melo (@reomelo)
      12             :  * @author Michel Batista (@MicchelFAB)
      13             :  * @warning Ensure that the file path is valid and accessible.
      14             :  * @see MileageFileHandler.hpp
      15             :  * @copyright Copyright (c) 2025
      16             :  */
      17             : 
      18             : #include "MileageFileHandler.hpp"
      19             : #include <QDebug>
      20             : 
      21             : /*!
      22             :  * @brief Constructs a MileageFileHandler object with the specified file path and functions.
      23             :  * @param filePath The path to the mileage file.
      24             :  * @param openFunc The function to open a file.
      25             :  * @param readFunc The function to read from a file.
      26             :  * @param writeFunc The function to write to a file.
      27             :  * @param existsFunc The function to check if a file exists.
      28             :  *
      29             :  * @details This constructor initializes the MileageFileHandler object with the specified
      30             :  * file path and functions. It also calls ensureFileExists() to create the file if it
      31             :  * does not exist.
      32             :  */
      33           5 : MileageFileHandler::MileageFileHandler(const QString &filePath,
      34             :                                                                            FileOpenFunc openFunc,
      35             :                                                                            FileReadFunc readFunc,
      36             :                                                                            FileWriteFunc writeFunc,
      37           5 :                                                                            FileExistsFunc existsFunc)
      38             :         : filePath(filePath)
      39             :         , openFunc(openFunc)
      40             :         , readFunc(readFunc)
      41             :         , writeFunc(writeFunc)
      42           5 :         , existsFunc(existsFunc)
      43             : {
      44           5 :         ensureFileExists();
      45           5 : }
      46             : 
      47             : /*!
      48             :  * @brief Checks if the file exists and creates it if it does not.
      49             :  *
      50             :  * @details This method checks if the file exists by calling the existsFunc
      51             :  * function. If the file does not exist, it is created by calling the openFunc
      52             :  * function with the QIODevice::WriteOnly and QIODevice::Text flags. The default
      53             :  * value of the mileage is written to the file by calling the writeFunc function.
      54             :  * If the file is created successfully, a message is logged to the console
      55             :  * indicating that the file was created.
      56             :  *
      57             :  * @warning If the file cannot be created, a warning message is logged to the
      58             :  * console.
      59             :  */
      60           5 : void MileageFileHandler::ensureFileExists() const
      61             : {
      62           5 :         if (!existsFunc(filePath)) {
      63          10 :                 QFile file(filePath);
      64           5 :                 if (openFunc(file, QIODevice::WriteOnly | QIODevice::Text)) {
      65           1 :                         if (!writeFunc(file, "0.0")) {
      66           0 :                                 qWarning() << "Failed to initialize mileage file with default value.";
      67             :                         }
      68           1 :                         file.close();
      69           1 :                         qDebug() << "Mileage file created at:" << filePath;
      70             :                 } else {
      71           4 :                         qWarning() << "Failed to create mileage file at:" << filePath;
      72             :                 }
      73             :         }
      74           5 : }
      75             : 
      76             : /*!
      77             :  * @brief Reads the mileage from the file.
      78             :  * @return The mileage value read from the file or 0.0 if the file is invalid.
      79             :  *
      80             :  * @details This method reads the mileage value from the file by calling the
      81             :  * readFunc function. If the file cannot be opened for reading, a warning message
      82             :  * is logged to the console. If the mileage value is invalid, a warning message is
      83             :  * logged to the console and the default value of 0.0 is returned.
      84             :  */
      85           2 : double MileageFileHandler::readMileage() const
      86             : {
      87           4 :         QFile file(filePath);
      88           2 :         if (!openFunc(file, QIODevice::ReadOnly | QIODevice::Text)) {
      89           0 :                 qWarning() << "Failed to open mileage file for reading:" << filePath;
      90           0 :                 return 0.0;
      91             :         }
      92             : 
      93           4 :         QString content = readFunc(file);
      94           2 :         file.close();
      95             : 
      96           2 :         bool ok = false;
      97           2 :         double mileage = content.toDouble(&ok);
      98           2 :         if (!ok) {
      99           1 :                 qWarning() << "Invalid mileage value in file. Defaulting to 0.";
     100           1 :                 return 0.0;
     101             :         }
     102           1 :         return mileage;
     103             : }
     104             : /*!
     105             :  * @brief Writes the mileage to the file.
     106             :  * @param mileage The mileage value to write.
     107             :  *
     108             :  * @details This method writes the mileage value to the file by calling the
     109             :  * writeFunc function. If the file cannot be opened for writing, a warning message
     110             :  * is logged to the console.
     111             :  */
     112           1 : void MileageFileHandler::writeMileage(double mileage) const
     113             : {
     114           1 :         QFile file(filePath);
     115           1 :         if (!openFunc(file, QIODevice::WriteOnly | QIODevice::Text)) {
     116           0 :                 qWarning() << "Failed to open mileage file for writing:" << filePath;
     117           0 :                 return;
     118             :         }
     119             : 
     120           1 :         bool success = writeFunc(file, QString::number(mileage, 'f', 2));
     121           1 :         if (!success) {
     122           0 :                 qWarning() << "Failed to write mileage data.";
     123             :         }
     124           1 :         file.close();
     125             : }

Generated by: LCOV version 1.14