Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-23 09:22:02

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
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 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
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 // return element ii
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 // return first four strings
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 // Memory safe multi-platform getcwd
0103 // http://stackoverflow.com/questions/2869594/
0104 G4String Getcwd()
0105 {
0106   const size_t chunkSize = 255;
0107   const int maxChunks = 10240;  // 2550 KiBs of current path is plenty
0108 
0109   char stackBuffer[chunkSize];  // Stack buffer for the "normal" case
0110   if (::getcwd(stackBuffer, sizeof(stackBuffer)) != nullptr) return stackBuffer;
0111   if (errno != ERANGE) {
0112     // It's not ERANGE, so we don't know how to handle it
0113     throw std::runtime_error("Cannot determine the current path.");
0114     // Of course you may choose a different error reporting method
0115   }
0116   // Ok, the stack buffer isn't long enough; fallback to heap allocation
0117   for (int chunks = 2; chunks < maxChunks; chunks++) {
0118     // With boost use scoped_ptr; in C++0x, use unique_ptr
0119     // If you want to be less C++ but more efficient
0120     // you may want to use realloc
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       // It's not ERANGE, so we don't know how to handle it
0125       throw std::runtime_error("Cannot determine the current path.");
0126       // Of course you may choose a different error reporting method
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 }  // namespace utility
0139 
0140 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......