summaryrefslogtreecommitdiff
path: root/src/gtest-filepath.cc
diff options
context:
space:
mode:
authorshiqian <shiqian@861a406c-534a-0410-8894-cb66d6ee9925>2008-09-26 16:08:30 +0000
committershiqian <shiqian@861a406c-534a-0410-8894-cb66d6ee9925>2008-09-26 16:08:30 +0000
commitf0e809a3c9946e99595d4faeb0a16bdc2ca9ffd5 (patch)
tree68f49941b72ce8f866c4f762cb3043504804800f /src/gtest-filepath.cc
parentdc8c9fa9f38a6a8fc2b78d8096e8d3f1c969cb7c (diff)
downloadgtest-f0e809a3c9946e99595d4faeb0a16bdc2ca9ffd5.tar.gz
gtest-f0e809a3c9946e99595d4faeb0a16bdc2ca9ffd5.tar.bz2
gtest-f0e809a3c9946e99595d4faeb0a16bdc2ca9ffd5.tar.xz
Lots of changes:
* changes the XML report format to match JUnit/Ant's. * improves file path handling. * allows the user to disable RTTI using the GTEST_HAS_RTTI macro. * makes the code compile with -Wswitch-enum. git-svn-id: http://googletest.googlecode.com/svn/trunk@98 861a406c-534a-0410-8894-cb66d6ee9925
Diffstat (limited to 'src/gtest-filepath.cc')
-rw-r--r--src/gtest-filepath.cc54
1 files changed, 49 insertions, 5 deletions
diff --git a/src/gtest-filepath.cc b/src/gtest-filepath.cc
index 2a5be8c..dc0d78f 100644
--- a/src/gtest-filepath.cc
+++ b/src/gtest-filepath.cc
@@ -161,10 +161,13 @@ bool FilePath::FileOrDirectoryExists() const {
// that exists.
bool FilePath::DirectoryExists() const {
bool result = false;
-#ifdef _WIN32
- FilePath removed_sep(this->RemoveTrailingPathSeparator());
+#ifdef GTEST_OS_WINDOWS
+ // Don't strip off trailing separator if path is a root directory on
+ // Windows (like "C:\\").
+ const FilePath& path(IsRootDirectory() ? *this :
+ RemoveTrailingPathSeparator());
#ifdef _WIN32_WCE
- LPCWSTR unicode = String::AnsiToUtf16(removed_sep.c_str());
+ LPCWSTR unicode = String::AnsiToUtf16(path.c_str());
const DWORD attributes = GetFileAttributes(unicode);
delete [] unicode;
if ((attributes != kInvalidFileAttributes) &&
@@ -173,17 +176,32 @@ bool FilePath::DirectoryExists() const {
}
#else
struct _stat file_stat = {};
- result = _stat(removed_sep.c_str(), &file_stat) == 0 &&
+ result = _stat(path.c_str(), &file_stat) == 0 &&
(_S_IFDIR & file_stat.st_mode) != 0;
#endif // _WIN32_WCE
#else
struct stat file_stat = {};
result = stat(pathname_.c_str(), &file_stat) == 0 &&
S_ISDIR(file_stat.st_mode);
-#endif // _WIN32
+#endif // GTEST_OS_WINDOWS
return result;
}
+// Returns true if pathname describes a root directory. (Windows has one
+// root directory per disk drive.)
+bool FilePath::IsRootDirectory() const {
+#ifdef GTEST_OS_WINDOWS
+ const char* const name = pathname_.c_str();
+ return pathname_.GetLength() == 3 &&
+ ((name[0] >= 'a' && name[0] <= 'z') ||
+ (name[0] >= 'A' && name[0] <= 'Z')) &&
+ name[1] == ':' &&
+ name[2] == kPathSeparator;
+#else
+ return pathname_ == kPathSeparatorString;
+#endif
+}
+
// Returns a pathname for a file that does not currently exist. The pathname
// will be directory/base_name.extension or
// directory/base_name_<number>.extension if directory/base_name.extension
@@ -258,5 +276,31 @@ FilePath FilePath::RemoveTrailingPathSeparator() const {
: *this;
}
+// Normalize removes any redundant separators that might be in the pathname.
+// For example, "bar///foo" becomes "bar/foo". Does not eliminate other
+// redundancies that might be in a pathname involving "." or "..".
+void FilePath::Normalize() {
+ if (pathname_.c_str() == NULL) {
+ pathname_ = "";
+ return;
+ }
+ const char* src = pathname_.c_str();
+ char* const dest = new char[pathname_.GetLength() + 1];
+ char* dest_ptr = dest;
+ memset(dest_ptr, 0, pathname_.GetLength() + 1);
+
+ while (*src != '\0') {
+ *dest_ptr++ = *src;
+ if (*src != kPathSeparator)
+ src++;
+ else
+ while (*src == kPathSeparator)
+ src++;
+ }
+ *dest_ptr = '\0';
+ pathname_ = dest;
+ delete[] dest;
+}
+
} // namespace internal
} // namespace testing