diff options
author | Michi Henning <michi@zeroc.com> | 2004-12-29 03:50:52 +0000 |
---|---|---|
committer | Michi Henning <michi@zeroc.com> | 2004-12-29 03:50:52 +0000 |
commit | 5e8559fce24e8408a70d2dc5c175cecf350aec9b (patch) | |
tree | 9cddcc10d35c05de7d75e377a854c4473935f1b7 /cpp/src/IcePatch2/Util.cpp | |
parent | fixed retry logging (diff) | |
download | ice-5e8559fce24e8408a70d2dc5c175cecf350aec9b.tar.bz2 ice-5e8559fce24e8408a70d2dc5c175cecf350aec9b.tar.xz ice-5e8559fce24e8408a70d2dc5c175cecf350aec9b.zip |
Fixed bug in checkedCast template.
Diffstat (limited to 'cpp/src/IcePatch2/Util.cpp')
-rw-r--r-- | cpp/src/IcePatch2/Util.cpp | 122 |
1 files changed, 83 insertions, 39 deletions
diff --git a/cpp/src/IcePatch2/Util.cpp b/cpp/src/IcePatch2/Util.cpp index 0bf6c71106c..5f2f92b5e14 100644 --- a/cpp/src/IcePatch2/Util.cpp +++ b/cpp/src/IcePatch2/Util.cpp @@ -210,10 +210,56 @@ IcePatch2::stringToBytes(const string& str) return bytes; } +bool +IcePatch2::isDir(const string& path) +{ + struct stat buf; + if(stat(path.c_str(), &buf) == -1) + { + throw "cannot stat `" + path + "':\n" + lastError(); + } + return S_ISDIR(buf.st_mode); +} + string IcePatch2::normalize(const string& path) { - string result = path; + assert(!path.empty()); + + static IceUtil::StaticMutex mutex = ICE_STATIC_MUTEX_INITIALIZER; + +#ifdef _WIN32 + static char cwd[_MAX_PATH]; +#else + static char cwd[PATH_MAX]; +#endif + + { + IceUtil::StaticMutex::Lock sync(mutex); + + if(*cwd == '\0') + { +#ifdef _WIN32 + if(_getcwd(cwd, _MAX_PATH) == NULL) +#else + if(getcwd(cwd, PATH_MAX) == NULL) +#endif + { + throw "cannot get the current directory:\n" + lastError(); + } + } + } + + string result; +#ifdef _WIN32 + if(path[0] != '/' && path[0] != '\\' && !(path.size() > 1 && isalpha(path[0]) && path[1] == ':')) +#else + if(path[0] != '/') +#endif + { + result = cwd + '/'; + } + result += path; string::size_type pos; @@ -236,23 +282,41 @@ IcePatch2::normalize(const string& path) pos = 0; while((pos = result.find("/./", pos)) != string::npos) { - result.erase(pos, 2); + result.erase(pos, 2); // Remove redundant current directory components. } + if(result.size() > 1 && result[result.size() - 1] == '/') + { + result.erase(result.size() - 1, 1); + } + + while(result.size() > 2 && result.substr(0, 2) == "./") + { + result.erase(0, 2); + } + + + if(result.substr(0, 2) == "./") { result.erase(0, 2); } - if(result.size() >= 2 && result.substr(result.size() - 2, 2) == "/.") + if(result == "/.") + { + return "/"; + } + + while(result.size() > 2 && result.substr(result.size() - 2, 2) == "/.") { result.erase(result.size() - 2, 2); } - if(result.size() >= 1 && result[result.size() - 1] == '/') + if(result.size() > 1 && result[result.size() - 1] == '/') { result.erase(result.size() - 1); } + cerr << "normalize: returning " << result << endl; return result; } @@ -261,32 +325,20 @@ string IcePatch2::getSuffix(const string& pa) { const string path = normalize(pa); - - string::size_type pos = path.rfind('.'); - if(pos == string::npos) - { - return string(); - } - else - { - return path.substr(pos + 1); - } + string::size_type slashPos = path.rfind('/'); + slashPos = slashPos == string::npos ? 0 : slashPos + 1; + string::size_type dotPos = path.find('.', slashPos); + return dotPos == string::npos ? string() : path.substr(dotPos + 1); } string IcePatch2::getWithoutSuffix(const string& pa) { const string path = normalize(pa); - - string::size_type pos = path.rfind('.'); - if(pos == string::npos) - { - return path; - } - else - { - return path.substr(0, pos); - } + string::size_type slashPos = path.rfind('/'); + slashPos = slashPos == string::npos ? 0 : slashPos + 1; + string::size_type dotPos = path.rfind('.', slashPos); + return dotPos == string::npos ? path : path.substr(0, dotPos); } bool @@ -302,32 +354,24 @@ string IcePatch2::getBasename(const string& pa) { const string path = normalize(pa); - - string::size_type pos = path.rfind('/'); - if(pos == string::npos) + if(path == "/") { return path; } - else - { - return path.substr(pos + 1); - } + string::size_type slashPos = path.rfind('/'); + return slashPos == string::npos ? path : path.substr(slashPos + 1); } string IcePatch2::getDirname(const string& pa) { const string path = normalize(pa); - - string::size_type pos = path.rfind('/'); - if(pos == string::npos) + if(path == "/") { - return string(); - } - else - { - return path.substr(0, pos); + return path; } + string::size_type slashPos = path.rfind('/'); + return slashPos == string::npos ? string() : path.substr(0, slashPos); } void |