106f32e7eSjoerg // Copyright 2008, Google Inc. 206f32e7eSjoerg // All rights reserved. 306f32e7eSjoerg // 406f32e7eSjoerg // Redistribution and use in source and binary forms, with or without 506f32e7eSjoerg // modification, are permitted provided that the following conditions are 606f32e7eSjoerg // met: 706f32e7eSjoerg // 806f32e7eSjoerg // * Redistributions of source code must retain the above copyright 906f32e7eSjoerg // notice, this list of conditions and the following disclaimer. 1006f32e7eSjoerg // * Redistributions in binary form must reproduce the above 1106f32e7eSjoerg // copyright notice, this list of conditions and the following disclaimer 1206f32e7eSjoerg // in the documentation and/or other materials provided with the 1306f32e7eSjoerg // distribution. 1406f32e7eSjoerg // * Neither the name of Google Inc. nor the names of its 1506f32e7eSjoerg // contributors may be used to endorse or promote products derived from 1606f32e7eSjoerg // this software without specific prior written permission. 1706f32e7eSjoerg // 1806f32e7eSjoerg // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1906f32e7eSjoerg // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2006f32e7eSjoerg // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 2106f32e7eSjoerg // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2206f32e7eSjoerg // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2306f32e7eSjoerg // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2406f32e7eSjoerg // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2506f32e7eSjoerg // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2606f32e7eSjoerg // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2706f32e7eSjoerg // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2806f32e7eSjoerg // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2906f32e7eSjoerg // 3006f32e7eSjoerg // Google Test filepath utilities 3106f32e7eSjoerg // 3206f32e7eSjoerg // This header file declares classes and functions used internally by 3306f32e7eSjoerg // Google Test. They are subject to change without notice. 3406f32e7eSjoerg // 35*da58b97aSjoerg // This file is #included in gtest/internal/gtest-internal.h. 3606f32e7eSjoerg // Do not include this header file separately! 3706f32e7eSjoerg 38*da58b97aSjoerg // GOOGLETEST_CM0001 DO NOT DELETE 39*da58b97aSjoerg 4006f32e7eSjoerg #ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_ 4106f32e7eSjoerg #define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_ 4206f32e7eSjoerg 4306f32e7eSjoerg #include "gtest/internal/gtest-string.h" 4406f32e7eSjoerg 45*da58b97aSjoerg GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \ 46*da58b97aSjoerg /* class A needs to have dll-interface to be used by clients of class B */) 47*da58b97aSjoerg 4806f32e7eSjoerg namespace testing { 4906f32e7eSjoerg namespace internal { 5006f32e7eSjoerg 5106f32e7eSjoerg // FilePath - a class for file and directory pathname manipulation which 5206f32e7eSjoerg // handles platform-specific conventions (like the pathname separator). 5306f32e7eSjoerg // Used for helper functions for naming files in a directory for xml output. 5406f32e7eSjoerg // Except for Set methods, all methods are const or static, which provides an 5506f32e7eSjoerg // "immutable value object" -- useful for peace of mind. 5606f32e7eSjoerg // A FilePath with a value ending in a path separator ("like/this/") represents 5706f32e7eSjoerg // a directory, otherwise it is assumed to represent a file. In either case, 5806f32e7eSjoerg // it may or may not represent an actual file or directory in the file system. 5906f32e7eSjoerg // Names are NOT checked for syntax correctness -- no checking for illegal 6006f32e7eSjoerg // characters, malformed paths, etc. 6106f32e7eSjoerg 6206f32e7eSjoerg class GTEST_API_ FilePath { 6306f32e7eSjoerg public: FilePath()6406f32e7eSjoerg FilePath() : pathname_("") { } FilePath(const FilePath & rhs)6506f32e7eSjoerg FilePath(const FilePath& rhs) : pathname_(rhs.pathname_) { } 6606f32e7eSjoerg FilePath(const std::string & pathname)6706f32e7eSjoerg explicit FilePath(const std::string& pathname) : pathname_(pathname) { 6806f32e7eSjoerg Normalize(); 6906f32e7eSjoerg } 7006f32e7eSjoerg 7106f32e7eSjoerg FilePath& operator=(const FilePath& rhs) { 7206f32e7eSjoerg Set(rhs); 7306f32e7eSjoerg return *this; 7406f32e7eSjoerg } 7506f32e7eSjoerg Set(const FilePath & rhs)7606f32e7eSjoerg void Set(const FilePath& rhs) { 7706f32e7eSjoerg pathname_ = rhs.pathname_; 7806f32e7eSjoerg } 7906f32e7eSjoerg string()8006f32e7eSjoerg const std::string& string() const { return pathname_; } c_str()8106f32e7eSjoerg const char* c_str() const { return pathname_.c_str(); } 8206f32e7eSjoerg 8306f32e7eSjoerg // Returns the current working directory, or "" if unsuccessful. 8406f32e7eSjoerg static FilePath GetCurrentDir(); 8506f32e7eSjoerg 8606f32e7eSjoerg // Given directory = "dir", base_name = "test", number = 0, 8706f32e7eSjoerg // extension = "xml", returns "dir/test.xml". If number is greater 8806f32e7eSjoerg // than zero (e.g., 12), returns "dir/test_12.xml". 8906f32e7eSjoerg // On Windows platform, uses \ as the separator rather than /. 9006f32e7eSjoerg static FilePath MakeFileName(const FilePath& directory, 9106f32e7eSjoerg const FilePath& base_name, 9206f32e7eSjoerg int number, 9306f32e7eSjoerg const char* extension); 9406f32e7eSjoerg 9506f32e7eSjoerg // Given directory = "dir", relative_path = "test.xml", 9606f32e7eSjoerg // returns "dir/test.xml". 9706f32e7eSjoerg // On Windows, uses \ as the separator rather than /. 9806f32e7eSjoerg static FilePath ConcatPaths(const FilePath& directory, 9906f32e7eSjoerg const FilePath& relative_path); 10006f32e7eSjoerg 10106f32e7eSjoerg // Returns a pathname for a file that does not currently exist. The pathname 10206f32e7eSjoerg // will be directory/base_name.extension or 10306f32e7eSjoerg // directory/base_name_<number>.extension if directory/base_name.extension 10406f32e7eSjoerg // already exists. The number will be incremented until a pathname is found 10506f32e7eSjoerg // that does not already exist. 10606f32e7eSjoerg // Examples: 'dir/foo_test.xml' or 'dir/foo_test_1.xml'. 10706f32e7eSjoerg // There could be a race condition if two or more processes are calling this 10806f32e7eSjoerg // function at the same time -- they could both pick the same filename. 10906f32e7eSjoerg static FilePath GenerateUniqueFileName(const FilePath& directory, 11006f32e7eSjoerg const FilePath& base_name, 11106f32e7eSjoerg const char* extension); 11206f32e7eSjoerg 113*da58b97aSjoerg // Returns true if and only if the path is "". IsEmpty()11406f32e7eSjoerg bool IsEmpty() const { return pathname_.empty(); } 11506f32e7eSjoerg 11606f32e7eSjoerg // If input name has a trailing separator character, removes it and returns 11706f32e7eSjoerg // the name, otherwise return the name string unmodified. 11806f32e7eSjoerg // On Windows platform, uses \ as the separator, other platforms use /. 11906f32e7eSjoerg FilePath RemoveTrailingPathSeparator() const; 12006f32e7eSjoerg 12106f32e7eSjoerg // Returns a copy of the FilePath with the directory part removed. 12206f32e7eSjoerg // Example: FilePath("path/to/file").RemoveDirectoryName() returns 12306f32e7eSjoerg // FilePath("file"). If there is no directory part ("just_a_file"), it returns 12406f32e7eSjoerg // the FilePath unmodified. If there is no file part ("just_a_dir/") it 12506f32e7eSjoerg // returns an empty FilePath (""). 12606f32e7eSjoerg // On Windows platform, '\' is the path separator, otherwise it is '/'. 12706f32e7eSjoerg FilePath RemoveDirectoryName() const; 12806f32e7eSjoerg 12906f32e7eSjoerg // RemoveFileName returns the directory path with the filename removed. 13006f32e7eSjoerg // Example: FilePath("path/to/file").RemoveFileName() returns "path/to/". 13106f32e7eSjoerg // If the FilePath is "a_file" or "/a_file", RemoveFileName returns 13206f32e7eSjoerg // FilePath("./") or, on Windows, FilePath(".\\"). If the filepath does 13306f32e7eSjoerg // not have a file, like "just/a/dir/", it returns the FilePath unmodified. 13406f32e7eSjoerg // On Windows platform, '\' is the path separator, otherwise it is '/'. 13506f32e7eSjoerg FilePath RemoveFileName() const; 13606f32e7eSjoerg 13706f32e7eSjoerg // Returns a copy of the FilePath with the case-insensitive extension removed. 13806f32e7eSjoerg // Example: FilePath("dir/file.exe").RemoveExtension("EXE") returns 13906f32e7eSjoerg // FilePath("dir/file"). If a case-insensitive extension is not 14006f32e7eSjoerg // found, returns a copy of the original FilePath. 14106f32e7eSjoerg FilePath RemoveExtension(const char* extension) const; 14206f32e7eSjoerg 14306f32e7eSjoerg // Creates directories so that path exists. Returns true if successful or if 14406f32e7eSjoerg // the directories already exist; returns false if unable to create 14506f32e7eSjoerg // directories for any reason. Will also return false if the FilePath does 14606f32e7eSjoerg // not represent a directory (that is, it doesn't end with a path separator). 14706f32e7eSjoerg bool CreateDirectoriesRecursively() const; 14806f32e7eSjoerg 14906f32e7eSjoerg // Create the directory so that path exists. Returns true if successful or 15006f32e7eSjoerg // if the directory already exists; returns false if unable to create the 15106f32e7eSjoerg // directory for any reason, including if the parent directory does not 15206f32e7eSjoerg // exist. Not named "CreateDirectory" because that's a macro on Windows. 15306f32e7eSjoerg bool CreateFolder() const; 15406f32e7eSjoerg 15506f32e7eSjoerg // Returns true if FilePath describes something in the file-system, 15606f32e7eSjoerg // either a file, directory, or whatever, and that something exists. 15706f32e7eSjoerg bool FileOrDirectoryExists() const; 15806f32e7eSjoerg 15906f32e7eSjoerg // Returns true if pathname describes a directory in the file-system 16006f32e7eSjoerg // that exists. 16106f32e7eSjoerg bool DirectoryExists() const; 16206f32e7eSjoerg 16306f32e7eSjoerg // Returns true if FilePath ends with a path separator, which indicates that 16406f32e7eSjoerg // it is intended to represent a directory. Returns false otherwise. 16506f32e7eSjoerg // This does NOT check that a directory (or file) actually exists. 16606f32e7eSjoerg bool IsDirectory() const; 16706f32e7eSjoerg 16806f32e7eSjoerg // Returns true if pathname describes a root directory. (Windows has one 16906f32e7eSjoerg // root directory per disk drive.) 17006f32e7eSjoerg bool IsRootDirectory() const; 17106f32e7eSjoerg 17206f32e7eSjoerg // Returns true if pathname describes an absolute path. 17306f32e7eSjoerg bool IsAbsolutePath() const; 17406f32e7eSjoerg 17506f32e7eSjoerg private: 17606f32e7eSjoerg // Replaces multiple consecutive separators with a single separator. 17706f32e7eSjoerg // For example, "bar///foo" becomes "bar/foo". Does not eliminate other 17806f32e7eSjoerg // redundancies that might be in a pathname involving "." or "..". 17906f32e7eSjoerg // 18006f32e7eSjoerg // A pathname with multiple consecutive separators may occur either through 18106f32e7eSjoerg // user error or as a result of some scripts or APIs that generate a pathname 18206f32e7eSjoerg // with a trailing separator. On other platforms the same API or script 18306f32e7eSjoerg // may NOT generate a pathname with a trailing "/". Then elsewhere that 18406f32e7eSjoerg // pathname may have another "/" and pathname components added to it, 18506f32e7eSjoerg // without checking for the separator already being there. 18606f32e7eSjoerg // The script language and operating system may allow paths like "foo//bar" 18706f32e7eSjoerg // but some of the functions in FilePath will not handle that correctly. In 18806f32e7eSjoerg // particular, RemoveTrailingPathSeparator() only removes one separator, and 18906f32e7eSjoerg // it is called in CreateDirectoriesRecursively() assuming that it will change 19006f32e7eSjoerg // a pathname from directory syntax (trailing separator) to filename syntax. 19106f32e7eSjoerg // 19206f32e7eSjoerg // On Windows this method also replaces the alternate path separator '/' with 19306f32e7eSjoerg // the primary path separator '\\', so that for example "bar\\/\\foo" becomes 19406f32e7eSjoerg // "bar\\foo". 19506f32e7eSjoerg 19606f32e7eSjoerg void Normalize(); 19706f32e7eSjoerg 198*da58b97aSjoerg // Returns a pointer to the last occurence of a valid path separator in 19906f32e7eSjoerg // the FilePath. On Windows, for example, both '/' and '\' are valid path 20006f32e7eSjoerg // separators. Returns NULL if no path separator was found. 20106f32e7eSjoerg const char* FindLastPathSeparator() const; 20206f32e7eSjoerg 20306f32e7eSjoerg std::string pathname_; 20406f32e7eSjoerg }; // class FilePath 20506f32e7eSjoerg 20606f32e7eSjoerg } // namespace internal 20706f32e7eSjoerg } // namespace testing 20806f32e7eSjoerg 209*da58b97aSjoerg GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251 210*da58b97aSjoerg 21106f32e7eSjoerg #endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_ 212