g4tools  5.4.0
app
Go to the documentation of this file.
1 // Copyright (C) 2010, Guy Barrand. All rights reserved.
2 // See the file tools.license for terms.
3 
4 #ifndef tools_app
5 #define tools_app
6 
7 #include "path_env"
8 #include "sout"
9 #include "file"
10 #include "forit"
11 #include "sys/dir"
12 
13 namespace tools {
14 
15 #ifdef __APPLE__
16 inline bool is_mac_app(const std::string& a_arg0){
17  if(a_arg0.empty()) return false;
18  std::string bname = base_name(dir_name(a_arg0));
19  if(bname=="MacOS") return true;
20  return false;
21 }
22 #else
23 inline bool is_mac_app(const std::string&){return false;}
24 #endif
25 
26 inline bool program_path(const std::string& a_arg0,std::string& a_path) {
27  if(a_arg0.empty()) {
28  a_path = "";
29  return false;
30  }
31  std::string path,name,suffix;
32  path_name_suffix(a_arg0,path,name,suffix);
33  if(path.empty()) {
34  std::string PATH;
35  if(get_env("PATH",PATH)) {
36  std::string program = a_arg0;
37 #ifdef _WIN32
38  if(program.find(".exe")==std::string::npos) program += ".exe";
39 #endif
40  std::vector<std::string> paths;
41  words(PATH,psep(),false,paths);
42  tools_vforit(std::string,paths,it) {
43  std::string dir = *it;
44  if(file::exists(dir+sep()+program)) {
45  path = dir;
46  break;
47  }
48  }
49  }
50  }
51  if(path.empty()) {
52  a_path = "";
53  return false;
54  }
55  if(!is_absolute_path(path)) {
56  std::string pwd;
57  if(!dir::pwd(pwd)) {
58  a_path = "";
59  return false;
60  }
61  path = pwd+sep()+path;
62  }
63  //printf("debug : path : %s\n",path.c_str());
64  a_path = path;
65  return true;
66 }
67 
68 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){
69  std::string exe_dir = dir_name(a_arg0);
70  std::string bname = base_name(exe_dir);
71  if(bname=="MacOS") {
72  // cd in a_doc_dir :
73  if(!dir::cd_home()) {
74  a_out << "if_mouse_startup : can't go home." << std::endl;
75  return false;
76  }
77  tools_vforcit(std::string,a_home_dirs,it) {
78  if(!dir::mkcd(*it)) {
79  a_out << "if_mouse_startup :"
80  << " can't mkcd " << sout(*it)
81  << std::endl;
82  return false;
83  }
84  }
85 
86  // set a_env_HOME_DIR from exe_dir :
87  std::string Contents_dir = dir_name(exe_dir);
88  if(!put_env(a_env_HOME_DIR,Contents_dir)) {
89  a_out << "if_mouse_startup :"
90  << " can't putenv " << a_env_HOME_DIR
91  << " to " << sout(Contents_dir)
92  << "."
93  << std::endl;
94  return false;
95  }
96  {std::vector<std::string> vals;
97  vals.push_back(Contents_dir+"/bin");
98  vals.push_back("."); // for on-the-fly compilation and loading :
99  if(!env_path_append("DYLD_LIBRARY_PATH",vals)) {
100  a_out << "if_mouse_startup :"
101  << " can't putenv DYLD_LIBRARY_PATH."
102  //<< " to " << sout(DYLD_LIBRARY_PATH)
103  << std::endl;
104  return false;
105  }}
106 
107 // {std::string DYLD_LIBRARY_PATH;
108 // if(!get_env("DYLD_LIBRARY_PATH",DYLD_LIBRARY_PATH)) {}
109 // a_out << "debug : DYLD_LIBRARY_PATH = " << sout(DYLD_LIBRARY_PATH) << std::endl;}
110 
111  if(!is_env("DISPLAY")) {
112  if(!put_env("DISPLAY",":0.0")) return false;
113  }
114 
115  } else if(bname=="bin") {
116  //Linux, Windows : mouse startup ?
117 
118 /* FIXME : do we want to treat the case of a terminal startup
119  whence the user did not <source setup> ???
120  If so if_mouse_startup should be renamed no_env_startup.
121 
122  } else if(exe_dir=="") { //for exa when typing "osc-plot".
123  if(!program_path(a_arg0,exe_dir)) {
124  a_out << "if_mouse_startup :"
125  << " program_path failed for " << sout(bname) << "."
126  << std::endl;
127  return false;
128  }
129  a_out << "if_mouse_startup :"
130  << " program_path is " << sout(exe_dir) << "."
131  << std::endl;
132 
133 #if defined(__APPLE__) || defined(Linux)
134  bool is_abs,is_win;
135  std::string drive;
136  std::vector<std::string> words;
137  path_words(exe_dir,words,is_abs,is_win,drive);
138  std::vector<std::string>::size_type n = words.size();
139 
140  if( (n>=3) &&
141  (words[n-1]=="bin") &&
142  is_version(words[n-2]) &&
143  (words[n-3].substr(0,4)=="osc_")
144  ){
145 
146  std::string osc_vers_dir = dir_name(exe_dir);
147 
148  printf("debug : auto env 000 \"%s\"\n",osc_vers_dir.c_str());
149 
150  //if(a_env_HOME_DIR.size()) {
151  if(!put_env(a_env_HOME_DIR,osc_vers_dir)) {
152  a_out << "arg0_setenv :"
153  << " can't putenv " << a_env_HOME_DIR
154  << " to " << sout(osc_vers_dir)
155  << "."
156  << std::endl;
157  return false;
158  }
159  //}
160 
161 #ifdef __APPLE__
162 
163  {std::vector<std::string> vals;
164  vals.push_back(osc_vers_dir+"/bin");
165  vals.push_back(osc_vers_dir+"/lib");
166  vals.push_back("."); // for on-the-fly compilation and loading :
167  if(!env_path_append("DYLD_LIBRARY_PATH",vals)) return false;}
168 
169 #else
170 #endif
171 
172  if(!is_env("DISPLAY")) {
173  if(!put_env("DISPLAY",":0.0")) return false;
174  }
175 
176  }
177 #endif
178 */
179 
180  }
181 
182  return true;
183 }
184 
185 inline void app_res_dir(const std::string& a_exe_path,std::string& a_res_dir) {
186  a_res_dir = a_exe_path+sep()+".."+sep()+"res";
187 }
188 
189 }
190 
191 #endif
tools::get_env
bool get_env(const std::string &a_string, std::string &a_value)
Definition: get_env:18
dir
tools::file::exists
bool exists(const std::string &a_string)
Definition: file:23
tools::dir::mkcd
bool mkcd(const std::string &a_name)
Definition: dir:131
tools::psep
const std::string & psep()
Definition: sep:20
tools::if_mouse_startup
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)
Definition: app:68
tools::env_path_append
bool env_path_append(const std::string &a_env, const std::vector< std::string > &a_paths)
Definition: path_env:44
tools::app_res_dir
void app_res_dir(const std::string &a_exe_path, std::string &a_res_dir)
Definition: app:185
tools::is_mac_app
bool is_mac_app(const std::string &)
Definition: app:23
tools::dir_name
std::string dir_name(const std::string &a_path, unsigned int a_num=1)
Definition: path:208
tools::suffix
void suffix(const std::string &a_string, std::string &a_value, bool a_back=true)
Definition: path:11
tools::put_env
bool put_env(const std::string &a_env, const std::string &a_value)
Definition: put_env:15
tools::path_name_suffix
bool path_name_suffix(const std::string &a_string, std::string &a_path, std::string &a_name, std::string &a_suffix)
Definition: path:115
tools::sout
Definition: sout:17
tools::is_env
bool is_env(const std::string &a_string)
Definition: get_env:13
tools_vforit
#define tools_vforit(a__T, a__v, a__it)
Definition: forit:13
tools::dir::pwd
bool pwd(std::string &a_pwd)
Definition: dir:25
tools
inlined C code : ///////////////////////////////////
Definition: aida_ntuple:26
tools::words
void words(const std::string &a_string, const std::string &a_sep, bool a_take_empty, std::vector< std::string > &a_words, bool a_clear=true)
Definition: words:12
file
tools::base_name
void base_name(const std::string &a_path, std::string &a_value)
Definition: path:54
forit
tools::dir::cd_home
bool cd_home()
Definition: dir:74
tools::sep
const std::string & sep()
Definition: sep:11
tools_vforcit
#define tools_vforcit(a__T, a__v, a__it)
Definition: forit:7
path_env
tools::is_absolute_path
bool is_absolute_path(const std::string &a_path)
Definition: path:106
tools::program_path
bool program_path(const std::string &a_arg0, std::string &a_path)
Definition: app:26
sout