1b89a7cc2SEnji Cooper // Copyright 2008, Google Inc. 2b89a7cc2SEnji Cooper // All rights reserved. 3b89a7cc2SEnji Cooper // 4b89a7cc2SEnji Cooper // Redistribution and use in source and binary forms, with or without 5b89a7cc2SEnji Cooper // modification, are permitted provided that the following conditions are 6b89a7cc2SEnji Cooper // met: 7b89a7cc2SEnji Cooper // 8b89a7cc2SEnji Cooper // * Redistributions of source code must retain the above copyright 9b89a7cc2SEnji Cooper // notice, this list of conditions and the following disclaimer. 10b89a7cc2SEnji Cooper // * Redistributions in binary form must reproduce the above 11b89a7cc2SEnji Cooper // copyright notice, this list of conditions and the following disclaimer 12b89a7cc2SEnji Cooper // in the documentation and/or other materials provided with the 13b89a7cc2SEnji Cooper // distribution. 14b89a7cc2SEnji Cooper // * Neither the name of Google Inc. nor the names of its 15b89a7cc2SEnji Cooper // contributors may be used to endorse or promote products derived from 16b89a7cc2SEnji Cooper // this software without specific prior written permission. 17b89a7cc2SEnji Cooper // 18b89a7cc2SEnji Cooper // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19b89a7cc2SEnji Cooper // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20b89a7cc2SEnji Cooper // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21b89a7cc2SEnji Cooper // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22b89a7cc2SEnji Cooper // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23b89a7cc2SEnji Cooper // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24b89a7cc2SEnji Cooper // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25b89a7cc2SEnji Cooper // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26b89a7cc2SEnji Cooper // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27b89a7cc2SEnji Cooper // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28b89a7cc2SEnji Cooper // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2928f6c2f2SEnji Cooper 30b89a7cc2SEnji Cooper // Google Test filepath utilities 31b89a7cc2SEnji Cooper // 32b89a7cc2SEnji Cooper // This header file declares classes and functions used internally by 33b89a7cc2SEnji Cooper // Google Test. They are subject to change without notice. 34b89a7cc2SEnji Cooper // 35b89a7cc2SEnji Cooper // This file is #included in gtest/internal/gtest-internal.h. 36b89a7cc2SEnji Cooper // Do not include this header file separately! 37b89a7cc2SEnji Cooper 3828f6c2f2SEnji Cooper // IWYU pragma: private, include "gtest/gtest.h" 3928f6c2f2SEnji Cooper // IWYU pragma: friend gtest/.* 4028f6c2f2SEnji Cooper // IWYU pragma: friend gmock/.* 41b89a7cc2SEnji Cooper 4228f6c2f2SEnji Cooper #ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_ 4328f6c2f2SEnji Cooper #define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_ 44b89a7cc2SEnji Cooper 4528f6c2f2SEnji Cooper #include <string> 4628f6c2f2SEnji Cooper 4728f6c2f2SEnji Cooper #include "gtest/internal/gtest-port.h" 48b89a7cc2SEnji Cooper #include "gtest/internal/gtest-string.h" 49b89a7cc2SEnji Cooper 50b89a7cc2SEnji Cooper GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \ 51b89a7cc2SEnji Cooper /* class A needs to have dll-interface to be used by clients of class B */) 52b89a7cc2SEnji Cooper 5328f6c2f2SEnji Cooper #if GTEST_HAS_FILE_SYSTEM 5428f6c2f2SEnji Cooper 55b89a7cc2SEnji Cooper namespace testing { 56b89a7cc2SEnji Cooper namespace internal { 57b89a7cc2SEnji Cooper 58b89a7cc2SEnji Cooper // FilePath - a class for file and directory pathname manipulation which 59b89a7cc2SEnji Cooper // handles platform-specific conventions (like the pathname separator). 60b89a7cc2SEnji Cooper // Used for helper functions for naming files in a directory for xml output. 61b89a7cc2SEnji Cooper // Except for Set methods, all methods are const or static, which provides an 62b89a7cc2SEnji Cooper // "immutable value object" -- useful for peace of mind. 63b89a7cc2SEnji Cooper // A FilePath with a value ending in a path separator ("like/this/") represents 64b89a7cc2SEnji Cooper // a directory, otherwise it is assumed to represent a file. In either case, 65b89a7cc2SEnji Cooper // it may or may not represent an actual file or directory in the file system. 66b89a7cc2SEnji Cooper // Names are NOT checked for syntax correctness -- no checking for illegal 67b89a7cc2SEnji Cooper // characters, malformed paths, etc. 68b89a7cc2SEnji Cooper 69b89a7cc2SEnji Cooper class GTEST_API_ FilePath { 70b89a7cc2SEnji Cooper public: FilePath()71b89a7cc2SEnji Cooper FilePath() : pathname_("") {} FilePath(const FilePath & rhs)72b89a7cc2SEnji Cooper FilePath(const FilePath& rhs) : pathname_(rhs.pathname_) {} 73b89a7cc2SEnji Cooper FilePath(const std::string & pathname)74b89a7cc2SEnji Cooper explicit FilePath(const std::string& pathname) : pathname_(pathname) { 75b89a7cc2SEnji Cooper Normalize(); 76b89a7cc2SEnji Cooper } 77b89a7cc2SEnji Cooper 78b89a7cc2SEnji Cooper FilePath& operator=(const FilePath& rhs) { 79b89a7cc2SEnji Cooper Set(rhs); 80b89a7cc2SEnji Cooper return *this; 81b89a7cc2SEnji Cooper } 82b89a7cc2SEnji Cooper Set(const FilePath & rhs)8328f6c2f2SEnji Cooper void Set(const FilePath& rhs) { pathname_ = rhs.pathname_; } 84b89a7cc2SEnji Cooper string()85b89a7cc2SEnji Cooper const std::string& string() const { return pathname_; } c_str()86b89a7cc2SEnji Cooper const char* c_str() const { return pathname_.c_str(); } 87b89a7cc2SEnji Cooper 88b89a7cc2SEnji Cooper // Returns the current working directory, or "" if unsuccessful. 89b89a7cc2SEnji Cooper static FilePath GetCurrentDir(); 90b89a7cc2SEnji Cooper 91b89a7cc2SEnji Cooper // Given directory = "dir", base_name = "test", number = 0, 92b89a7cc2SEnji Cooper // extension = "xml", returns "dir/test.xml". If number is greater 93b89a7cc2SEnji Cooper // than zero (e.g., 12), returns "dir/test_12.xml". 94b89a7cc2SEnji Cooper // On Windows platform, uses \ as the separator rather than /. 95b89a7cc2SEnji Cooper static FilePath MakeFileName(const FilePath& directory, 9628f6c2f2SEnji Cooper const FilePath& base_name, int number, 97b89a7cc2SEnji Cooper const char* extension); 98b89a7cc2SEnji Cooper 99b89a7cc2SEnji Cooper // Given directory = "dir", relative_path = "test.xml", 100b89a7cc2SEnji Cooper // returns "dir/test.xml". 101b89a7cc2SEnji Cooper // On Windows, uses \ as the separator rather than /. 102b89a7cc2SEnji Cooper static FilePath ConcatPaths(const FilePath& directory, 103b89a7cc2SEnji Cooper const FilePath& relative_path); 104b89a7cc2SEnji Cooper 105b89a7cc2SEnji Cooper // Returns a pathname for a file that does not currently exist. The pathname 106b89a7cc2SEnji Cooper // will be directory/base_name.extension or 107b89a7cc2SEnji Cooper // directory/base_name_<number>.extension if directory/base_name.extension 108b89a7cc2SEnji Cooper // already exists. The number will be incremented until a pathname is found 109b89a7cc2SEnji Cooper // that does not already exist. 110b89a7cc2SEnji Cooper // Examples: 'dir/foo_test.xml' or 'dir/foo_test_1.xml'. 111b89a7cc2SEnji Cooper // There could be a race condition if two or more processes are calling this 112b89a7cc2SEnji Cooper // function at the same time -- they could both pick the same filename. 113b89a7cc2SEnji Cooper static FilePath GenerateUniqueFileName(const FilePath& directory, 114b89a7cc2SEnji Cooper const FilePath& base_name, 115b89a7cc2SEnji Cooper const char* extension); 116b89a7cc2SEnji Cooper 11728f6c2f2SEnji Cooper // Returns true if and only if the path is "". IsEmpty()118b89a7cc2SEnji Cooper bool IsEmpty() const { return pathname_.empty(); } 119b89a7cc2SEnji Cooper 120b89a7cc2SEnji Cooper // If input name has a trailing separator character, removes it and returns 121b89a7cc2SEnji Cooper // the name, otherwise return the name string unmodified. 122b89a7cc2SEnji Cooper // On Windows platform, uses \ as the separator, other platforms use /. 123b89a7cc2SEnji Cooper FilePath RemoveTrailingPathSeparator() const; 124b89a7cc2SEnji Cooper 125b89a7cc2SEnji Cooper // Returns a copy of the FilePath with the directory part removed. 126b89a7cc2SEnji Cooper // Example: FilePath("path/to/file").RemoveDirectoryName() returns 127b89a7cc2SEnji Cooper // FilePath("file"). If there is no directory part ("just_a_file"), it returns 128b89a7cc2SEnji Cooper // the FilePath unmodified. If there is no file part ("just_a_dir/") it 129b89a7cc2SEnji Cooper // returns an empty FilePath (""). 130b89a7cc2SEnji Cooper // On Windows platform, '\' is the path separator, otherwise it is '/'. 131b89a7cc2SEnji Cooper FilePath RemoveDirectoryName() const; 132b89a7cc2SEnji Cooper 133b89a7cc2SEnji Cooper // RemoveFileName returns the directory path with the filename removed. 134b89a7cc2SEnji Cooper // Example: FilePath("path/to/file").RemoveFileName() returns "path/to/". 135b89a7cc2SEnji Cooper // If the FilePath is "a_file" or "/a_file", RemoveFileName returns 136b89a7cc2SEnji Cooper // FilePath("./") or, on Windows, FilePath(".\\"). If the filepath does 137b89a7cc2SEnji Cooper // not have a file, like "just/a/dir/", it returns the FilePath unmodified. 138b89a7cc2SEnji Cooper // On Windows platform, '\' is the path separator, otherwise it is '/'. 139b89a7cc2SEnji Cooper FilePath RemoveFileName() const; 140b89a7cc2SEnji Cooper 141b89a7cc2SEnji Cooper // Returns a copy of the FilePath with the case-insensitive extension removed. 142b89a7cc2SEnji Cooper // Example: FilePath("dir/file.exe").RemoveExtension("EXE") returns 143b89a7cc2SEnji Cooper // FilePath("dir/file"). If a case-insensitive extension is not 144b89a7cc2SEnji Cooper // found, returns a copy of the original FilePath. 145b89a7cc2SEnji Cooper FilePath RemoveExtension(const char* extension) const; 146b89a7cc2SEnji Cooper 147b89a7cc2SEnji Cooper // Creates directories so that path exists. Returns true if successful or if 148b89a7cc2SEnji Cooper // the directories already exist; returns false if unable to create 149b89a7cc2SEnji Cooper // directories for any reason. Will also return false if the FilePath does 150b89a7cc2SEnji Cooper // not represent a directory (that is, it doesn't end with a path separator). 151b89a7cc2SEnji Cooper bool CreateDirectoriesRecursively() const; 152b89a7cc2SEnji Cooper 153b89a7cc2SEnji Cooper // Create the directory so that path exists. Returns true if successful or 154b89a7cc2SEnji Cooper // if the directory already exists; returns false if unable to create the 155b89a7cc2SEnji Cooper // directory for any reason, including if the parent directory does not 156b89a7cc2SEnji Cooper // exist. Not named "CreateDirectory" because that's a macro on Windows. 157b89a7cc2SEnji Cooper bool CreateFolder() const; 158b89a7cc2SEnji Cooper 159b89a7cc2SEnji Cooper // Returns true if FilePath describes something in the file-system, 160b89a7cc2SEnji Cooper // either a file, directory, or whatever, and that something exists. 161b89a7cc2SEnji Cooper bool FileOrDirectoryExists() const; 162b89a7cc2SEnji Cooper 163b89a7cc2SEnji Cooper // Returns true if pathname describes a directory in the file-system 164b89a7cc2SEnji Cooper // that exists. 165b89a7cc2SEnji Cooper bool DirectoryExists() const; 166b89a7cc2SEnji Cooper 167b89a7cc2SEnji Cooper // Returns true if FilePath ends with a path separator, which indicates that 168b89a7cc2SEnji Cooper // it is intended to represent a directory. Returns false otherwise. 169b89a7cc2SEnji Cooper // This does NOT check that a directory (or file) actually exists. 170b89a7cc2SEnji Cooper bool IsDirectory() const; 171b89a7cc2SEnji Cooper 172b89a7cc2SEnji Cooper // Returns true if pathname describes a root directory. (Windows has one 173b89a7cc2SEnji Cooper // root directory per disk drive.) 174b89a7cc2SEnji Cooper bool IsRootDirectory() const; 175b89a7cc2SEnji Cooper 176b89a7cc2SEnji Cooper // Returns true if pathname describes an absolute path. 177b89a7cc2SEnji Cooper bool IsAbsolutePath() const; 178b89a7cc2SEnji Cooper 179b89a7cc2SEnji Cooper private: 180b89a7cc2SEnji Cooper // Replaces multiple consecutive separators with a single separator. 181b89a7cc2SEnji Cooper // For example, "bar///foo" becomes "bar/foo". Does not eliminate other 182b89a7cc2SEnji Cooper // redundancies that might be in a pathname involving "." or "..". 183b89a7cc2SEnji Cooper // 184b89a7cc2SEnji Cooper // A pathname with multiple consecutive separators may occur either through 185b89a7cc2SEnji Cooper // user error or as a result of some scripts or APIs that generate a pathname 186b89a7cc2SEnji Cooper // with a trailing separator. On other platforms the same API or script 187b89a7cc2SEnji Cooper // may NOT generate a pathname with a trailing "/". Then elsewhere that 188b89a7cc2SEnji Cooper // pathname may have another "/" and pathname components added to it, 189b89a7cc2SEnji Cooper // without checking for the separator already being there. 190b89a7cc2SEnji Cooper // The script language and operating system may allow paths like "foo//bar" 191b89a7cc2SEnji Cooper // but some of the functions in FilePath will not handle that correctly. In 192b89a7cc2SEnji Cooper // particular, RemoveTrailingPathSeparator() only removes one separator, and 193b89a7cc2SEnji Cooper // it is called in CreateDirectoriesRecursively() assuming that it will change 194b89a7cc2SEnji Cooper // a pathname from directory syntax (trailing separator) to filename syntax. 195b89a7cc2SEnji Cooper // 196b89a7cc2SEnji Cooper // On Windows this method also replaces the alternate path separator '/' with 197b89a7cc2SEnji Cooper // the primary path separator '\\', so that for example "bar\\/\\foo" becomes 198b89a7cc2SEnji Cooper // "bar\\foo". 199b89a7cc2SEnji Cooper 200b89a7cc2SEnji Cooper void Normalize(); 201b89a7cc2SEnji Cooper 20228f6c2f2SEnji Cooper // Returns a pointer to the last occurrence of a valid path separator in 203b89a7cc2SEnji Cooper // the FilePath. On Windows, for example, both '/' and '\' are valid path 204b89a7cc2SEnji Cooper // separators. Returns NULL if no path separator was found. 205b89a7cc2SEnji Cooper const char* FindLastPathSeparator() const; 206b89a7cc2SEnji Cooper 20728f6c2f2SEnji Cooper // Returns the length of the path root, including the directory separator at 20828f6c2f2SEnji Cooper // the end of the prefix. Returns zero by definition if the path is relative. 20928f6c2f2SEnji Cooper // Examples: 21028f6c2f2SEnji Cooper // - [Windows] "..\Sibling" => 0 21128f6c2f2SEnji Cooper // - [Windows] "\Windows" => 1 21228f6c2f2SEnji Cooper // - [Windows] "C:/Windows\Notepad.exe" => 3 21328f6c2f2SEnji Cooper // - [Windows] "\\Host\Share\C$/Windows" => 13 21428f6c2f2SEnji Cooper // - [UNIX] "/bin" => 1 21528f6c2f2SEnji Cooper size_t CalculateRootLength() const; 21628f6c2f2SEnji Cooper 217b89a7cc2SEnji Cooper std::string pathname_; 218b89a7cc2SEnji Cooper }; // class FilePath 219b89a7cc2SEnji Cooper 220b89a7cc2SEnji Cooper } // namespace internal 221b89a7cc2SEnji Cooper } // namespace testing 222b89a7cc2SEnji Cooper 223b89a7cc2SEnji Cooper GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251 224b89a7cc2SEnji Cooper 22528f6c2f2SEnji Cooper #endif // GTEST_HAS_FILE_SYSTEM 22628f6c2f2SEnji Cooper 22728f6c2f2SEnji Cooper #endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_ 228