ChimeraTK-DeviceAccess 03.25.00
Loading...
Searching...
No Matches
DMapFileParser.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Deutsches Elektronen-Synchrotron DESY, MSK, ChimeraTK Project <chimeratk-support@desy.de>
2// SPDX-License-Identifier: LGPL-3.0-or-later
3
4#include "DMapFileParser.h"
5
6#include "parserUtilities.h"
7// #include "Utilities.h"
8
9#include <algorithm>
10#include <fstream>
11#include <iostream>
12#include <sstream>
13
15
16namespace ChimeraTK {
17
18 DeviceInfoMapPointer DMapFileParser::parse(const std::string& file_name) {
19 std::ifstream file;
20 std::string line;
21 uint32_t line_nr = 0;
22
23 std::string absPathToDMapFile = utilities::convertToAbsolutePath(file_name);
24
25 file.open(absPathToDMapFile.c_str());
26 if(!file) {
27 throw ChimeraTK::logic_error("Cannot open dmap file: \"" + absPathToDMapFile + "\"");
28 }
29
30 DeviceInfoMapPointer dmap(new DeviceInfoMap(absPathToDMapFile));
31 while(std::getline(file, line)) {
32 line_nr++;
33 line.erase(line.begin(), std::find_if(line.begin(), line.end(), [](int c) { return !isspace(c); }));
34 if(line.empty()) {
35 continue;
36 }
37 if(line[0] == '#') {
38 continue;
39 }
40 if(line[0] == '@') {
41 parseForLoadLib(file_name, line, line_nr, dmap);
42 continue;
43 }
44 parseRegularLine(file_name, line, line_nr, dmap);
45 }
46 file.close();
47
48 if(dmap->getSize() == 0) {
49 throw ChimeraTK::logic_error("No data in in dmap file: \"" + file_name + "\"");
50 }
51 return dmap;
52 }
53
55 const std::string& file_name, const std::string& line, uint32_t line_nr, const DeviceInfoMapPointer& dmap) {
56 // we expect at leat two tokens: the key and the value
57 std::istringstream s;
58 std::string key, value;
59 s.str(line);
60 s >> key >> value;
61 if(s && (key == "@LOAD_LIB")) {
62 dmap->addPluginLibrary(absPathOfDMapContent(value, file_name));
63 }
64 else {
65 raiseException(file_name, line, line_nr);
66 }
67 }
68
70 const std::string& file_name, const std::string& line, uint32_t line_nr, const DeviceInfoMapPointer& dmap) {
71 std::istringstream inStream;
73
74 inStream.str(line);
75 inStream >> deviceInfo.deviceName >> deviceInfo.uri >> deviceInfo.mapFileName;
76
77 if(inStream) {
78 std::string absPathToDMapFile = utilities::convertToAbsolutePath(file_name);
79 std::string absPathToMapFile = absPathOfDMapContent(deviceInfo.mapFileName, file_name);
80 deviceInfo.mapFileName = absPathToMapFile;
81 deviceInfo.dmapFileName = absPathToDMapFile;
82 deviceInfo.dmapFileLineNumber = line_nr;
83 dmap->insert(deviceInfo);
84 }
85 else {
86 std::istringstream inStream2;
87 inStream2.str(line);
88 inStream2 >> deviceInfo.deviceName >> deviceInfo.uri;
89
90 if(inStream2) {
91 std::string absPathToDMapFile = utilities::convertToAbsolutePath(file_name);
92 deviceInfo.mapFileName = "";
93 deviceInfo.dmapFileName = absPathToDMapFile;
94 deviceInfo.dmapFileLineNumber = line_nr;
95 dmap->insert(deviceInfo);
96 }
97 else {
98 raiseException(file_name, line, line_nr);
99 }
100 }
101 }
102
103 std::string DMapFileParser::absPathOfDMapContent(const std::string& dmapContent, const std::string& dmapFileName) {
104 // first determine the absolute path to the dmap file
105 std::string absPathToDMapFile = utilities::convertToAbsolutePath(dmapFileName);
106 // then extract the directory
107 std::string absPathToDmapDirectory = utilities::extractDirectory(absPathToDMapFile);
108 // now concatenate the dmap diretory to the entry in the dmap file (if the
109 // latter is not absolute)
110 return utilities::concatenatePaths(absPathToDmapDirectory, dmapContent);
111 }
112
113 void DMapFileParser::raiseException(const std::string& file_name, const std::string& line, uint32_t line_nr) {
114 std::stringstream errorMessage;
115 errorMessage << "Error in dmap file: \"" << file_name << "\" in line (" << line_nr << ") \"" << line << "\"";
116 throw ChimeraTK::logic_error(errorMessage.str());
117 }
118
119} // namespace ChimeraTK
static void raiseException(const std::string &file_name, const std::string &line, uint32_t line_nr)
static void parseRegularLine(const std::string &file_name, const std::string &line, uint32_t line_nr, const DeviceInfoMapPointer &dmap)
static void parseForLoadLib(const std::string &file_name, const std::string &line, uint32_t line_nr, const DeviceInfoMapPointer &dmap)
static std::string absPathOfDMapContent(const std::string &dmapContent, const std::string &dmapFileName)
static DeviceInfoMapPointer parse(const std::string &file_name)
Performs parsing of specified DMAP file.
Stores information about one device.
std::string uri
uri which describes the device (or name of the device file in /dev in backward compatibility mode)
std::string dmapFileName
name of the DMAP file
std::string mapFileName
name of the MAP file storing information about PCIe registers mapping
uint32_t dmapFileLineNumber
line number in DMAP file storing listed above information
std::string deviceName
logical name of the device
Exception thrown when a logic error has occured.
Definition Exception.h:51
std::string extractDirectory(std::string const &path)
Returns the path to the directory containing the file provided as the input parameter.
std::string concatenatePaths(const std::string &path1, const std::string &path2)
Concatenates two given paths using custom rules.
std::string convertToAbsolutePath(std::string const &relativePath)
Converts a relative path to its absolute path.
boost::shared_ptr< DeviceInfoMap > DeviceInfoMapPointer
Introduce specialisation of shared_pointer template for pointers to RegisterInfoMap object as a Devic...