SetDirectory.cpp

Go to the documentation of this file.
00001 /******************************************************************************
00002 *       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 3      *
00003 *                (c) 2006-2008 MGH, INRIA, USTL, UJF, CNRS                    *
00004 *                                                                             *
00005 * This library is free software; you can redistribute it and/or modify it     *
00006 * under the terms of the GNU Lesser General Public License as published by    *
00007 * the Free Software Foundation; either version 2.1 of the License, or (at     *
00008 * your option) any later version.                                             *
00009 *                                                                             *
00010 * This library is distributed in the hope that it will be useful, but WITHOUT *
00011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       *
00012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
00013 * for more details.                                                           *
00014 *                                                                             *
00015 * You should have received a copy of the GNU Lesser General Public License    *
00016 * along with this library; if not, write to the Free Software Foundation,     *
00017 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.          *
00018 *******************************************************************************
00019 *                              SOFA :: Framework                              *
00020 *                                                                             *
00021 * Authors: M. Adam, J. Allard, B. Andre, P-J. Bensoussan, S. Cotin, C. Duriez,*
00022 * H. Delingette, F. Falipou, F. Faure, S. Fonteneau, L. Heigeas, C. Mendoza,  *
00023 * M. Nesme, P. Neumann, J-P. de la Plata Alcade, F. Poyer and F. Roy          *
00024 *                                                                             *
00025 * Contact information: contact@sofa-framework.org                             *
00026 ******************************************************************************/
00027 #include <sofa/helper/system/SetDirectory.h>
00028 
00029 #ifndef WIN32
00030 #include <unistd.h>
00031 #else
00032 #include <windows.h>
00033 #include <direct.h>
00034 #endif
00035 #if defined (__APPLE__)
00036 #include <sys/param.h>
00037 #include <mach-o/dyld.h>
00038 #endif
00039 #include <string.h>
00040 #include <iostream>
00041 
00042 namespace sofa
00043 {
00044 
00045 namespace helper
00046 {
00047 
00048 namespace system
00049 {
00050 
00051 SetDirectory::SetDirectory(const char* filename)
00052 {
00053     directory = GetParentDir(filename);
00054     if (!directory.empty())
00055     {
00056 //         std::cout << ">chdir("<<directory<<")"<<std::endl;
00057     previousDir = GetCurrentDir();
00058 #ifndef WIN32
00059         chdir(directory.c_str());
00060 #else
00061         _chdir(directory.c_str());
00062 #endif
00063     }
00064 }
00065 
00066 SetDirectory::SetDirectory(const std::string& filename)
00067 {
00068     directory = GetParentDir(filename.c_str());
00069     if (!directory.empty())
00070     {
00071 //         std::cout << ">chdir("<<directory<<")"<<std::endl;
00072     previousDir = GetCurrentDir();
00073 #ifndef WIN32
00074         chdir(directory.c_str());
00075 #else
00076         _chdir(directory.c_str());
00077 #endif
00078     }
00079 }
00080 
00081 SetDirectory::~SetDirectory()
00082 {
00083     if (!directory.empty() && !previousDir.empty())
00084     {
00085 //         std::cout << "<chdir("<<directory<<")"<<std::endl;
00086 #ifndef WIN32
00087         chdir(previousDir.c_str());
00088 #else
00089         _chdir(previousDir.c_str());
00090 #endif
00091     }
00092 }
00093 
00095 bool SetDirectory::IsAbsolute(const std::string& filename)
00096 {
00097     if (filename.empty()) return false;
00098     if (filename[0] == '/' || filename[0] == '\\') return true;
00099 #ifdef WIN32
00100     if (filename.length() >= 2 && ((filename[0]>='a' && filename[0]<='z') || (filename[0]>='A' && filename[0]<='Z')) && filename[1]==':') return true;
00101 #endif
00102     return false;
00103 }
00104 
00106 std::string SetDirectory::GetCurrentDir()
00107 {
00108     char dir[1024];
00109     memset(dir,0,sizeof(dir));
00110 #ifndef WIN32
00111     getcwd(dir, sizeof(dir));
00112 #else
00113     _getcwd(dir, sizeof(dir));
00114 #endif
00115     return dir;
00116 }
00117 
00118 std::string SetDirectory::GetParentDir(const char* filename)
00119 {
00120     std::string s = filename;
00121     std::string::size_type pos = s.find_last_of("/\\");
00122     if (pos == std::string::npos)
00123         return ""; // no directory
00124     else
00125         return s.substr(0,pos);
00126 }
00127 
00128 std::string SetDirectory::GetFileName(const char* filename)
00129 {
00130     std::string s = filename;
00131     std::string::size_type pos = s.find_last_of("/\\");
00132     if (pos == std::string::npos)
00133         return s; // no directory
00134     else
00135         return s.substr(pos+1);
00136 }
00137 
00138 std::string SetDirectory::GetExtension(const char* filename)
00139 {
00140     std::string s = filename;
00141     std::string::size_type pos = s.find_last_of('.');
00142     if (pos == std::string::npos)
00143         return ""; // no extension
00144     else
00145         return s.substr(pos+1);
00146 }
00147 
00148 std::string SetDirectory::GetRelativeFromDir(const char* filename, const char* basename)
00149 {
00150     if (!filename || !filename[0]) return "";
00151     if (IsAbsolute(filename)) return filename; // absolute path
00152     std::string base = basename;
00153     std::string s = filename;
00154     // remove any ".."
00155     while ((s.substr(0,3)=="../" || s.substr(0,3)=="..\\") && !base.empty())
00156     {
00157         s = s.substr(3);
00158         base = GetParentDir(base.c_str());
00159     }
00160     if (base.empty())
00161         return s;
00162     else if (base[base.length()-1] == '/')
00163     return base + s;
00164     else
00165         return base + "/" + s;
00166 }
00167 
00168 std::string SetDirectory::GetRelativeFromFile(const char* filename, const char* basename)
00169 {
00170     std::string base = GetParentDir(basename);
00171     return GetRelativeFromDir(filename, base.c_str());
00172 }
00173 
00174 std::string SetDirectory::GetRelativeFromProcess(const char* filename, const char* basename)
00175 {
00176     std::string base = GetProcessFullPath(basename);
00177     return GetRelativeFromFile(filename, base.c_str());
00178 }
00179 
00181 std::string SetDirectory::GetProcessFullPath(const char* filename)
00182 {
00183 #if defined (WIN32)
00184     if (!filename || !filename[0])
00185     {
00186         //return __argv[0];
00187         int n=0;
00188     LPWSTR wpath = *CommandLineToArgvW(GetCommandLineW(),&n);
00189     if (wpath)
00190     {
00191         char path[1024];
00192         memset(path,0,sizeof(path));
00193         wcstombs(path, wpath, sizeof(path)-1);
00194         //std::cout << "Current process: "<<path<<std::endl;
00195         if (path[0]) return path;
00196     }
00197     }
00199 #elif defined (__linux__)
00200     if (!filename || filename[0]!='/')
00201     {
00202         char path[1024];
00203         memset(path,0,sizeof(path));
00204         readlink("/proc/self/exe",path,sizeof(path)-1);
00205 //      std::cout << "Current process: "<< path <<std::endl;
00206         if (path[0]) 
00207             return path;
00208         else
00209             std::cout << "ERROR: can't get current process path..." << std::endl;
00210     }
00211 #elif defined (__APPLE__)
00212     if (!filename || filename[0]!='/')
00213     {
00214         char path[1024];
00215         unsigned int size;
00216         _NSGetExecutablePath( path, &size );
00217 //      std::cout << "Current process path: "<<path<<std::endl;
00218 
00219         return path;
00220     }
00221 #endif
00222     
00223     return filename;
00224 }
00225 
00226 } // namespace system
00227 
00228 } // namespace helper
00229 
00230 } // namespace sofa
00231 

Generated on Sun Nov 23 02:29:30 2008 for Sofa by  doxygen 1.5.7.1