Path: blob/master/dep/googletest/include/gtest/internal/gtest-filepath.h
4808 views
// Copyright 2008, Google Inc.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above10// copyright notice, this list of conditions and the following disclaimer11// in the documentation and/or other materials provided with the12// distribution.13// * Neither the name of Google Inc. nor the names of its14// contributors may be used to endorse or promote products derived from15// this software without specific prior written permission.16//17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT21// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,22// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT23// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,24// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY25// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT26// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE27// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2829// Google Test filepath utilities30//31// This header file declares classes and functions used internally by32// Google Test. They are subject to change without notice.33//34// This file is #included in gtest/internal/gtest-internal.h.35// Do not include this header file separately!3637// IWYU pragma: private, include "gtest/gtest.h"38// IWYU pragma: friend gtest/.*39// IWYU pragma: friend gmock/.*4041#ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_42#define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_4344#include <string>45#include <utility>4647#include "gtest/internal/gtest-port.h"48#include "gtest/internal/gtest-string.h"4950GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \51/* class A needs to have dll-interface to be used by clients of class B */)5253#if GTEST_HAS_FILE_SYSTEM5455namespace testing {56namespace internal {5758// FilePath - a class for file and directory pathname manipulation which59// handles platform-specific conventions (like the pathname separator).60// Used for helper functions for naming files in a directory for xml output.61// Except for Set methods, all methods are const or static, which provides an62// "immutable value object" -- useful for peace of mind.63// A FilePath with a value ending in a path separator ("like/this/") represents64// a directory, otherwise it is assumed to represent a file. In either case,65// it may or may not represent an actual file or directory in the file system.66// Names are NOT checked for syntax correctness -- no checking for illegal67// characters, malformed paths, etc.6869class GTEST_API_ FilePath {70public:71FilePath() : pathname_("") {}72FilePath(const FilePath& rhs) : pathname_(rhs.pathname_) {}73FilePath(FilePath&& rhs) noexcept : pathname_(std::move(rhs.pathname_)) {}7475explicit FilePath(std::string pathname) : pathname_(std::move(pathname)) {76Normalize();77}7879FilePath& operator=(const FilePath& rhs) {80Set(rhs);81return *this;82}83FilePath& operator=(FilePath&& rhs) noexcept {84pathname_ = std::move(rhs.pathname_);85return *this;86}8788void Set(const FilePath& rhs) { pathname_ = rhs.pathname_; }8990const std::string& string() const { return pathname_; }91const char* c_str() const { return pathname_.c_str(); }9293// Returns the current working directory, or "" if unsuccessful.94static FilePath GetCurrentDir();9596// Given directory = "dir", base_name = "test", number = 0,97// extension = "xml", returns "dir/test.xml". If number is greater98// than zero (e.g., 12), returns "dir/test_12.xml".99// On Windows platform, uses \ as the separator rather than /.100static FilePath MakeFileName(const FilePath& directory,101const FilePath& base_name, int number,102const char* extension);103104// Given directory = "dir", relative_path = "test.xml",105// returns "dir/test.xml".106// On Windows, uses \ as the separator rather than /.107static FilePath ConcatPaths(const FilePath& directory,108const FilePath& relative_path);109110// Returns a pathname for a file that does not currently exist. The pathname111// will be directory/base_name.extension or112// directory/base_name_<number>.extension if directory/base_name.extension113// already exists. The number will be incremented until a pathname is found114// that does not already exist.115// Examples: 'dir/foo_test.xml' or 'dir/foo_test_1.xml'.116// There could be a race condition if two or more processes are calling this117// function at the same time -- they could both pick the same filename.118static FilePath GenerateUniqueFileName(const FilePath& directory,119const FilePath& base_name,120const char* extension);121122// Returns true if and only if the path is "".123bool IsEmpty() const { return pathname_.empty(); }124125// If input name has a trailing separator character, removes it and returns126// the name, otherwise return the name string unmodified.127// On Windows platform, uses \ as the separator, other platforms use /.128FilePath RemoveTrailingPathSeparator() const;129130// Returns a copy of the FilePath with the directory part removed.131// Example: FilePath("path/to/file").RemoveDirectoryName() returns132// FilePath("file"). If there is no directory part ("just_a_file"), it returns133// the FilePath unmodified. If there is no file part ("just_a_dir/") it134// returns an empty FilePath ("").135// On Windows platform, '\' is the path separator, otherwise it is '/'.136FilePath RemoveDirectoryName() const;137138// RemoveFileName returns the directory path with the filename removed.139// Example: FilePath("path/to/file").RemoveFileName() returns "path/to/".140// If the FilePath is "a_file" or "/a_file", RemoveFileName returns141// FilePath("./") or, on Windows, FilePath(".\\"). If the filepath does142// not have a file, like "just/a/dir/", it returns the FilePath unmodified.143// On Windows platform, '\' is the path separator, otherwise it is '/'.144FilePath RemoveFileName() const;145146// Returns a copy of the FilePath with the case-insensitive extension removed.147// Example: FilePath("dir/file.exe").RemoveExtension("EXE") returns148// FilePath("dir/file"). If a case-insensitive extension is not149// found, returns a copy of the original FilePath.150FilePath RemoveExtension(const char* extension) const;151152// Creates directories so that path exists. Returns true if successful or if153// the directories already exist; returns false if unable to create154// directories for any reason. Will also return false if the FilePath does155// not represent a directory (that is, it doesn't end with a path separator).156bool CreateDirectoriesRecursively() const;157158// Create the directory so that path exists. Returns true if successful or159// if the directory already exists; returns false if unable to create the160// directory for any reason, including if the parent directory does not161// exist. Not named "CreateDirectory" because that's a macro on Windows.162bool CreateFolder() const;163164// Returns true if FilePath describes something in the file-system,165// either a file, directory, or whatever, and that something exists.166bool FileOrDirectoryExists() const;167168// Returns true if pathname describes a directory in the file-system169// that exists.170bool DirectoryExists() const;171172// Returns true if FilePath ends with a path separator, which indicates that173// it is intended to represent a directory. Returns false otherwise.174// This does NOT check that a directory (or file) actually exists.175bool IsDirectory() const;176177// Returns true if pathname describes a root directory. (Windows has one178// root directory per disk drive.)179bool IsRootDirectory() const;180181// Returns true if pathname describes an absolute path.182bool IsAbsolutePath() const;183184private:185// Replaces multiple consecutive separators with a single separator.186// For example, "bar///foo" becomes "bar/foo". Does not eliminate other187// redundancies that might be in a pathname involving "." or "..".188//189// A pathname with multiple consecutive separators may occur either through190// user error or as a result of some scripts or APIs that generate a pathname191// with a trailing separator. On other platforms the same API or script192// may NOT generate a pathname with a trailing "/". Then elsewhere that193// pathname may have another "/" and pathname components added to it,194// without checking for the separator already being there.195// The script language and operating system may allow paths like "foo//bar"196// but some of the functions in FilePath will not handle that correctly. In197// particular, RemoveTrailingPathSeparator() only removes one separator, and198// it is called in CreateDirectoriesRecursively() assuming that it will change199// a pathname from directory syntax (trailing separator) to filename syntax.200//201// On Windows this method also replaces the alternate path separator '/' with202// the primary path separator '\\', so that for example "bar\\/\\foo" becomes203// "bar\\foo".204205void Normalize();206207// Returns a pointer to the last occurrence of a valid path separator in208// the FilePath. On Windows, for example, both '/' and '\' are valid path209// separators. Returns NULL if no path separator was found.210const char* FindLastPathSeparator() const;211212// Returns the length of the path root, including the directory separator at213// the end of the prefix. Returns zero by definition if the path is relative.214// Examples:215// - [Windows] "..\Sibling" => 0216// - [Windows] "\Windows" => 1217// - [Windows] "C:/Windows\Notepad.exe" => 3218// - [Windows] "\\Host\Share\C$/Windows" => 13219// - [UNIX] "/bin" => 1220size_t CalculateRootLength() const;221222std::string pathname_;223}; // class FilePath224225} // namespace internal226} // namespace testing227228GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251229230#endif // GTEST_HAS_FILE_SYSTEM231232#endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_233234235