ChimeraTK-DeviceAccess  03.18.00
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 
16 namespace 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;
72  DeviceInfoMap::DeviceInfo deviceInfo;
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
ChimeraTK::DMapFileParser::parse
static DeviceInfoMapPointer parse(const std::string &file_name)
Performs parsing of specified DMAP file.
Definition: DMapFileParser.cpp:18
ChimeraTK::DMapFileParser::parseForLoadLib
static void parseForLoadLib(const std::string &file_name, const std::string &line, uint32_t line_nr, const DeviceInfoMapPointer &dmap)
Definition: DMapFileParser.cpp:54
ChimeraTK::DMapFileParser::parseRegularLine
static void parseRegularLine(const std::string &file_name, const std::string &line, uint32_t line_nr, const DeviceInfoMapPointer &dmap)
Definition: DMapFileParser.cpp:69
ChimeraTK::DeviceInfoMap
Definition: DeviceInfoMap.h:25
ChimeraTK::DeviceInfoMap::DeviceInfo::deviceName
std::string deviceName
logical name of the device
Definition: DeviceInfoMap.h:32
ChimeraTK::DeviceInfoMap::DeviceInfo::mapFileName
std::string mapFileName
name of the MAP file storing information about PCIe registers mapping
Definition: DeviceInfoMap.h:35
ChimeraTK::DeviceInfoMap::DeviceInfo::dmapFileName
std::string dmapFileName
name of the DMAP file
Definition: DeviceInfoMap.h:37
parserUtilities.h
ChimeraTK::parserUtilities::convertToAbsolutePath
std::string convertToAbsolutePath(std::string const &relativePath)
Converts a relative path to its absolute path.
Definition: parserUtilities.cc:23
ChimeraTK::DeviceInfoMapPointer
boost::shared_ptr< DeviceInfoMap > DeviceInfoMapPointer
Definition: DeviceInfoMap.h:221
ChimeraTK::DeviceInfoMap::DeviceInfo::dmapFileLineNumber
uint32_t dmapFileLineNumber
line number in DMAP file storing listed above information
Definition: DeviceInfoMap.h:38
ChimeraTK::DMapFileParser::raiseException
static void raiseException(const std::string &file_name, const std::string &line, uint32_t line_nr)
Definition: DMapFileParser.cpp:113
ChimeraTK::parserUtilities::extractDirectory
std::string extractDirectory(std::string const &path)
Returns the path to the directory containing the file provided as the input parameter.
Definition: parserUtilities.cc:35
ChimeraTK::parserUtilities::concatenatePaths
std::string concatenatePaths(const std::string &path1, const std::string &path2)
Concatenates two given paths using custom rules.
Definition: parserUtilities.cc:27
DMapFileParser.h
ChimeraTK::DeviceInfoMap::DeviceInfo
Stores information about one device.
Definition: DeviceInfoMap.h:30
ChimeraTK::DMapFileParser::absPathOfDMapContent
static std::string absPathOfDMapContent(const std::string &dmapContent, const std::string &dmapFileName)
Definition: DMapFileParser.cpp:103
ChimeraTK::DeviceInfoMap::DeviceInfo::uri
std::string uri
uri which describes the device (or name of the device file in /dev in backward compatibility mode)
Definition: DeviceInfoMap.h:33
ChimeraTK
Definition: DummyBackend.h:16
ChimeraTK::parserUtilities
Definition: parserUtilities.h:7
ChimeraTK::logic_error
Exception thrown when a logic error has occured.
Definition: Exception.h:51