SetDirectory.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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
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
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
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 "";
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;
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 "";
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;
00152 std::string base = basename;
00153 std::string s = filename;
00154
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
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
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
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
00218
00219 return path;
00220 }
00221 #endif
00222
00223 return filename;
00224 }
00225
00226 }
00227
00228 }
00229
00230 }
00231