Files
geant4/source/externals/g4tools/include/tools/app
T
2021-06-25 16:12:29 +02:00

192 lines
5.0 KiB
Plaintext

// Copyright (C) 2010, Guy Barrand. All rights reserved.
// See the file tools.license for terms.
#ifndef tools_app
#define tools_app
#include "path_env"
#include "sout"
#include "file"
#include "forit"
#include "sys/dir"
namespace tools {
#ifdef __APPLE__
inline bool is_mac_app(const std::string& a_arg0){
if(a_arg0.empty()) return false;
std::string bname = base_name(dir_name(a_arg0));
if(bname=="MacOS") return true;
return false;
}
#else
inline bool is_mac_app(const std::string&){return false;}
#endif
inline bool program_path(const std::string& a_arg0,std::string& a_path) {
if(a_arg0.empty()) {
a_path = "";
return false;
}
std::string path,name,suffix;
path_name_suffix(a_arg0,path,name,suffix);
if(path.empty()) {
std::string PATH;
if(get_env("PATH",PATH)) {
std::string program = a_arg0;
#ifdef _WIN32
if(program.find(".exe")==std::string::npos) program += ".exe";
#endif
std::vector<std::string> paths;
words(PATH,psep(),false,paths);
tools_vforit(std::string,paths,it) {
std::string dir = *it;
if(file::exists(dir+sep()+program)) {
path = dir;
break;
}
}
}
}
if(path.empty()) {
a_path = "";
return false;
}
if(!is_absolute_path(path)) {
std::string pwd;
if(!dir::pwd(pwd)) {
a_path = "";
return false;
}
path = pwd+sep()+path;
}
//printf("debug : path : %s\n",path.c_str());
a_path = path;
return true;
}
inline bool if_mouse_startup(const std::string& a_arg0,const std::string& a_env_HOME_DIR,const std::vector<std::string>& a_home_dirs,std::ostream& a_out){
std::string exe_dir = dir_name(a_arg0);
std::string bname = base_name(exe_dir);
if(bname=="MacOS") {
// cd in a_doc_dir :
if(!dir::cd_home()) {
a_out << "if_mouse_startup : can't go home." << std::endl;
return false;
}
tools_vforcit(std::string,a_home_dirs,it) {
if(!dir::mkcd(*it)) {
a_out << "if_mouse_startup :"
<< " can't mkcd " << sout(*it)
<< std::endl;
return false;
}
}
// set a_env_HOME_DIR from exe_dir :
std::string Contents_dir = dir_name(exe_dir);
if(!put_env(a_env_HOME_DIR,Contents_dir)) {
a_out << "if_mouse_startup :"
<< " can't putenv " << a_env_HOME_DIR
<< " to " << sout(Contents_dir)
<< "."
<< std::endl;
return false;
}
{std::vector<std::string> vals;
vals.push_back(Contents_dir+"/bin");
vals.push_back("."); // for on-the-fly compilation and loading :
if(!env_path_append("DYLD_LIBRARY_PATH",vals)) {
a_out << "if_mouse_startup :"
<< " can't putenv DYLD_LIBRARY_PATH."
//<< " to " << sout(DYLD_LIBRARY_PATH)
<< std::endl;
return false;
}}
// {std::string DYLD_LIBRARY_PATH;
// if(!get_env("DYLD_LIBRARY_PATH",DYLD_LIBRARY_PATH)) {}
// a_out << "debug : DYLD_LIBRARY_PATH = " << sout(DYLD_LIBRARY_PATH) << std::endl;}
if(!is_env("DISPLAY")) {
if(!put_env("DISPLAY",":0.0")) return false;
}
} else if(bname=="bin") {
//Linux, Windows : mouse startup ?
/* FIXME : do we want to treat the case of a terminal startup
whence the user did not <source setup> ???
If so if_mouse_startup should be renamed no_env_startup.
} else if(exe_dir=="") { //for exa when typing "osc-plot".
if(!program_path(a_arg0,exe_dir)) {
a_out << "if_mouse_startup :"
<< " program_path failed for " << sout(bname) << "."
<< std::endl;
return false;
}
a_out << "if_mouse_startup :"
<< " program_path is " << sout(exe_dir) << "."
<< std::endl;
#if defined(__APPLE__) || defined(Linux)
bool is_abs,is_win;
std::string drive;
std::vector<std::string> words;
path_words(exe_dir,words,is_abs,is_win,drive);
std::vector<std::string>::size_type n = words.size();
if( (n>=3) &&
(words[n-1]=="bin") &&
is_version(words[n-2]) &&
(words[n-3].substr(0,4)=="osc_")
){
std::string osc_vers_dir = dir_name(exe_dir);
printf("debug : auto env 000 \"%s\"\n",osc_vers_dir.c_str());
//if(a_env_HOME_DIR.size()) {
if(!put_env(a_env_HOME_DIR,osc_vers_dir)) {
a_out << "arg0_setenv :"
<< " can't putenv " << a_env_HOME_DIR
<< " to " << sout(osc_vers_dir)
<< "."
<< std::endl;
return false;
}
//}
#ifdef __APPLE__
{std::vector<std::string> vals;
vals.push_back(osc_vers_dir+"/bin");
vals.push_back(osc_vers_dir+"/lib");
vals.push_back("."); // for on-the-fly compilation and loading :
if(!env_path_append("DYLD_LIBRARY_PATH",vals)) return false;}
#else
#endif
if(!is_env("DISPLAY")) {
if(!put_env("DISPLAY",":0.0")) return false;
}
}
#endif
*/
}
return true;
}
inline void app_res_dir(const std::string& a_exe_path,std::string& a_res_dir) {
a_res_dir = a_exe_path+sep()+".."+sep()+"res";
}
}
#endif