File indexing completed on 2025-02-23 09:22:02
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026 #include "UtilityFunctions.hh"
0027
0028 #include <cstdio>
0029 #include <memory>
0030
0031 #ifdef WIN32
0032 # include <direct.h>
0033 #else
0034 # include <unistd.h>
0035 #endif
0036
0037
0038
0039 namespace utility
0040 {
0041 std::vector<G4String>& Split(const G4String& str, char delim, std::vector<G4String>& elems)
0042 {
0043 std::stringstream ss(str);
0044 std::string item;
0045 while (std::getline(ss, item, delim)) {
0046 if (!item.empty()) elems.push_back((G4String)item);
0047 }
0048 return elems;
0049 }
0050
0051 std::vector<G4String> Split(const G4String& str, char delim)
0052 {
0053 std::vector<G4String> elems;
0054 Split(str, delim, elems);
0055 return elems;
0056 }
0057
0058
0059 G4String Get_seperated_element(const G4String& str, char delim, G4int ii)
0060 {
0061 G4int delims = 0;
0062 G4String ss;
0063 for (char c : str) {
0064 if (delims > ii) {
0065 return ss;
0066 }
0067 if (c == delim) {
0068 delims++;
0069 }
0070 else if (delims == ii) {
0071 ss += c;
0072 }
0073 }
0074 return ss;
0075 }
0076
0077
0078 std::array<G4String, 4> Get_four_elements(const G4String& str, char delim)
0079 {
0080 G4int delims = 0;
0081 std::array<G4String, 4> arr;
0082 for (char c : str) {
0083 if (delims >= 4) return arr;
0084 if (c == delim)
0085 delims++;
0086 else
0087 arr.at(delims) += c;
0088 }
0089 return arr;
0090 }
0091
0092 G4bool Path_exists(const G4String& fname)
0093 {
0094 G4bool bool_val = false;
0095 if (FILE* file = std::fopen(fname, "r")) {
0096 fclose(file);
0097 bool_val = true;
0098 }
0099 return bool_val;
0100 }
0101
0102
0103
0104 G4String Getcwd()
0105 {
0106 const size_t chunkSize = 255;
0107 const int maxChunks = 10240;
0108
0109 char stackBuffer[chunkSize];
0110 if (::getcwd(stackBuffer, sizeof(stackBuffer)) != nullptr) return stackBuffer;
0111 if (errno != ERANGE) {
0112
0113 throw std::runtime_error("Cannot determine the current path.");
0114
0115 }
0116
0117 for (int chunks = 2; chunks < maxChunks; chunks++) {
0118
0119
0120
0121 std::unique_ptr<char[]> cwd(new char[chunkSize * chunks]);
0122 if (::getcwd(cwd.get(), chunkSize * chunks) != nullptr) return cwd.get();
0123 if (errno != ERANGE) {
0124
0125 throw std::runtime_error("Cannot determine the current path.");
0126
0127 }
0128 }
0129 throw std::runtime_error(
0130 "Cannot determine the current path; the path is "
0131 "apparently unreasonably long");
0132 }
0133
0134 G4double Min(const G4ThreeVector& v)
0135 {
0136 return std::min(std::min(v.x(), v.y()), v.z());
0137 }
0138 }
0139
0140