From a19a843072a661d917956131d87bf0174ba827e2 Mon Sep 17 00:00:00 2001 From: Filipe Marques <eagle.software3@gmail.com> Date: Sat, 28 Feb 2015 13:16:16 +0000 Subject: [PATCH] merge development branch --- src/Config.cpp | 23 +- src/Config.hpp | 23 +- src/Data.cpp | 120 ++++---- src/Data.hpp | 25 +- src/Device.cpp | 23 +- src/Device.hpp | 24 +- src/Enums.hpp | 23 +- src/Mhwd.cpp | 660 ++++++++++++++++++++++---------------------- src/Mhwd.hpp | 46 ++- src/Printer.cpp | 119 ++++---- src/Printer.hpp | 32 ++- src/Transaction.cpp | 23 +- src/Transaction.hpp | 23 +- src/main.cpp | 20 +- 14 files changed, 701 insertions(+), 483 deletions(-) diff --git a/src/Config.cpp b/src/Config.cpp index 37b88d4..065fd76 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -1,8 +1,25 @@ /* - * Config.cpp + * This file is part of the mhwd - Manjaro Hardware Detection project + * + * mhwd - Manjaro Hardware Detection + * Roland Singer <roland@manjaro.org> + * Åukasz Matysiak <december0123@gmail.com> + * Filipe Marques <eagle.software3@gmail.com> * - * Created on: 28 sie 2014 - * Author: dec + * Copyright (C) 2007 Free Software Foundation, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include <fstream> diff --git a/src/Config.hpp b/src/Config.hpp index f4b7593..701861c 100644 --- a/src/Config.hpp +++ b/src/Config.hpp @@ -1,8 +1,25 @@ /* - * Config.hpp + * This file is part of the mhwd - Manjaro Hardware Detection project + * + * mhwd - Manjaro Hardware Detection + * Roland Singer <roland@manjaro.org> + * Åukasz Matysiak <december0123@gmail.com> + * Filipe Marques <eagle.software3@gmail.com> * - * Created on: 28 sie 2014 - * Author: dec + * Copyright (C) 2007 Free Software Foundation, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. */ #ifndef CONFIG_HPP_ diff --git a/src/Data.cpp b/src/Data.cpp index d2ae78f..4d226ed 100644 --- a/src/Data.cpp +++ b/src/Data.cpp @@ -1,8 +1,25 @@ /* - * Data.cpp + * This file is part of the mhwd - Manjaro Hardware Detection project + * + * mhwd - Manjaro Hardware Detection + * Roland Singer <roland@manjaro.org> + * Åukasz Matysiak <december0123@gmail.com> + * Filipe Marques <eagle.software3@gmail.com> * - * Created on: 28 sie 2014 - * Author: dec + * Copyright (C) 2007 Free Software Foundation, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include <dirent.h> @@ -32,13 +49,13 @@ void Data::updateInstalledConfigData() { // Clear config vectors in each device element for (auto&& PCIDevice = PCIDevices.begin(); - PCIDevice != PCIDevices.end(); PCIDevice++) + PCIDevice != PCIDevices.end(); ++PCIDevice) { (*PCIDevice)->installedConfigs_.clear(); } for (auto&& USBDevice = USBDevices.begin(); - USBDevice != USBDevices.end(); USBDevice++) + USBDevice != USBDevices.end(); ++USBDevice) { (*USBDevice)->installedConfigs_.clear(); } @@ -280,12 +297,11 @@ void Data::getAllDependenciesToInstall(std::shared_ptr<Config> config, std::vector<std::shared_ptr<Config>> *dependencies) { for (auto&& configDependency = config->dependencies_.begin(); - configDependency != config->dependencies_.end(); configDependency++) + configDependency != config->dependencies_.end(); ++configDependency) { auto found = std::find_if(installedConfigs.begin(), installedConfigs.end(), - [configDependency](const std::shared_ptr<Config> rhs)->bool - { - return (*configDependency == rhs->name_); + [configDependency](const std::shared_ptr<Config>& rhs) -> bool { + return (rhs->name_ == *configDependency); }); if (found != installedConfigs.end()) @@ -295,9 +311,8 @@ void Data::getAllDependenciesToInstall(std::shared_ptr<Config> config, else { found = std::find_if(dependencies->begin(), dependencies->end(), - [configDependency](const std::shared_ptr<Config> rhs)->bool - { - return (*configDependency == rhs->name_); + [configDependency](const std::shared_ptr<Config>& rhs) -> bool { + return (rhs->name_ == *configDependency); }); if (found != dependencies->end()) @@ -315,7 +330,7 @@ void Data::getAllDependenciesToInstall(std::shared_ptr<Config> config, } else { - dependencies->push_back(std::shared_ptr<Config>{dependconfig}); + dependencies->emplace_back(dependconfig); getAllDependenciesToInstall(dependconfig, installedConfigs, dependencies); } } @@ -339,7 +354,7 @@ std::shared_ptr<Config> Data::getDatabaseConfig(const std::string configName, } for (auto&& config = allConfigs.begin(); config != allConfigs.end(); - config++) + ++config) { if (configName == (*config)->name_) { @@ -366,17 +381,16 @@ std::vector<std::shared_ptr<Config>> Data::getAllLocalConflicts(std::shared_ptr< installedConfigs = installedPCIConfigs; } - dependencies.push_back(std::shared_ptr<Config>{config}); + dependencies.emplace_back(config); for (auto&& dependency = dependencies.begin(); - dependency != dependencies.end(); dependency++) + dependency != dependencies.end(); ++dependency) { - for (auto&& dependencyConflict = - (*dependency)->conflicts_.begin(); - dependencyConflict != (*dependency)->conflicts_.end(); dependencyConflict++) + for (auto&& dependencyConflict = (*dependency)->conflicts_.begin(); + dependencyConflict != (*dependency)->conflicts_.end(); ++dependencyConflict) { for (auto&& installedConfig = installedConfigs.begin(); - installedConfig != installedConfigs.end(); installedConfig++) + installedConfig != installedConfigs.end(); ++installedConfig) { if ((*dependencyConflict) != (*installedConfig)->name_) { @@ -387,7 +401,7 @@ std::vector<std::shared_ptr<Config>> Data::getAllLocalConflicts(std::shared_ptr< // Check if already in vector bool found = false; for (auto&& conflict = conflicts.begin(); - conflict != conflicts.end(); conflict++) + conflict != conflicts.end(); ++conflict) { if ((*conflict)->name_ == (*dependencyConflict)) { @@ -402,7 +416,7 @@ std::vector<std::shared_ptr<Config>> Data::getAllLocalConflicts(std::shared_ptr< } else { - conflicts.push_back(std::shared_ptr<Config>{*installedConfig}); + conflicts.emplace_back(*installedConfig); break; } } @@ -430,7 +444,7 @@ std::vector<std::shared_ptr<Config>> Data::getAllLocalRequirements(std::shared_p // Check if this config is required by another installed config for (auto&& installedConfig = installedConfigs.begin(); - installedConfig != installedConfigs.end(); installedConfig++) + installedConfig != installedConfigs.end(); ++installedConfig) { for (auto&& dependency = (*installedConfig)->dependencies_.begin(); dependency != (*installedConfig)->dependencies_.end(); dependency++) @@ -444,7 +458,7 @@ std::vector<std::shared_ptr<Config>> Data::getAllLocalRequirements(std::shared_p // Check if already in vector bool found = false; for (auto&& requirement = requirements.begin(); - requirement != requirements.end(); requirement++) + requirement != requirements.end(); ++requirement) { if ((*requirement)->name_ == (*installedConfig)->name_) { @@ -455,7 +469,7 @@ std::vector<std::shared_ptr<Config>> Data::getAllLocalRequirements(std::shared_p if (!found) { - requirements.push_back(std::shared_ptr<Config>{*installedConfig}); + requirements.emplace_back(*installedConfig); break; } } @@ -482,29 +496,27 @@ void Data::fillDevices(std::string type) } // Get the hardware devices - hd_data_t *hd_data = new hd_data_t(); - hd_t *hd = hd_list(hd_data, hw, 1, nullptr); - hd_t *beginningOfhd = hd; + std::unique_ptr<hd_data_t> hd_data{new hd_data_t()}; + hd_t *hd = hd_list(hd_data.get(), hw, 1, nullptr); - Device *device; - for (; hd; hd = hd->next) + std::unique_ptr<Device> device; + for (hd_t *hdIter = hd; hdIter; hdIter = hdIter->next) { - device = new Device(); + device.reset(new Device()); device->type_ = type; - device->classID_ = from_Hex(hd->base_class.id, 2) + from_Hex(hd->sub_class.id, 2).toLower(); - device->vendorID_ = from_Hex(hd->vendor.id, 4).toLower(); - device->deviceID_ = from_Hex(hd->device.id, 4).toLower(); - device->className_ = from_CharArray(hd->base_class.name); - device->vendorName_ = from_CharArray(hd->vendor.name); - device->deviceName_ = from_CharArray(hd->device.name); - device->sysfsBusID_ = from_CharArray(hd->sysfs_bus_id); - device->sysfsID_ = from_CharArray(hd->sysfs_id); - devices->push_back(std::shared_ptr<Device>{device}); - } - - hd_free_hd_list(beginningOfhd); - hd_free_hd_data(hd_data); - delete hd_data; + device->classID_ = from_Hex(hdIter->base_class.id, 2) + from_Hex(hdIter->sub_class.id, 2).toLower(); + device->vendorID_ = from_Hex(hdIter->vendor.id, 4).toLower(); + device->deviceID_ = from_Hex(hdIter->device.id, 4).toLower(); + device->className_ = from_CharArray(hdIter->base_class.name); + device->vendorName_ = from_CharArray(hdIter->vendor.name); + device->deviceName_ = from_CharArray(hdIter->device.name); + device->sysfsBusID_ = from_CharArray(hdIter->sysfs_bus_id); + device->sysfsID_ = from_CharArray(hdIter->sysfs_id); + devices->emplace_back(device.release()); + } + + hd_free_hd_list(hd); + hd_free_hd_data(hd_data.get()); } void Data::fillAllConfigs(std::string type) @@ -526,15 +538,15 @@ void Data::fillAllConfigs(std::string type) for (auto&& configPath = configPaths.begin(); configPath != configPaths.end(); ++configPath) { - Config *config = new Config((*configPath), type); + std::unique_ptr<Config> config{new Config((*configPath), type)}; if (config->readConfigFile((*configPath))) { - configs->push_back(std::shared_ptr<Config>{config}); + configs->emplace_back(config.release()); } else { - invalidConfigs.push_back(std::shared_ptr<Config>{config}); + invalidConfigs.emplace_back(config.release()); } } } @@ -550,8 +562,7 @@ bool Data::fillConfig(std::shared_ptr<Config> config, std::string configPath, st // Add new HardwareIDs group to vector if vector is empty if (config->hwdIDs_.empty()) { - Config::HardwareID hwdID; - config->hwdIDs_.push_back(hwdID); + config->hwdIDs_.emplace_back(); } return config->readConfigFile(config->configPath_); @@ -562,8 +573,7 @@ std::vector<std::string> Data::getRecursiveDirectoryFileList(const std::string& { std::vector<std::string> list; struct dirent *dir = nullptr; - DIR *d = opendir(directoryPath.c_str()); - + DIR* d = opendir(directoryPath.c_str()); if (d) { while (nullptr != (dir = readdir(d))) @@ -719,7 +729,7 @@ void Data::addConfigSorted(std::vector<std::shared_ptr<Config>>& configs, } } - configs.push_back(std::shared_ptr<Config>(config)); + configs.emplace_back(config); } Vita::string Data::from_Hex(std::uint16_t hexnum, int fill) @@ -729,12 +739,12 @@ Vita::string Data::from_Hex(std::uint16_t hexnum, int fill) return stream.str(); } -Vita::string Data::from_CharArray(char* c) +std::string Data::from_CharArray(char* c) { if (nullptr == c) { return ""; } - return Vita::string(c); + return std::string(c); } diff --git a/src/Data.hpp b/src/Data.hpp index ff8c78d..540397c 100644 --- a/src/Data.hpp +++ b/src/Data.hpp @@ -1,8 +1,25 @@ /* - * Data.hpp + * This file is part of the mhwd - Manjaro Hardware Detection project + * + * mhwd - Manjaro Hardware Detection + * Roland Singer <roland@manjaro.org> + * Åukasz Matysiak <december0123@gmail.com> + * Filipe Marques <eagle.software3@gmail.com> * - * Created on: 28 sie 2014 - * Author: dec + * Copyright (C) 2007 Free Software Foundation, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. */ #ifndef DATA_HPP_ @@ -74,7 +91,7 @@ private: void updateConfigData(); Vita::string from_Hex(uint16_t hexnum, int fill); - Vita::string from_CharArray(char* c); + std::string from_CharArray(char* c); }; #endif /* DATA_HPP_ */ diff --git a/src/Device.cpp b/src/Device.cpp index 3928313..f923aa8 100644 --- a/src/Device.cpp +++ b/src/Device.cpp @@ -1,8 +1,25 @@ /* - * Device.cpp + * This file is part of the mhwd - Manjaro Hardware Detection project + * + * mhwd - Manjaro Hardware Detection + * Roland Singer <roland@manjaro.org> + * Åukasz Matysiak <december0123@gmail.com> + * Filipe Marques <eagle.software3@gmail.com> * - * Created on: 28 sie 2014 - * Author: dec + * Copyright (C) 2007 Free Software Foundation, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include "Device.hpp" diff --git a/src/Device.hpp b/src/Device.hpp index cb9338d..6d3d74d 100644 --- a/src/Device.hpp +++ b/src/Device.hpp @@ -1,8 +1,25 @@ /* - * Device.hpp + * This file is part of the mhwd - Manjaro Hardware Detection project + * + * mhwd - Manjaro Hardware Detection + * Roland Singer <roland@manjaro.org> + * Åukasz Matysiak <december0123@gmail.com> + * Filipe Marques <eagle.software3@gmail.com> * - * Created on: 28 sie 2014 - * Author: dec + * Copyright (C) 2007 Free Software Foundation, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. */ #ifndef DEVICE_HPP_ @@ -13,7 +30,6 @@ #include <vector> #include "Config.hpp" -#include "Enums.hpp" struct Device { diff --git a/src/Enums.hpp b/src/Enums.hpp index f762ed7..7253955 100644 --- a/src/Enums.hpp +++ b/src/Enums.hpp @@ -1,8 +1,25 @@ /* - * Enums.hpp + * This file is part of the mhwd - Manjaro Hardware Detection project + * + * mhwd - Manjaro Hardware Detection + * Roland Singer <roland@manjaro.org> + * Åukasz Matysiak <december0123@gmail.com> + * Filipe Marques <eagle.software3@gmail.com> * - * Created on: 28 sie 2014 - * Author: dec + * Copyright (C) 2007 Free Software Foundation, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. */ #ifndef ENUMS_HPP_ diff --git a/src/Mhwd.cpp b/src/Mhwd.cpp index 925c334..0e0e792 100644 --- a/src/Mhwd.cpp +++ b/src/Mhwd.cpp @@ -1,37 +1,51 @@ /* - * Mhwd.cpp + * This file is part of the mhwd - Manjaro Hardware Detection project + * + * mhwd - Manjaro Hardware Detection + * Roland Singer <roland@manjaro.org> + * Åukasz Matysiak <december0123@gmail.com> + * Filipe Marques <eagle.software3@gmail.com> * - * Created on: 26 sie 2014 - * Author: dec + * Copyright (C) 2007 Free Software Foundation, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. */ +#include "Mhwd.hpp" +#include "vita/string.hpp" + #include <unistd.h> #include <sys/stat.h> #include <sys/types.h> #include <algorithm> #include <cctype> -#include <cstdlib> #include <cstdio> +#include <cstdlib> #include <cstring> #include <fstream> #include <iomanip> #include <iostream> #include <memory> +#include <stdexcept> #include <string> #include <vector> -#include "Mhwd.hpp" -#include "vita/string.hpp" - Mhwd::Mhwd() : arguments_(), data_(), printer_() { } -Mhwd::~Mhwd() -{ -} - bool Mhwd::performTransaction(std::shared_ptr<Config> config, MHWD::TRANSACTIONTYPE transactionType) { Transaction transaction (data_, config, transactionType, @@ -108,8 +122,7 @@ bool Mhwd::performTransaction(std::shared_ptr<Config> config, MHWD::TRANSACTIONT printer_.printError("config '" + config->name_ + "' is not installed!"); break; case MHWD::STATUS::ERROR_ALREADY_INSTALLED: - printer_.printWarning( - "a version of config '" + config->name_ + + printer_.printWarning("a version of config '" + config->name_ + "' is already installed!\nUse -f/--force to force installation..."); break; case MHWD::STATUS::ERROR_NO_MATCH_LOCAL_CONFIG: @@ -156,27 +169,27 @@ bool Mhwd::isUserRoot() const std::string Mhwd::checkEnvironment() { - std::string retValue; + std::string missingDir; // Check if required directories exists. Otherwise return missing directory... if (!dirExists(MHWD_USB_CONFIG_DIR)) { - retValue = MHWD_USB_CONFIG_DIR; + missingDir = MHWD_USB_CONFIG_DIR; } if (!dirExists(MHWD_PCI_CONFIG_DIR)) { - retValue = MHWD_PCI_CONFIG_DIR; + missingDir = MHWD_PCI_CONFIG_DIR; } if (!dirExists(MHWD_USB_DATABASE_DIR)) { - retValue = MHWD_USB_DATABASE_DIR; + missingDir = MHWD_USB_DATABASE_DIR; } if (!dirExists(MHWD_PCI_DATABASE_DIR)) { - retValue = MHWD_PCI_DATABASE_DIR; + missingDir = MHWD_PCI_DATABASE_DIR; } - return retValue; + return missingDir; } void Mhwd::printDeviceDetails(std::string type, FILE *f) @@ -193,14 +206,13 @@ void Mhwd::printDeviceDetails(std::string type, FILE *f) std::unique_ptr<hd_data_t> hd_data{new hd_data_t()}; hd_t *hd = hd_list(hd_data.get(), hw, 1, nullptr); - hd_t *beginningOfhd = hd; - for (; hd; hd = hd->next) + for (hd_t* hdIter = hd; hdIter; hdIter = hdIter->next) { - hd_dump_entry(hd_data.get(), hd, f); + hd_dump_entry(hd_data.get(), hdIter, f); } - hd_free_hd_list(beginningOfhd); + hd_free_hd_list(hd); hd_free_hd_data(hd_data.get()); } @@ -247,7 +259,7 @@ std::shared_ptr<Config> Mhwd::getDatabaseConfig(const std::string& configName, } for (auto&& iterator = allConfigs->begin(); - iterator != allConfigs->end(); iterator++) + iterator != allConfigs->end(); ++iterator) { if (configName == (*iterator)->name_) { @@ -274,7 +286,7 @@ std::shared_ptr<Config> Mhwd::getAvailableConfig(const std::string& configName, } for (auto&& device = devices->begin(); device != devices->end(); - device++) + ++device) { if ((*device)->availableConfigs_.empty()) { @@ -454,18 +466,16 @@ bool Mhwd::copyFile(const std::string& source, const std::string destination, co { std::ifstream src(source, std::ios::binary); std::ofstream dst(destination, std::ios::binary); - if (src.is_open() && dst.is_open()) - { - dst << src.rdbuf(); - mode_t process_mask = umask(0); - chmod(destination.c_str(), mode); - umask(process_mask); - return true; - } - else + if (!src || !dst) { - return false; + return false; } + + dst << src.rdbuf(); + mode_t process_mask = umask(0); + chmod(destination.c_str(), mode); + umask(process_mask); + return true; } bool Mhwd::removeDirectory(const std::string& directory) @@ -527,10 +537,8 @@ bool Mhwd::dirExists(const std::string& path) { return false; } - else - { - return true; - } + + return true; } bool Mhwd::createDir(const std::string& path, const mode_t mode) @@ -540,7 +548,7 @@ bool Mhwd::createDir(const std::string& path, const mode_t mode) umask(process_mask); constexpr unsigned short SUCCESS = 0; - return (ret == SUCCESS); + return (SUCCESS == ret); } MHWD::STATUS Mhwd::installConfig(std::shared_ptr<Config> config) @@ -632,16 +640,16 @@ bool Mhwd::runScript(std::shared_ptr<Config> config, MHWD::TRANSACTIONTYPE opera std::vector<std::shared_ptr<Device>> devices; data_.getAllDevicesOfConfig(config, foundDevices); - for (auto&& iterator = foundDevices.begin(); - iterator != foundDevices.end(); iterator++) + for (auto&& foundDevice = foundDevices.begin(); + foundDevice != foundDevices.end(); ++foundDevice) { bool found = false; // Check if already in list - for (auto&& dev = devices.begin(); dev != devices.end(); dev++) + for (auto&& dev = devices.begin(); dev != devices.end(); ++dev) { - if ((*iterator)->sysfsBusID_ == (*dev)->sysfsBusID_ - && (*iterator)->sysfsID_ == (*dev)->sysfsID_) + if ((*foundDevice)->sysfsBusID_ == (*dev)->sysfsBusID_ + && (*foundDevice)->sysfsID_ == (*dev)->sysfsID_) { found = true; break; @@ -650,18 +658,18 @@ bool Mhwd::runScript(std::shared_ptr<Config> config, MHWD::TRANSACTIONTYPE opera if (!found) { - devices.push_back(std::shared_ptr<Device>{*iterator}); + devices.push_back(std::shared_ptr<Device>{*foundDevice}); } } - for (auto&& dev = devices.begin(); dev != devices.end(); dev++) + for (auto&& dev = devices.begin(); dev != devices.end(); ++dev) { Vita::string busID = (*dev)->sysfsBusID_; if ("PCI" == config->type_) { std::vector<Vita::string> split = Vita::string(busID).replace(".", ":").explode(":"); - const unsigned int size = split.size(); + const unsigned long size = split.size(); if (size >= 3) { @@ -689,7 +697,7 @@ bool Mhwd::runScript(std::shared_ptr<Config> config, MHWD::TRANSACTIONTYPE opera char buff[512]; while (fgets(buff, sizeof(buff), in) != nullptr) { - printer_.printMessage(MHWD::MESSAGETYPE::CONSOLE_OUTPUT, std::string(buff)); + printer_.printMessage(MHWD::MESSAGETYPE::CONSOLE_OUTPUT, buff); } int stat = pclose(in); @@ -710,264 +718,263 @@ bool Mhwd::runScript(std::shared_ptr<Config> config, MHWD::TRANSACTIONTYPE opera } } +void Mhwd::set_version_mhwd(std::string version_of_software, std::string year_copyright) +{ + version_ = version_of_software; + year_ = year_copyright; +} + +void Mhwd::tryToParseCmdLineOptions(int argc, char* argv[], bool& autoConfigureNonFreeDriver, + std::string& operationType, std::string& autoConfigureClassID) +{ + if (argc <= 1) + { + arguments_.LIST_AVAILABLE = true; + } + for (int nArg = 1; nArg < argc; ++nArg) + { + const std::string option{ argv[nArg] }; + if (("-h" == option) || ("--help" == option)) + { + printer_.printHelp(); + } + else if (("-v" == option) || ("--version" == option)) + { + printer_.printVersion(version_, year_); + } + else if (("-f" == option) || ("--force" == option)) + { + arguments_.FORCE = true; + } + else if (("-d" == option) || ("--detail" == option)) + { + arguments_.DETAIL = true; + } + else if (("-la" == option) || ("--listall" == option)) + { + arguments_.LIST_ALL = true; + } + else if (("-li" == option) || ("--listinstalled" == option)) + { + arguments_.LIST_INSTALLED = true; + } + else if (("-l" == option) || ("--list" == option)) + { + arguments_.LIST_AVAILABLE = true; + } + else if (("-lh" == option) || ("--listhardware" == option)) + { + arguments_.LIST_HARDWARE = true; + } + else if ("--pci" == option) + { + arguments_.SHOW_PCI = true; + } + else if ("--usb" == option) + { + arguments_.SHOW_USB = true; + } + else if (("-a" == option) || ("--auto" == option)) + { + if ((nArg + 3) >= argc) + { + throw std::runtime_error{"invalid use of option: -a/--auto\n"}; + } + else + { + const std::string deviceType{ argv[nArg + 1] }; + const std::string driverType{ argv[nArg + 2] }; + const std::string classID{ argv[nArg + 3] }; + if ((("pci" != deviceType) && ("usb" != deviceType)) + || (("free" != driverType) && ("nonfree" != driverType))) + { + throw std::runtime_error{"invalid use of option: -a/--auto\n"}; + } + else + { + operationType = Vita::string{ deviceType }.toUpper(); + autoConfigureNonFreeDriver = ("nonfree" == driverType); + autoConfigureClassID = Vita::string(classID).toLower().trim(); + arguments_.AUTOCONFIGURE = true; + nArg += 3; + } + } + } + else if (("-ic" == option) || ("--installcustom" == option)) + { + if ((nArg + 1) >= argc) + { + throw std::runtime_error{"invalid use of option: -ic/--installcustom\n"}; + } + else + { + const std::string deviceType{ argv[++nArg] }; + if (("pci" != deviceType) && ("usb" != deviceType)) + { + throw std::runtime_error{"invalid use of option: -ic/--installcustom\n"}; + } + else + { + operationType = Vita::string{ deviceType }.toUpper(); + arguments_.CUSTOM_INSTALL = true; + } + } + } + else if (("-i" == option) || ("--install" == option)) + { + if ((nArg + 1) >= argc) + { + throw std::runtime_error{"invalid use of option: -i/--install\n"}; + } + else + { + const std::string deviceType{ argv[++nArg] }; + if (("pci" != deviceType) && ("usb" != deviceType)) + { + throw std::runtime_error{"invalid use of option: -i/--install\n"}; + } + else + { + operationType = Vita::string{ deviceType }.toUpper(); + arguments_.INSTALL = true; + } + } + } + else if (("-r" == option) || ("--remove" == option)) + { + if ((nArg + 1) >= argc) + { + throw std::runtime_error{"invalid use of option: -r/--remove\n"}; + } + else + { + const std::string deviceType{ argv[++nArg] }; + if (("pci" != deviceType) && ("usb" != deviceType)) + { + throw std::runtime_error{"invalid use of option: -r/--remove\n"}; + } + else + { + operationType = Vita::string{ deviceType }.toUpper(); + arguments_.REMOVE = true; + } + } + } + else if ("--pmcachedir" == option) + { + if (nArg + 1 >= argc) + { + throw std::runtime_error{"invalid use of option: --pmcachedir\n"}; + } + else + { + data_.environment.PMCachePath = Vita::string(argv[++nArg]).trim("\"").trim(); + } + } + else if ("--pmconfig" == option) + { + if (nArg + 1 >= argc) + { + throw std::runtime_error{"invalid use of option: --pmconfig\n"}; + } + else + { + data_.environment.PMConfigPath = Vita::string(argv[++nArg]).trim("\"").trim(); + } + } + else if ("--pmroot" == option) + { + if (nArg + 1 >= argc) + { + throw std::runtime_error{"invalid use of option: --pmroot\n"}; + } + else + { + data_.environment.PMRootPath = Vita::string(argv[++nArg]).trim("\"").trim(); + } + } + else if (arguments_.INSTALL || arguments_.REMOVE) + { + bool found = false; + std::string name; + if (arguments_.CUSTOM_INSTALL) + { + name = std::string{ argv[nArg] }; + } + else + { + name = Vita::string(argv[nArg]).toLower(); + } + for (const auto& config : configs_) + { + if (config == name) + { + found = true; + break; + } + } + if (!found) + { + configs_.push_back(name); + } + } + else + { + throw std::runtime_error{"invalid option: " + std::string(argv[nArg]) + "\n"}; + } + } + if (!arguments_.SHOW_PCI && !arguments_.SHOW_USB) + { + arguments_.SHOW_USB = true; + arguments_.SHOW_PCI = true; + } +} + +bool Mhwd::optionsDontInterfereWithEachOther() const +{ + if (arguments_.INSTALL && arguments_.REMOVE) + { + printer_.printError("install and remove options can only be used separately!\n"); + printer_.printHelp(); + return false; + } + else if ((arguments_.INSTALL || arguments_.REMOVE) && arguments_.AUTOCONFIGURE) + { + printer_.printError("auto option can't be combined with install and remove options!\n"); + printer_.printHelp(); + return false; + } + else if ((arguments_.REMOVE || arguments_.INSTALL) && configs_.empty()) + { + printer_.printError("nothing to do?!\n"); + printer_.printHelp(); + return false; + } + + return true; +} + int Mhwd::launch(int argc, char *argv[]) { - std::vector<std::string> configs; std::string operationType; - bool autoConfigureNonFreeDriver; + bool autoConfigureNonFreeDriver = false; std::string autoConfigureClassID; - if (argc <= 1) + try { - arguments_.LISTAVAILABLE = true; + tryToParseCmdLineOptions(argc, argv, autoConfigureNonFreeDriver, operationType, + autoConfigureClassID); } - - for (int nArg = 1; nArg < argc; nArg++) + catch(const std::runtime_error& e) { - std::string option { argv[nArg] }; - - if (("-h" == option) || ("--help" == option)) - { - printer_.printHelp(); - return 0; - } - else if (("-f" == option) || ("--force" == option)) - { - arguments_.FORCE = true; - } - else if (("-d" == option) || ("--detail" == option)) - { - arguments_.DETAIL = true; - } - else if (("-la" == option) || ("--listall" == option)) - { - arguments_.LISTALL = true; - } - else if (("-li" == option) || ("--listinstalled" == option)) - { - arguments_.LISTINSTALLED = true; - } - else if (("-l" == option) || ("--list" == option)) - { - arguments_.LISTAVAILABLE = true; - } - else if (("-lh" == option) || ("--listhardware" == option)) - { - arguments_.LISTHARDWARE = true; - } - else if ("--pci" == option) - { - arguments_.SHOWPCI = true; - } - else if ("--usb" == option) - { - arguments_.SHOWUSB = true; - } - else if (("-a" == option) || ("--auto" == option)) - { - if (nArg + 3 < argc) - { - std::string deviceType {argv[nArg + 1]}; - std::string driverType {argv[nArg + 2]}; - std::string classID {argv[nArg + 3]}; - if ((("pci" != deviceType) && ("usb" != deviceType)) || - (("free" != driverType) && ("nonfree" != driverType))) - { - printer_.printError("invalid use of option: -a/--auto\n qwe"); - printer_.printHelp(); - return 1; - } - else - { - operationType = Vita::string{deviceType}.toUpper(); - - autoConfigureNonFreeDriver = ("nonfree" == driverType); - - autoConfigureClassID = Vita::string(classID).toLower().trim(); - arguments_.AUTOCONFIGURE = true; - nArg += 3; - } - } - else - { - printer_.printError("invalid use of option: -a/--auto\n"); - printer_.printHelp(); - return 1; - } - } - else if (("-ic" == option) || ("--installcustom" == option)) - { - if ((nArg + 1) < argc) - { - std::string deviceType {argv[nArg + 1]}; - if (("pci" != deviceType) && ("usb" != deviceType)) - { - printer_.printError("invalid use of option: -ic/--installcustom\n"); - printer_.printHelp(); - return 1; - } - else - { - operationType = Vita::string{deviceType}.toUpper(); - arguments_.CUSTOMINSTALL = true; - ++nArg; - } - } - else - { - printer_.printError("invalid use of option: -ic/--installcustom\n"); - printer_.printHelp(); - return 1; - } - } - else if (("-i" == option) || ("--install" == option)) - { - if ((nArg + 1) < argc) - { - std::string deviceType {argv[nArg + 1]}; - if (("pci" != deviceType) && ("usb" != deviceType)) - { - printer_.printError("invalid use of option: -i/--install\n"); - printer_.printHelp(); - return 1; - } - else - { - operationType = Vita::string{deviceType}.toUpper(); - arguments_.INSTALL = true; - ++nArg; - } - } - else - { - printer_.printError("invalid use of option: -i/--install\n"); - printer_.printHelp(); - return 1; - } - } - else if (("-r" == option) || ("--remove" == option)) - { - if ((nArg + 1) < argc) - { - std::string deviceType {argv[nArg + 1]}; - if (("pci" != deviceType) && ("usb" != deviceType)) - { - printer_.printError("invalid use of option: -r/--remove\n"); - printer_.printHelp(); - return 1; - } - else - { - operationType = Vita::string{deviceType}.toUpper(); - arguments_.REMOVE = true; - ++nArg; - } - } - else - { - printer_.printError("invalid use of option: -r/--remove\n"); - printer_.printHelp(); - return 1; - } - } - else if ("--pmcachedir" == option) - { - if (nArg + 1 >= argc) - { - printer_.printError("invalid use of option: --pmcachedir\n"); - printer_.printHelp(); - return 1; - } - else - { - data_.environment.PMCachePath = Vita::string(argv[++nArg]).trim("\"").trim(); - } - } - else if ("--pmconfig" == option) - { - if (nArg + 1 >= argc) - { - printer_.printError("invalid use of option: --pmconfig\n"); - printer_.printHelp(); - return 1; - } - else - { - data_.environment.PMConfigPath = Vita::string(argv[++nArg]).trim("\"").trim(); - } - } - else if ("--pmroot" == option) - { - if (nArg + 1 >= argc) - { - printer_.printError("invalid use of option: --pmroot\n"); - printer_.printHelp(); - return 1; - } - else - { - data_.environment.PMRootPath = Vita::string(argv[++nArg]).trim("\"").trim(); - } - } - else if (arguments_.INSTALL || arguments_.REMOVE) - { - bool found = false; - std::string name; - - if (arguments_.CUSTOMINSTALL) - { - name = std::string{argv[nArg]}; - } - else - { - name = Vita::string(argv[nArg]).toLower(); - } - - for (auto&& config : configs) - { - if (config == name) - { - found = true; - break; - } - } - - if (!found) - { - configs.push_back(name); - } - } - else - { - printer_.printError("invalid option: " + std::string(argv[nArg]) + "\n"); - printer_.printHelp(); - return 1; - } + printer_.printError(e.what()); + printer_.printHelp(); + return 1; } - // Check if arguments_ are right - if (arguments_.INSTALL && arguments_.REMOVE) + if (!optionsDontInterfereWithEachOther()) { - printer_.printError("install and remove options can only be used separately!\n"); - printer_.printHelp(); - return 1; - } - else if ((arguments_.INSTALL || arguments_.REMOVE) && arguments_.AUTOCONFIGURE) - { - printer_.printError("auto option can't be combined with install and remove options!\n"); - printer_.printHelp(); - return 1; - } - else if ((arguments_.REMOVE || arguments_.INSTALL) && configs.empty()) - { - printer_.printError("nothing to do?!\n"); - printer_.printHelp(); - return 1; - } - else if (!arguments_.SHOWPCI && !arguments_ .SHOWUSB) - { - arguments_.SHOWUSB = true; - arguments_.SHOWPCI = true; + return 1; } // Check environment @@ -987,7 +994,7 @@ int Mhwd::launch(int argc, char *argv[]) // > Perform operations: // List all configs - if (arguments_.LISTALL && arguments_.SHOWPCI) + if (arguments_.LIST_ALL && arguments_.SHOW_PCI) { if (!data_.allPCIConfigs.empty()) { @@ -998,7 +1005,7 @@ int Mhwd::launch(int argc, char *argv[]) printer_.printWarning("No PCI configs found!"); } } - if (arguments_.LISTALL && arguments_.SHOWUSB) + if (arguments_.LIST_ALL && arguments_.SHOW_USB) { if (!data_.allUSBConfigs.empty()) { @@ -1011,7 +1018,7 @@ int Mhwd::launch(int argc, char *argv[]) } // List installed configs - if (arguments_.LISTINSTALLED && arguments_.SHOWPCI) + if (arguments_.LIST_INSTALLED && arguments_.SHOW_PCI) { if (arguments_.DETAIL) { @@ -1029,7 +1036,7 @@ int Mhwd::launch(int argc, char *argv[]) } } } - if (arguments_.LISTINSTALLED && arguments_.SHOWUSB) + if (arguments_.LIST_INSTALLED && arguments_.SHOW_USB) { if (arguments_.DETAIL) { @@ -1049,7 +1056,7 @@ int Mhwd::launch(int argc, char *argv[]) } // List available configs - if (arguments_.LISTAVAILABLE && arguments_.SHOWPCI) + if (arguments_.LIST_AVAILABLE && arguments_.SHOW_PCI) { if (arguments_.DETAIL) { @@ -1070,7 +1077,7 @@ int Mhwd::launch(int argc, char *argv[]) } } - if (arguments_.LISTAVAILABLE && arguments_.SHOWUSB) + if (arguments_.LIST_AVAILABLE && arguments_.SHOW_USB) { if (arguments_.DETAIL) { @@ -1084,8 +1091,8 @@ int Mhwd::launch(int argc, char *argv[]) if (!USBdevice->availableConfigs_.empty()) { printer_.listConfigs(USBdevice->availableConfigs_, - USBdevice->sysfsBusID_ + " (" + USBdevice->classID_ + ":" + - USBdevice->vendorID_ + ":" + USBdevice->deviceID_ + ") " + USBdevice->sysfsBusID_ + " (" + USBdevice->classID_ + ":" + + USBdevice->vendorID_ + ":" + USBdevice->deviceID_ + ") " + USBdevice->className_ + " " + USBdevice->vendorName_ + ":"); } } @@ -1093,7 +1100,7 @@ int Mhwd::launch(int argc, char *argv[]) } // List hardware information - if (arguments_.LISTHARDWARE && arguments_.SHOWPCI) + if (arguments_.LIST_HARDWARE && arguments_.SHOW_PCI) { if (arguments_.DETAIL) { @@ -1104,7 +1111,7 @@ int Mhwd::launch(int argc, char *argv[]) printer_.listDevices(data_.PCIDevices, "PCI"); } } - if (arguments_.LISTHARDWARE && arguments_.SHOWUSB) + if (arguments_.LIST_HARDWARE && arguments_.SHOW_USB) { if (arguments_.DETAIL) { @@ -1164,33 +1171,18 @@ int Mhwd::launch(int argc, char *argv[]) } else { - // Check if already in list - bool found = false; - for (auto&& iter = configs.begin(); - iter != configs.end(); iter++) - { - if ((*iter) == config->name_) - { - found = true; - break; - } - } + bool alreadyInList = std::find(configs_.begin(), configs_.end(), config->name_) != configs_.end(); // If force is not set then skip found config bool skip = false; - if (!(arguments_.FORCE)) + if (!arguments_.FORCE) { - for (auto&& iter = installedConfigs->begin(); - iter != installedConfigs->end(); iter++) - { - if ((*iter)->name_ == config->name_) - { - skip = true; - break; - } - } + skip = std::find_if(installedConfigs->begin(), installedConfigs->end(), + [&config](const std::shared_ptr<Config>& conf) -> bool { + return conf->name_ == config->name_; + }) + != installedConfigs->end(); } - // Print found config if (skip) { @@ -1211,9 +1203,9 @@ int Mhwd::launch(int argc, char *argv[]) device->deviceName_); } - if (!found && !skip) + if (!alreadyInList && !skip) { - configs.push_back(config->name_); + configs_.push_back(config->name_); } } } @@ -1223,7 +1215,7 @@ int Mhwd::launch(int argc, char *argv[]) { printer_.printWarning("No device of class " + autoConfigureClassID + " found!"); } - else if (!configs.empty()) + else if (!configs_.empty()) { arguments_.INSTALL = true; } @@ -1234,10 +1226,10 @@ int Mhwd::launch(int argc, char *argv[]) { if (isUserRoot()) { - for (auto&& configName = configs.begin(); - configName != configs.end(); configName++) + for (auto&& configName = configs_.begin(); + configName != configs_.end(); configName++) { - if (arguments_.CUSTOMINSTALL) + if (arguments_.CUSTOM_INSTALL) { // Custom install -> get configs struct stat filestatus; diff --git a/src/Mhwd.hpp b/src/Mhwd.hpp index 81719cf..bbac0da 100644 --- a/src/Mhwd.hpp +++ b/src/Mhwd.hpp @@ -1,8 +1,25 @@ /* - * Mhwd.hpp + * This file is part of the mhwd - Manjaro Hardware Detection project + * + * mhwd - Manjaro Hardware Detection + * Roland Singer <roland@manjaro.org> + * Åukasz Matysiak <december0123@gmail.com> + * Filipe Marques <eagle.software3@gmail.com> * - * Created on: 26 sie 2014 - * Author: dec + * Copyright (C) 2007 Free Software Foundation, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. */ #ifndef MHWD_HPP_ @@ -32,29 +49,33 @@ class Mhwd { public: Mhwd(); - ~Mhwd(); + ~Mhwd() = default; + void set_version_mhwd(std::string version_of_software, std::string year_copyright); int launch(int argc, char *argv[]); private: struct Arguments { - bool SHOWPCI = false; - bool SHOWUSB = false; + bool SHOW_PCI = false; + bool SHOW_USB = false; bool INSTALL = false; bool REMOVE = false; bool DETAIL = false; bool FORCE = false; - bool LISTALL = false; - bool LISTINSTALLED = false; - bool LISTAVAILABLE = false; - bool LISTHARDWARE = false; - bool CUSTOMINSTALL = false; + bool LIST_ALL = false; + bool LIST_INSTALLED = false; + bool LIST_AVAILABLE = false; + bool LIST_HARDWARE = false; + bool CUSTOM_INSTALL = false; bool AUTOCONFIGURE = false; } arguments_; std::shared_ptr<Config> config_; Data data_; Printer printer_; + std::vector<std::string> configs_; + std::string version_, year_; + bool performTransaction(std::shared_ptr<Config> config, MHWD::TRANSACTIONTYPE type); bool isUserRoot() const; std::string checkEnvironment(); @@ -81,6 +102,9 @@ private: MHWD::STATUS installConfig(std::shared_ptr<Config> config); MHWD::STATUS uninstallConfig(Config *config); bool runScript(std::shared_ptr<Config> config, MHWD::TRANSACTIONTYPE operationType); + void tryToParseCmdLineOptions(int argc, char* argv[], bool& autoConfigureNonFreeDriver, + std::string& operationType, std::string& autoConfigureClassID); + bool optionsDontInterfereWithEachOther() const; }; #endif /* MHWD_HPP_ */ diff --git a/src/Printer.cpp b/src/Printer.cpp index f4a343b..7778877 100644 --- a/src/Printer.cpp +++ b/src/Printer.cpp @@ -1,8 +1,25 @@ /* - * Printer.cpp + * This file is part of the mhwd - Manjaro Hardware Detection project + * + * mhwd - Manjaro Hardware Detection + * Roland Singer <roland@manjaro.org> + * Åukasz Matysiak <december0123@gmail.com> + * Filipe Marques <eagle.software3@gmail.com> * - * Created on: 28 sie 2014 - * Author: dec + * Copyright (C) 2007 Free Software Foundation, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include <iomanip> @@ -32,38 +49,33 @@ void Printer::printWarning(std::string warningMsg) const void Printer::printMessage(MHWD::MESSAGETYPE type, std::string msg) const { - if (type == MHWD::MESSAGETYPE::CONSOLE_OUTPUT) - { - std::cout << CONSOLE_TEXT_OUTPUT_COLOR << msg << CONSOLE_COLOR_RESET; - } - else if (type == MHWD::MESSAGETYPE::INSTALLDEPENDENCY_START) - { - printStatus("Installing dependency " + msg + "..."); - } - else if (type == MHWD::MESSAGETYPE::INSTALLDEPENDENCY_END) - { - printStatus("Successfully installed dependency " + msg); - } - else if (type == MHWD::MESSAGETYPE::INSTALL_START) - { - printStatus("Installing " + msg + "..."); - } - else if (type == MHWD::MESSAGETYPE::INSTALL_END) - { - printStatus("Successfully installed " + msg); - } - else if (type == MHWD::MESSAGETYPE::REMOVE_START) - { - printStatus("Removing " + msg + "..."); - } - else if (type == MHWD::MESSAGETYPE::REMOVE_END) - { - printStatus("Successfully removed " + msg); - } - else - { - printError("You shouldn't see this?! Unknown message type!"); - } + switch(type) + { + case MHWD::MESSAGETYPE::CONSOLE_OUTPUT: + std::cout << CONSOLE_TEXT_OUTPUT_COLOR << msg << CONSOLE_COLOR_RESET; + break; + case MHWD::MESSAGETYPE::INSTALLDEPENDENCY_START: + printStatus("Installing dependency " + msg + "..."); + break; + case MHWD::MESSAGETYPE::INSTALLDEPENDENCY_END: + printStatus("Successfully installed dependency " + msg); + break; + case MHWD::MESSAGETYPE::INSTALL_START: + printStatus("Installing " + msg + "..."); + break; + case MHWD::MESSAGETYPE::INSTALL_END: + printStatus("Successfully installed " + msg); + break; + case MHWD::MESSAGETYPE::REMOVE_START: + printStatus("Removing " + msg + "..."); + break; + case MHWD::MESSAGETYPE::REMOVE_END: + printStatus("Successfully removed " + msg); + break; + default: + printError("You shouldn't see this?! Unknown message type!"); + break; + } } void Printer::printHelp() const @@ -72,6 +84,7 @@ void Printer::printHelp() const << " --pci\t\t\t\t\tlist only pci devices and driver configs\n" << " --usb\t\t\t\t\tlist only usb devices and driver configs\n" << " -h/--help\t\t\t\tshow help\n" + << " -v/--version\t\t\t\tshow version of mhwd\n" << " -f/--force\t\t\t\tforce reinstallation\n" << " -d/--detail\t\t\t\tshow detailed info for -l/-li/-lh\n" << " -l/--list\t\t\t\tlist available configs for devices\n" @@ -87,6 +100,16 @@ void Printer::printHelp() const << " --pmroot <path>\t\t\tset package manager root\n" << std::endl; } +void Printer::printVersion(std::string version_mhwd, std::string year_copy) const +{ + std::cout << "Manjaro Hardware Detection version "<< version_mhwd <<"\n\n" + << "Copyright (C) "<< year_copy <<" Manjaro Linux Developers\n" + << "This is free software licensed under GNU GPL v.3\n" + << "There is NO warranty; not even for MERCHANTABILITY or \n" + << "FITNESS FOR A PARTICULAR PURPOSE.\n" + << std::endl; +} + void Printer::listDevices(const std::vector<std::shared_ptr<Device>>& devices, std::string type) const { if (devices.empty()) @@ -104,7 +127,7 @@ void Printer::listDevices(const std::vector<std::shared_ptr<Device>>& devices, s << std::setw(8) << "DEVICE" << std::setw(10) << "CONFIGS" << std::endl; printLine(); - for (auto&& device : devices) + for (const auto& device : devices) { std::cout << std::setw(30) << device->className_ << std::setw(15) << device->sysfsBusID_ @@ -126,7 +149,7 @@ void Printer::listConfigs(const std::vector<std::shared_ptr<Config>>& configs, s << std::setw(20) << "FREEDRIVER" << std::setw(15) << "TYPE" << std::endl; printLine(); - for (auto&& config : configs) + for (const auto& config : configs) { std::cout << std::setw(22) << config->name_ << std::setw(22) << config->version_ @@ -141,7 +164,7 @@ void Printer::printAvailableConfigsInDetail(const std::string& deviceType, { bool configFound = false; - for (auto&& device : devices) + for (const auto& device : devices) { if (device->availableConfigs_.empty() && device->installedConfigs_.empty()) { @@ -161,16 +184,16 @@ void Printer::printAvailableConfigsInDetail(const std::string& deviceType, printLine(); if (!device->installedConfigs_.empty()) { - std::cout << " > INSTALLED:" << std::endl; + std::cout << " > INSTALLED:\n\n"; for (auto&& installedConfig : device->installedConfigs_) { printConfigDetails(*installedConfig); } - std::cout << std::endl << std::endl; + std::cout << "\n\n"; } if (!device->availableConfigs_.empty()) { - std::cout << " > AVAILABLE:" << std::endl; + std::cout << " > AVAILABLE:\n\n"; for (auto&& availableConfig : device->availableConfigs_) { printConfigDetails(*availableConfig); @@ -195,7 +218,7 @@ void Printer::printInstalledConfigs(const std::string& deviceType, } else { - for (auto&& config : installedConfigs) + for (const auto& config : installedConfigs) { printConfigDetails(*config); } @@ -207,25 +230,25 @@ void Printer::printConfigDetails(const Config& config) const { std::string classids; std::string vendorids; - for (auto&& hwd : config.hwdIDs_) + for (const auto& hwd : config.hwdIDs_) { - for (auto&& vendorID : hwd.vendorIDs) + for (const auto& vendorID : hwd.vendorIDs) { vendorids += vendorID + " "; } - for (auto&& classID : hwd.classIDs) + for (const auto& classID : hwd.classIDs) { classids += classID + " "; } } std::string dependencies; - for (auto&& dependency : config.dependencies_) + for (const auto& dependency : config.dependencies_) { dependencies += dependency + " "; } std::string conflicts; - for (auto&& conflict : config.conflicts_) + for (const auto& conflict : config.conflicts_) { conflicts += conflict + " "; } @@ -239,7 +262,7 @@ void Printer::printConfigDetails(const Config& config) const << "\n DEPENDS:\t" << (dependencies.empty() ? "-" : dependencies) << "\n CONFLICTS:\t" << (conflicts.empty() ? "-" : conflicts) << "\n CLASSIDS:\t" << classids - << "\n VENDORIDS:\t" << vendorids << std::endl; + << "\n VENDORIDS:\t" << vendorids << "\n" << std::endl; } void Printer::printLine() const diff --git a/src/Printer.hpp b/src/Printer.hpp index ab51e35..b987a0e 100644 --- a/src/Printer.hpp +++ b/src/Printer.hpp @@ -1,8 +1,25 @@ /* - * Printer.hpp + * This file is part of the mhwd - Manjaro Hardware Detection project + * + * mhwd - Manjaro Hardware Detection + * Roland Singer <roland@manjaro.org> + * Åukasz Matysiak <december0123@gmail.com> + * Filipe Marques <eagle.software3@gmail.com> * - * Created on: 28 sie 2014 - * Author: dec + * Copyright (C) 2007 Free Software Foundation, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. */ #ifndef PRINTER_HPP_ @@ -23,7 +40,8 @@ public: void printWarning(std::string warningMsg) const; void printMessage(MHWD::MESSAGETYPE type, std::string str) const; void printHelp() const; - void listDevices(const std::vector<std::shared_ptr<Device>>& devices, std::string typeOfDevice) const; + void printVersion(std::string version_mhwd, std::string year_copy) const; + void listDevices(const std::vector<std::shared_ptr<Device>>& devices, std::string typeOfDevice) const; void listConfigs(const std::vector<std::shared_ptr<Config>>& configs, std::string header) const; void printAvailableConfigsInDetail(const std::string& deviceType, @@ -34,9 +52,9 @@ public: private: void printLine() const; - const char* CONSOLE_COLOR_RESET {"\e[m"}; - const char* CONSOLE_MESSAGE_COLOR {"\e[1m\e[31m"}; - const char* CONSOLE_TEXT_OUTPUT_COLOR {"\e[0;32m"}; + const char* CONSOLE_COLOR_RESET {"\033[m"}; + const char* CONSOLE_MESSAGE_COLOR {"\033[1m\033[31m"}; + const char* CONSOLE_TEXT_OUTPUT_COLOR {"\033[0;32m"}; }; #endif /* PRINTER_HPP_ */ diff --git a/src/Transaction.cpp b/src/Transaction.cpp index 59baaab..b6a0015 100644 --- a/src/Transaction.cpp +++ b/src/Transaction.cpp @@ -1,8 +1,25 @@ /* - * Transaction.cpp + * This file is part of the mhwd - Manjaro Hardware Detection project + * + * mhwd - Manjaro Hardware Detection + * Roland Singer <roland@manjaro.org> + * Åukasz Matysiak <december0123@gmail.com> + * Filipe Marques <eagle.software3@gmail.com> * - * Created on: 28 sie 2014 - * Author: dec + * Copyright (C) 2007 Free Software Foundation, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include "Transaction.hpp" diff --git a/src/Transaction.hpp b/src/Transaction.hpp index ee2155b..50834a8 100644 --- a/src/Transaction.hpp +++ b/src/Transaction.hpp @@ -1,8 +1,25 @@ /* - * Transaction.hpp + * This file is part of the mhwd - Manjaro Hardware Detection project + * + * mhwd - Manjaro Hardware Detection + * Roland Singer <roland@manjaro.org> + * Åukasz Matysiak <december0123@gmail.com> + * Filipe Marques <eagle.software3@gmail.com> * - * Created on: 28 sie 2014 - * Author: dec + * Copyright (C) 2007 Free Software Foundation, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. */ #ifndef TRANSACTION_HPP_ diff --git a/src/main.cpp b/src/main.cpp index 5f21558..56aeec1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,10 @@ /* + * This file is part of the mhwd - Manjaro Hardware Detection project + * * mhwd - Manjaro Hardware Detection * Roland Singer <roland@manjaro.org> + * Åukasz Matysiak <december0123@gmail.com> + * Filipe Marques <eagle.software3@gmail.com> * * Copyright (C) 2007 Free Software Foundation, Inc. * @@ -17,10 +21,22 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ + #include "Mhwd.hpp" +#include <iostream> + int main(int argc, char *argv[]) { - Mhwd mhwd; - return mhwd.launch(argc, argv); + try + { + Mhwd mhwd; + mhwd.set_version_mhwd("0.5.1","2012 2013 2014 2015"); + return mhwd.launch(argc, argv); + } + catch(...) + { + std::cerr << "Unknown errors occured..."; + return -1; + } } -- GitLab