17ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Copyright 2008, Google Inc.
27ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// All rights reserved.
37ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//
47ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Redistribution and use in source and binary forms, with or without
57ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// modification, are permitted provided that the following conditions are
67ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// met:
77ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//
87ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//     * Redistributions of source code must retain the above copyright
97ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// notice, this list of conditions and the following disclaimer.
107ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//     * Redistributions in binary form must reproduce the above
117ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// copyright notice, this list of conditions and the following disclaimer
127ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// in the documentation and/or other materials provided with the
137ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// distribution.
147ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//     * Neither the name of Google Inc. nor the names of its
157ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// contributors may be used to endorse or promote products derived from
167ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// this software without specific prior written permission.
177ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//
187ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
197ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
207ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
217ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
227ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
237ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
247ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
257ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
267ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
277ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
287ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
297ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//
307ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Authors: keith.ray@gmail.com (Keith Ray)
317ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
32b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad#include "gtest/internal/gtest-filepath.h"
33b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad#include "gtest/internal/gtest-port.h"
347ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
357ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#include <stdlib.h>
367ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
37190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer#if GTEST_OS_WINDOWS_MOBILE
38b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad# include <windows.h>
39e4b9c93fc1b531fe0cfe25a042f6b81c1e7c15c0Benjamin Kramer#elif GTEST_OS_WINDOWS
40b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad# include <direct.h>
41b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad# include <io.h>
42b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad#elif GTEST_OS_SYMBIAN || GTEST_OS_NACL
43b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// Symbian OpenC and NaCl have PATH_MAX in sys/syslimits.h
44b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad# include <sys/syslimits.h>
457ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#else
46b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad# include <limits.h>
47b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad# include <climits>  // Some Linux distributions define PATH_MAX here.
48190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer#endif  // GTEST_OS_WINDOWS_MOBILE
497ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
50e4b9c93fc1b531fe0cfe25a042f6b81c1e7c15c0Benjamin Kramer#if GTEST_OS_WINDOWS
51b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad# define GTEST_PATH_MAX_ _MAX_PATH
527ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#elif defined(PATH_MAX)
53b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad# define GTEST_PATH_MAX_ PATH_MAX
547ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#elif defined(_XOPEN_PATH_MAX)
55b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad# define GTEST_PATH_MAX_ _XOPEN_PATH_MAX
567ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#else
57b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad# define GTEST_PATH_MAX_ _POSIX_PATH_MAX
587ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#endif  // GTEST_OS_WINDOWS
597ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
60b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad#include "gtest/internal/gtest-string.h"
617ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
627ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmannamespace testing {
637ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmannamespace internal {
647ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
65e4b9c93fc1b531fe0cfe25a042f6b81c1e7c15c0Benjamin Kramer#if GTEST_OS_WINDOWS
6657240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer// On Windows, '\\' is the standard path separator, but many tools and the
6757240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer// Windows API also accept '/' as an alternate path separator. Unless otherwise
6857240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer// noted, a file path can contain either kind of path separators, or a mixture
6957240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer// of them.
707ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanconst char kPathSeparator = '\\';
7157240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramerconst char kAlternatePathSeparator = '/';
7257240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramerconst char kAlternatePathSeparatorString[] = "/";
73b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad# if GTEST_OS_WINDOWS_MOBILE
747ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Windows CE doesn't have a current directory. You should not use
757ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// the current directory in tests on Windows CE, but this at least
767ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// provides a reasonable fallback.
777ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanconst char kCurrentDirectoryString[] = "\\";
787ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Windows CE doesn't define INVALID_FILE_ATTRIBUTES
797ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanconst DWORD kInvalidFileAttributes = 0xffffffff;
80b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad# else
817ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanconst char kCurrentDirectoryString[] = ".\\";
82b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad# endif  // GTEST_OS_WINDOWS_MOBILE
837ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#else
847ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanconst char kPathSeparator = '/';
857ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanconst char kCurrentDirectoryString[] = "./";
867ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#endif  // GTEST_OS_WINDOWS
877ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
8857240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer// Returns whether the given character is a valid path separator.
8957240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramerstatic bool IsPathSeparator(char c) {
9057240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer#if GTEST_HAS_ALT_PATH_SEP_
9157240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer  return (c == kPathSeparator) || (c == kAlternatePathSeparator);
9257240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer#else
9357240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer  return c == kPathSeparator;
9457240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer#endif
9557240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer}
9657240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer
977ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Returns the current working directory, or "" if unsuccessful.
987ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha BrukmanFilePath FilePath::GetCurrentDir() {
99190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer#if GTEST_OS_WINDOWS_MOBILE
100190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer  // Windows CE doesn't have a current directory, so we just return
101190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer  // something reasonable.
1027ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  return FilePath(kCurrentDirectoryString);
103e4b9c93fc1b531fe0cfe25a042f6b81c1e7c15c0Benjamin Kramer#elif GTEST_OS_WINDOWS
104190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer  char cwd[GTEST_PATH_MAX_ + 1] = { '\0' };
1057ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  return FilePath(_getcwd(cwd, sizeof(cwd)) == NULL ? "" : cwd);
1067ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#else
107190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer  char cwd[GTEST_PATH_MAX_ + 1] = { '\0' };
1087ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  return FilePath(getcwd(cwd, sizeof(cwd)) == NULL ? "" : cwd);
109190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer#endif  // GTEST_OS_WINDOWS_MOBILE
1107ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman}
1117ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
1127ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Returns a copy of the FilePath with the case-insensitive extension removed.
1137ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Example: FilePath("dir/file.exe").RemoveExtension("EXE") returns
1147ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// FilePath("dir/file"). If a case-insensitive extension is not
1157ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// found, returns a copy of the original FilePath.
1167ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha BrukmanFilePath FilePath::RemoveExtension(const char* extension) const {
1177ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  String dot_extension(String::Format(".%s", extension));
1187ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  if (pathname_.EndsWithCaseInsensitive(dot_extension.c_str())) {
119190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer    return FilePath(String(pathname_.c_str(), pathname_.length() - 4));
1207ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  }
1217ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  return *this;
1227ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman}
1237ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
1247a2bdde0a0eebcd2125055e0eacaca040f0b766cChris Lattner// Returns a pointer to the last occurrence of a valid path separator in
12557240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer// the FilePath. On Windows, for example, both '/' and '\' are valid path
12657240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer// separators. Returns NULL if no path separator was found.
12757240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramerconst char* FilePath::FindLastPathSeparator() const {
12857240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer  const char* const last_sep = strrchr(c_str(), kPathSeparator);
12957240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer#if GTEST_HAS_ALT_PATH_SEP_
13057240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer  const char* const last_alt_sep = strrchr(c_str(), kAlternatePathSeparator);
13157240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer  // Comparing two pointers of which only one is NULL is undefined.
13257240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer  if (last_alt_sep != NULL &&
13357240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer      (last_sep == NULL || last_alt_sep > last_sep)) {
13457240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer    return last_alt_sep;
13557240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer  }
13657240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer#endif
13757240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer  return last_sep;
13857240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer}
13957240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer
1407ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Returns a copy of the FilePath with the directory part removed.
1417ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Example: FilePath("path/to/file").RemoveDirectoryName() returns
1427ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// FilePath("file"). If there is no directory part ("just_a_file"), it returns
1437ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// the FilePath unmodified. If there is no file part ("just_a_dir/") it
1447ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// returns an empty FilePath ("").
1457ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// On Windows platform, '\' is the path separator, otherwise it is '/'.
1467ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha BrukmanFilePath FilePath::RemoveDirectoryName() const {
14757240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer  const char* const last_sep = FindLastPathSeparator();
1487ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  return last_sep ? FilePath(String(last_sep + 1)) : *this;
1497ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman}
1507ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
1517ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// RemoveFileName returns the directory path with the filename removed.
1527ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Example: FilePath("path/to/file").RemoveFileName() returns "path/to/".
1537ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// If the FilePath is "a_file" or "/a_file", RemoveFileName returns
1547ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// FilePath("./") or, on Windows, FilePath(".\\"). If the filepath does
1557ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// not have a file, like "just/a/dir/", it returns the FilePath unmodified.
1567ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// On Windows platform, '\' is the path separator, otherwise it is '/'.
1577ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha BrukmanFilePath FilePath::RemoveFileName() const {
15857240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer  const char* const last_sep = FindLastPathSeparator();
159190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer  String dir;
160190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer  if (last_sep) {
161190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer    dir = String(c_str(), last_sep + 1 - c_str());
162190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer  } else {
163190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer    dir = kCurrentDirectoryString;
164190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer  }
165190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer  return FilePath(dir);
1667ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman}
1677ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
1687ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Helper functions for naming files in a directory for xml output.
1697ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
1707ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Given directory = "dir", base_name = "test", number = 0,
1717ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// extension = "xml", returns "dir/test.xml". If number is greater
1727ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// than zero (e.g., 12), returns "dir/test_12.xml".
1737ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// On Windows platform, uses \ as the separator rather than /.
1747ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha BrukmanFilePath FilePath::MakeFileName(const FilePath& directory,
1757ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman                                const FilePath& base_name,
1767ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman                                int number,
1777ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman                                const char* extension) {
178190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer  String file;
179190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer  if (number == 0) {
180190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer    file = String::Format("%s.%s", base_name.c_str(), extension);
181190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer  } else {
182190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer    file = String::Format("%s_%d.%s", base_name.c_str(), number, extension);
183190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer  }
184190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer  return ConcatPaths(directory, FilePath(file));
185e4b9c93fc1b531fe0cfe25a042f6b81c1e7c15c0Benjamin Kramer}
186e4b9c93fc1b531fe0cfe25a042f6b81c1e7c15c0Benjamin Kramer
187e4b9c93fc1b531fe0cfe25a042f6b81c1e7c15c0Benjamin Kramer// Given directory = "dir", relative_path = "test.xml", returns "dir/test.xml".
188e4b9c93fc1b531fe0cfe25a042f6b81c1e7c15c0Benjamin Kramer// On Windows, uses \ as the separator rather than /.
189e4b9c93fc1b531fe0cfe25a042f6b81c1e7c15c0Benjamin KramerFilePath FilePath::ConcatPaths(const FilePath& directory,
190e4b9c93fc1b531fe0cfe25a042f6b81c1e7c15c0Benjamin Kramer                               const FilePath& relative_path) {
191e4b9c93fc1b531fe0cfe25a042f6b81c1e7c15c0Benjamin Kramer  if (directory.IsEmpty())
192e4b9c93fc1b531fe0cfe25a042f6b81c1e7c15c0Benjamin Kramer    return relative_path;
193e4b9c93fc1b531fe0cfe25a042f6b81c1e7c15c0Benjamin Kramer  const FilePath dir(directory.RemoveTrailingPathSeparator());
194e4b9c93fc1b531fe0cfe25a042f6b81c1e7c15c0Benjamin Kramer  return FilePath(String::Format("%s%c%s", dir.c_str(), kPathSeparator,
195e4b9c93fc1b531fe0cfe25a042f6b81c1e7c15c0Benjamin Kramer                                 relative_path.c_str()));
1967ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman}
1977ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
1987ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Returns true if pathname describes something findable in the file-system,
1997ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// either a file, directory, or whatever.
2007ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanbool FilePath::FileOrDirectoryExists() const {
201190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer#if GTEST_OS_WINDOWS_MOBILE
2027ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  LPCWSTR unicode = String::AnsiToUtf16(pathname_.c_str());
2037ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  const DWORD attributes = GetFileAttributes(unicode);
2047ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  delete [] unicode;
2057ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  return attributes != kInvalidFileAttributes;
2067ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#else
207190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer  posix::StatStruct file_stat;
208190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer  return posix::Stat(pathname_.c_str(), &file_stat) == 0;
209190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer#endif  // GTEST_OS_WINDOWS_MOBILE
2107ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman}
2117ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
2127ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Returns true if pathname describes a directory in the file-system
2137ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// that exists.
2147ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanbool FilePath::DirectoryExists() const {
2157ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  bool result = false;
216e4b9c93fc1b531fe0cfe25a042f6b81c1e7c15c0Benjamin Kramer#if GTEST_OS_WINDOWS
2177ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // Don't strip off trailing separator if path is a root directory on
2187ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // Windows (like "C:\\").
2197ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  const FilePath& path(IsRootDirectory() ? *this :
2207ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman                                           RemoveTrailingPathSeparator());
221190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer#else
222190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer  const FilePath& path(*this);
223190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer#endif
224190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer
225190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer#if GTEST_OS_WINDOWS_MOBILE
2267ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  LPCWSTR unicode = String::AnsiToUtf16(path.c_str());
2277ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  const DWORD attributes = GetFileAttributes(unicode);
2287ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  delete [] unicode;
2297ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  if ((attributes != kInvalidFileAttributes) &&
2307ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman      (attributes & FILE_ATTRIBUTE_DIRECTORY)) {
2317ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman    result = true;
2327ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  }
2337ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#else
234190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer  posix::StatStruct file_stat;
235190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer  result = posix::Stat(path.c_str(), &file_stat) == 0 &&
236190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer      posix::IsDir(file_stat);
237190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer#endif  // GTEST_OS_WINDOWS_MOBILE
238190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer
2397ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  return result;
2407ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman}
2417ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
2427ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Returns true if pathname describes a root directory. (Windows has one
2437ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// root directory per disk drive.)
2447ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanbool FilePath::IsRootDirectory() const {
245e4b9c93fc1b531fe0cfe25a042f6b81c1e7c15c0Benjamin Kramer#if GTEST_OS_WINDOWS
246e4b9c93fc1b531fe0cfe25a042f6b81c1e7c15c0Benjamin Kramer  // TODO(wan@google.com): on Windows a network share like
247e4b9c93fc1b531fe0cfe25a042f6b81c1e7c15c0Benjamin Kramer  // \\server\share can be a root directory, although it cannot be the
248e4b9c93fc1b531fe0cfe25a042f6b81c1e7c15c0Benjamin Kramer  // current directory.  Handle this properly.
249190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer  return pathname_.length() == 3 && IsAbsolutePath();
250e4b9c93fc1b531fe0cfe25a042f6b81c1e7c15c0Benjamin Kramer#else
25157240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer  return pathname_.length() == 1 && IsPathSeparator(pathname_.c_str()[0]);
252e4b9c93fc1b531fe0cfe25a042f6b81c1e7c15c0Benjamin Kramer#endif
253e4b9c93fc1b531fe0cfe25a042f6b81c1e7c15c0Benjamin Kramer}
254e4b9c93fc1b531fe0cfe25a042f6b81c1e7c15c0Benjamin Kramer
255e4b9c93fc1b531fe0cfe25a042f6b81c1e7c15c0Benjamin Kramer// Returns true if pathname describes an absolute path.
256e4b9c93fc1b531fe0cfe25a042f6b81c1e7c15c0Benjamin Kramerbool FilePath::IsAbsolutePath() const {
2577ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  const char* const name = pathname_.c_str();
258e4b9c93fc1b531fe0cfe25a042f6b81c1e7c15c0Benjamin Kramer#if GTEST_OS_WINDOWS
259190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer  return pathname_.length() >= 3 &&
2607ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman     ((name[0] >= 'a' && name[0] <= 'z') ||
2617ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman      (name[0] >= 'A' && name[0] <= 'Z')) &&
2627ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman     name[1] == ':' &&
26357240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer     IsPathSeparator(name[2]);
2647ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#else
26557240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer  return IsPathSeparator(name[0]);
2667ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#endif
2677ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman}
2687ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
2697ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Returns a pathname for a file that does not currently exist. The pathname
2707ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// will be directory/base_name.extension or
2717ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// directory/base_name_<number>.extension if directory/base_name.extension
2727ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// already exists. The number will be incremented until a pathname is found
2737ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// that does not already exist.
2747ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Examples: 'dir/foo_test.xml' or 'dir/foo_test_1.xml'.
2757ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// There could be a race condition if two or more processes are calling this
2767ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// function at the same time -- they could both pick the same filename.
2777ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha BrukmanFilePath FilePath::GenerateUniqueFileName(const FilePath& directory,
2787ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman                                          const FilePath& base_name,
2797ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman                                          const char* extension) {
2807ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  FilePath full_pathname;
2817ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  int number = 0;
2827ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  do {
2837ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman    full_pathname.Set(MakeFileName(directory, base_name, number++, extension));
2847ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  } while (full_pathname.FileOrDirectoryExists());
2857ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  return full_pathname;
2867ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman}
2877ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
2887ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Returns true if FilePath ends with a path separator, which indicates that
2897ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// it is intended to represent a directory. Returns false otherwise.
2907ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// This does NOT check that a directory (or file) actually exists.
2917ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanbool FilePath::IsDirectory() const {
29257240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer  return !pathname_.empty() &&
29357240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer         IsPathSeparator(pathname_.c_str()[pathname_.length() - 1]);
2947ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman}
2957ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
2967ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Create directories so that path exists. Returns true if successful or if
2977ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// the directories already exist; returns false if unable to create directories
2987ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// for any reason.
2997ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanbool FilePath::CreateDirectoriesRecursively() const {
3007ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  if (!this->IsDirectory()) {
3017ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman    return false;
3027ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  }
3037ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
304190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer  if (pathname_.length() == 0 || this->DirectoryExists()) {
3057ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman    return true;
3067ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  }
3077ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
3087ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  const FilePath parent(this->RemoveTrailingPathSeparator().RemoveFileName());
3097ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  return parent.CreateDirectoriesRecursively() && this->CreateFolder();
3107ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman}
3117ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
3127ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Create the directory so that path exists. Returns true if successful or
3137ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// if the directory already exists; returns false if unable to create the
3147ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// directory for any reason, including if the parent directory does not
3157ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// exist. Not named "CreateDirectory" because that's a macro on Windows.
3167ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanbool FilePath::CreateFolder() const {
317190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer#if GTEST_OS_WINDOWS_MOBILE
3187ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  FilePath removed_sep(this->RemoveTrailingPathSeparator());
3197ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  LPCWSTR unicode = String::AnsiToUtf16(removed_sep.c_str());
3207ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  int result = CreateDirectory(unicode, NULL) ? 0 : -1;
3217ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  delete [] unicode;
322190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer#elif GTEST_OS_WINDOWS
3237ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  int result = _mkdir(pathname_.c_str());
3247ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#else
3257ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  int result = mkdir(pathname_.c_str(), 0777);
326190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer#endif  // GTEST_OS_WINDOWS_MOBILE
327190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer
3287ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  if (result == -1) {
3297ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman    return this->DirectoryExists();  // An error is OK if the directory exists.
3307ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  }
3317ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  return true;  // No error.
3327ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman}
3337ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
3347ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// If input name has a trailing separator character, remove it and return the
3357ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// name, otherwise return the name string unmodified.
3367ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// On Windows platform, uses \ as the separator, other platforms use /.
3377ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha BrukmanFilePath FilePath::RemoveTrailingPathSeparator() const {
33857240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer  return IsDirectory()
339190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer      ? FilePath(String(pathname_.c_str(), pathname_.length() - 1))
3407ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman      : *this;
3417ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman}
3427ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
34357240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer// Removes any redundant separators that might be in the pathname.
3447ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// For example, "bar///foo" becomes "bar/foo". Does not eliminate other
3457ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// redundancies that might be in a pathname involving "." or "..".
34657240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer// TODO(wan@google.com): handle Windows network shares (e.g. \\server\share).
3477ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanvoid FilePath::Normalize() {
3487ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  if (pathname_.c_str() == NULL) {
3497ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman    pathname_ = "";
3507ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman    return;
3517ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  }
3527ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  const char* src = pathname_.c_str();
353190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer  char* const dest = new char[pathname_.length() + 1];
3547ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  char* dest_ptr = dest;
355190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer  memset(dest_ptr, 0, pathname_.length() + 1);
3567ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
3577ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  while (*src != '\0') {
35857240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer    *dest_ptr = *src;
35957240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer    if (!IsPathSeparator(*src)) {
3607ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman      src++;
36157240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer    } else {
36257240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer#if GTEST_HAS_ALT_PATH_SEP_
36357240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer      if (*dest_ptr == kAlternatePathSeparator) {
36457240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer        *dest_ptr = kPathSeparator;
36557240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer      }
36657240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer#endif
36757240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer      while (IsPathSeparator(*src))
3687ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman        src++;
36957240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer    }
37057240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer    dest_ptr++;
3717ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  }
3727ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  *dest_ptr = '\0';
3737ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  pathname_ = dest;
3747ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  delete[] dest;
3757ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman}
3767ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
3777ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman}  // namespace internal
3787ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman}  // namespace testing
379