guts
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 
12 namespace gutilities {
13 
14 using std::string;
15 using std::vector;
16 using std::map;
17 
27 std::string removeLeadingAndTrailingSpacesFromString(const std::string& input);
28 
37 std::string removeAllSpacesFromString(const std::string& str);
38 
47 std::string getFileFromPath(const std::string& path);
48 
57 std::string getDirFromPath(const std::string& path);
58 
59 
69 std::vector<std::string> getStringVectorFromString(const std::string& input);
70 
82 std::string replaceCharInStringWithChars(const std::string& input, const std::string& toReplace,
83  const std::string& replacement);
84 
85 
97 string replaceAllStringsWithString(const string& source, const string& from, const string& to);
98 
110 string fillDigits(const string& word, const string& c, int ndigits);
111 
122 double getG4Number(const string& v, bool warnIfNotUnit = false);
123 
133 double getG4Number(double input, const string& unit);
134 
144 vector<double> getG4NumbersFromStringVector(const vector<string>& vstring, bool warnIfNotUnit = false);
145 
155 vector<double> getG4NumbersFromString(const string& vstring, bool warnIfNotUnit = false);
156 
167 string parseFileAndRemoveComments(const string& filename, const string& commentChars = "#", int verbosity = 0);
168 
169 
180 string retrieveStringBetweenChars(const string& input, const string& firstDelimiter, const string& secondDelimiter);
181 
182 
192 vector<string> getStringVectorFromStringWithDelimiter(const string& input, const string& x);
193 
202 bool directoryExists(const std::string& path);
203 
204 
214 string searchForDirInLocations(const string& dirName, const vector<string>& possibleLocations);
215 
216 
226 bool hasExtension(const string& filename, const vector<string>& extensions);
227 
237 vector<string> getListOfFilesInDirectory(const string& dirName, const vector<string>& extensions);
238 
247 string convertToLowercase(const string& str);
248 
259 template <class KEY, class VALUE>
260 vector<KEY> getKeys(const map<KEY, VALUE>& map);
261 
269  sphere
270 };
271 
282 randomModel stringToRandomModel(const std::string& str);
283 
284 inline 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 
303 G4Colour 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 
322 namespace gutilities {
323 
324 
325 inline std::filesystem::path executable_path() {
326 #if defined(__APPLE__)
327  char buf[PATH_MAX];
328  uint32_t sz = sizeof(buf);
329  if (_NSGetExecutablePath(buf, &sz) != 0) { // buffer too small
330  std::string big(sz, '\0');
331  if (_NSGetExecutablePath(big.data(), &sz) != 0)
332  throw std::runtime_error("_NSGetExecutablePath failed");
333  return std::filesystem::canonical(big);
334  }
335  return std::filesystem::canonical(buf);
336 
337 #elif defined(__linux__)
338  char buf[PATH_MAX];
339  ssize_t len = ::readlink("/proc/self/exe", buf, sizeof(buf)-1);
340  if (len == -1)
341  throw std::runtime_error("readlink(/proc/self/exe) failed");
342  buf[len] = '\0';
343  return std::filesystem::canonical(buf);
344 
345 #elif defined(_WIN32)
346  std::wstring buf(MAX_PATH, L'\0');
347  DWORD len = ::GetModuleFileNameW(nullptr, buf.data(), buf.size());
348  if (len == 0)
349  throw std::runtime_error("GetModuleFileNameW failed");
350  // If the path is longer than MAX_PATH the buffer is truncated;
351  // do a second call with the returned length to get the full path.
352  if (len == buf.size()) {
353  buf.resize(len * 2);
354  len = ::GetModuleFileNameW(nullptr, buf.data(), buf.size());
355  }
356  buf.resize(len);
357  return std::filesystem::canonical(std::filesystem::path(buf));
358 #endif
359 }
360 
361 inline std::filesystem::path gemc_root() {
362 
363  auto exe_dir = executable_path().parent_path(); // where the executable is installed
364  auto gemc_root = exe_dir.parent_path(); // one dir up
365 
366  // Sanity‑check api dir
367  if (!std::filesystem::exists(gemc_root / "api"))
368  throw std::runtime_error("Cannot locate directory >api< check directory layout");
369 
370  return gemc_root;
371 }
372 
373 
374 }
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.
Definition: gutilities.cc:115
bool hasExtension(const std::string &filename, const std::vector< std::string > &extensions)
Checks if a filename has one of the specified extensions.
Definition: gutilities.cc:307
vector< double > getG4NumbersFromString(const string &vstring, bool warnIfNotUnit)
Converts a comma-separated string of numbers with units to a vector of doubles.
Definition: gutilities.cc:195
string removeAllSpacesFromString(const std::string &str)
Removes all spaces from a string.
Definition: gutilities.cc:32
std::filesystem::path executable_path()
Definition: gutilities.h:325
vector< string > getStringVectorFromStringWithDelimiter(const string &input, const string &x)
Splits a string into a vector of substrings using a specified delimiter.
Definition: gutilities.cc:234
randomModel stringToRandomModel(const std::string &str)
Converts a string to a corresponding randomModel enum value.
Definition: gutilities.cc:352
std::filesystem::path gemc_root()
Definition: gutilities.h:361
constexpr const char * to_string(randomModel m) noexcept
Definition: gutilities.h:284
vector< string > getListOfFilesInDirectory(const string &dirName, const vector< string > &extensions)
Retrieves a list of files with specific extensions from a directory.
Definition: gutilities.cc:315
vector< double > getG4NumbersFromStringVector(const vector< string > &vstring, bool warnIfNotUnit)
Converts a vector of strings representing numbers with units to a vector of doubles.
Definition: gutilities.cc:186
string retrieveStringBetweenChars(const string &input, const string &firstDelimiter, const string &secondDelimiter)
Retrieves a substring between two specified delimiters in a string.
Definition: gutilities.cc:226
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.
Definition: gutilities.cc:100
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.
Definition: gutilities.cc:335
bool directoryExists(const std::string &path)
Checks if a directory exists at the given path.
Definition: gutilities.cc:290
G4Colour makeColour(std::string_view code)
Convert a hex colour string to G4Colour.
Definition: gutilities.cc:366
vector< KEY > getKeys(const map< KEY, VALUE > &map)
Retrieves all keys from a map.
Definition: gutilities.cc:343
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.
Definition: gutilities.cc:298
string parseFileAndRemoveComments(const string &filename, const string &commentChars, int verbosity)
Parses a file and removes all lines containing specified comment characters.
Definition: gutilities.cc:200
string getFileFromPath(const std::string &path)
Extracts the filename from a given file path.
Definition: gutilities.cc:38
vector< std::string > getStringVectorFromString(const std::string &input)
Splits a string into a vector of strings using spaces as delimiters.
Definition: gutilities.cc:56