Cache result of getPath() on darwin

develop
lethosor 2015-07-27 20:32:33 -04:00
parent c3c625e7bd
commit 7eb4c33cb1
1 changed files with 9 additions and 3 deletions

@ -259,18 +259,24 @@ uint32_t Process::getTickCount()
string Process::getPath() string Process::getPath()
{ {
static string cached_path = "";
if (cached_path.size())
return cached_path;
char path[1024]; char path[1024];
char *real_path; char *real_path;
uint32_t size = sizeof(path); uint32_t size = sizeof(path);
if (getcwd(path, size)) if (getcwd(path, size))
return string(path); {
cached_path = string(path);
return cached_path;
}
if (_NSGetExecutablePath(path, &size) == 0) { if (_NSGetExecutablePath(path, &size) == 0) {
real_path = realpath(path, NULL); real_path = realpath(path, NULL);
} }
std::string path_string(real_path); std::string path_string(real_path);
int last_slash = path_string.find_last_of("/"); int last_slash = path_string.find_last_of("/");
std::string directory = path_string.substr(0,last_slash); cached_path = path_string.substr(0,last_slash);
return directory; return cached_path;
} }
int Process::getPID() int Process::getPID()