cepgen is hosted by Hepforge, IPPP Durham
CepGen 1.2.5
Central exclusive processes event generator
Loading...
Searching...
No Matches
Environment.cpp
Go to the documentation of this file.
1/*
2 * CepGen: a central exclusive processes event generator
3 * Copyright (C) 2013-2024 Laurent Forthomme
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <cstdlib>
20
23#include "CepGen/Utils/String.h"
24
25namespace cepgen {
26 namespace utils {
27 namespace env {
28 std::string get(const std::string& var, const std::string& def) {
29 if (const auto out = std::getenv(var.c_str()); out != nullptr)
30 return std::string(out);
31 return def;
32 }
33
34 std::vector<std::string> searchPaths() {
35 const auto cepgen_path = fs::path(env::get("CEPGEN_PATH", "."));
36 return std::vector<std::string>{cepgen_path,
37 cepgen_path / "CepGen",
38 cepgen_path / "lib",
39 cepgen_path / "lib64",
40 cepgen_path / "share" / "CepGen",
41 fs::current_path(),
42 fs::current_path().parent_path(),
43 fs::current_path().parent_path().parent_path(),
44 // additional paths for local builds
45 cepgen_path / "External",
46 cepgen_path / "build"};
47 }
48
49 void set(const std::string& var, const std::string& value) { setenv(var.c_str(), value.c_str(), 1); }
50
51#ifdef _WIN32
52 static constexpr char PATH_DELIM = ';';
53#else
54 static constexpr char PATH_DELIM = ':';
55#endif
56
57 void append(const std::string& var, const std::string& value) {
58 auto env = split(env::get(var, ""), PATH_DELIM);
59 env.emplace_back(value);
60 normalise(env);
61 setenv(var.c_str(), merge(env, std::string(1, PATH_DELIM)).c_str(), 1);
62 }
63
64 void unset(const std::string& var) { unsetenv(var.c_str()); }
65 } // namespace env
66 } // namespace utils
67} // namespace cepgen
std::string get(const std::string &var, const std::string &def)
Get an environment variable.
static constexpr char PATH_DELIM
void set(const std::string &var, const std::string &value)
Set an environment variable.
void unset(const std::string &var)
Clear an environment variable.
std::vector< std::string > searchPaths()
Retrieve a list of all search paths for external files.
void append(const std::string &var, const std::string &value)
Add a value to an environment variable.
void normalise(std::vector< T > &coll)
Remove duplicates and sort a collection.
Definition Collections.h:63
std::string merge(const std::vector< T > &vec, const std::string &delim)
Merge a collection of a printable type in a single string.
Definition String.cpp:248
std::vector< std::string > split(const std::string &str, char delim, bool trim)
Split a string according to a separation character.
Definition String.cpp:233
Common namespace for this Monte Carlo generator.