Commit 3e186c02 authored by Roland Singer's avatar Roland Singer

* removed database source

* implemented better installed database structure
* copying configs to database now instead of only saving the name
parent b17d6138
......@@ -6,7 +6,6 @@ set( HEADERS
const.h
device.h
config.h
database.h
vita/config.hpp
vita/string.hpp
)
......@@ -15,7 +14,6 @@ set( SOURCES
hwd.cpp
device.cpp
config.cpp
database.cpp
vita/config.cpp
vita/string.cpp
)
......
......@@ -20,42 +20,24 @@
#include "config.h"
// Static variables
mhwd::Database mhwd::Config::db(MHWD_DATABASE_PATH);
// Methods
mhwd::Config::Config(std::string path) :
path(path)
mhwd::Config::Config(std::string basePath) :
basePath(basePath)
{
priority = 0;
freedriver = true;
configValid = readConfig(path);
configValid = readConfig(basePath + "/" + MHWD_CONFIG_NAME);
}
bool mhwd::Config::operator==(const mhwd::Config& compare) {
return (path == compare.path);
}
bool mhwd::Config::isInstalled() {
if (!db.isValid())
return false;
// TODO: Warning!
return db.isAvailable(name);
return (basePath == compare.basePath);
}
// Private
......@@ -125,6 +107,9 @@ bool mhwd::Config::readConfig(const Vita::string path) {
else if (key == "name") {
name = value;
}
else if (key == "version") {
version = value;
}
else if (key == "info") {
info = value;
}
......@@ -205,5 +190,5 @@ Vita::string mhwd::Config::getRightPath(Vita::string str) {
if (str.size() <= 0 || str.substr(0, 1) == "/")
return str;
return path.substr(0, path.find_last_of('/')) + "/" + str;
return basePath + "/" + str;
}
......@@ -26,19 +26,17 @@
#include <vector>
#include "const.h"
#include "string.hpp"
#include "database.h"
namespace mhwd {
class Config
{
public:
Config(std::string path);
Config(std::string basePath);
bool operator==(const Config& compare);
bool isInstalled();
bool isValid() { return configValid; }
std::string getName() { return name; }
std::string getVersion() { return version; }
std::string getInfo() { return info; }
bool getIsFreeDriver() { return freedriver; }
int getPriority() { return priority; }
......@@ -50,9 +48,7 @@ namespace mhwd {
std::vector<IDsGroup> getIDsGroups() { return IDs; }
private:
static Database db;
std::string path, name, info;
std::string basePath, name, info, version;
std::vector<IDsGroup> IDs;
bool configValid, freedriver;
int priority;
......
......@@ -18,7 +18,8 @@
*/
#define MHWD_CONFIG_END "mhwd"
#define MHWD_CONFIG_NAME "MHWDCONFIG"
#define MHWD_USB_CONFIG_DIR "/var/lib/mhwd/usb"
#define MHWD_PCI_CONFIG_DIR "." //"/var/lib/mhwd/pci"
#define MHWD_DATABASE_PATH "/var/lib/mhwd/local.db"
#define MHWD_PCI_CONFIG_DIR "./db" //"/var/lib/mhwd/pci"
#define MHWD_USB_DATABASE_DIR "/var/lib/mhwd/local/usb"
#define MHWD_PCI_DATABASE_DIR "./local" //"/var/lib/mhwd/local/pci"
/*
* mhwd - Manjaro Hardware Detection
*
* 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 "database.h"
mhwd::Database::Database(std::string path) :
path(path)
{
valid = readDatabase();
}
bool mhwd::Database::isAvailable(std::string str) {
str = Vita::string(str).toLower().trim();
for (std::vector<std::string>::const_iterator iterator = content.begin(); iterator != content.end(); iterator++) {
if (*iterator == str)
return true;
}
return false;
}
bool mhwd::Database::setContent(std::vector<std::string> content) {
std::ofstream file(path.c_str(), std::ios::out);
if (!file.is_open())
return false;
Database::content.clear();
for (std::vector<std::string>::const_iterator iterator = content.begin(); iterator != content.end(); iterator++) {
Database::content.push_back(Vita::string(*iterator).toLower().trim());
file << Database::content.back() << std::endl;
}
file.close();
return true;
}
bool mhwd::Database::add(std::string str) {
str = Vita::string(str).toLower().trim();
std::vector<std::string> content = Database::content;
for (std::vector<std::string>::const_iterator iterator = content.begin(); iterator != content.end(); iterator++) {
if (*iterator == str)
return true;
}
content.push_back(str);
return setContent(content);
}
bool mhwd::Database::remove(std::string str) {
str = Vita::string(str).toLower().trim();
std::vector<std::string> content = Database::content;
for (std::vector<std::string>::iterator iterator = content.begin(); iterator != content.end(); iterator++) {
if (*iterator == str)
content.erase(iterator);
}
return setContent(content);
}
// Private
bool mhwd::Database::readDatabase() {
std::ifstream file(path.c_str(), std::ios::in);
if (!file.is_open())
return false;
Vita::string line;
while (!file.eof()) {
getline(file, line);
size_t pos = line.find_first_of('#');
if (pos != std::string::npos)
line.erase(pos);
if (line.trim().empty())
continue;
content.push_back(line.toLower().trim());
}
file.close();
return true;
}
/*
* mhwd - Manjaro Hardware Detection
*
* 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 DATABASE_H
#define DATABASE_H
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include "string.hpp"
namespace mhwd {
class Database
{
public:
Database(std::string path);
std::vector<std::string> getContent() { return content; }
bool setContent(std::vector<std::string> content);
bool add(std::string str);
bool remove(std::string str);
bool isAvailable(std::string str);
bool isValid() { return valid; }
private:
std::string path;
bool valid;
std::vector<std::string> content;
bool readDatabase();
};
}
#endif // DATABASE_H
......@@ -57,6 +57,24 @@ void mhwd::Device::addConfig(mhwd::Config& config) {
void mhwd::Device::addInstalledConfig(mhwd::Config& config) {
for (std::vector<mhwd::Config>::const_iterator iterator = installedConfigs.begin(); iterator != installedConfigs.end(); iterator++) {
if (config == *iterator)
return;
}
for (std::vector<mhwd::Config>::iterator iterator = installedConfigs.begin(); iterator != installedConfigs.end(); iterator++) {
if (config.getPriority() > (*iterator).getPriority()) {
installedConfigs.insert(iterator, config);
return;
}
}
installedConfigs.push_back(config);
}
Vita::string mhwd::Device::from_Hex(uint16_t hexnum, int fill) {
std::stringstream stream;
stream << std::hex << std::setfill('0') << std::setw(fill) << hexnum;
......
......@@ -48,13 +48,15 @@ namespace mhwd {
std::string getDeviceID() { return DeviceID; }
std::string getVendorID() { return VendorID; }
std::vector<mhwd::Config> getConfigs() { return configs; }
std::vector<mhwd::Config> getInstalledConfigs() { return installedConfigs; }
private:
std::string ClassName, DeviceName, VendorName, ClassID, DeviceID, VendorID;
TYPE type;
std::vector<mhwd::Config> configs;
std::vector<mhwd::Config> configs, installedConfigs;
void addConfig(Config& config);
void addInstalledConfig(Config& config);
Vita::string from_Hex(uint16_t hexnum, int fill = 4);
Vita::string from_CharArray(char* c);
};
......
......@@ -80,27 +80,29 @@ std::vector<mhwd::Device*> mhwd::hwd::getDevices(Device::TYPE type) {
hd_data_t *hd_data;
hd_t *hd;
hw_item hw;
std::string configDir;
std::string configDir, databaseDir;
if (type == Device::TYPE_USB) {
hw = hw_usb;
configDir = MHWD_USB_CONFIG_DIR;
databaseDir = MHWD_USB_DATABASE_DIR;
}
else {
hw = hw_pci;
configDir = MHWD_PCI_CONFIG_DIR;
databaseDir = MHWD_PCI_DATABASE_DIR;
}
hd_data = (hd_data_t*)calloc(1, sizeof *hd_data);
hd_data = (hd_data_t*)calloc(1, sizeof *hd_data);
hd = hd_list(hd_data, hw, 1, NULL);
for(; hd; hd = hd->next) {
devices.push_back(new Device(hd, type));
}
setMatchingConfigs(&devices, configDir);
setMatchingConfigs(&devices, configDir, false);
setMatchingConfigs(&devices, databaseDir, true);
hd_free_hd_list(hd);
hd_free_hd_data(hd_data);
......@@ -111,7 +113,7 @@ std::vector<mhwd::Device*> mhwd::hwd::getDevices(Device::TYPE type) {
void mhwd::hwd::setMatchingConfigs(std::vector<mhwd::Device*>* devices, const std::string configDir) {
void mhwd::hwd::setMatchingConfigs(std::vector<mhwd::Device*>* devices, const std::string configDir, bool setAsInstalled) {
struct dirent *dir;
DIR *d = opendir(configDir.c_str());
......@@ -129,10 +131,10 @@ void mhwd::hwd::setMatchingConfigs(std::vector<mhwd::Device*>* devices, const st
struct stat filestatus;
lstat(filepath.c_str(), &filestatus);
if (S_ISREG(filestatus.st_mode) && filename.explode(".").back() == MHWD_CONFIG_END)
setMatchingConfig(devices, filepath);
if (S_ISREG(filestatus.st_mode) && filename == MHWD_CONFIG_NAME)
setMatchingConfig(devices, configDir, setAsInstalled);
else if (S_ISDIR(filestatus.st_mode))
setMatchingConfigs(devices, filepath);
setMatchingConfigs(devices, filepath, setAsInstalled);
}
closedir(d);
......@@ -140,7 +142,7 @@ void mhwd::hwd::setMatchingConfigs(std::vector<mhwd::Device*>* devices, const st
void mhwd::hwd::setMatchingConfig(std::vector<mhwd::Device*>* devices, const std::string configPath) {
void mhwd::hwd::setMatchingConfig(std::vector<mhwd::Device*>* devices, const std::string configPath, bool setAsInstalled) {
mhwd::Config config(configPath);
// Check if config is valid
......@@ -206,7 +208,10 @@ void mhwd::hwd::setMatchingConfig(std::vector<mhwd::Device*>* devices, const std
// Set config to all matching devices
for (std::vector<mhwd::Device*>::iterator iterator = foundDevices.begin(); iterator != foundDevices.end(); iterator++) {
(*iterator)->addConfig(config);
if (setAsInstalled)
(*iterator)->addInstalledConfig(config);
else
(*iterator)->addConfig(config);
}
}
......
......@@ -50,8 +50,8 @@ namespace mhwd {
static std::vector<Device*> USBDevices, PCIDevices;
static std::vector<mhwd::Device*> getDevices(Device::TYPE type);
static void setMatchingConfigs(std::vector<mhwd::Device*>* devices, const std::string configDir);
static void setMatchingConfig(std::vector<mhwd::Device*>* devices, const std::string configPath);
static void setMatchingConfigs(std::vector<mhwd::Device*>* devices, const std::string configDir, bool setAsInstalled = false);
static void setMatchingConfig(std::vector<mhwd::Device*>* devices, const std::string configPath, bool setAsInstalled = false);
static void printDetails(hw_item hw);
};
......
......@@ -20,18 +20,25 @@ int main (int argc, char *argv[])
for (vector<mhwd::Device*>::const_iterator iterator = devices.begin(); iterator != devices.end(); iterator++) {
vector<mhwd::Config> configs = (*iterator)->getConfigs();
vector<mhwd::Config> installedConfigs = (*iterator)->getInstalledConfigs();
if (!configs.empty())
cout << (*iterator)->getClassName() << ": ";
cout << endl << endl << (*iterator)->getClassName() << " " << (*iterator)->getVendorName() << ":" << endl << "Available: ";
for (vector<mhwd::Config>::const_iterator iterator = configs.begin(); iterator != configs.end(); iterator++) {
mhwd::Config config = (*iterator);
cout << config.getName() << " ";
cout << config.getName() << "-" << config.getVersion() << " ";
}
if (!configs.empty())
cout << endl;
if (!installedConfigs.empty())
cout << endl <<"Installed: ";
for (vector<mhwd::Config>::const_iterator iterator = installedConfigs.begin(); iterator != installedConfigs.end(); iterator++) {
mhwd::Config config = (*iterator);
cout << config.getName() << "-" << config.getVersion() << " ";
}
}
/*for(unsigned int i = 0; i < devices.size(); i++) {
......@@ -47,4 +54,6 @@ int main (int argc, char *argv[])
cout << endl << endl;
mhwd::printDetails(mhwd::HW_USB);*/
cout << endl << endl;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment