DevMgr: Implement a wrapper around the yacc parser.

This wrapper reads the -d <devfile> arguments, collates
them into one big string and then feeds them into yacc.
This commit is contained in:
2025-01-07 14:11:51 -04:00
parent 5c3bbdf114
commit 42413d1cc6
3 changed files with 70 additions and 40 deletions
+37 -27
View File
@@ -4,43 +4,53 @@
#include <string>
#include <vector>
#include <sstream>
#include <utility>
#include <deviceManager/deviceSpecParser.h>
#include <memory>
#include <cstdio>
#include <deviceManager/deviceManager.h>
#include "deviceSpecp.hh"
#include "deviceSpecl.hh"
std::vector<std::string> readDeviceFile(const std::string& filename)
std::string DeviceManager::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);
throw std::runtime_error("Could not open file: " + filename);
}
std::string line;
while (std::getline(file, line)) {
if (!line.empty()) {
deviceStrings.push_back(line);
}
}
std::string content(
(std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
return deviceStrings;
return content;
}
std::vector<std::pair<std::string, std::string>> parseDeviceSpecifiers(
const std::vector<std::string>& deviceStrings)
void DeviceManager::collateAllDeviceSpecs(const OptionParser& options)
{
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);
allDeviceSpecs = options.deviceSpecs;
for (const auto& file : options.deviceSpecFiles)
{
std::string fileContent = readDeviceFile(file);
if (!allDeviceSpecs.empty()) {
allDeviceSpecs += "||";
}
allDeviceSpecs += fileContent;
}
}
void DeviceManager::parseAllDeviceSpecs(void)
{
std::unique_ptr<FILE, decltype(&fclose)> input(
fmemopen((void*)allDeviceSpecs.c_str(),
allDeviceSpecs.size(), "r"),
&fclose);
if (!input) {
throw std::runtime_error("Failed to open memory as file");
}
deviceSpeclin = input.get();
if (deviceSpecpparse()) {
throw std::runtime_error("Failed to parse device specs");
}
return deviceSpecifiers;
}