Files
salmanoff/smocore/deviceManager/deviceAttachmentPipeSpecParser.cpp
T

77 lines
1.9 KiB
C++
Raw Normal View History

#include <iostream>
#include <fstream>
#include <stdexcept>
#include <string>
#include <vector>
#include <sstream>
#include <memory>
#include <cstdio>
#include <deviceManager/deviceManager.h>
#include "deviceAttachmentPipeSpecp.hh"
#include "deviceAttachmentPipeSpecl.hh"
2025-07-22 06:48:04 -04:00
namespace smo {
namespace device {
2025-08-29 16:12:30 -04:00
std::string DeviceManager::readDapSpecFile(const std::string& filename)
{
std::ifstream file(filename);
2025-01-17 12:07:29 -04:00
if (!file.is_open())
{
throw std::runtime_error(
2025-08-29 16:12:30 -04:00
std::string(__func__) + ": Couldn't open DAP spec file: "
+ filename);
}
std::string content(
(std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
return content;
}
2025-08-29 16:12:30 -04:00
void DeviceManager::collateAllDapSpecs(void)
{
OptionParser &options = OptionParser::getOptions();
DeviceManager &dm = getInstance();
dm.s.rsrc.allDapSpecs = options.dapSpecs;
2025-08-29 16:12:30 -04:00
for (const auto& file : options.dapSpecFiles)
{
2025-08-29 16:12:30 -04:00
std::string fileContent = readDapSpecFile(file);
if (!dm.s.rsrc.allDapSpecs.empty()) {
dm.s.rsrc.allDapSpecs += "||";
}
dm.s.rsrc.allDapSpecs += fileContent;
}
}
2025-08-29 16:12:30 -04:00
void DeviceManager::parseAllDapSpecs(void)
{
DeviceManager &dm = getInstance();
2025-08-29 17:52:39 -04:00
auto file_deleter = [](FILE* f) { if (f) fclose(f); };
std::unique_ptr<FILE, decltype(file_deleter)> input(
fmemopen(
(void*)dm.s.rsrc.allDapSpecs.c_str(),
dm.s.rsrc.allDapSpecs.size(), "r"),
2025-08-29 17:52:39 -04:00
file_deleter);
2025-01-17 12:07:29 -04:00
if (!input)
{
throw std::runtime_error(
std::string(__func__) + ": Failed to fmemopen() a FILE* for "
2025-08-29 16:12:30 -04:00
"parsing DAP specs");
}
deviceAttachmentPipeSpeclin = input.get();
if (deviceAttachmentPipeSpecpparse())
2025-01-17 12:07:29 -04:00
{
throw std::runtime_error(
2025-08-29 16:12:30 -04:00
std::string(__func__) + ": Failed to parse DAP specs. "
"Check specs for errors");
}
}
} // namespace device
2025-07-22 06:48:04 -04:00
} // namespace smo