guts
Loading...
Searching...
No Matches
gutilities.h
Go to the documentation of this file.
1#pragma once
2
3// c++
4#include <vector>
5#include <string>
6#include <map>
7#include <string_view>
8#include <optional>
9
10// geant4
11#include "G4Colour.hh"
12#include "G4UImanager.hh" // Geant4 UI manager access
13
14namespace gutilities {
15using std::string;
16using std::vector;
17using std::map;
18
28std::string removeLeadingAndTrailingSpacesFromString(const std::string& input);
29
30std::string_view removeLeadingAndTrailingSpacesFromString(const std::string_view input);
31
40std::string removeAllSpacesFromString(const std::string& str);
41
50std::string getFileFromPath(const std::string& path);
51
60std::string getDirFromPath(const std::string& path);
61
62std::optional<std::string> searchForFileInLocations(
63 const std::vector<std::string>& locations,
64 std::string_view filename);
65
75std::vector<std::string> getStringVectorFromString(const std::string& input);
76
88std::string replaceCharInStringWithChars(const std::string& input, const std::string& toReplace,
89 const std::string& replacement);
90
91
103string replaceAllStringsWithString(const string& source, const string& from, const string& to);
104
116string fillDigits(const string& word, const string& c, int ndigits);
117
128double getG4Number(const string& v, bool warnIfNotUnit = false);
129
139double getG4Number(double input, const string& unit);
140
150vector<double> getG4NumbersFromStringVector(const vector<string>& vstring, bool warnIfNotUnit = false);
151
161vector<double> getG4NumbersFromString(const string& vstring, bool warnIfNotUnit = false);
162
173string parseFileAndRemoveComments(const string& filename, const string& commentChars = "#", int verbosity = 0);
174
175
186string retrieveStringBetweenChars(const string& input, const string& firstDelimiter, const string& secondDelimiter);
187
188
198vector<string> getStringVectorFromStringWithDelimiter(const string& input, const string& x);
199
208bool directoryExists(const std::string& path);
209
210
220string searchForDirInLocations(const string& dirName, const vector<string>& possibleLocations);
221
222
232bool hasExtension(const string& filename, const vector<string>& extensions);
233
243vector<string> getListOfFilesInDirectory(const string& dirName, const vector<string>& extensions);
244
253string convertToLowercase(const string& str);
254
265template <class KEY, class VALUE>
266vector<KEY> getKeys(const map<KEY, VALUE>& map);
267
278
289randomModel stringToRandomModel(const std::string& str);
290
291inline constexpr const char* to_string(randomModel m) noexcept {
292 switch (m) {
293 case uniform: return "uniform";
294 case gaussian: return "gaussian";
295 case cosine: return "cosine";
296 case sphere: return "sphere";
297 }
298 return "<unknown>";
299}
300
310G4Colour makeG4Colour(std::string_view code, double opacity);
311};
312
313
314#include <filesystem>
315
316#if defined(__APPLE__)
317#include <mach-o/dyld.h> // _NSGetExecutablePath
318#elif defined(__linux__)
319#include <unistd.h> // readlink
320#include <limits.h>
321#elif defined(_WIN32)
322#include <windows.h>
323#include <vector>
324#else
325#error "Unsupported platform"
326#endif
327
328namespace gutilities {
329inline std::filesystem::path executable_path() {
330#if defined(__APPLE__)
331 char buf[PATH_MAX];
332 uint32_t sz = sizeof(buf);
333 if (_NSGetExecutablePath(buf, &sz) != 0) {
334 // buffer too small
335 std::string big(sz, '\0');
336 if (_NSGetExecutablePath(big.data(), &sz) != 0)
337 throw std::runtime_error("_NSGetExecutablePath failed");
338 return std::filesystem::canonical(big);
339 }
340 return std::filesystem::canonical(buf);
341
342#elif defined(__linux__)
343 char buf[PATH_MAX];
344 ssize_t len = ::readlink("/proc/self/exe", buf, sizeof(buf) - 1);
345 if (len == -1)
346 throw std::runtime_error("readlink(/proc/self/exe) failed");
347 buf[len] = '\0';
348 return std::filesystem::canonical(buf);
349
350#elif defined(_WIN32)
351 std::wstring buf(MAX_PATH, L'\0');
352 DWORD len = ::GetModuleFileNameW(nullptr, buf.data(), buf.size());
353 if (len == 0)
354 throw std::runtime_error("GetModuleFileNameW failed");
355 // If the path is longer than MAX_PATH the buffer is truncated;
356 // do a second call with the returned length to get the full path.
357 if (len == buf.size()) {
358 buf.resize(len * 2);
359 len = ::GetModuleFileNameW(nullptr, buf.data(), buf.size());
360 }
361 buf.resize(len);
362 return std::filesystem::canonical(std::filesystem::path(buf));
363#endif
364}
365
366inline std::filesystem::path gemc_root() {
367 auto exe_dir = executable_path().parent_path(); // where the executable is installed
368 auto gemc_root = exe_dir.parent_path(); // one dir up. The danger here is where this is a
369
370 // Sanity‑check api dir
371 if (!std::filesystem::exists(gemc_root / "api"))
372 throw std::runtime_error("Cannot locate directory >api< check directory layout");
373
374 return gemc_root;
375}
376
377bool is_unset(std::string_view s);
378
379inline std::string success_or_fail(bool condition) { return condition ? "success" : "fail"; }
380void apply_uimanager_commands(const std::string& commands);
381
382}
string replaceAllStringsWithString(const string &source, const string &from, const string &to)
Replaces all occurrences of a substring with another string.
Definition gutilities.cc:85
double getG4Number(const string &v, bool warnIfNotUnit)
Converts a string representation of a number with optional units to a double.
bool hasExtension(const std::string &filename, const std::vector< std::string > &extensions)
Checks if a filename has one of the specified extensions.
vector< double > getG4NumbersFromString(const string &vstring, bool warnIfNotUnit)
Converts a comma-separated string of numbers with units to a vector of doubles.
string removeAllSpacesFromString(const std::string &str)
Removes all spaces from a string.
Definition gutilities.cc:39
std::filesystem::path executable_path()
Definition gutilities.h:329
vector< string > getStringVectorFromStringWithDelimiter(const string &input, const string &x)
Splits a string into a vector of substrings using a specified delimiter.
G4Colour makeG4Colour(std::string_view code, double opacity)
Convert a hex colour string to G4Colour.
randomModel stringToRandomModel(const std::string &str)
Converts a string to a corresponding randomModel enum value.
std::filesystem::path gemc_root()
Definition gutilities.h:366
vector< string > getListOfFilesInDirectory(const string &dirName, const vector< string > &extensions)
Retrieves a list of files with specific extensions from a directory.
vector< double > getG4NumbersFromStringVector(const vector< string > &vstring, bool warnIfNotUnit)
Converts a vector of strings representing numbers with units to a vector of doubles.
string retrieveStringBetweenChars(const string &input, const string &firstDelimiter, const string &secondDelimiter)
Retrieves a substring between two specified delimiters in a string.
string replaceCharInStringWithChars(const std::string &input, const std::string &toReplace, const std::string &replacement)
Replaces all occurrences of specified characters in a string with another string.
Definition gutilities.cc:75
string fillDigits(const string &word, const string &c, int ndigits)
Pads a string with a specified character until it reaches a desired length.
std::string success_or_fail(bool condition)
Definition gutilities.h:379
bool is_unset(std::string_view s)
randomModel
Enumeration of random models.
Definition gutilities.h:272
@ gaussian
Gaussian distribution.
Definition gutilities.h:274
@ uniform
Uniform distribution.
Definition gutilities.h:273
@ sphere
Sphere distribution.
Definition gutilities.h:276
@ cosine
Cosine distribution.
Definition gutilities.h:275
string getDirFromPath(const std::string &path)
Extracts the directory path from a given file path.
Definition gutilities.cc:54
string convertToLowercase(const string &str)
Converts a string to lowercase.
bool directoryExists(const std::string &path)
Checks if a directory exists at the given path.
void apply_uimanager_commands(const std::string &command)
vector< KEY > getKeys(const map< KEY, VALUE > &map)
Retrieves all keys from a map.
string removeLeadingAndTrailingSpacesFromString(const std::string &input)
Removes leading and trailing spaces and tabs from a string.
Definition gutilities.cc:21
std::optional< std::string > searchForFileInLocations(const std::vector< std::string > &locations, std::string_view filename)
string searchForDirInLocations(const string &dirName, const vector< string > &possibleLocations)
Searches for a directory within a list of possible locations.
string parseFileAndRemoveComments(const string &filename, const string &commentChars, int verbosity)
Parses a file and removes all lines containing specified comment characters.
string getFileFromPath(const std::string &path)
Extracts the filename from a given file path.
Definition gutilities.cc:45
constexpr const char * to_string(randomModel m) noexcept
Definition gutilities.h:291
vector< std::string > getStringVectorFromString(const std::string &input)
Splits a string into a vector of strings using spaces as delimiters.
Definition gutilities.cc:63