11be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Copyright 2008, Google Inc.
21be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// All rights reserved.
31be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
41be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Redistribution and use in source and binary forms, with or without
51be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// modification, are permitted provided that the following conditions are
61be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// met:
71be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
81be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//     * Redistributions of source code must retain the above copyright
91be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// notice, this list of conditions and the following disclaimer.
101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//     * Redistributions in binary form must reproduce the above
111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// copyright notice, this list of conditions and the following disclaimer
121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// in the documentation and/or other materials provided with the
131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// distribution.
141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//     * Neither the name of Google Inc. nor the names of its
151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// contributors may be used to endorse or promote products derived from
161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// this software without specific prior written permission.
171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Authors: keith.ray@gmail.com (Keith Ray)
311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
32fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes#include "gtest/gtest-message.h"
3341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include "gtest/internal/gtest-filepath.h"
3441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include "gtest/internal/gtest-port.h"
351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#include <stdlib.h>
371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_OS_WINDOWS_MOBILE
3941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <windows.h>
401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#elif GTEST_OS_WINDOWS
4141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <direct.h>
4241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <io.h>
43fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes#elif GTEST_OS_SYMBIAN
44fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// Symbian OpenC has PATH_MAX in sys/syslimits.h
4541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <sys/syslimits.h>
461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#else
4741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <limits.h>
4841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <climits>  // Some Linux distributions define PATH_MAX here.
4941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif  // GTEST_OS_WINDOWS_MOBILE
501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_OS_WINDOWS
5241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define GTEST_PATH_MAX_ _MAX_PATH
531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#elif defined(PATH_MAX)
5441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define GTEST_PATH_MAX_ PATH_MAX
551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#elif defined(_XOPEN_PATH_MAX)
5641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define GTEST_PATH_MAX_ _XOPEN_PATH_MAX
571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#else
5841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define GTEST_PATH_MAX_ _POSIX_PATH_MAX
591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif  // GTEST_OS_WINDOWS
601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
6141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include "gtest/internal/gtest-string.h"
621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace testing {
641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace internal {
651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_OS_WINDOWS
6741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// On Windows, '\\' is the standard path separator, but many tools and the
6841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Windows API also accept '/' as an alternate path separator. Unless otherwise
6941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// noted, a file path can contain either kind of path separators, or a mixture
7041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// of them.
711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaconst char kPathSeparator = '\\';
7241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotconst char kAlternatePathSeparator = '/';
731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaconst char kPathSeparatorString[] = "\\";
7441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotconst char kAlternatePathSeparatorString[] = "/";
7541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# if GTEST_OS_WINDOWS_MOBILE
761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Windows CE doesn't have a current directory. You should not use
771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// the current directory in tests on Windows CE, but this at least
781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// provides a reasonable fallback.
791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaconst char kCurrentDirectoryString[] = "\\";
801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Windows CE doesn't define INVALID_FILE_ATTRIBUTES
811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaconst DWORD kInvalidFileAttributes = 0xffffffff;
8241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# else
831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaconst char kCurrentDirectoryString[] = ".\\";
8441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif  // GTEST_OS_WINDOWS_MOBILE
851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#else
861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaconst char kPathSeparator = '/';
871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaconst char kPathSeparatorString[] = "/";
881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaconst char kCurrentDirectoryString[] = "./";
891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif  // GTEST_OS_WINDOWS
901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
9141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Returns whether the given character is a valid path separator.
9241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotstatic bool IsPathSeparator(char c) {
9341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_HAS_ALT_PATH_SEP_
9441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return (c == kPathSeparator) || (c == kAlternatePathSeparator);
9541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#else
9641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return c == kPathSeparator;
9741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif
9841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
9941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
1001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns the current working directory, or "" if unsuccessful.
1011be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaFilePath FilePath::GetCurrentDir() {
10241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_OS_WINDOWS_MOBILE
10341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // Windows CE doesn't have a current directory, so we just return
10441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // something reasonable.
1051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return FilePath(kCurrentDirectoryString);
1061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#elif GTEST_OS_WINDOWS
10741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  char cwd[GTEST_PATH_MAX_ + 1] = { '\0' };
1081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return FilePath(_getcwd(cwd, sizeof(cwd)) == NULL ? "" : cwd);
1091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#else
11041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  char cwd[GTEST_PATH_MAX_ + 1] = { '\0' };
1111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return FilePath(getcwd(cwd, sizeof(cwd)) == NULL ? "" : cwd);
11241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif  // GTEST_OS_WINDOWS_MOBILE
1131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
1141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns a copy of the FilePath with the case-insensitive extension removed.
1161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Example: FilePath("dir/file.exe").RemoveExtension("EXE") returns
1171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// FilePath("dir/file"). If a case-insensitive extension is not
1181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// found, returns a copy of the original FilePath.
1191be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaFilePath FilePath::RemoveExtension(const char* extension) const {
120fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  const std::string dot_extension = std::string(".") + extension;
121fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  if (String::EndsWithCaseInsensitive(pathname_, dot_extension)) {
122fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    return FilePath(pathname_.substr(
123fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes        0, pathname_.length() - dot_extension.length()));
1241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
1251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return *this;
1261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
1271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
12841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Returns a pointer to the last occurence of a valid path separator in
12941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// the FilePath. On Windows, for example, both '/' and '\' are valid path
13041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// separators. Returns NULL if no path separator was found.
13141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotconst char* FilePath::FindLastPathSeparator() const {
13241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  const char* const last_sep = strrchr(c_str(), kPathSeparator);
13341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_HAS_ALT_PATH_SEP_
13441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  const char* const last_alt_sep = strrchr(c_str(), kAlternatePathSeparator);
13541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // Comparing two pointers of which only one is NULL is undefined.
13641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (last_alt_sep != NULL &&
13741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      (last_sep == NULL || last_alt_sep > last_sep)) {
13841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    return last_alt_sep;
13941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  }
14041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif
14141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return last_sep;
14241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
14341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
1441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns a copy of the FilePath with the directory part removed.
1451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Example: FilePath("path/to/file").RemoveDirectoryName() returns
1461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// FilePath("file"). If there is no directory part ("just_a_file"), it returns
1471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// the FilePath unmodified. If there is no file part ("just_a_dir/") it
1481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// returns an empty FilePath ("").
1491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// On Windows platform, '\' is the path separator, otherwise it is '/'.
1501be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaFilePath FilePath::RemoveDirectoryName() const {
15141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  const char* const last_sep = FindLastPathSeparator();
152fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  return last_sep ? FilePath(last_sep + 1) : *this;
1531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
1541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// RemoveFileName returns the directory path with the filename removed.
1561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Example: FilePath("path/to/file").RemoveFileName() returns "path/to/".
1571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// If the FilePath is "a_file" or "/a_file", RemoveFileName returns
1581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// FilePath("./") or, on Windows, FilePath(".\\"). If the filepath does
1591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// not have a file, like "just/a/dir/", it returns the FilePath unmodified.
1601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// On Windows platform, '\' is the path separator, otherwise it is '/'.
1611be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaFilePath FilePath::RemoveFileName() const {
16241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  const char* const last_sep = FindLastPathSeparator();
163fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  std::string dir;
16441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (last_sep) {
165fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    dir = std::string(c_str(), last_sep + 1 - c_str());
16641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  } else {
16741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    dir = kCurrentDirectoryString;
16841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  }
16941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return FilePath(dir);
1701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
1711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Helper functions for naming files in a directory for xml output.
1731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Given directory = "dir", base_name = "test", number = 0,
1751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// extension = "xml", returns "dir/test.xml". If number is greater
1761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// than zero (e.g., 12), returns "dir/test_12.xml".
1771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// On Windows platform, uses \ as the separator rather than /.
1781be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaFilePath FilePath::MakeFileName(const FilePath& directory,
1791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                const FilePath& base_name,
1801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                int number,
1811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                const char* extension) {
182fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  std::string file;
18341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (number == 0) {
184fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    file = base_name.string() + "." + extension;
18541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  } else {
186fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    file = base_name.string() + "_" + StreamableToString(number)
187fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes        + "." + extension;
18841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  }
18941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return ConcatPaths(directory, FilePath(file));
1901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
1911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Given directory = "dir", relative_path = "test.xml", returns "dir/test.xml".
1931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// On Windows, uses \ as the separator rather than /.
1941be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaFilePath FilePath::ConcatPaths(const FilePath& directory,
1951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                               const FilePath& relative_path) {
1961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (directory.IsEmpty())
1971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return relative_path;
1981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const FilePath dir(directory.RemoveTrailingPathSeparator());
199fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  return FilePath(dir.string() + kPathSeparator + relative_path.string());
2001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
2011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns true if pathname describes something findable in the file-system,
2031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// either a file, directory, or whatever.
2041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool FilePath::FileOrDirectoryExists() const {
20541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_OS_WINDOWS_MOBILE
2061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  LPCWSTR unicode = String::AnsiToUtf16(pathname_.c_str());
2071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const DWORD attributes = GetFileAttributes(unicode);
2081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  delete [] unicode;
2091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return attributes != kInvalidFileAttributes;
2101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#else
21141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  posix::StatStruct file_stat;
21241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return posix::Stat(pathname_.c_str(), &file_stat) == 0;
21341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif  // GTEST_OS_WINDOWS_MOBILE
2141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
2151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns true if pathname describes a directory in the file-system
2171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// that exists.
2181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool FilePath::DirectoryExists() const {
2191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  bool result = false;
2201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_OS_WINDOWS
2211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Don't strip off trailing separator if path is a root directory on
2221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Windows (like "C:\\").
2231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const FilePath& path(IsRootDirectory() ? *this :
2241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                           RemoveTrailingPathSeparator());
22541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#else
22641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  const FilePath& path(*this);
22741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif
22841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
22941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_OS_WINDOWS_MOBILE
2301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  LPCWSTR unicode = String::AnsiToUtf16(path.c_str());
2311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const DWORD attributes = GetFileAttributes(unicode);
2321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  delete [] unicode;
2331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if ((attributes != kInvalidFileAttributes) &&
2341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      (attributes & FILE_ATTRIBUTE_DIRECTORY)) {
2351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    result = true;
2361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
2371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#else
23841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  posix::StatStruct file_stat;
23941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  result = posix::Stat(path.c_str(), &file_stat) == 0 &&
24041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      posix::IsDir(file_stat);
24141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif  // GTEST_OS_WINDOWS_MOBILE
24241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
2431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return result;
2441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
2451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns true if pathname describes a root directory. (Windows has one
2471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// root directory per disk drive.)
2481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool FilePath::IsRootDirectory() const {
2491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_OS_WINDOWS
2501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // TODO(wan@google.com): on Windows a network share like
2511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // \\server\share can be a root directory, although it cannot be the
2521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // current directory.  Handle this properly.
25341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return pathname_.length() == 3 && IsAbsolutePath();
2541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#else
25541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return pathname_.length() == 1 && IsPathSeparator(pathname_.c_str()[0]);
2561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif
2571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
2581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns true if pathname describes an absolute path.
2601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool FilePath::IsAbsolutePath() const {
2611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const char* const name = pathname_.c_str();
2621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_OS_WINDOWS
26341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return pathname_.length() >= 3 &&
2641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania     ((name[0] >= 'a' && name[0] <= 'z') ||
2651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      (name[0] >= 'A' && name[0] <= 'Z')) &&
2661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania     name[1] == ':' &&
26741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot     IsPathSeparator(name[2]);
2681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#else
26941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return IsPathSeparator(name[0]);
2701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif
2711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
2721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns a pathname for a file that does not currently exist. The pathname
2741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// will be directory/base_name.extension or
2751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// directory/base_name_<number>.extension if directory/base_name.extension
2761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// already exists. The number will be incremented until a pathname is found
2771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// that does not already exist.
2781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Examples: 'dir/foo_test.xml' or 'dir/foo_test_1.xml'.
2791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// There could be a race condition if two or more processes are calling this
2801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// function at the same time -- they could both pick the same filename.
2811be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaFilePath FilePath::GenerateUniqueFileName(const FilePath& directory,
2821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                          const FilePath& base_name,
2831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                          const char* extension) {
2841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  FilePath full_pathname;
2851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  int number = 0;
2861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  do {
2871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    full_pathname.Set(MakeFileName(directory, base_name, number++, extension));
2881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  } while (full_pathname.FileOrDirectoryExists());
2891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return full_pathname;
2901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
2911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns true if FilePath ends with a path separator, which indicates that
2931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// it is intended to represent a directory. Returns false otherwise.
2941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This does NOT check that a directory (or file) actually exists.
2951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool FilePath::IsDirectory() const {
29641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return !pathname_.empty() &&
29741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot         IsPathSeparator(pathname_.c_str()[pathname_.length() - 1]);
2981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
2991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Create directories so that path exists. Returns true if successful or if
3011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// the directories already exist; returns false if unable to create directories
3021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// for any reason.
3031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool FilePath::CreateDirectoriesRecursively() const {
3041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (!this->IsDirectory()) {
3051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return false;
3061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
3071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
30841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (pathname_.length() == 0 || this->DirectoryExists()) {
3091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return true;
3101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
3111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const FilePath parent(this->RemoveTrailingPathSeparator().RemoveFileName());
3131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return parent.CreateDirectoriesRecursively() && this->CreateFolder();
3141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
3151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Create the directory so that path exists. Returns true if successful or
3171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// if the directory already exists; returns false if unable to create the
3181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// directory for any reason, including if the parent directory does not
3191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// exist. Not named "CreateDirectory" because that's a macro on Windows.
3201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool FilePath::CreateFolder() const {
32141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_OS_WINDOWS_MOBILE
3221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  FilePath removed_sep(this->RemoveTrailingPathSeparator());
3231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  LPCWSTR unicode = String::AnsiToUtf16(removed_sep.c_str());
3241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  int result = CreateDirectory(unicode, NULL) ? 0 : -1;
3251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  delete [] unicode;
32641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#elif GTEST_OS_WINDOWS
3271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  int result = _mkdir(pathname_.c_str());
3281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#else
3291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  int result = mkdir(pathname_.c_str(), 0777);
33041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif  // GTEST_OS_WINDOWS_MOBILE
33141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
3321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (result == -1) {
3331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return this->DirectoryExists();  // An error is OK if the directory exists.
3341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
3351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return true;  // No error.
3361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
3371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// If input name has a trailing separator character, remove it and return the
3391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// name, otherwise return the name string unmodified.
3401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// On Windows platform, uses \ as the separator, other platforms use /.
3411be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaFilePath FilePath::RemoveTrailingPathSeparator() const {
34241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return IsDirectory()
343fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      ? FilePath(pathname_.substr(0, pathname_.length() - 1))
3441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      : *this;
3451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
3461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
34741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Removes any redundant separators that might be in the pathname.
3481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// For example, "bar///foo" becomes "bar/foo". Does not eliminate other
3491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// redundancies that might be in a pathname involving "." or "..".
35041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// TODO(wan@google.com): handle Windows network shares (e.g. \\server\share).
3511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid FilePath::Normalize() {
3521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (pathname_.c_str() == NULL) {
3531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    pathname_ = "";
3541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return;
3551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
3561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const char* src = pathname_.c_str();
35741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  char* const dest = new char[pathname_.length() + 1];
3581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  char* dest_ptr = dest;
35941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  memset(dest_ptr, 0, pathname_.length() + 1);
3601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  while (*src != '\0') {
36241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    *dest_ptr = *src;
36341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    if (!IsPathSeparator(*src)) {
3641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      src++;
36541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    } else {
36641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_HAS_ALT_PATH_SEP_
36741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      if (*dest_ptr == kAlternatePathSeparator) {
36841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot        *dest_ptr = kPathSeparator;
36941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      }
37041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif
37141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      while (IsPathSeparator(*src))
3721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        src++;
37341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    }
37441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    dest_ptr++;
3751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
3761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  *dest_ptr = '\0';
3771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  pathname_ = dest;
3781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  delete[] dest;
3791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
3801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}  // namespace internal
3821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}  // namespace testing
383