1f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Copyright 2008, Google Inc. 2f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// All rights reserved. 3f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 4f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Redistribution and use in source and binary forms, with or without 5f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// modification, are permitted provided that the following conditions are 6f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// met: 7f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 8f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// * Redistributions of source code must retain the above copyright 9f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// notice, this list of conditions and the following disclaimer. 10f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// * Redistributions in binary form must reproduce the above 11f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// copyright notice, this list of conditions and the following disclaimer 12f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// in the documentation and/or other materials provided with the 13f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// distribution. 14f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// * Neither the name of Google Inc. nor the names of its 15f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// contributors may be used to endorse or promote products derived from 16f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// this software without specific prior written permission. 17f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 18f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 30f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Authors: keith.ray@gmail.com (Keith Ray) 31f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 32f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "gtest/internal/gtest-filepath.h" 33f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "gtest/internal/gtest-port.h" 34f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 35f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include <stdlib.h> 36f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 37f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_OS_WINDOWS_MOBILE 38f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# include <windows.h> 39f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#elif GTEST_OS_WINDOWS 40f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# include <direct.h> 41f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# include <io.h> 42f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#elif GTEST_OS_SYMBIAN || GTEST_OS_NACL 43f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Symbian OpenC and NaCl have PATH_MAX in sys/syslimits.h 44f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# include <sys/syslimits.h> 45f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#else 46f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# include <limits.h> 47f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# include <climits> // Some Linux distributions define PATH_MAX here. 48f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif // GTEST_OS_WINDOWS_MOBILE 49f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 50f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_OS_WINDOWS 51f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# define GTEST_PATH_MAX_ _MAX_PATH 52f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#elif defined(PATH_MAX) 53f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# define GTEST_PATH_MAX_ PATH_MAX 54f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#elif defined(_XOPEN_PATH_MAX) 55f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# define GTEST_PATH_MAX_ _XOPEN_PATH_MAX 56f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#else 57f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# define GTEST_PATH_MAX_ _POSIX_PATH_MAX 58f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif // GTEST_OS_WINDOWS 59f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 60f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "gtest/internal/gtest-string.h" 61f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 62f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgnamespace testing { 63f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgnamespace internal { 64f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 65f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_OS_WINDOWS 66f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// On Windows, '\\' is the standard path separator, but many tools and the 67f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Windows API also accept '/' as an alternate path separator. Unless otherwise 68f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// noted, a file path can contain either kind of path separators, or a mixture 69f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// of them. 70f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgconst char kPathSeparator = '\\'; 71f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgconst char kAlternatePathSeparator = '/'; 72f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgconst char kPathSeparatorString[] = "\\"; 73f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgconst char kAlternatePathSeparatorString[] = "/"; 74f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# if GTEST_OS_WINDOWS_MOBILE 75f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Windows CE doesn't have a current directory. You should not use 76f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// the current directory in tests on Windows CE, but this at least 77f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// provides a reasonable fallback. 78f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgconst char kCurrentDirectoryString[] = "\\"; 79f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Windows CE doesn't define INVALID_FILE_ATTRIBUTES 80f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgconst DWORD kInvalidFileAttributes = 0xffffffff; 81f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# else 82f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgconst char kCurrentDirectoryString[] = ".\\"; 83f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# endif // GTEST_OS_WINDOWS_MOBILE 84f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#else 85f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgconst char kPathSeparator = '/'; 86f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgconst char kPathSeparatorString[] = "/"; 87f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgconst char kCurrentDirectoryString[] = "./"; 88f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif // GTEST_OS_WINDOWS 89f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 90f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns whether the given character is a valid path separator. 91f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic bool IsPathSeparator(char c) { 92f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_HAS_ALT_PATH_SEP_ 93f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return (c == kPathSeparator) || (c == kAlternatePathSeparator); 94f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#else 95f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return c == kPathSeparator; 96f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif 97f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 98f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 99f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns the current working directory, or "" if unsuccessful. 100f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgFilePath FilePath::GetCurrentDir() { 101f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_OS_WINDOWS_MOBILE 102f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Windows CE doesn't have a current directory, so we just return 103f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // something reasonable. 104f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return FilePath(kCurrentDirectoryString); 105f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#elif GTEST_OS_WINDOWS 106f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org char cwd[GTEST_PATH_MAX_ + 1] = { '\0' }; 107f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return FilePath(_getcwd(cwd, sizeof(cwd)) == NULL ? "" : cwd); 108f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#else 109f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org char cwd[GTEST_PATH_MAX_ + 1] = { '\0' }; 110f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return FilePath(getcwd(cwd, sizeof(cwd)) == NULL ? "" : cwd); 111f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif // GTEST_OS_WINDOWS_MOBILE 112f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 113f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 114f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns a copy of the FilePath with the case-insensitive extension removed. 115f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Example: FilePath("dir/file.exe").RemoveExtension("EXE") returns 116f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// FilePath("dir/file"). If a case-insensitive extension is not 117f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// found, returns a copy of the original FilePath. 118f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgFilePath FilePath::RemoveExtension(const char* extension) const { 119f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org String dot_extension(String::Format(".%s", extension)); 120f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (pathname_.EndsWithCaseInsensitive(dot_extension.c_str())) { 121f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return FilePath(String(pathname_.c_str(), pathname_.length() - 4)); 122f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 123f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return *this; 124f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 125f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 126f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns a pointer to the last occurence of a valid path separator in 127f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// the FilePath. On Windows, for example, both '/' and '\' are valid path 128f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// separators. Returns NULL if no path separator was found. 129f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgconst char* FilePath::FindLastPathSeparator() const { 130f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* const last_sep = strrchr(c_str(), kPathSeparator); 131f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_HAS_ALT_PATH_SEP_ 132f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* const last_alt_sep = strrchr(c_str(), kAlternatePathSeparator); 133f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Comparing two pointers of which only one is NULL is undefined. 134f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (last_alt_sep != NULL && 135f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org (last_sep == NULL || last_alt_sep > last_sep)) { 136f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return last_alt_sep; 137f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 138f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif 139f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return last_sep; 140f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 141f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 142f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns a copy of the FilePath with the directory part removed. 143f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Example: FilePath("path/to/file").RemoveDirectoryName() returns 144f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// FilePath("file"). If there is no directory part ("just_a_file"), it returns 145f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// the FilePath unmodified. If there is no file part ("just_a_dir/") it 146f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// returns an empty FilePath (""). 147f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// On Windows platform, '\' is the path separator, otherwise it is '/'. 148f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgFilePath FilePath::RemoveDirectoryName() const { 149f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* const last_sep = FindLastPathSeparator(); 150f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return last_sep ? FilePath(String(last_sep + 1)) : *this; 151f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 152f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 153f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// RemoveFileName returns the directory path with the filename removed. 154f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Example: FilePath("path/to/file").RemoveFileName() returns "path/to/". 155f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// If the FilePath is "a_file" or "/a_file", RemoveFileName returns 156f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// FilePath("./") or, on Windows, FilePath(".\\"). If the filepath does 157f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// not have a file, like "just/a/dir/", it returns the FilePath unmodified. 158f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// On Windows platform, '\' is the path separator, otherwise it is '/'. 159f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgFilePath FilePath::RemoveFileName() const { 160f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* const last_sep = FindLastPathSeparator(); 161f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org String dir; 162f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (last_sep) { 163f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org dir = String(c_str(), last_sep + 1 - c_str()); 164f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } else { 165f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org dir = kCurrentDirectoryString; 166f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 167f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return FilePath(dir); 168f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 169f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 170f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Helper functions for naming files in a directory for xml output. 171f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 172f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Given directory = "dir", base_name = "test", number = 0, 173f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// extension = "xml", returns "dir/test.xml". If number is greater 174f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// than zero (e.g., 12), returns "dir/test_12.xml". 175f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// On Windows platform, uses \ as the separator rather than /. 176f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgFilePath FilePath::MakeFileName(const FilePath& directory, 177f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const FilePath& base_name, 178f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org int number, 179f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* extension) { 180f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org String file; 181f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (number == 0) { 182f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org file = String::Format("%s.%s", base_name.c_str(), extension); 183f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } else { 184f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org file = String::Format("%s_%d.%s", base_name.c_str(), number, extension); 185f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 186f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return ConcatPaths(directory, FilePath(file)); 187f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 188f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 189f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Given directory = "dir", relative_path = "test.xml", returns "dir/test.xml". 190f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// On Windows, uses \ as the separator rather than /. 191f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgFilePath FilePath::ConcatPaths(const FilePath& directory, 192f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const FilePath& relative_path) { 193f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (directory.IsEmpty()) 194f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return relative_path; 195f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const FilePath dir(directory.RemoveTrailingPathSeparator()); 196f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return FilePath(String::Format("%s%c%s", dir.c_str(), kPathSeparator, 197f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org relative_path.c_str())); 198f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 199f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 200f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns true if pathname describes something findable in the file-system, 201f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// either a file, directory, or whatever. 202f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgbool FilePath::FileOrDirectoryExists() const { 203f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_OS_WINDOWS_MOBILE 204f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org LPCWSTR unicode = String::AnsiToUtf16(pathname_.c_str()); 205f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const DWORD attributes = GetFileAttributes(unicode); 206f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org delete [] unicode; 207f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return attributes != kInvalidFileAttributes; 208f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#else 209f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org posix::StatStruct file_stat; 210f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return posix::Stat(pathname_.c_str(), &file_stat) == 0; 211f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif // GTEST_OS_WINDOWS_MOBILE 212f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 213f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 214f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns true if pathname describes a directory in the file-system 215f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// that exists. 216f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgbool FilePath::DirectoryExists() const { 217f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org bool result = false; 218f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_OS_WINDOWS 219f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Don't strip off trailing separator if path is a root directory on 220f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Windows (like "C:\\"). 221f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const FilePath& path(IsRootDirectory() ? *this : 222f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org RemoveTrailingPathSeparator()); 223f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#else 224f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const FilePath& path(*this); 225f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif 226f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 227f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_OS_WINDOWS_MOBILE 228f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org LPCWSTR unicode = String::AnsiToUtf16(path.c_str()); 229f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const DWORD attributes = GetFileAttributes(unicode); 230f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org delete [] unicode; 231f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if ((attributes != kInvalidFileAttributes) && 232f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org (attributes & FILE_ATTRIBUTE_DIRECTORY)) { 233f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org result = true; 234f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 235f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#else 236f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org posix::StatStruct file_stat; 237f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org result = posix::Stat(path.c_str(), &file_stat) == 0 && 238f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org posix::IsDir(file_stat); 239f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif // GTEST_OS_WINDOWS_MOBILE 240f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 241f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return result; 242f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 243f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 244f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns true if pathname describes a root directory. (Windows has one 245f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// root directory per disk drive.) 246f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgbool FilePath::IsRootDirectory() const { 247f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_OS_WINDOWS 248f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // TODO(wan@google.com): on Windows a network share like 249f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // \\server\share can be a root directory, although it cannot be the 250f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // current directory. Handle this properly. 251f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return pathname_.length() == 3 && IsAbsolutePath(); 252f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#else 253f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return pathname_.length() == 1 && IsPathSeparator(pathname_.c_str()[0]); 254f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif 255f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 256f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 257f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns true if pathname describes an absolute path. 258f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgbool FilePath::IsAbsolutePath() const { 259f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* const name = pathname_.c_str(); 260f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_OS_WINDOWS 261f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return pathname_.length() >= 3 && 262f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ((name[0] >= 'a' && name[0] <= 'z') || 263f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org (name[0] >= 'A' && name[0] <= 'Z')) && 264f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org name[1] == ':' && 265f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org IsPathSeparator(name[2]); 266f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#else 267f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return IsPathSeparator(name[0]); 268f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif 269f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 270f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 271f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns a pathname for a file that does not currently exist. The pathname 272f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// will be directory/base_name.extension or 273f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// directory/base_name_<number>.extension if directory/base_name.extension 274f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// already exists. The number will be incremented until a pathname is found 275f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// that does not already exist. 276f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Examples: 'dir/foo_test.xml' or 'dir/foo_test_1.xml'. 277f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// There could be a race condition if two or more processes are calling this 278f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// function at the same time -- they could both pick the same filename. 279f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgFilePath FilePath::GenerateUniqueFileName(const FilePath& directory, 280f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const FilePath& base_name, 281f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* extension) { 282f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org FilePath full_pathname; 283f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org int number = 0; 284f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org do { 285f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org full_pathname.Set(MakeFileName(directory, base_name, number++, extension)); 286f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } while (full_pathname.FileOrDirectoryExists()); 287f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return full_pathname; 288f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 289f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 290f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns true if FilePath ends with a path separator, which indicates that 291f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// it is intended to represent a directory. Returns false otherwise. 292f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// This does NOT check that a directory (or file) actually exists. 293f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgbool FilePath::IsDirectory() const { 294f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return !pathname_.empty() && 295f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org IsPathSeparator(pathname_.c_str()[pathname_.length() - 1]); 296f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 297f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 298f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Create directories so that path exists. Returns true if successful or if 299f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// the directories already exist; returns false if unable to create directories 300f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// for any reason. 301f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgbool FilePath::CreateDirectoriesRecursively() const { 302f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (!this->IsDirectory()) { 303f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return false; 304f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 305f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 306f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (pathname_.length() == 0 || this->DirectoryExists()) { 307f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return true; 308f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 309f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 310f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const FilePath parent(this->RemoveTrailingPathSeparator().RemoveFileName()); 311f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return parent.CreateDirectoriesRecursively() && this->CreateFolder(); 312f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 313f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 314f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Create the directory so that path exists. Returns true if successful or 315f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// if the directory already exists; returns false if unable to create the 316f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// directory for any reason, including if the parent directory does not 317f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// exist. Not named "CreateDirectory" because that's a macro on Windows. 318f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgbool FilePath::CreateFolder() const { 319f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_OS_WINDOWS_MOBILE 320f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org FilePath removed_sep(this->RemoveTrailingPathSeparator()); 321f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org LPCWSTR unicode = String::AnsiToUtf16(removed_sep.c_str()); 322f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org int result = CreateDirectory(unicode, NULL) ? 0 : -1; 323f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org delete [] unicode; 324f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#elif GTEST_OS_WINDOWS 325f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org int result = _mkdir(pathname_.c_str()); 326f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#else 327f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org int result = mkdir(pathname_.c_str(), 0777); 328f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif // GTEST_OS_WINDOWS_MOBILE 329f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 330f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (result == -1) { 331f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return this->DirectoryExists(); // An error is OK if the directory exists. 332f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 333f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return true; // No error. 334f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 335f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 336f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// If input name has a trailing separator character, remove it and return the 337f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// name, otherwise return the name string unmodified. 338f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// On Windows platform, uses \ as the separator, other platforms use /. 339f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgFilePath FilePath::RemoveTrailingPathSeparator() const { 340f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return IsDirectory() 341f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ? FilePath(String(pathname_.c_str(), pathname_.length() - 1)) 342f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org : *this; 343f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 344f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 345f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Removes any redundant separators that might be in the pathname. 346f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// For example, "bar///foo" becomes "bar/foo". Does not eliminate other 347f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// redundancies that might be in a pathname involving "." or "..". 348f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// TODO(wan@google.com): handle Windows network shares (e.g. \\server\share). 349f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid FilePath::Normalize() { 350f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (pathname_.c_str() == NULL) { 351f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org pathname_ = ""; 352f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return; 353f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 354f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* src = pathname_.c_str(); 355f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org char* const dest = new char[pathname_.length() + 1]; 356f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org char* dest_ptr = dest; 357f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org memset(dest_ptr, 0, pathname_.length() + 1); 358f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 359f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org while (*src != '\0') { 360f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *dest_ptr = *src; 361f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (!IsPathSeparator(*src)) { 362f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org src++; 363f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } else { 364f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_HAS_ALT_PATH_SEP_ 365f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (*dest_ptr == kAlternatePathSeparator) { 366f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *dest_ptr = kPathSeparator; 367f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 368f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif 369f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org while (IsPathSeparator(*src)) 370f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org src++; 371f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 372f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org dest_ptr++; 373f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 374f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *dest_ptr = '\0'; 375f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org pathname_ = dest; 376f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org delete[] dest; 377f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 378f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 379f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} // namespace internal 380f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} // namespace testing 381