00001 #include "BDSUtilities.hh"
00002 #include <algorithm>
00003 #include <cctype>
00004 #include <functional>
00005 #include <iostream>
00006 #include <limits>
00007
00008 #include <signal.h>
00009 #include <unistd.h>
00010 #ifdef __APPLE__
00011 #include <mach-o/dyld.h>
00012 #endif
00013
00014 #include "BDSRunManager.hh"
00015 #include "parser/getEnv.h"
00016
00017 G4bool BDS::non_alpha::operator()(char c)
00018 {
00019 return !isalpha(c);
00020 }
00021
00022 G4String BDS::PrepareSafeName(G4String name)
00023 {
00024
00025 name.erase(std::remove_if(name.begin(),name.end(),isspace),name.end());
00026
00027 std::replace_if(name.begin(),name.end(),BDS::non_alpha(),'_');
00028
00029 return name;
00030 }
00031
00032 G4int BDS::CalculateOrientation(G4double angle)
00033 {
00034 G4int orientation;
00035 if (angle < 0)
00036 {orientation = 1;}
00037 else
00038 {orientation = -1;}
00039 return orientation;
00040 }
00041
00042 std::string BDS::GetBDSIMExecPath()
00043 {
00044
00045 char path[1024];
00046 #ifdef __linux__
00047
00048 ssize_t len = ::readlink("/proc/self/exe", path, sizeof(path) - 1);
00049 if (len != -1) {
00050 path[len] = '\0';
00051 }
00052 #elif __APPLE__
00053 uint32_t size = sizeof(path);
00054 if (_NSGetExecutablePath(path, &size) != 0)
00055 std::cout << "buffer too small; need size " << size << std::endl;
00056 #endif
00057 std::string bdsimPath(path);
00058
00059 std::string::size_type found = bdsimPath.rfind("/");
00060 if (found != std::string::npos){
00061 bdsimPath = bdsimPath.substr(0,found+1);
00062 }
00063 return bdsimPath;
00064 }
00065
00066 G4String BDS::GetFullPath(G4String fileName, bool excludePathFromFileName)
00067 {
00068
00069 G4String fullPath = getEnv("BDSIMPATH");
00070 if(fullPath.length()<=0){
00071 G4String inputFilepath = "";
00072 if (!excludePathFromFileName) {
00073
00074 G4String::size_type found = fileName.rfind("/");
00075 if (found != G4String::npos){
00076 inputFilepath = fileName.substr(0,found);
00077 }
00078 }
00079
00080 if ((fileName.substr(0,1)) == "/"){
00081
00082 fullPath = inputFilepath;
00083 } else {
00084
00085 char cwdchars[200];
00086
00087 G4String cwd = (G4String)getcwd(cwdchars, sizeof(cwdchars)) + "/";
00088 fullPath = cwd + inputFilepath;
00089 }
00090 }
00091
00092 fullPath += "/";
00093 return fullPath;
00094 }
00095
00096 void BDS::HandleAborts(int signal_number)
00097 {
00102
00103 static int nrOfCalls=0;
00104 if (nrOfCalls>0) exit(1);
00105 nrOfCalls++;
00106 std::cout << "BDSIM is about to crash or was interrupted! " << std::endl;
00107 std::cout << "With signal: " << strsignal(signal_number) << std::endl;
00108 std::cout << "Trying to write and close output file" << std::endl;
00109 std::cout << "Terminate run" << std::endl;
00110 BDSRunManager::GetRunManager()->RunTermination();
00111 std::cout << "Ave, Imperator, morituri te salutant!" << std::endl;
00112 }
00113
00114 G4bool BDS::IsFinite(const G4double& variable)
00115 {
00116 if (std::abs(variable) > std::numeric_limits<double>::epsilon())
00117 {return true;}
00118 else
00119 {return false;}
00120 }