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
9// geant4
10#include "G4Colour.hh"
11
12namespace gutilities {
13
14using std::string;
15using std::vector;
16using std::map;
17
27std::string removeLeadingAndTrailingSpacesFromString(const std::string& input);
28
37std::string removeAllSpacesFromString(const std::string& str);
38
47std::string getFileFromPath(const std::string& path);
48
57std::string getDirFromPath(const std::string& path);
58
59
69std::vector<std::string> getStringVectorFromString(const std::string& input);
70
82std::string replaceCharInStringWithChars(const std::string& input, const std::string& toReplace,
83 const std::string& replacement);
84
85
97string replaceAllStringsWithString(const string& source, const string& from, const string& to);
98
110string fillDigits(const string& word, const string& c, int ndigits);
111
122double getG4Number(const string& v, bool warnIfNotUnit = false);
123
133double getG4Number(double input, const string& unit);
134
144vector<double> getG4NumbersFromStringVector(const vector<string>& vstring, bool warnIfNotUnit = false);
145
155vector<double> getG4NumbersFromString(const string& vstring, bool warnIfNotUnit = false);
156
167string parseFileAndRemoveComments(const string& filename, const string& commentChars = "#", int verbosity = 0);
168
169
180string retrieveStringBetweenChars(const string& input, const string& firstDelimiter, const string& secondDelimiter);
181
182
192vector<string> getStringVectorFromStringWithDelimiter(const string& input, const string& x);
193
202bool directoryExists(const std::string& path);
203
204
214string searchForDirInLocations(const string& dirName, const vector<string>& possibleLocations);
215
216
226bool hasExtension(const string& filename, const vector<string>& extensions);
227
237vector<string> getListOfFilesInDirectory(const string& dirName, const vector<string>& extensions);
238
247string convertToLowercase(const string& str);
248
259template <class KEY, class VALUE>
260vector<KEY> getKeys(const map<KEY, VALUE>& map);
261
271
282randomModel stringToRandomModel(const std::string& str);
283
284inline constexpr const char* to_string(randomModel m) noexcept {
285 switch (m) {
286 case uniform: return "uniform";
287 case gaussian: return "gaussian";
288 case cosine: return "cosine";
289 case sphere: return "sphere";
290 }
291 return "<unknown>";
292}
293
303G4Colour makeColour(std::string_view code);
304
305};
306
307
308#include <filesystem>
309
310#if defined(__APPLE__)
311#include <mach-o/dyld.h> // _NSGetExecutablePath
312#elif defined(__linux__)
313#include <unistd.h> // readlink
314#include <limits.h>
315#elif defined(_WIN32)
316#include <windows.h>
317#include <vector>
318#else
319#error "Unsupported platform"
320#endif
321
322namespace gutilities {
323inline std::filesystem::path executable_path() {
324#if defined(__APPLE__)
325 char buf[PATH_MAX];
326 uint32_t sz = sizeof(buf);
327 if (_NSGetExecutablePath(buf, &sz) != 0) { // buffer too small
328 std::string big(sz, '\0');
329 if (_NSGetExecutablePath(big.data(), &sz) != 0)
330 throw std::runtime_error("_NSGetExecutablePath failed");
331 return std::filesystem::canonical(big);
332 }
333 return std::filesystem::canonical(buf);
334
335#elif defined(__linux__)
336 char buf[PATH_MAX];
337 ssize_t len = ::readlink("/proc/self/exe", buf, sizeof(buf)-1);
338 if (len == -1)
339 throw std::runtime_error("readlink(/proc/self/exe) failed");
340 buf[len] = '\0';
341 return std::filesystem::canonical(buf);
342
343#elif defined(_WIN32)
344 std::wstring buf(MAX_PATH, L'\0');
345 DWORD len = ::GetModuleFileNameW(nullptr, buf.data(), buf.size());
346 if (len == 0)
347 throw std::runtime_error("GetModuleFileNameW failed");
348 // If the path is longer than MAX_PATH the buffer is truncated;
349 // do a second call with the returned length to get the full path.
350 if (len == buf.size()) {
351 buf.resize(len * 2);
352 len = ::GetModuleFileNameW(nullptr, buf.data(), buf.size());
353 }
354 buf.resize(len);
355 return std::filesystem::canonical(std::filesystem::path(buf));
356#endif
357}
358
359inline std::filesystem::path gemc_root() {
360 auto exe_dir = executable_path().parent_path(); // where the executable is installed
361 auto gemc_root = exe_dir.parent_path(); // one dir up. The danger here is where this is a
362
363 // Sanity‑check api dir
364 if (!std::filesystem::exists(gemc_root / "api"))
365 throw std::runtime_error("Cannot locate directory >api< check directory layout");
366
367 return gemc_root;
368}
369
370inline std::string success_or_fail(bool condition) { return condition ? "success" : "fail"; }
371
372}
string replaceAllStringsWithString(const string &source, const string &from, const string &to)
Replaces all occurrences of a substring with another string.
Definition gutilities.cc:78
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:32
std::filesystem::path executable_path()
Definition gutilities.h:323
vector< string > getStringVectorFromStringWithDelimiter(const string &input, const string &x)
Splits a string into a vector of substrings using a specified delimiter.
randomModel stringToRandomModel(const std::string &str)
Converts a string to a corresponding randomModel enum value.
std::filesystem::path gemc_root()
Definition gutilities.h:359
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:68
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:370
randomModel
Enumeration of random models.
Definition gutilities.h:265
@ gaussian
Gaussian distribution.
Definition gutilities.h:267
@ uniform
Uniform distribution.
Definition gutilities.h:266
@ sphere
Sphere distribution.
Definition gutilities.h:269
@ cosine
Cosine distribution.
Definition gutilities.h:268
string getDirFromPath(const std::string &path)
Extracts the directory path from a given file path.
Definition gutilities.cc:47
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.
G4Colour makeColour(std::string_view code)
Convert a hex colour string to G4Colour.
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
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:38
constexpr const char * to_string(randomModel m) noexcept
Definition gutilities.h:284
vector< std::string > getStringVectorFromString(const std::string &input)
Splits a string into a vector of strings using spaces as delimiters.
Definition gutilities.cc:56