otsdaq  v2_05_02_indev
FileConfigurationInterface.cc
1 #include "otsdaq/ConfigurationInterface/FileConfigurationInterface.h"
2 #include <dirent.h>
3 #include <errno.h>
4 #include <iostream>
5 #include <set>
6 
7 #include "otsdaq/ConfigurationInterface/ConfigurationHandler.h"
8 #include "otsdaq/Macros/CoutMacros.h"
9 #include "otsdaq/MessageFacility/MessageFacility.h"
10 #include "otsdaq/TableCore/TableBase.h"
11 
12 using namespace ots;
13 
14 //==============================================================================
15 void FileConfigurationInterface::fill(TableBase* configuration, TableVersion version) const { ConfigurationHandler::readXML(configuration, version); }
16 
17 //==============================================================================
18 // findLatestVersion
19 // return INVALID if no existing versions
20 TableVersion FileConfigurationInterface::findLatestVersion(const TableBase* configuration) const
21 {
22  auto versions = getVersions(configuration);
23  if(!versions.size())
24  return TableVersion(); // return INVALID
25  return *(versions.rbegin());
26 }
27 
28 //==============================================================================
29 // save active configuration view to file
30 void FileConfigurationInterface::saveActiveVersion(const TableBase* configuration, bool /*overwrite*/) const { ConfigurationHandler::writeXML(configuration); }
31 
32 //==============================================================================
33 std::set<TableVersion> FileConfigurationInterface::getVersions(const TableBase* configuration) const
34 {
35  std::string configDir = ConfigurationHandler::getXMLDir(configuration);
36  std::cout << __COUT_HDR_FL__ << "ConfigurationDir: " << configDir << std::endl;
37  DIR* dp;
38 
39  struct dirent* dirp;
40 
41  if((dp = opendir(configDir.c_str())) == 0)
42  {
43  __SS__ << "ERROR:(" << errno << "). Can't open directory: " << configDir << std::endl;
44  __COUT_ERR__ << ss.str();
45  __SS_THROW__;
46  }
47 
48  const unsigned char isDir = 0x4;
49  // const std::string pDir = ".";
50  // const std::string ppDir = "..";
51  // int dirVersion;
52  std::string::const_iterator it;
53  std::string dirName;
54  std::set<TableVersion> dirNumbers;
55 
56  while((dirp = readdir(dp)) != 0)
57  {
58  if(dirp->d_type == isDir && dirp->d_name[0] != '.')
59  {
60  dirName = dirp->d_name;
61  // Check if there are non numeric directories
62  it = dirName.begin();
63 
64  while(it != dirName.end() && std::isdigit(*it))
65  ++it;
66 
67  if(dirName.empty() || it != dirName.end())
68  {
69  std::cout << __COUT_HDR_FL__ << "WARNING: there is a non numeric directory in " << configDir << " called " << dirName
70  << " please remove it from there since only numeric "
71  "directories are considered."
72  << std::endl;
73  continue;
74  }
75 
76  dirNumbers.insert(TableVersion(strtol(dirp->d_name, 0, 10)));
77  }
78  }
79  closedir(dp);
80  return dirNumbers;
81 }