Commit 2dd1e13a authored by Roland Singer's avatar Roland Singer

forgot to add source ;)

parent 48ede120
Main developers:
Roland Singer <roland@manjaro.org>
cmake_minimum_required (VERSION 2.6)
project(mhwd)
add_definitions(-Wall)
include_directories(vita-1.0)
add_subdirectory(libmhwd)
This diff is collapsed.
include_directories (.)
set( HEADERS
mhwd.h
device.h
)
set( SOURCES
mhwd.cpp
device.cpp
)
set( LIBS hd)
add_library (mhwd SHARED ${SOURCES} ${HEADERS})
target_link_libraries(mhwd ${LIBS})
add_subdirectory(test)
/*
* 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 "device.h"
mhwd::Device::Device(hd_t *hd, TYPE type) {
Device::type = type;
ClassID = from_Hex(hd->base_class.id, 2) + from_Hex(hd->sub_class.id, 2);
VendorID = from_Hex(hd->vendor.id, 4);
DeviceID = from_Hex(hd->device.id, 4);
ClassName = from_CharArray(hd->base_class.name);
VendorName = from_CharArray(hd->vendor.name);
DeviceName = from_CharArray(hd->device.name);
}
// Private
string mhwd::Device::from_Hex(uint16_t hexnum, int fill) {
stringstream stream;
stream << hex << setfill('0') << setw(fill) << hexnum;
return stream.str();
}
string mhwd::Device::from_CharArray(char* c) {
if (c == NULL)
return "";
return string(c);
}
/*
* 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 DEVICE_H
#define DEVICE_H
#include <stdlib.h>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
#include <hd.h>
#include "string.hpp"
using namespace std;
namespace mhwd {
class Device
{
public:
enum TYPE { TYPE_PCI, TYPE_USB };
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; }
private:
string ClassName, DeviceName, VendorName, ClassID, DeviceID, VendorID;
TYPE type;
string from_Hex(uint16_t hexnum, int fill = 4);
string from_CharArray(char* c);
};
}
#endif // DEVICE_H
/*
* 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 "mhwd.h"
vector<mhwd::Device*> mhwd::getDevices(HW hw) {
vector<Device*> devices;
hd_data_t *hd_data;
hd_t *hd;
hd_data = (hd_data_t*)calloc(1, sizeof *hd_data);
hd = hd_list(hd_data, (hw_item)hw, 1, NULL);
for(; hd; hd = hd->next) {
Device::TYPE type = Device::TYPE_PCI;
if (hw == HW_USB)
type = Device::TYPE_USB;
devices.push_back(new Device(hd, type));
}
hd_free_hd_list(hd);
hd_free_hd_data(hd_data);
free(hd_data);
return devices;
}
void mhwd::printDetails(HW hw) {
hd_data_t *hd_data;
hd_t *hd;
hd_data = (hd_data_t*)calloc(1, sizeof *hd_data);
hd = hd_list(hd_data, (hw_item)hw, 1, NULL);
for(; hd; hd = hd->next) {
hd_dump_entry(hd_data, hd, stdout);
}
hd_free_hd_list(hd);
hd_free_hd_data(hd_data);
free(hd_data);
}
/*
* 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 MHWD_H
#define MHWD_H
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <string>
#include <hd.h>
#include "device.h"
using namespace std;
namespace mhwd {
enum HW { HW_PCI = hw_pci, HW_USB = hw_usb };
vector<Device*> getDevices(HW hw);
void printDetails(HW hw);
}
#else
namespace mhwd {
extern vector<Device*> getDevices(hw_item hw);
extern void printDetails(hw_item hw);
}
#endif // LIBMHWD_H
add_executable(test test.cpp)
target_link_libraries(test mhwd)
#include <iostream>
#include "mhwd.h"
using namespace std;
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
#include "mhwd.h"
using namespace std;
int main (int argc, char *argv[])
{
vector<mhwd::Device*> devices = mhwd::getDevices(mhwd::HW_PCI);
for(unsigned int i = 0; i < devices.size(); i++) {
cout << setw(30) << devices[i]->getClassName();
cout << setw(10)<< devices[i]->getClassID();
cout << setw(40)<< devices[i]->getVendorName();
cout << setw(10)<< devices[i]->getVendorID();
cout << setw(30)<< devices[i]->getDeviceName();
cout << setw(10)<< devices[i]->getDeviceID();
cout << endl;
}
cout << endl << endl;
mhwd::printDetails(mhwd::HW_USB);
}
The MIT License
Copyright (c) 2008 Vita Smid <me@ze.phyr.us>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
/**
* @file string.cpp
* Vita::string definition.
*
* Licensed under the terms of the MIT/X11 license.
* Copyright (c) 2008 Vita Smid <me@ze.phyr.us>
*
* $Id: string.cpp 6 2008-07-10 16:41:38Z zephyr $
*/
#include <string.hpp>
namespace Vita {
string string::toLower() const {
string result;
for (size_t i = 0; i < this->length(); i++) {
result += tolower((*this)[i]);
}
return result;
}
string string::toUpper() const {
string result;
for (size_t i = 0; i < this->length(); i++) {
result += toupper((*this)[i]);
}
return result;
}
string string::ucfirst() const {
string result = *this;
result[0] = toupper(result[0]);
return result;
}
string string::lcfirst() const {
string result = *this;
result[0] = tolower(result[0]);
return result;
}
string string::operator+(int operand) const {
return (*this) + string::toStr<int>(operand);
}
string string::operator+(long int operand) const {
return (*this) + string::toStr<long int>(operand);
}
string string::operator+(double operand) const {
return (*this) + string::toStr<double>(operand);
}
string string::operator+(float operand) const {
return (*this) + string::toStr<float>(operand);
}
string string::replace(const string& search, const string& replace, size_t limit) const {
string result;
size_t previous = 0, current;
current = this->find(search);
while (current != npos && limit) {
result += this->substr(previous, current - previous);
result += replace;
previous = current + search.length();
current = this->find(search, previous);
limit--;
}
result += this->substr(previous);
return result;
}
std::vector<string> string::explode(const string& delimiter) const {
std::vector<string> result;
size_t previous = 0, current;
current = this->find(delimiter);
while (current != npos) {
result.push_back(this->substr(previous, current - previous));
previous = current + delimiter.length();
current = this->find(delimiter, previous);
}
result.push_back(this->substr(previous));
return result;
}
string string::trim(const string& what) const {
string result = *this;
size_t pos = result.find_first_not_of(what);
result.erase(0, pos);
pos = result.find_last_not_of(what);
result.erase(pos + 1);
return result;
}
} // namespace Vita
/**
* @file string.hpp
* Vita::string declaration.
*
* Licensed under the terms of the MIT/X11 license.
* Copyright (c) 2008 Vita Smid <me@ze.phyr.us>
*
* $Id: string.hpp 6 2008-07-10 16:41:38Z zephyr $
*/
#ifndef INC_VITA_STRING_HPP
#define INC_VITA_STRING_HPP
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
namespace Vita {
/**
* Slightly enhanced version of std::string.
*/
class string : public std::string {
public:
/**
* Directly call <tt>std::string::string()</tt>.
*/
explicit string():std::string() {};
/**
* Directly call <tt>std::string::string(const char*)</tt>.
*/
string(const char* cstr):std::string(cstr) {};
/**
* Directly call <tt>std::string::string(const char*, size_t)</tt>.
*/
string(const char* cstr, size_t n):std::string(cstr, n) {};
/**
* Directly call <tt>std::string::string(const std::string&)</tt>.
*/
string(const std::string& str):std::string(str) {};
/**
* Directly call <tt>std::string::string(const std::string&, size_t, size_t)</tt>.
*/
string(const std::string& str, size_t pos, size_t n = npos):std::string(str, pos, n) {};
/**
* Directly call <tt>std::string::string(size_t, char)</tt>.
*/
string(size_t n, char c):std::string(n, c) {};
/**
* Convert all characters to lower case.
*/
string toLower() const;
/**
* Convert all characters to upper case.
*/
string toUpper() const;
/**
* Make the first character uppercase.
*/
string ucfirst() const;
/**
* Make the first character lowercase.
*/
string lcfirst() const;
/**
* Convert the operand to string and append it.
*
* This overrides the behavior of std::string.
*
* @param operand The number to be appended.
* @return The string with @a operand appended.
*/
string operator+(int operand) const;
/**
* Convert the operand to string and append it.
*
* This overrides the behavior of std::string.
*
* @param operand The number to be appended.
* @return The string with @a operand appended.
*/
string operator+(long int operand) const;
/**
* Convert the operand to string and append it.
*
* This overrides the behavior of std::string.
*
* @param operand The number to be appended.
* @return The string with @a operand appended.
*/
string operator+(double operand) const;
/**
* Convert the operand to string and append it.
*
* This overrides the behavior of std::string.
*
* @param operand The number to be appended.
* @return The string with @a operand appended.
*/
string operator+(float operand) const;
/**
* Replace all occurences of a certain substring in the string.
*
* @param search The substring that will be replaced.
* @param replace The replacement.
* @param limit How many replacements should be done. Set to Vita::string::npos to disable the limit.
* @return String with the replacement(s) in place.
*/
string replace(const string& search, const string& replace, size_t limit = npos) const;
/**
* Split the string by another string.
*
* This method is similar to the <tt>explode</tt> function known from PHP.
*
* @param delimiter The boundary string.
* @return A vector of strings, each of which is a substring of the original.
*/
std::vector<string> explode(const string& delimiter) const;
/**
* Trim unwanted characters from the beginning and the end of the string.
*
* @param what The characters to trim. Defaults to whitespace (ASCII #9, #10, #13, #32).
* @return The trimmed string.
*/
string trim(const string& what = "\x9\xa\xd\x20") const;
/**
* Convert a generic data type to string.
*
* The conversion is done via std::ostringstream.
*
* @param source The value to convert.
* @return string
*/
template<class T> static string toStr(const T& source) {
std::ostringstream stream;
stream << source;
return string(stream.str());
}
/**
* Convert the string to a generic data type.
*
* The conversion is done via std::istringstream.
*
* @return The converted string.
*/
template<class T> T convert() const {
std::istringstream stream(*this);
T result;
stream >> result;
return result;
}
};
} // namespace Vita
#endif // INC_VITA_STRING_HPP
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