11cb0ef41Sopenharmony_ci// Copyright 2008, Google Inc. 21cb0ef41Sopenharmony_ci// All rights reserved. 31cb0ef41Sopenharmony_ci// 41cb0ef41Sopenharmony_ci// Redistribution and use in source and binary forms, with or without 51cb0ef41Sopenharmony_ci// modification, are permitted provided that the following conditions are 61cb0ef41Sopenharmony_ci// met: 71cb0ef41Sopenharmony_ci// 81cb0ef41Sopenharmony_ci// * Redistributions of source code must retain the above copyright 91cb0ef41Sopenharmony_ci// notice, this list of conditions and the following disclaimer. 101cb0ef41Sopenharmony_ci// * Redistributions in binary form must reproduce the above 111cb0ef41Sopenharmony_ci// copyright notice, this list of conditions and the following disclaimer 121cb0ef41Sopenharmony_ci// in the documentation and/or other materials provided with the 131cb0ef41Sopenharmony_ci// distribution. 141cb0ef41Sopenharmony_ci// * Neither the name of Google Inc. nor the names of its 151cb0ef41Sopenharmony_ci// contributors may be used to endorse or promote products derived from 161cb0ef41Sopenharmony_ci// this software without specific prior written permission. 171cb0ef41Sopenharmony_ci// 181cb0ef41Sopenharmony_ci// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 191cb0ef41Sopenharmony_ci// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 201cb0ef41Sopenharmony_ci// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 211cb0ef41Sopenharmony_ci// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 221cb0ef41Sopenharmony_ci// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 231cb0ef41Sopenharmony_ci// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 241cb0ef41Sopenharmony_ci// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 251cb0ef41Sopenharmony_ci// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 261cb0ef41Sopenharmony_ci// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 271cb0ef41Sopenharmony_ci// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 281cb0ef41Sopenharmony_ci// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ci// Google Test filepath utilities 311cb0ef41Sopenharmony_ci// 321cb0ef41Sopenharmony_ci// This header file declares classes and functions used internally by 331cb0ef41Sopenharmony_ci// Google Test. They are subject to change without notice. 341cb0ef41Sopenharmony_ci// 351cb0ef41Sopenharmony_ci// This file is #included in gtest/internal/gtest-internal.h. 361cb0ef41Sopenharmony_ci// Do not include this header file separately! 371cb0ef41Sopenharmony_ci 381cb0ef41Sopenharmony_ci// IWYU pragma: private, include "gtest/gtest.h" 391cb0ef41Sopenharmony_ci// IWYU pragma: friend gtest/.* 401cb0ef41Sopenharmony_ci// IWYU pragma: friend gmock/.* 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_ci#ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_ 431cb0ef41Sopenharmony_ci#define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_ 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_ci#include <string> 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_ci#include "gtest/internal/gtest-port.h" 481cb0ef41Sopenharmony_ci#include "gtest/internal/gtest-string.h" 491cb0ef41Sopenharmony_ci 501cb0ef41Sopenharmony_ciGTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \ 511cb0ef41Sopenharmony_ci/* class A needs to have dll-interface to be used by clients of class B */) 521cb0ef41Sopenharmony_ci 531cb0ef41Sopenharmony_ci#if GTEST_HAS_FILE_SYSTEM 541cb0ef41Sopenharmony_ci 551cb0ef41Sopenharmony_cinamespace testing { 561cb0ef41Sopenharmony_cinamespace internal { 571cb0ef41Sopenharmony_ci 581cb0ef41Sopenharmony_ci// FilePath - a class for file and directory pathname manipulation which 591cb0ef41Sopenharmony_ci// handles platform-specific conventions (like the pathname separator). 601cb0ef41Sopenharmony_ci// Used for helper functions for naming files in a directory for xml output. 611cb0ef41Sopenharmony_ci// Except for Set methods, all methods are const or static, which provides an 621cb0ef41Sopenharmony_ci// "immutable value object" -- useful for peace of mind. 631cb0ef41Sopenharmony_ci// A FilePath with a value ending in a path separator ("like/this/") represents 641cb0ef41Sopenharmony_ci// a directory, otherwise it is assumed to represent a file. In either case, 651cb0ef41Sopenharmony_ci// it may or may not represent an actual file or directory in the file system. 661cb0ef41Sopenharmony_ci// Names are NOT checked for syntax correctness -- no checking for illegal 671cb0ef41Sopenharmony_ci// characters, malformed paths, etc. 681cb0ef41Sopenharmony_ci 691cb0ef41Sopenharmony_ciclass GTEST_API_ FilePath { 701cb0ef41Sopenharmony_ci public: 711cb0ef41Sopenharmony_ci FilePath() : pathname_("") {} 721cb0ef41Sopenharmony_ci FilePath(const FilePath& rhs) : pathname_(rhs.pathname_) {} 731cb0ef41Sopenharmony_ci 741cb0ef41Sopenharmony_ci explicit FilePath(const std::string& pathname) : pathname_(pathname) { 751cb0ef41Sopenharmony_ci Normalize(); 761cb0ef41Sopenharmony_ci } 771cb0ef41Sopenharmony_ci 781cb0ef41Sopenharmony_ci FilePath& operator=(const FilePath& rhs) { 791cb0ef41Sopenharmony_ci Set(rhs); 801cb0ef41Sopenharmony_ci return *this; 811cb0ef41Sopenharmony_ci } 821cb0ef41Sopenharmony_ci 831cb0ef41Sopenharmony_ci void Set(const FilePath& rhs) { pathname_ = rhs.pathname_; } 841cb0ef41Sopenharmony_ci 851cb0ef41Sopenharmony_ci const std::string& string() const { return pathname_; } 861cb0ef41Sopenharmony_ci const char* c_str() const { return pathname_.c_str(); } 871cb0ef41Sopenharmony_ci 881cb0ef41Sopenharmony_ci // Returns the current working directory, or "" if unsuccessful. 891cb0ef41Sopenharmony_ci static FilePath GetCurrentDir(); 901cb0ef41Sopenharmony_ci 911cb0ef41Sopenharmony_ci // Given directory = "dir", base_name = "test", number = 0, 921cb0ef41Sopenharmony_ci // extension = "xml", returns "dir/test.xml". If number is greater 931cb0ef41Sopenharmony_ci // than zero (e.g., 12), returns "dir/test_12.xml". 941cb0ef41Sopenharmony_ci // On Windows platform, uses \ as the separator rather than /. 951cb0ef41Sopenharmony_ci static FilePath MakeFileName(const FilePath& directory, 961cb0ef41Sopenharmony_ci const FilePath& base_name, int number, 971cb0ef41Sopenharmony_ci const char* extension); 981cb0ef41Sopenharmony_ci 991cb0ef41Sopenharmony_ci // Given directory = "dir", relative_path = "test.xml", 1001cb0ef41Sopenharmony_ci // returns "dir/test.xml". 1011cb0ef41Sopenharmony_ci // On Windows, uses \ as the separator rather than /. 1021cb0ef41Sopenharmony_ci static FilePath ConcatPaths(const FilePath& directory, 1031cb0ef41Sopenharmony_ci const FilePath& relative_path); 1041cb0ef41Sopenharmony_ci 1051cb0ef41Sopenharmony_ci // Returns a pathname for a file that does not currently exist. The pathname 1061cb0ef41Sopenharmony_ci // will be directory/base_name.extension or 1071cb0ef41Sopenharmony_ci // directory/base_name_<number>.extension if directory/base_name.extension 1081cb0ef41Sopenharmony_ci // already exists. The number will be incremented until a pathname is found 1091cb0ef41Sopenharmony_ci // that does not already exist. 1101cb0ef41Sopenharmony_ci // Examples: 'dir/foo_test.xml' or 'dir/foo_test_1.xml'. 1111cb0ef41Sopenharmony_ci // There could be a race condition if two or more processes are calling this 1121cb0ef41Sopenharmony_ci // function at the same time -- they could both pick the same filename. 1131cb0ef41Sopenharmony_ci static FilePath GenerateUniqueFileName(const FilePath& directory, 1141cb0ef41Sopenharmony_ci const FilePath& base_name, 1151cb0ef41Sopenharmony_ci const char* extension); 1161cb0ef41Sopenharmony_ci 1171cb0ef41Sopenharmony_ci // Returns true if and only if the path is "". 1181cb0ef41Sopenharmony_ci bool IsEmpty() const { return pathname_.empty(); } 1191cb0ef41Sopenharmony_ci 1201cb0ef41Sopenharmony_ci // If input name has a trailing separator character, removes it and returns 1211cb0ef41Sopenharmony_ci // the name, otherwise return the name string unmodified. 1221cb0ef41Sopenharmony_ci // On Windows platform, uses \ as the separator, other platforms use /. 1231cb0ef41Sopenharmony_ci FilePath RemoveTrailingPathSeparator() const; 1241cb0ef41Sopenharmony_ci 1251cb0ef41Sopenharmony_ci // Returns a copy of the FilePath with the directory part removed. 1261cb0ef41Sopenharmony_ci // Example: FilePath("path/to/file").RemoveDirectoryName() returns 1271cb0ef41Sopenharmony_ci // FilePath("file"). If there is no directory part ("just_a_file"), it returns 1281cb0ef41Sopenharmony_ci // the FilePath unmodified. If there is no file part ("just_a_dir/") it 1291cb0ef41Sopenharmony_ci // returns an empty FilePath (""). 1301cb0ef41Sopenharmony_ci // On Windows platform, '\' is the path separator, otherwise it is '/'. 1311cb0ef41Sopenharmony_ci FilePath RemoveDirectoryName() const; 1321cb0ef41Sopenharmony_ci 1331cb0ef41Sopenharmony_ci // RemoveFileName returns the directory path with the filename removed. 1341cb0ef41Sopenharmony_ci // Example: FilePath("path/to/file").RemoveFileName() returns "path/to/". 1351cb0ef41Sopenharmony_ci // If the FilePath is "a_file" or "/a_file", RemoveFileName returns 1361cb0ef41Sopenharmony_ci // FilePath("./") or, on Windows, FilePath(".\\"). If the filepath does 1371cb0ef41Sopenharmony_ci // not have a file, like "just/a/dir/", it returns the FilePath unmodified. 1381cb0ef41Sopenharmony_ci // On Windows platform, '\' is the path separator, otherwise it is '/'. 1391cb0ef41Sopenharmony_ci FilePath RemoveFileName() const; 1401cb0ef41Sopenharmony_ci 1411cb0ef41Sopenharmony_ci // Returns a copy of the FilePath with the case-insensitive extension removed. 1421cb0ef41Sopenharmony_ci // Example: FilePath("dir/file.exe").RemoveExtension("EXE") returns 1431cb0ef41Sopenharmony_ci // FilePath("dir/file"). If a case-insensitive extension is not 1441cb0ef41Sopenharmony_ci // found, returns a copy of the original FilePath. 1451cb0ef41Sopenharmony_ci FilePath RemoveExtension(const char* extension) const; 1461cb0ef41Sopenharmony_ci 1471cb0ef41Sopenharmony_ci // Creates directories so that path exists. Returns true if successful or if 1481cb0ef41Sopenharmony_ci // the directories already exist; returns false if unable to create 1491cb0ef41Sopenharmony_ci // directories for any reason. Will also return false if the FilePath does 1501cb0ef41Sopenharmony_ci // not represent a directory (that is, it doesn't end with a path separator). 1511cb0ef41Sopenharmony_ci bool CreateDirectoriesRecursively() const; 1521cb0ef41Sopenharmony_ci 1531cb0ef41Sopenharmony_ci // Create the directory so that path exists. Returns true if successful or 1541cb0ef41Sopenharmony_ci // if the directory already exists; returns false if unable to create the 1551cb0ef41Sopenharmony_ci // directory for any reason, including if the parent directory does not 1561cb0ef41Sopenharmony_ci // exist. Not named "CreateDirectory" because that's a macro on Windows. 1571cb0ef41Sopenharmony_ci bool CreateFolder() const; 1581cb0ef41Sopenharmony_ci 1591cb0ef41Sopenharmony_ci // Returns true if FilePath describes something in the file-system, 1601cb0ef41Sopenharmony_ci // either a file, directory, or whatever, and that something exists. 1611cb0ef41Sopenharmony_ci bool FileOrDirectoryExists() const; 1621cb0ef41Sopenharmony_ci 1631cb0ef41Sopenharmony_ci // Returns true if pathname describes a directory in the file-system 1641cb0ef41Sopenharmony_ci // that exists. 1651cb0ef41Sopenharmony_ci bool DirectoryExists() const; 1661cb0ef41Sopenharmony_ci 1671cb0ef41Sopenharmony_ci // Returns true if FilePath ends with a path separator, which indicates that 1681cb0ef41Sopenharmony_ci // it is intended to represent a directory. Returns false otherwise. 1691cb0ef41Sopenharmony_ci // This does NOT check that a directory (or file) actually exists. 1701cb0ef41Sopenharmony_ci bool IsDirectory() const; 1711cb0ef41Sopenharmony_ci 1721cb0ef41Sopenharmony_ci // Returns true if pathname describes a root directory. (Windows has one 1731cb0ef41Sopenharmony_ci // root directory per disk drive.) 1741cb0ef41Sopenharmony_ci bool IsRootDirectory() const; 1751cb0ef41Sopenharmony_ci 1761cb0ef41Sopenharmony_ci // Returns true if pathname describes an absolute path. 1771cb0ef41Sopenharmony_ci bool IsAbsolutePath() const; 1781cb0ef41Sopenharmony_ci 1791cb0ef41Sopenharmony_ci private: 1801cb0ef41Sopenharmony_ci // Replaces multiple consecutive separators with a single separator. 1811cb0ef41Sopenharmony_ci // For example, "bar///foo" becomes "bar/foo". Does not eliminate other 1821cb0ef41Sopenharmony_ci // redundancies that might be in a pathname involving "." or "..". 1831cb0ef41Sopenharmony_ci // 1841cb0ef41Sopenharmony_ci // A pathname with multiple consecutive separators may occur either through 1851cb0ef41Sopenharmony_ci // user error or as a result of some scripts or APIs that generate a pathname 1861cb0ef41Sopenharmony_ci // with a trailing separator. On other platforms the same API or script 1871cb0ef41Sopenharmony_ci // may NOT generate a pathname with a trailing "/". Then elsewhere that 1881cb0ef41Sopenharmony_ci // pathname may have another "/" and pathname components added to it, 1891cb0ef41Sopenharmony_ci // without checking for the separator already being there. 1901cb0ef41Sopenharmony_ci // The script language and operating system may allow paths like "foo//bar" 1911cb0ef41Sopenharmony_ci // but some of the functions in FilePath will not handle that correctly. In 1921cb0ef41Sopenharmony_ci // particular, RemoveTrailingPathSeparator() only removes one separator, and 1931cb0ef41Sopenharmony_ci // it is called in CreateDirectoriesRecursively() assuming that it will change 1941cb0ef41Sopenharmony_ci // a pathname from directory syntax (trailing separator) to filename syntax. 1951cb0ef41Sopenharmony_ci // 1961cb0ef41Sopenharmony_ci // On Windows this method also replaces the alternate path separator '/' with 1971cb0ef41Sopenharmony_ci // the primary path separator '\\', so that for example "bar\\/\\foo" becomes 1981cb0ef41Sopenharmony_ci // "bar\\foo". 1991cb0ef41Sopenharmony_ci 2001cb0ef41Sopenharmony_ci void Normalize(); 2011cb0ef41Sopenharmony_ci 2021cb0ef41Sopenharmony_ci // Returns a pointer to the last occurrence of a valid path separator in 2031cb0ef41Sopenharmony_ci // the FilePath. On Windows, for example, both '/' and '\' are valid path 2041cb0ef41Sopenharmony_ci // separators. Returns NULL if no path separator was found. 2051cb0ef41Sopenharmony_ci const char* FindLastPathSeparator() const; 2061cb0ef41Sopenharmony_ci 2071cb0ef41Sopenharmony_ci // Returns the length of the path root, including the directory separator at 2081cb0ef41Sopenharmony_ci // the end of the prefix. Returns zero by definition if the path is relative. 2091cb0ef41Sopenharmony_ci // Examples: 2101cb0ef41Sopenharmony_ci // - [Windows] "..\Sibling" => 0 2111cb0ef41Sopenharmony_ci // - [Windows] "\Windows" => 1 2121cb0ef41Sopenharmony_ci // - [Windows] "C:/Windows\Notepad.exe" => 3 2131cb0ef41Sopenharmony_ci // - [Windows] "\\Host\Share\C$/Windows" => 13 2141cb0ef41Sopenharmony_ci // - [UNIX] "/bin" => 1 2151cb0ef41Sopenharmony_ci size_t CalculateRootLength() const; 2161cb0ef41Sopenharmony_ci 2171cb0ef41Sopenharmony_ci std::string pathname_; 2181cb0ef41Sopenharmony_ci}; // class FilePath 2191cb0ef41Sopenharmony_ci 2201cb0ef41Sopenharmony_ci} // namespace internal 2211cb0ef41Sopenharmony_ci} // namespace testing 2221cb0ef41Sopenharmony_ci 2231cb0ef41Sopenharmony_ciGTEST_DISABLE_MSC_WARNINGS_POP_() // 4251 2241cb0ef41Sopenharmony_ci 2251cb0ef41Sopenharmony_ci#endif // GTEST_HAS_FILE_SYSTEM 2261cb0ef41Sopenharmony_ci 2271cb0ef41Sopenharmony_ci#endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_ 228