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