Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-15 09:01:15

0001 // Copyright 2008, Google Inc.
0002 // All rights reserved.
0003 //
0004 // Redistribution and use in source and binary forms, with or without
0005 // modification, are permitted provided that the following conditions are
0006 // met:
0007 //
0008 //     * Redistributions of source code must retain the above copyright
0009 // notice, this list of conditions and the following disclaimer.
0010 //     * Redistributions in binary form must reproduce the above
0011 // copyright notice, this list of conditions and the following disclaimer
0012 // in the documentation and/or other materials provided with the
0013 // distribution.
0014 //     * Neither the name of Google Inc. nor the names of its
0015 // contributors may be used to endorse or promote products derived from
0016 // this software without specific prior written permission.
0017 //
0018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
0021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
0022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
0028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0029 
0030 // Google Test filepath utilities
0031 //
0032 // This header file declares classes and functions used internally by
0033 // Google Test.  They are subject to change without notice.
0034 //
0035 // This file is #included in gtest/internal/gtest-internal.h.
0036 // Do not include this header file separately!
0037 
0038 // IWYU pragma: private, include "gtest/gtest.h"
0039 // IWYU pragma: friend gtest/.*
0040 // IWYU pragma: friend gmock/.*
0041 
0042 #ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_
0043 #define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_
0044 
0045 #include <string>
0046 #include <utility>
0047 
0048 #include "gtest/internal/gtest-port.h"
0049 #include "gtest/internal/gtest-string.h"
0050 
0051 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \
0052 /* class A needs to have dll-interface to be used by clients of class B */)
0053 
0054 #if GTEST_HAS_FILE_SYSTEM
0055 
0056 namespace testing {
0057 namespace internal {
0058 
0059 // FilePath - a class for file and directory pathname manipulation which
0060 // handles platform-specific conventions (like the pathname separator).
0061 // Used for helper functions for naming files in a directory for xml output.
0062 // Except for Set methods, all methods are const or static, which provides an
0063 // "immutable value object" -- useful for peace of mind.
0064 // A FilePath with a value ending in a path separator ("like/this/") represents
0065 // a directory, otherwise it is assumed to represent a file. In either case,
0066 // it may or may not represent an actual file or directory in the file system.
0067 // Names are NOT checked for syntax correctness -- no checking for illegal
0068 // characters, malformed paths, etc.
0069 
0070 class GTEST_API_ FilePath {
0071  public:
0072   FilePath() : pathname_("") {}
0073   FilePath(const FilePath& rhs) : pathname_(rhs.pathname_) {}
0074   FilePath(FilePath&& rhs) noexcept : pathname_(std::move(rhs.pathname_)) {}
0075 
0076   explicit FilePath(std::string pathname) : pathname_(std::move(pathname)) {
0077     Normalize();
0078   }
0079 
0080   FilePath& operator=(const FilePath& rhs) {
0081     Set(rhs);
0082     return *this;
0083   }
0084   FilePath& operator=(FilePath&& rhs) noexcept {
0085     pathname_ = std::move(rhs.pathname_);
0086     return *this;
0087   }
0088 
0089   void Set(const FilePath& rhs) { pathname_ = rhs.pathname_; }
0090 
0091   const std::string& string() const { return pathname_; }
0092   const char* c_str() const { return pathname_.c_str(); }
0093 
0094   // Returns the current working directory, or "" if unsuccessful.
0095   static FilePath GetCurrentDir();
0096 
0097   // Given directory = "dir", base_name = "test", number = 0,
0098   // extension = "xml", returns "dir/test.xml". If number is greater
0099   // than zero (e.g., 12), returns "dir/test_12.xml".
0100   // On Windows platform, uses \ as the separator rather than /.
0101   static FilePath MakeFileName(const FilePath& directory,
0102                                const FilePath& base_name, int number,
0103                                const char* extension);
0104 
0105   // Given directory = "dir", relative_path = "test.xml",
0106   // returns "dir/test.xml".
0107   // On Windows, uses \ as the separator rather than /.
0108   static FilePath ConcatPaths(const FilePath& directory,
0109                               const FilePath& relative_path);
0110 
0111   // Returns a pathname for a file that does not currently exist. The pathname
0112   // will be directory/base_name.extension or
0113   // directory/base_name_<number>.extension if directory/base_name.extension
0114   // already exists. The number will be incremented until a pathname is found
0115   // that does not already exist.
0116   // Examples: 'dir/foo_test.xml' or 'dir/foo_test_1.xml'.
0117   // There could be a race condition if two or more processes are calling this
0118   // function at the same time -- they could both pick the same filename.
0119   static FilePath GenerateUniqueFileName(const FilePath& directory,
0120                                          const FilePath& base_name,
0121                                          const char* extension);
0122 
0123   // Returns true if and only if the path is "".
0124   bool IsEmpty() const { return pathname_.empty(); }
0125 
0126   // If input name has a trailing separator character, removes it and returns
0127   // the name, otherwise return the name string unmodified.
0128   // On Windows platform, uses \ as the separator, other platforms use /.
0129   FilePath RemoveTrailingPathSeparator() const;
0130 
0131   // Returns a copy of the FilePath with the directory part removed.
0132   // Example: FilePath("path/to/file").RemoveDirectoryName() returns
0133   // FilePath("file"). If there is no directory part ("just_a_file"), it returns
0134   // the FilePath unmodified. If there is no file part ("just_a_dir/") it
0135   // returns an empty FilePath ("").
0136   // On Windows platform, '\' is the path separator, otherwise it is '/'.
0137   FilePath RemoveDirectoryName() const;
0138 
0139   // RemoveFileName returns the directory path with the filename removed.
0140   // Example: FilePath("path/to/file").RemoveFileName() returns "path/to/".
0141   // If the FilePath is "a_file" or "/a_file", RemoveFileName returns
0142   // FilePath("./") or, on Windows, FilePath(".\\"). If the filepath does
0143   // not have a file, like "just/a/dir/", it returns the FilePath unmodified.
0144   // On Windows platform, '\' is the path separator, otherwise it is '/'.
0145   FilePath RemoveFileName() const;
0146 
0147   // Returns a copy of the FilePath with the case-insensitive extension removed.
0148   // Example: FilePath("dir/file.exe").RemoveExtension("EXE") returns
0149   // FilePath("dir/file"). If a case-insensitive extension is not
0150   // found, returns a copy of the original FilePath.
0151   FilePath RemoveExtension(const char* extension) const;
0152 
0153   // Creates directories so that path exists. Returns true if successful or if
0154   // the directories already exist; returns false if unable to create
0155   // directories for any reason. Will also return false if the FilePath does
0156   // not represent a directory (that is, it doesn't end with a path separator).
0157   bool CreateDirectoriesRecursively() const;
0158 
0159   // Create the directory so that path exists. Returns true if successful or
0160   // if the directory already exists; returns false if unable to create the
0161   // directory for any reason, including if the parent directory does not
0162   // exist. Not named "CreateDirectory" because that's a macro on Windows.
0163   bool CreateFolder() const;
0164 
0165   // Returns true if FilePath describes something in the file-system,
0166   // either a file, directory, or whatever, and that something exists.
0167   bool FileOrDirectoryExists() const;
0168 
0169   // Returns true if pathname describes a directory in the file-system
0170   // that exists.
0171   bool DirectoryExists() const;
0172 
0173   // Returns true if FilePath ends with a path separator, which indicates that
0174   // it is intended to represent a directory. Returns false otherwise.
0175   // This does NOT check that a directory (or file) actually exists.
0176   bool IsDirectory() const;
0177 
0178   // Returns true if pathname describes a root directory. (Windows has one
0179   // root directory per disk drive.)
0180   bool IsRootDirectory() const;
0181 
0182   // Returns true if pathname describes an absolute path.
0183   bool IsAbsolutePath() const;
0184 
0185  private:
0186   // Replaces multiple consecutive separators with a single separator.
0187   // For example, "bar///foo" becomes "bar/foo". Does not eliminate other
0188   // redundancies that might be in a pathname involving "." or "..".
0189   //
0190   // A pathname with multiple consecutive separators may occur either through
0191   // user error or as a result of some scripts or APIs that generate a pathname
0192   // with a trailing separator. On other platforms the same API or script
0193   // may NOT generate a pathname with a trailing "/". Then elsewhere that
0194   // pathname may have another "/" and pathname components added to it,
0195   // without checking for the separator already being there.
0196   // The script language and operating system may allow paths like "foo//bar"
0197   // but some of the functions in FilePath will not handle that correctly. In
0198   // particular, RemoveTrailingPathSeparator() only removes one separator, and
0199   // it is called in CreateDirectoriesRecursively() assuming that it will change
0200   // a pathname from directory syntax (trailing separator) to filename syntax.
0201   //
0202   // On Windows this method also replaces the alternate path separator '/' with
0203   // the primary path separator '\\', so that for example "bar\\/\\foo" becomes
0204   // "bar\\foo".
0205 
0206   void Normalize();
0207 
0208   // Returns a pointer to the last occurrence of a valid path separator in
0209   // the FilePath. On Windows, for example, both '/' and '\' are valid path
0210   // separators. Returns NULL if no path separator was found.
0211   const char* FindLastPathSeparator() const;
0212 
0213   // Returns the length of the path root, including the directory separator at
0214   // the end of the prefix. Returns zero by definition if the path is relative.
0215   // Examples:
0216   // - [Windows] "..\Sibling" => 0
0217   // - [Windows] "\Windows" => 1
0218   // - [Windows] "C:/Windows\Notepad.exe" => 3
0219   // - [Windows] "\\Host\Share\C$/Windows" => 13
0220   // - [UNIX] "/bin" => 1
0221   size_t CalculateRootLength() const;
0222 
0223   std::string pathname_;
0224 };  // class FilePath
0225 
0226 }  // namespace internal
0227 }  // namespace testing
0228 
0229 GTEST_DISABLE_MSC_WARNINGS_POP_()  //  4251
0230 
0231 #endif  // GTEST_HAS_FILE_SYSTEM
0232 
0233 #endif  // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_