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
13namespace gutilities {
14
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
277
288randomModel stringToRandomModel(const std::string& str);
289
290inline constexpr const char* to_string(randomModel m) noexcept {
291 switch (m) {
292 case uniform: return "uniform";
293 case gaussian: return "gaussian";
294 case cosine: return "cosine";
295 case sphere: return "sphere";
296 }
297 return "<unknown>";
298}
299
309G4Colour makeColour(std::string_view code);
310
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) { // buffer too small
334 std::string big(sz, '\0');
335 if (_NSGetExecutablePath(big.data(), &sz) != 0)
336 throw std::runtime_error("_NSGetExecutablePath failed");
337 return std::filesystem::canonical(big);
338 }
339 return std::filesystem::canonical(buf);
340
341#elif defined(__linux__)
342 char buf[PATH_MAX];
343 ssize_t len = ::readlink("/proc/self/exe", buf, sizeof(buf) - 1);
344 if (len == -1)
345 throw std::runtime_error("readlink(/proc/self/exe) failed");
346 buf[len] = '\0';
347 return std::filesystem::canonical(buf);
348
349#elif defined(_WIN32)
350 std::wstring buf(MAX_PATH, L'\0');
351 DWORD len = ::GetModuleFileNameW(nullptr, buf.data(), buf.size());
352 if (len == 0)
353 throw std::runtime_error("GetModuleFileNameW failed");
354 // If the path is longer than MAX_PATH the buffer is truncated;
355 // do a second call with the returned length to get the full path.
356 if (len == buf.size()) {
357 buf.resize(len * 2);
358 len = ::GetModuleFileNameW(nullptr, buf.data(), buf.size());
359 }
360 buf.resize(len);
361 return std::filesystem::canonical(std::filesystem::path(buf));
362#endif
363}
364
365inline std::filesystem::path gemc_root() {
366 auto exe_dir = executable_path().parent_path(); // where the executable is installed
367 auto gemc_root = exe_dir.parent_path(); // one dir up. The danger here is where this is a
368
369 // Sanity‑check api dir
370 if (!std::filesystem::exists(gemc_root / "api"))
371 throw std::runtime_error("Cannot locate directory >api< check directory layout");
372
373 return gemc_root;
374}
375
376bool is_unset(std::string_view s);
377
378inline std::string success_or_fail(bool condition) { return condition ? "success" : "fail"; }
379
380}
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.
randomModel stringToRandomModel(const std::string &str)
Converts a string to a corresponding randomModel enum value.
std::filesystem::path gemc_root()
Definition gutilities.h:365
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:378
bool is_unset(std::string_view s)
randomModel
Enumeration of random models.
Definition gutilities.h:271
@ gaussian
Gaussian distribution.
Definition gutilities.h:273
@ uniform
Uniform distribution.
Definition gutilities.h:272
@ sphere
Sphere distribution.
Definition gutilities.h:275
@ cosine
Cosine distribution.
Definition gutilities.h:274
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.
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
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:290
vector< std::string > getStringVectorFromString(const std::string &input)
Splits a string into a vector of strings using spaces as delimiters.
Definition gutilities.cc:63