summaryrefslogtreecommitdiff
path: root/utils/unittest/googletest/gtest-filepath.cc
diff options
context:
space:
mode:
Diffstat (limited to 'utils/unittest/googletest/gtest-filepath.cc')
-rw-r--r--utils/unittest/googletest/gtest-filepath.cc62
1 files changed, 42 insertions, 20 deletions
diff --git a/utils/unittest/googletest/gtest-filepath.cc b/utils/unittest/googletest/gtest-filepath.cc
index 493ba0bfa7..07f828c914 100644
--- a/utils/unittest/googletest/gtest-filepath.cc
+++ b/utils/unittest/googletest/gtest-filepath.cc
@@ -36,21 +36,22 @@
#ifdef _WIN32_WCE
#include <windows.h>
-#elif defined(GTEST_OS_WINDOWS)
+#elif GTEST_OS_WINDOWS
#include <direct.h>
#include <io.h>
#include <sys/stat.h>
-#elif defined(GTEST_OS_SYMBIAN)
+#elif GTEST_OS_SYMBIAN
// Symbian OpenC has PATH_MAX in sys/syslimits.h
#include <sys/syslimits.h>
#include <unistd.h>
#else
#include <limits.h>
-#include <sys/stat.h>
-#include <unistd.h>
+#include <sys/stat.h> // NOLINT
+#include <unistd.h> // NOLINT
+#include <climits> // Some Linux distributions define PATH_MAX here.
#endif // _WIN32_WCE or _WIN32
-#ifdef GTEST_OS_WINDOWS
+#if GTEST_OS_WINDOWS
#define GTEST_PATH_MAX_ _MAX_PATH
#elif defined(PATH_MAX)
#define GTEST_PATH_MAX_ PATH_MAX
@@ -65,7 +66,7 @@
namespace testing {
namespace internal {
-#ifdef GTEST_OS_WINDOWS
+#if GTEST_OS_WINDOWS
const char kPathSeparator = '\\';
const char kPathSeparatorString[] = "\\";
#ifdef _WIN32_WCE
@@ -90,7 +91,7 @@ FilePath FilePath::GetCurrentDir() {
// Windows CE doesn't have a current directory, so we just return
// something reasonable.
return FilePath(kCurrentDirectoryString);
-#elif defined(GTEST_OS_WINDOWS)
+#elif GTEST_OS_WINDOWS
char cwd[GTEST_PATH_MAX_ + 1] = {};
return FilePath(_getcwd(cwd, sizeof(cwd)) == NULL ? "" : cwd);
#else
@@ -144,19 +145,28 @@ FilePath FilePath::MakeFileName(const FilePath& directory,
const FilePath& base_name,
int number,
const char* extension) {
- FilePath dir(directory.RemoveTrailingPathSeparator());
- if (number == 0) {
- return FilePath(String::Format("%s%c%s.%s", dir.c_str(), kPathSeparator,
- base_name.c_str(), extension));
- }
- return FilePath(String::Format("%s%c%s_%d.%s", dir.c_str(), kPathSeparator,
- base_name.c_str(), number, extension));
+ const FilePath file_name(
+ (number == 0) ?
+ String::Format("%s.%s", base_name.c_str(), extension) :
+ String::Format("%s_%d.%s", base_name.c_str(), number, extension));
+ return ConcatPaths(directory, file_name);
+}
+
+// Given directory = "dir", relative_path = "test.xml", returns "dir/test.xml".
+// On Windows, uses \ as the separator rather than /.
+FilePath FilePath::ConcatPaths(const FilePath& directory,
+ const FilePath& relative_path) {
+ if (directory.IsEmpty())
+ return relative_path;
+ const FilePath dir(directory.RemoveTrailingPathSeparator());
+ return FilePath(String::Format("%s%c%s", dir.c_str(), kPathSeparator,
+ relative_path.c_str()));
}
// Returns true if pathname describes something findable in the file-system,
// either a file, directory, or whatever.
bool FilePath::FileOrDirectoryExists() const {
-#ifdef GTEST_OS_WINDOWS
+#if GTEST_OS_WINDOWS
#ifdef _WIN32_WCE
LPCWSTR unicode = String::AnsiToUtf16(pathname_.c_str());
const DWORD attributes = GetFileAttributes(unicode);
@@ -176,7 +186,7 @@ bool FilePath::FileOrDirectoryExists() const {
// that exists.
bool FilePath::DirectoryExists() const {
bool result = false;
-#ifdef GTEST_OS_WINDOWS
+#if GTEST_OS_WINDOWS
// Don't strip off trailing separator if path is a root directory on
// Windows (like "C:\\").
const FilePath& path(IsRootDirectory() ? *this :
@@ -205,15 +215,27 @@ bool FilePath::DirectoryExists() const {
// Returns true if pathname describes a root directory. (Windows has one
// root directory per disk drive.)
bool FilePath::IsRootDirectory() const {
-#ifdef GTEST_OS_WINDOWS
+#if GTEST_OS_WINDOWS
+ // TODO(wan@google.com): on Windows a network share like
+ // \\server\share can be a root directory, although it cannot be the
+ // current directory. Handle this properly.
+ return pathname_.GetLength() == 3 && IsAbsolutePath();
+#else
+ return pathname_ == kPathSeparatorString;
+#endif
+}
+
+// Returns true if pathname describes an absolute path.
+bool FilePath::IsAbsolutePath() const {
const char* const name = pathname_.c_str();
- return pathname_.GetLength() == 3 &&
+#if GTEST_OS_WINDOWS
+ 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;
+ return name[0] == kPathSeparator;
#endif
}
@@ -264,7 +286,7 @@ bool FilePath::CreateDirectoriesRecursively() const {
// directory for any reason, including if the parent directory does not
// exist. Not named "CreateDirectory" because that's a macro on Windows.
bool FilePath::CreateFolder() const {
-#ifdef GTEST_OS_WINDOWS
+#if GTEST_OS_WINDOWS
#ifdef _WIN32_WCE
FilePath removed_sep(this->RemoveTrailingPathSeparator());
LPCWSTR unicode = String::AnsiToUtf16(removed_sep.c_str());