Jpp 21.0.1
the software that should make you happy
Loading...
Searching...
No Matches
meta_utils.cc
Go to the documentation of this file.
2
3#include <unordered_map>
4#include <sstream>
5#include <regex>
6#include <stdexcept>
7#include "TFile.h"
8#include "TDirectoryFile.h"
9#include "TKey.h"
11
12std::unordered_map<std::string, std::unordered_map<std::string, std::string>> read_meta(const char *filename)
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}
37
38std::string get_jpp_version_from_meta(const std::unordered_map<std::string, std::unordered_map<std::string, std::string>> &meta)
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}
56
57std::string get_jpp_version_from_file(const char *filename)
58{
59 auto meta = read_meta(filename);
60 return get_jpp_version_from_meta(meta);
61}
62
63int get_jpp_major_version(const std::string &version_str)
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}
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::string get_jpp_version_from_file(const char *filename)
Reads the jpp version from file.
Definition meta_utils.cc:57
std::unordered_map< std::string, std::unordered_map< std::string, std::string > > read_meta(const char *filename)
Definition meta_utils.cc:12
int get_jpp_major_version(const std::string &version_str)
Get the jpp major version.
Definition meta_utils.cc:63
static const char *const GIT_META_KEY
GIT key in META.
Definition root.hh:33
static const char *const TDIRECTORY_META
META directory name.
Definition root.hh:32