inlib  1.2.0
/Users/barrand/private/dev/softinex/old/inexlib-1.2/inlib/inlib/app
Go to the documentation of this file.
00001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
00002 // See the file inlib.license for terms.
00003 
00004 #ifndef inlib_app
00005 #define inlib_app
00006 
00007 #include "smanip"
00008 #include "system"
00009 #include "file"
00010 #include "sys/dir"
00011 
00012 namespace inlib {
00013 
00014 #ifdef __APPLE__
00015 inline bool is_mac_app(const std::string& a_arg0){
00016   if(a_arg0.empty()) return false;
00017   std::string bname = inlib::base_name(inlib::dir_name(a_arg0));
00018   if(bname=="MacOS") return true;
00019   return false;
00020 }
00021 #else
00022 inline bool is_mac_app(const std::string&){return false;}
00023 #endif
00024 
00025 inline bool program_path(const std::string& a_arg0,std::string& a_path) {
00026   if(a_arg0.empty()) {
00027     a_path = "";
00028     return false;
00029   }
00030   std::string path,name,suffix;
00031   path_name_suffix(a_arg0,path,name,suffix);
00032   if(path.empty()) {
00033     std::string PATH;
00034     if(inlib::getenv("PATH",PATH)) {
00035       std::string program = a_arg0;
00036 #if defined(WIN32) || (defined(__CYGWIN__) && defined(__GNUC__))
00037       if(program.find(".exe")==std::string::npos) program += ".exe";
00038 #endif
00039       std::vector<std::string> paths = words(PATH,psep());
00040       std::vector<std::string>::iterator it;
00041       for(it=paths.begin();it!=paths.end();++it) {
00042         std::string dir = *it;
00043         if(file::exists(dir+sep()+program)) {
00044           path = dir;
00045           break;
00046         }
00047       }
00048     }
00049   }
00050   if(path.empty()) {
00051     a_path = "";
00052     return false;
00053   }
00054   if(!is_absolute_path(path)) {
00055     std::string pwd;
00056     if(!dir::pwd(pwd)) {
00057       a_path = "";
00058       return false;
00059     }
00060     path = pwd+sep()+path;
00061   }
00062   //printf("debug : path : %s\n",path.c_str());
00063   a_path = path;
00064   return true;
00065 }
00066 
00067 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){
00068   std::string exe_dir = inlib::dir_name(a_arg0);
00069   std::string bname = inlib::base_name(exe_dir);
00070   if(bname=="MacOS") {
00071     // cd in a_doc_dir :
00072     if(!inlib::dir::cd_home()) {
00073       a_out << "if_mouse_startup : can't go home." << std::endl;
00074       return false;
00075     }
00076     std::vector<std::string>::const_iterator it;
00077     for(it=a_home_dirs.begin();it!=a_home_dirs.end();it++) {
00078       if(!inlib::dir::mkcd(*it)) {
00079         a_out << "if_mouse_startup :"
00080               << " can't mkcd " << sout(*it)
00081               << std::endl;
00082         return false;
00083       }
00084     }
00085 
00086     // set a_env_HOME_DIR from exe_dir :
00087     std::string Contents_dir = inlib::dir_name(exe_dir);
00088     if(!inlib::putenv(a_env_HOME_DIR,Contents_dir)) {
00089       a_out << "if_mouse_startup :"
00090             << " can't putenv " << a_env_HOME_DIR 
00091             << " to " << inlib::sout(Contents_dir)
00092             << "."
00093             << std::endl;
00094       return false;
00095     }
00096 
00097     if(!inlib::env_path_append("DYLD_LIBRARY_PATH",Contents_dir+"/bin"))
00098       return false;
00099     // for on-the-fly compilation and loading :
00100     if(!inlib::env_path_append("DYLD_LIBRARY_PATH",".")) return false;
00101 
00102     if(!inlib::isenv("DISPLAY")) {
00103       if(!inlib::putenv("DISPLAY",":0.0")) return false;
00104     }
00105 
00106   } else if(bname=="bin") {
00107     //Linux, Windows : mouse startup ?
00108 
00109 /* FIXME : do we want to treat the case of a terminal startup
00110            whence the user did not <source setup> ???
00111            If so if_mouse_startup should be renamed no_env_startup.
00112 
00113   } else if(exe_dir=="") { //for exa when typing "osc-plot".
00114     if(!program_path(a_arg0,exe_dir)) {
00115       a_out << "if_mouse_startup :"
00116             << " program_path failed for " << inlib::sout(bname) << "."
00117             << std::endl;
00118       return false;
00119     }
00120     a_out << "if_mouse_startup :"
00121           << " program_path is " << inlib::sout(exe_dir) << "."
00122           << std::endl;
00123 
00124 #if defined(__APPLE__) || defined(Linux)
00125     bool is_abs,is_win;
00126     std::string drive;
00127     std::vector<std::string> words = path_words(exe_dir,is_abs,is_win,drive);
00128     std::vector<std::string>::size_type n = words.size();
00129     
00130     if( (n>=3) && 
00131         (words[n-1]=="bin") && 
00132         is_version(words[n-2]) &&
00133         (words[n-3].substr(0,4)=="osc_") 
00134       ){
00135 
00136       std::string osc_vers_dir = inlib::dir_name(exe_dir);
00137 
00138       printf("debug : auto env 000 \"%s\"\n",osc_vers_dir.c_str());
00139 
00140       //if(a_env_HOME_DIR.size()) {
00141         if(!inlib::putenv(a_env_HOME_DIR,osc_vers_dir)) {
00142           a_out << "arg0_setenv :"
00143                 << " can't putenv " << a_env_HOME_DIR 
00144                 << " to " << inlib::sout(osc_vers_dir)
00145                 << "."
00146                 << std::endl;
00147           return false;
00148         }
00149       //}
00150 
00151 #ifdef __APPLE__
00152 
00153      {std::string x;
00154       inlib::getenv("DYLD_LIBRARY_PATH",x);
00155       printf("debug : auto env 001 \"%s\"\n",x.c_str());}
00156 
00157       if(!inlib::env_path_append("DYLD_LIBRARY_PATH",osc_vers_dir+"/bin"))
00158         return false;
00159 
00160      {std::string x;
00161       inlib::getenv("DYLD_LIBRARY_PATH",x);
00162       printf("debug : auto env 002 \"%s\"\n",x.c_str());}
00163 
00164       if(!inlib::env_path_append("DYLD_LIBRARY_PATH",osc_vers_dir+"/lib"))
00165         return false;
00166 
00167      {std::string x;
00168       inlib::getenv("DYLD_LIBRARY_PATH",x);
00169       printf("debug : auto env 003 \"%s\"\n",x.c_str());}
00170 
00171       // for on-the-fly compilation and loading :
00172       if(!inlib::env_path_append("DYLD_LIBRARY_PATH",".")) return false;
00173 
00174      {std::string x;
00175       inlib::getenv("DYLD_LIBRARY_PATH",x);
00176       printf("debug : auto env 004 \"%s\"\n",x.c_str());}
00177 
00178 
00179 #else
00180 #endif
00181 
00182       if(!inlib::isenv("DISPLAY")) {
00183         if(!inlib::putenv("DISPLAY",":0.0")) return false;
00184       }
00185 
00186     }
00187 #endif
00188 */
00189 
00190   }
00191 
00192   return true;
00193 }
00194 
00195 }
00196 
00197 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines