Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:01:05

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 "gtest/internal/gtest-string.h"
0046 
0047 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \
0048 /* class A needs to have dll-interface to be used by clients of class B */)
0049 
0050 namespace testing {
0051 namespace internal {
0052 
0053 // FilePath - a class for file and directory pathname manipulation which
0054 // handles platform-specific conventions (like the pathname separator).
0055 // Used for helper functions for naming files in a directory for xml output.
0056 // Except for Set methods, all methods are const or static, which provides an
0057 // "immutable value object" -- useful for peace of mind.
0058 // A FilePath with a value ending in a path separator ("like/this/") represents
0059 // a directory, otherwise it is assumed to represent a file. In either case,
0060 // it may or may not represent an actual file or directory in the file system.
0061 // Names are NOT checked for syntax correctness -- no checking for illegal
0062 // characters, malformed paths, etc.
0063 
0064 class GTEST_API_ FilePath {
0065  public:
0066   FilePath() : pathname_("") {}
0067   FilePath(const FilePath& rhs) : pathname_(rhs.pathname_) {}
0068 
0069   explicit FilePath(const std::string& pathname) : pathname_(pathname) {
0070     Normalize();
0071   }
0072 
0073   FilePath& operator=(const FilePath& rhs) {
0074     Set(rhs);
0075     return *this;
0076   }
0077 
0078   void Set(const FilePath& rhs) { pathname_ = rhs.pathname_; }
0079 
0080   const std::string& string() const { return pathname_; }
0081   const char* c_str() const { return pathname_.c_str(); }
0082 
0083   // Returns the current working directory, or "" if unsuccessful.
0084   static FilePath GetCurrentDir();
0085 
0086   // Given directory = "dir", base_name = "test", number = 0,
0087   // extension = "xml", returns "dir/test.xml". If number is greater
0088   // than zero (e.g., 12), returns "dir/test_12.xml".
0089   // On Windows platform, uses \ as the separator rather than /.
0090   static FilePath MakeFileName(const FilePath& directory,
0091                                const FilePath& base_name, int number,
0092                                const char* extension);
0093 
0094   // Given directory = "dir", relative_path = "test.xml",
0095   // returns "dir/test.xml".
0096   // On Windows, uses \ as the separator rather than /.
0097   static FilePath ConcatPaths(const FilePath& directory,
0098                               const FilePath& relative_path);
0099 
0100   // Returns a pathname for a file that does not currently exist. The pathname
0101   // will be directory/base_name.extension or
0102   // directory/base_name_<number>.extension if directory/base_name.extension
0103   // already exists. The number will be incremented until a pathname is found
0104   // that does not already exist.
0105   // Examples: 'dir/foo_test.xml' or 'dir/foo_test_1.xml'.
0106   // There could be a race condition if two or more processes are calling this
0107   // function at the same time -- they could both pick the same filename.
0108   static FilePath GenerateUniqueFileName(const FilePath& directory,
0109                                          const FilePath& base_name,
0110                                          const char* extension);
0111 
0112   // Returns true if and only if the path is "".
0113   bool IsEmpty() const { return pathname_.empty(); }
0114 
0115   // If input name has a trailing separator character, removes it and returns
0116   // the name, otherwise return the name string unmodified.
0117   // On Windows platform, uses \ as the separator, other platforms use /.
0118   FilePath RemoveTrailingPathSeparator() const;
0119 
0120   // Returns a copy of the FilePath with the directory part removed.
0121   // Example: FilePath("path/to/file").RemoveDirectoryName() returns
0122   // FilePath("file"). If there is no directory part ("just_a_file"), it returns
0123   // the FilePath unmodified. If there is no file part ("just_a_dir/") it
0124   // returns an empty FilePath ("").
0125   // On Windows platform, '\' is the path separator, otherwise it is '/'.
0126   FilePath RemoveDirectoryName() const;
0127 
0128   // RemoveFileName returns the directory path with the filename removed.
0129   // Example: FilePath("path/to/file").RemoveFileName() returns "path/to/".
0130   // If the FilePath is "a_file" or "/a_file", RemoveFileName returns
0131   // FilePath("./") or, on Windows, FilePath(".\\"). If the filepath does
0132   // not have a file, like "just/a/dir/", it returns the FilePath unmodified.
0133   // On Windows platform, '\' is the path separator, otherwise it is '/'.
0134   FilePath RemoveFileName() const;
0135 
0136   // Returns a copy of the FilePath with the case-insensitive extension removed.
0137   // Example: FilePath("dir/file.exe").RemoveExtension("EXE") returns
0138   // FilePath("dir/file"). If a case-insensitive extension is not
0139   // found, returns a copy of the original FilePath.
0140   FilePath RemoveExtension(const char* extension) const;
0141 
0142   // Creates directories so that path exists. Returns true if successful or if
0143   // the directories already exist; returns false if unable to create
0144   // directories for any reason. Will also return false if the FilePath does
0145   // not represent a directory (that is, it doesn't end with a path separator).
0146   bool CreateDirectoriesRecursively() const;
0147 
0148   // Create the directory so that path exists. Returns true if successful or
0149   // if the directory already exists; returns false if unable to create the
0150   // directory for any reason, including if the parent directory does not
0151   // exist. Not named "CreateDirectory" because that's a macro on Windows.
0152   bool CreateFolder() const;
0153 
0154   // Returns true if FilePath describes something in the file-system,
0155   // either a file, directory, or whatever, and that something exists.
0156   bool FileOrDirectoryExists() const;
0157 
0158   // Returns true if pathname describes a directory in the file-system
0159   // that exists.
0160   bool DirectoryExists() const;
0161 
0162   // Returns true if FilePath ends with a path separator, which indicates that
0163   // it is intended to represent a directory. Returns false otherwise.
0164   // This does NOT check that a directory (or file) actually exists.
0165   bool IsDirectory() const;
0166 
0167   // Returns true if pathname describes a root directory. (Windows has one
0168   // root directory per disk drive.)
0169   bool IsRootDirectory() const;
0170 
0171   // Returns true if pathname describes an absolute path.
0172   bool IsAbsolutePath() const;
0173 
0174  private:
0175   // Replaces multiple consecutive separators with a single separator.
0176   // For example, "bar///foo" becomes "bar/foo". Does not eliminate other
0177   // redundancies that might be in a pathname involving "." or "..".
0178   //
0179   // A pathname with multiple consecutive separators may occur either through
0180   // user error or as a result of some scripts or APIs that generate a pathname
0181   // with a trailing separator. On other platforms the same API or script
0182   // may NOT generate a pathname with a trailing "/". Then elsewhere that
0183   // pathname may have another "/" and pathname components added to it,
0184   // without checking for the separator already being there.
0185   // The script language and operating system may allow paths like "foo//bar"
0186   // but some of the functions in FilePath will not handle that correctly. In
0187   // particular, RemoveTrailingPathSeparator() only removes one separator, and
0188   // it is called in CreateDirectoriesRecursively() assuming that it will change
0189   // a pathname from directory syntax (trailing separator) to filename syntax.
0190   //
0191   // On Windows this method also replaces the alternate path separator '/' with
0192   // the primary path separator '\\', so that for example "bar\\/\\foo" becomes
0193   // "bar\\foo".
0194 
0195   void Normalize();
0196 
0197   // Returns a pointer to the last occurrence of a valid path separator in
0198   // the FilePath. On Windows, for example, both '/' and '\' are valid path
0199   // separators. Returns NULL if no path separator was found.
0200   const char* FindLastPathSeparator() const;
0201 
0202   std::string pathname_;
0203 };  // class FilePath
0204 
0205 }  // namespace internal
0206 }  // namespace testing
0207 
0208 GTEST_DISABLE_MSC_WARNINGS_POP_()  //  4251
0209 
0210 #endif  // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_