DeviceManager: Initial class with DeviceSpecParser
Hopefully by the end of tonight we should be done with the dev spec parser.
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
AM_CPPFLAGS+= -I"$(top_srcdir)/hcore/include"
|
||||
|
||||
noinst_LIBRARIES = libdeviceManager.a
|
||||
libdeviceManager_a_SOURCES = deviceManager.cpp deviceSpecParser.cpp
|
||||
@@ -0,0 +1,35 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <sstream>
|
||||
#include <opts.h>
|
||||
#include <deviceManager/deviceSpecParser.h>
|
||||
#include <deviceManager/deviceManager.h>
|
||||
|
||||
void DeviceManager::handleDeviceSpecifiers(const OptionParser& options)
|
||||
{
|
||||
std::vector<std::string> deviceStrings;
|
||||
deviceStrings.insert(deviceStrings.end(), options.extro.begin(),
|
||||
options.extro.end());
|
||||
deviceStrings.insert(deviceStrings.end(), options.intero.begin(),
|
||||
options.intero.end());
|
||||
deviceStrings.insert(deviceStrings.end(), options.actuator.begin(),
|
||||
options.actuator.end());
|
||||
|
||||
for (const auto& file : options.deviceSpecFiles) {
|
||||
std::vector<std::string> fileDeviceStrings = readDeviceFile(file);
|
||||
deviceStrings.insert(deviceStrings.end(), fileDeviceStrings.begin(),
|
||||
fileDeviceStrings.end());
|
||||
}
|
||||
|
||||
std::vector<std::pair<std::string, std::string>> deviceSpecifiers =
|
||||
parseDeviceSpecifiers(deviceStrings);
|
||||
|
||||
// Use deviceSpecifiers with the device manager
|
||||
for (const auto& specifier : deviceSpecifiers) {
|
||||
std::cout << "Device: " << specifier.first << ", Specifier: "
|
||||
<< specifier.second << std::endl;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <sstream>
|
||||
#include <utility>
|
||||
#include <deviceManager/deviceSpecParser.h>
|
||||
|
||||
std::vector<std::string> readDeviceFile(const std::string& filename)
|
||||
{
|
||||
std::vector<std::string> deviceStrings;
|
||||
std::cerr <<"Here in readDeviceFile" << std::endl;
|
||||
std::ifstream file(filename);
|
||||
std::cerr <<"Here in readDeviceFile after opening ifstream" << std::endl;
|
||||
if (!file.is_open()) {
|
||||
throw std::runtime_error("Could not open device file: " + filename);
|
||||
}
|
||||
|
||||
std::string line;
|
||||
while (std::getline(file, line)) {
|
||||
if (!line.empty()) {
|
||||
deviceStrings.push_back(line);
|
||||
}
|
||||
}
|
||||
|
||||
return deviceStrings;
|
||||
}
|
||||
|
||||
std::vector<std::pair<std::string, std::string>> parseDeviceSpecifiers(
|
||||
const std::vector<std::string>& deviceStrings)
|
||||
{
|
||||
std::vector<std::pair<std::string, std::string>> deviceSpecifiers;
|
||||
for (const auto& deviceString : deviceStrings) {
|
||||
auto pos = deviceString.find(':');
|
||||
if (pos != std::string::npos) {
|
||||
std::string deviceName = deviceString.substr(0, pos);
|
||||
std::string deviceSpec = deviceString.substr(pos + 1);
|
||||
deviceSpecifiers.emplace_back(deviceName, deviceSpec);
|
||||
} else {
|
||||
throw std::invalid_argument("Invalid device specifier: " +
|
||||
deviceString);
|
||||
}
|
||||
}
|
||||
return deviceSpecifiers;
|
||||
}
|
||||
Reference in New Issue
Block a user