Line data Source code
1 : /*! 2 : * @file SystemCommandExecutor.cpp 3 : * @brief Implementation of the SystemCommandExecutor class. 4 : * @version 0.1 5 : * @date 2025-02-12 6 : * @details This file contains the implementation of the SystemCommandExecutor 7 : * class, which is used to execute system commands and read files. 8 : * @author Félix LE BIHAN (@Fle-bihh) 9 : * @author Tiago Pereira (@t-pereira06) 10 : * @author Ricardo Melo (@reomelo) 11 : * @author Michel Batista (@MicchelFAB) 12 : * 13 : * @copyright Copyright (c) 2025 14 : */ 15 : 16 : #include "SystemCommandExecutor.hpp" 17 : 18 : /*! 19 : * @brief Executes a system command. 20 : * @param command The command to execute as a QString. 21 : * @return The standard output of the executed command, trimmed of any leading 22 : * or trailing whitespace. 23 : */ 24 : 25 2 : QString SystemCommandExecutor::executeCommand(const QString &command) const 26 : { 27 2 : QProcess process; 28 6 : process.start("sh", {"-c", command}); 29 2 : process.waitForFinished(); 30 6 : return process.readAllStandardOutput().trimmed(); 31 : } 32 : 33 : /*! 34 : * @brief Reads a file and returns its contents. 35 : * @param filePath The path to the file to read as a QString. 36 : * @return The contents of the file, trimmed of any leading or trailing 37 : * whitespace, or an empty string if the file could not be opened. 38 : */ 39 2 : QString SystemCommandExecutor::readFile(const QString &filePath) const 40 : { 41 4 : QFile file(filePath); 42 2 : if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { 43 1 : QTextStream in(&file); 44 2 : return in.readLine().trimmed(); 45 : } 46 1 : return ""; 47 : }