Jpp 21.0.0
the software that should make you happy
Loading...
Searching...
No Matches
meta_utils.cc File Reference
#include "km3net-dataformat/tools/meta_utils.hh"
#include <unordered_map>
#include <sstream>
#include <regex>
#include <stdexcept>
#include "TFile.h"
#include "TDirectoryFile.h"
#include "TKey.h"
#include "km3net-dataformat/definitions/root.hh"

Go to the source code of this file.

Functions

std::unordered_map< std::string, std::unordered_map< std::string, std::string > > read_meta (const char *filename)
 
std::string get_jpp_version_from_meta (const std::unordered_map< std::string, std::unordered_map< std::string, std::string > > &meta)
 Retrieve the JPP version from the metadata (mainly used for testing purposes)
 
std::string get_jpp_version_from_file (const char *filename)
 Reads the jpp version from file.
 
int get_jpp_major_version (const std::string &version_str)
 Get the jpp major version.
 

Function Documentation

◆ read_meta()

std::unordered_map< std::string, std::unordered_map< std::string, std::string > > read_meta ( const char * filename)

Definition at line 12 of file meta_utils.cc.

13{
14 std::unique_ptr<TFile> file(TFile::Open(filename));
15 if (!file)
16 {
17 throw std::runtime_error("Cannot open file");
18 }
19 auto *dir = file->Get<TDirectoryFile>(TDIRECTORY_META);
20 if (!dir)
21 {
22 throw std::runtime_error("No META dir found in file");
23 }
24 std::unordered_map<std::string, std::unordered_map<std::string, std::string>> meta;
25 for (auto *key : *dir->GetListOfKeys())
26 {
27 std::istringstream buffer(key->GetTitle());
28 std::string line;
29 while (std::getline(buffer, line))
30 {
31 auto idx = line.find("=");
32 meta[key->GetName()][line.substr(0, idx).c_str()] = line.substr(idx + 1).c_str();
33 }
34 }
35 return meta;
36}
static const char *const TDIRECTORY_META
META directory name.
Definition root.hh:32

◆ get_jpp_version_from_meta()

std::string get_jpp_version_from_meta ( const std::unordered_map< std::string, std::unordered_map< std::string, std::string > > & meta)

Retrieve the JPP version from the metadata (mainly used for testing purposes)

Parameters
metaMetadata
Returns
JPP version or empty string if nothing is found

Definition at line 38 of file meta_utils.cc.

39{
40 for (const auto &[app, md] : meta)
41 {
42 if (app.rfind("J", 0) == 0)
43 {
44 for (const auto &[key, val] : md)
45 {
46 if (key == GIT_META_KEY)
47 {
48 return val;
49 }
50 }
51 }
52 }
53 throw std::runtime_error("Cannot find JPP version from metadata");
54 return "";
55}
static const char *const GIT_META_KEY
GIT key in META.
Definition root.hh:33

◆ get_jpp_version_from_file()

std::string get_jpp_version_from_file ( const char * filename)

Reads the jpp version from file.

Parameters
filenameInput filename
Returns
JPP version or empty string if nothing is found

Definition at line 57 of file meta_utils.cc.

58{
59 auto meta = read_meta(filename);
60 return get_jpp_version_from_meta(meta);
61}
std::string get_jpp_version_from_meta(const std::unordered_map< std::string, std::unordered_map< std::string, std::string > > &meta)
Retrieve the JPP version from the metadata (mainly used for testing purposes)
Definition meta_utils.cc:38
std::unordered_map< std::string, std::unordered_map< std::string, std::string > > read_meta(const char *filename)
Definition meta_utils.cc:12

◆ get_jpp_major_version()

int get_jpp_major_version ( const std::string & version_str)

Get the jpp major version.

Parameters
version_strVersion string
Returns
Major version as integer

Definition at line 63 of file meta_utils.cc.

64{
65 std::regex re(R"(^v?(\d+)(?:\.|$))");
66 std::smatch match;
67
68 if (std::regex_search(version_str, match, re))
69 {
70 return std::stoi(match[1].str());
71 }
72
73 return -1;
74}