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
38std::string removeAllSpacesFromString(const std::string& str);
39
48std::string getFileFromPath(const std::string& path);
49
58std::string getDirFromPath(const std::string& path);
59
60std::optional<std::string> searchForFileInLocations(
61 const std::vector<std::string>& locations,
62 std::string_view filename);
63
73std::vector<std::string> getStringVectorFromString(const std::string& input);
74
86std::string replaceCharInStringWithChars(const std::string& input, const std::string& toReplace,
87 const std::string& replacement);
88
89
101string replaceAllStringsWithString(const string& source, const string& from, const string& to);
102
114string fillDigits(const string& word, const string& c, int ndigits);
115
126double getG4Number(const string& v, bool warnIfNotUnit = false);
127
137double getG4Number(double input, const string& unit);
138
148vector<double> getG4NumbersFromStringVector(const vector<string>& vstring, bool warnIfNotUnit = false);
149
159vector<double> getG4NumbersFromString(const string& vstring, bool warnIfNotUnit = false);
160
171string parseFileAndRemoveComments(const string& filename, const string& commentChars = "#", int verbosity = 0);
172
173
184string retrieveStringBetweenChars(const string& input, const string& firstDelimiter, const string& secondDelimiter);
185
186
196vector<string> getStringVectorFromStringWithDelimiter(const string& input, const string& x);
197
206bool directoryExists(const std::string& path);
207
208
218string searchForDirInLocations(const string& dirName, const vector<string>& possibleLocations);
219
220
230bool hasExtension(const string& filename, const vector<string>& extensions);
231
241vector<string> getListOfFilesInDirectory(const string& dirName, const vector<string>& extensions);
242
251string convertToLowercase(const string& str);
252
263template <class KEY, class VALUE>
264vector<KEY> getKeys(const map<KEY, VALUE>& map);
265
275
286randomModel stringToRandomModel(const std::string& str);
287
288inline constexpr const char* to_string(randomModel m) noexcept {
289 switch (m) {
290 case uniform: return "uniform";
291 case gaussian: return "gaussian";
292 case cosine: return "cosine";
293 case sphere: return "sphere";
294 }
295 return "<unknown>";
296}
297
307G4Colour makeColour(std::string_view code);
308
309};
310
311
312#include <filesystem>
313
314#if defined(__APPLE__)
315#include <mach-o/dyld.h> // _NSGetExecutablePath
316#elif defined(__linux__)
317#include <unistd.h> // readlink
318#include <limits.h>
319#elif defined(_WIN32)
320#include <windows.h>
321#include <vector>
322#else
323#error "Unsupported platform"
324#endif
325
326namespace gutilities {
327inline std::filesystem::path executable_path() {
328#if defined(__APPLE__)
329 char buf[PATH_MAX];
330 uint32_t sz = sizeof(buf);
331 if (_NSGetExecutablePath(buf, &sz) != 0) { // buffer too small
332 std::string big(sz, '\0');
333 if (_NSGetExecutablePath(big.data(), &sz) != 0)
334 throw std::runtime_error("_NSGetExecutablePath failed");
335 return std::filesystem::canonical(big);
336 }
337 return std::filesystem::canonical(buf);
338
339#elif defined(__linux__)
340 char buf[PATH_MAX];
341 ssize_t len = ::readlink("/proc/self/exe", buf, sizeof(buf) - 1);
342 if (len == -1)
343 throw std::runtime_error("readlink(/proc/self/exe) failed");
344 buf[len] = '\0';
345 return std::filesystem::canonical(buf);
346
347#elif defined(_WIN32)
348 std::wstring buf(MAX_PATH, L'\0');
349 DWORD len = ::GetModuleFileNameW(nullptr, buf.data(), buf.size());
350 if (len == 0)
351 throw std::runtime_error("GetModuleFileNameW failed");
352 // If the path is longer than MAX_PATH the buffer is truncated;
353 // do a second call with the returned length to get the full path.
354 if (len == buf.size()) {
355 buf.resize(len * 2);
356 len = ::GetModuleFileNameW(nullptr, buf.data(), buf.size());
357 }
358 buf.resize(len);
359 return std::filesystem::canonical(std::filesystem::path(buf));
360#endif
361}
362
363inline std::filesystem::path gemc_root() {
364 auto exe_dir = executable_path().parent_path(); // where the executable is installed
365 auto gemc_root = exe_dir.parent_path(); // one dir up. The danger here is where this is a
366
367 // Sanity‑check api dir
368 if (!std::filesystem::exists(gemc_root / "api"))
369 throw std::runtime_error("Cannot locate directory >api< check directory layout");
370
371 return gemc_root;
372}
373
374inline std::string success_or_fail(bool condition) { return condition ? "success" : "fail"; }
375
376}
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:327
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:363
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:374
randomModel
Enumeration of random models.
Definition gutilities.h:269
@ gaussian
Gaussian distribution.
Definition gutilities.h:271
@ uniform
Uniform distribution.
Definition gutilities.h:270
@ sphere
Sphere distribution.
Definition gutilities.h:273
@ cosine
Cosine distribution.
Definition gutilities.h:272
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
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:38
constexpr const char * to_string(randomModel m) noexcept
Definition gutilities.h:288
vector< std::string > getStringVectorFromString(const std::string &input)
Splits a string into a vector of strings using spaces as delimiters.
Definition gutilities.cc:56