Commit b17d6138 authored by Roland Singer's avatar Roland Singer

* added database implementation

* removed using namespace std
parent defeeb34
......@@ -6,6 +6,7 @@ set( HEADERS
const.h
device.h
config.h
database.h
vita/config.hpp
vita/string.hpp
)
......@@ -14,6 +15,7 @@ set( SOURCES
hwd.cpp
device.cpp
config.cpp
database.cpp
vita/config.cpp
vita/string.cpp
)
......
......@@ -20,12 +20,19 @@
#include "config.h"
// Static variables
mhwd::Config::Config(string path)
mhwd::Database mhwd::Config::db(MHWD_DATABASE_PATH);
// Methods
mhwd::Config::Config(std::string path) :
path(path)
{
priority = 0;
freedriver = true;
Config::path = path;
configValid = readConfig(path);
}
......@@ -38,6 +45,16 @@ bool mhwd::Config::operator==(const mhwd::Config& compare) {
bool mhwd::Config::isInstalled() {
if (!db.isValid())
return false;
// TODO: Warning!
return db.isAvailable(name);
}
// Private
......@@ -47,19 +64,19 @@ bool mhwd::Config::readConfig(const Vita::string path) {
if (IDs.empty())
addNewIDsGroup();
ifstream file(path.c_str(), ios::in);
std::ifstream file(path.c_str(), std::ios::in);
if (!file.is_open())
return false;
Vita::string line, key, value;
vector<Vita::string> parts;
std::vector<Vita::string> parts;
while (!file.eof()) {
getline(file, line);
size_t pos = line.find_first_of('#');
if (pos != string::npos)
if (pos != std::string::npos)
line.erase(pos);
if (line.trim().empty())
......@@ -71,7 +88,7 @@ bool mhwd::Config::readConfig(const Vita::string path) {
// Read in extern file
if (value.size() > 1 && value.substr(0, 1) == ">") {
ifstream file(getRightPath(value.substr(1)).c_str(), ios::in);
std::ifstream file(getRightPath(value.substr(1)).c_str(), std::ios::in);
if (!file.is_open())
return false;
......@@ -82,7 +99,7 @@ bool mhwd::Config::readConfig(const Vita::string path) {
getline(file, line);
size_t pos = line.find_first_of('#');
if (pos != string::npos)
if (pos != std::string::npos)
line.erase(pos);
if (line.trim().empty())
......@@ -96,7 +113,7 @@ bool mhwd::Config::readConfig(const Vita::string path) {
value = value.trim();
// remove all multiple spaces
while (value.find(" ") != string::npos) {
while (value.find(" ") != std::string::npos) {
value = value.replace(" ", " ");
}
}
......@@ -145,7 +162,7 @@ bool mhwd::Config::readConfig(const Vita::string path) {
file.close();
// Append * to all empty vectors
for (vector<IDsGroup>::iterator iterator = IDs.begin(); iterator != IDs.end(); iterator++) {
for (std::vector<IDsGroup>::iterator iterator = IDs.begin(); iterator != IDs.end(); iterator++) {
if ((*iterator).classIDs.empty())
(*iterator).classIDs.push_back("*");
......@@ -161,11 +178,11 @@ bool mhwd::Config::readConfig(const Vita::string path) {
vector<string> mhwd::Config::getIDs(Vita::string str) {
vector<Vita::string> work = str.toLower().explode(" ");
vector<string> final;
std::vector<std::string> mhwd::Config::getIDs(Vita::string str) {
std::vector<Vita::string> work = str.toLower().explode(" ");
std::vector<std::string> final;
for (vector<Vita::string>::const_iterator iterator = work.begin(); iterator != work.end(); iterator++) {
for (std::vector<Vita::string>::const_iterator iterator = work.begin(); iterator != work.end(); iterator++) {
if (*iterator != "")
final.push_back(*iterator);
}
......
......@@ -24,38 +24,41 @@
#include <iostream>
#include <string>
#include <vector>
#include "const.h"
#include "string.hpp"
using namespace std;
#include "database.h"
namespace mhwd {
class Config
{
public:
Config(string path);
Config(std::string path);
bool operator==(const Config& compare);
bool isInstalled();
bool isValid() { return configValid; }
string getName() { return name; }
string getInfo() { return info; }
std::string getName() { return name; }
std::string getInfo() { return info; }
bool getIsFreeDriver() { return freedriver; }
int getPriority() { return priority; }
struct IDsGroup {
vector<string> classIDs, vendorIDs, deviceIDs;
std::vector<std::string> classIDs, vendorIDs, deviceIDs;
};
vector<IDsGroup> getIDsGroups() { return IDs; }
std::vector<IDsGroup> getIDsGroups() { return IDs; }
private:
string path, name, info;
vector<IDsGroup> IDs;
static Database db;
std::string path, name, info;
std::vector<IDsGroup> IDs;
bool configValid, freedriver;
int priority;
bool readConfig(const Vita::string path);
vector<string> getIDs(Vita::string str);
std::vector<std::string> getIDs(Vita::string str);
inline void addNewIDsGroup();
Vita::string getRightPath(Vita::string str);
};
......
......@@ -21,3 +21,4 @@
#define MHWD_CONFIG_END "mhwd"
#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"
/*
* 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
......@@ -40,12 +40,12 @@ mhwd::Device::Device(hd_t *hd, TYPE type) {
void mhwd::Device::addConfig(mhwd::Config& config) {
for (vector<mhwd::Config>::const_iterator iterator = configs.begin(); iterator != configs.end(); iterator++) {
for (std::vector<mhwd::Config>::const_iterator iterator = configs.begin(); iterator != configs.end(); iterator++) {
if (config == *iterator)
return;
}
for (vector<mhwd::Config>::iterator iterator = configs.begin(); iterator != configs.end(); iterator++) {
for (std::vector<mhwd::Config>::iterator iterator = configs.begin(); iterator != configs.end(); iterator++) {
if (config.getPriority() > (*iterator).getPriority()) {
configs.insert(iterator, config);
return;
......@@ -58,8 +58,8 @@ void mhwd::Device::addConfig(mhwd::Config& config) {
Vita::string mhwd::Device::from_Hex(uint16_t hexnum, int fill) {
stringstream stream;
stream << hex << setfill('0') << setw(fill) << hexnum;
std::stringstream stream;
stream << std::hex << std::setfill('0') << std::setw(fill) << hexnum;
return stream.str();
}
......@@ -69,6 +69,6 @@ Vita::string mhwd::Device::from_CharArray(char* c) {
if (c == NULL)
return "";
return string(c);
return std::string(c);
}
......@@ -29,8 +29,6 @@
#include "string.hpp"
#include "config.h"
using namespace std;
namespace mhwd {
class Device
......@@ -43,18 +41,18 @@ namespace mhwd {
Device(hd_t *hd, TYPE type);
TYPE getType() { return type; }
string getClassName() { return ClassName; }
string getDeviceName() { return DeviceName; }
string getVendorName() { return VendorName; }
string getClassID() { return ClassID; }
string getDeviceID() { return DeviceID; }
string getVendorID() { return VendorID; }
vector<mhwd::Config> getConfigs() { return configs; }
std::string getClassName() { return ClassName; }
std::string getDeviceName() { return DeviceName; }
std::string getVendorName() { return VendorName; }
std::string getClassID() { return ClassID; }
std::string getDeviceID() { return DeviceID; }
std::string getVendorID() { return VendorID; }
std::vector<mhwd::Config> getConfigs() { return configs; }
private:
string ClassName, DeviceName, VendorName, ClassID, DeviceID, VendorID;
std::string ClassName, DeviceName, VendorName, ClassID, DeviceID, VendorID;
TYPE type;
vector<mhwd::Config> configs;
std::vector<mhwd::Config> configs;
void addConfig(Config& config);
Vita::string from_Hex(uint16_t hexnum, int fill = 4);
......
......@@ -23,7 +23,7 @@
// Static variables
vector<mhwd::Device*> mhwd::hwd::USBDevices, mhwd::hwd::PCIDevices;
std::vector<mhwd::Device*> mhwd::hwd::USBDevices, mhwd::hwd::PCIDevices;
......@@ -31,7 +31,7 @@ vector<mhwd::Device*> mhwd::hwd::USBDevices, mhwd::hwd::PCIDevices;
vector<mhwd::Device*> mhwd::hwd::getUSBDevices() {
std::vector<mhwd::Device*> mhwd::hwd::getUSBDevices() {
if (USBDevices.empty())
update();
......@@ -40,7 +40,7 @@ vector<mhwd::Device*> mhwd::hwd::getUSBDevices() {
vector<mhwd::Device*> mhwd::hwd::getPCIDevices() {
std::vector<mhwd::Device*> mhwd::hwd::getPCIDevices() {
if (PCIDevices.empty())
update();
......@@ -75,12 +75,12 @@ void mhwd::hwd::printPCIDetails() {
vector<mhwd::Device*> mhwd::hwd::getDevices(Device::TYPE type) {
vector<Device*> devices;
std::vector<mhwd::Device*> mhwd::hwd::getDevices(Device::TYPE type) {
std::vector<mhwd::Device*> devices;
hd_data_t *hd_data;
hd_t *hd;
hw_item hw;
string configDir;
std::string configDir;
if (type == Device::TYPE_USB) {
hw = hw_usb;
......@@ -111,7 +111,7 @@ vector<mhwd::Device*> mhwd::hwd::getDevices(Device::TYPE type) {
void mhwd::hwd::setMatchingConfigs(vector<mhwd::Device*>* devices, const string configDir) {
void mhwd::hwd::setMatchingConfigs(std::vector<mhwd::Device*>* devices, const std::string configDir) {
struct dirent *dir;
DIR *d = opendir(configDir.c_str());
......@@ -140,7 +140,7 @@ void mhwd::hwd::setMatchingConfigs(vector<mhwd::Device*>* devices, const string
void mhwd::hwd::setMatchingConfig(vector<mhwd::Device*>* devices, const string configPath) {
void mhwd::hwd::setMatchingConfig(std::vector<mhwd::Device*>* devices, const std::string configPath) {
mhwd::Config config(configPath);
// Check if config is valid
......@@ -148,18 +148,18 @@ void mhwd::hwd::setMatchingConfig(vector<mhwd::Device*>* devices, const string c
return;
// TODO: print warning!
vector<mhwd::Device*> foundDevices;
vector<mhwd::Config::IDsGroup> IDsGroups = config.getIDsGroups();
std::vector<mhwd::Device*> foundDevices;
std::vector<mhwd::Config::IDsGroup> IDsGroups = config.getIDsGroups();
for (vector<mhwd::Config::IDsGroup>::const_iterator i_idsgroup = IDsGroups.begin(); i_idsgroup != IDsGroups.end(); i_idsgroup++) {
for (std::vector<mhwd::Config::IDsGroup>::const_iterator i_idsgroup = IDsGroups.begin(); i_idsgroup != IDsGroups.end(); i_idsgroup++) {
bool foundDevice = false;
// Check all devices
for (vector<mhwd::Device*>::iterator i_device = devices->begin(); i_device != devices->end(); i_device++) {
for (std::vector<mhwd::Device*>::iterator i_device = devices->begin(); i_device != devices->end(); i_device++) {
bool found = false;
// Check class ids
for (vector<string>::const_iterator iterator = (*i_idsgroup).classIDs.begin(); iterator != (*i_idsgroup).classIDs.end(); iterator++) {
for (std::vector<std::string>::const_iterator iterator = (*i_idsgroup).classIDs.begin(); iterator != (*i_idsgroup).classIDs.end(); iterator++) {
if (*iterator == "*" || *iterator == (*i_device)->ClassID) {
found = true;
break;
......@@ -172,7 +172,7 @@ void mhwd::hwd::setMatchingConfig(vector<mhwd::Device*>* devices, const string c
// Check vendor ids
found = false;
for (vector<string>::const_iterator iterator = (*i_idsgroup).vendorIDs.begin(); iterator != (*i_idsgroup).vendorIDs.end(); iterator++) {
for (std::vector<std::string>::const_iterator iterator = (*i_idsgroup).vendorIDs.begin(); iterator != (*i_idsgroup).vendorIDs.end(); iterator++) {
if (*iterator == "*" || *iterator == (*i_device)->VendorID) {
found = true;
break;
......@@ -185,7 +185,7 @@ void mhwd::hwd::setMatchingConfig(vector<mhwd::Device*>* devices, const string c
// Check device ids
found = false;
for (vector<string>::const_iterator iterator = (*i_idsgroup).deviceIDs.begin(); iterator != (*i_idsgroup).deviceIDs.end(); iterator++) {
for (std::vector<std::string>::const_iterator iterator = (*i_idsgroup).deviceIDs.begin(); iterator != (*i_idsgroup).deviceIDs.end(); iterator++) {
if (*iterator == "*" || *iterator == (*i_device)->DeviceID) {
found = true;
break;
......@@ -205,7 +205,7 @@ void mhwd::hwd::setMatchingConfig(vector<mhwd::Device*>* devices, const string c
// Set config to all matching devices
for (vector<mhwd::Device*>::iterator iterator = foundDevices.begin(); iterator != foundDevices.end(); iterator++) {
for (std::vector<mhwd::Device*>::iterator iterator = foundDevices.begin(); iterator != foundDevices.end(); iterator++) {
(*iterator)->addConfig(config);
}
}
......
......@@ -35,25 +35,23 @@
#include "config.h"
#include "string.hpp"
using namespace std;
namespace mhwd {
class hwd
{
public:
static vector<Device*> getUSBDevices();
static vector<Device*> getPCIDevices();
static std::vector<Device*> getUSBDevices();
static std::vector<Device*> getPCIDevices();
static void printUSBDetails();
static void printPCIDetails();
static void update();
private:
static vector<Device*> USBDevices, PCIDevices;
static std::vector<Device*> USBDevices, PCIDevices;
static vector<mhwd::Device*> getDevices(Device::TYPE type);
static void setMatchingConfigs(vector<mhwd::Device*>* devices, const string configDir);
static void setMatchingConfig(vector<mhwd::Device*>* devices, const string configPath);
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 printDetails(hw_item hw);
};
......
......@@ -52,20 +52,20 @@ bool config::loadFile(string path) {
}
bool config::saveFile(string path) const {
std::ofstream file(path.c_str(), std::ios::out);
std::ofstream file(path.c_str(), std::ios::out);
if (!file.is_open()) {
return false;
}
if (!file.is_open()) {
return false;
}
std::map<string, string>::const_iterator iter;
std::map<string, string>::const_iterator iter;
for (iter = values.begin(); iter != values.end(); iter++){
file << iter->first << " = " << iter->second << std::endl;
}
for (iter = values.begin(); iter != values.end(); iter++){
file << iter->first << " = " << iter->second << std::endl;
}
file.close();
return true;
file.close();
return true;
}
string config::get(string key) {
......
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