00001 // Copyright 2008, Google Inc. 00002 // All rights reserved. 00003 // 00004 // Redistribution and use in source and binary forms, with or without 00005 // modification, are permitted provided that the following conditions are 00006 // met: 00007 // 00008 // * Redistributions of source code must retain the above copyright 00009 // notice, this list of conditions and the following disclaimer. 00010 // * Redistributions in binary form must reproduce the above 00011 // copyright notice, this list of conditions and the following disclaimer 00012 // in the documentation and/or other materials provided with the 00013 // distribution. 00014 // * Neither the name of Google Inc. nor the names of its 00015 // contributors may be used to endorse or promote products derived from 00016 // this software without specific prior written permission. 00017 // 00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00029 // 00030 // Author: keith.ray@gmail.com (Keith Ray) 00031 // 00032 // Google Test filepath utilities 00033 // 00034 // This header file declares classes and functions used internally by 00035 // Google Test. They are subject to change without notice. 00036 // 00037 // This file is #included in <gtest/internal/gtest-internal.h>. 00038 // Do not include this header file separately! 00039 00040 #ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_ 00041 #define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_ 00042 00043 #include "gtest/internal/gtest-string.h" 00044 00045 namespace testing { 00046 namespace internal { 00047 00048 // FilePath - a class for file and directory pathname manipulation which 00049 // handles platform-specific conventions (like the pathname separator). 00050 // Used for helper functions for naming files in a directory for xml output. 00051 // Except for Set methods, all methods are const or static, which provides an 00052 // "immutable value object" -- useful for peace of mind. 00053 // A FilePath with a value ending in a path separator ("like/this/") represents 00054 // a directory, otherwise it is assumed to represent a file. In either case, 00055 // it may or may not represent an actual file or directory in the file system. 00056 // Names are NOT checked for syntax correctness -- no checking for illegal 00057 // characters, malformed paths, etc. 00058 00059 class GTEST_API_ FilePath { 00060 public: 00061 FilePath() : pathname_("") { } 00062 FilePath(const FilePath& rhs) : pathname_(rhs.pathname_) { } 00063 00064 explicit FilePath(const char* pathname) : pathname_(pathname) { 00065 Normalize(); 00066 } 00067 00068 explicit FilePath(const String& pathname) : pathname_(pathname) { 00069 Normalize(); 00070 } 00071 00072 FilePath& operator=(const FilePath& rhs) { 00073 Set(rhs); 00074 return *this; 00075 } 00076 00077 void Set(const FilePath& rhs) { 00078 pathname_ = rhs.pathname_; 00079 } 00080 00081 String ToString() const { return pathname_; } 00082 const char* c_str() const { return pathname_.c_str(); } 00083 00084 // Returns the current working directory, or "" if unsuccessful. 00085 static FilePath GetCurrentDir(); 00086 00087 // Given directory = "dir", base_name = "test", number = 0, 00088 // extension = "xml", returns "dir/test.xml". If number is greater 00089 // than zero (e.g., 12), returns "dir/test_12.xml". 00090 // On Windows platform, uses \ as the separator rather than /. 00091 static FilePath MakeFileName(const FilePath& directory, 00092 const FilePath& base_name, 00093 int number, 00094 const char* extension); 00095 00096 // Given directory = "dir", relative_path = "test.xml", 00097 // returns "dir/test.xml". 00098 // On Windows, uses \ as the separator rather than /. 00099 static FilePath ConcatPaths(const FilePath& directory, 00100 const FilePath& relative_path); 00101 00102 // Returns a pathname for a file that does not currently exist. The pathname 00103 // will be directory/base_name.extension or 00104 // directory/base_name_<number>.extension if directory/base_name.extension 00105 // already exists. The number will be incremented until a pathname is found 00106 // that does not already exist. 00107 // Examples: 'dir/foo_test.xml' or 'dir/foo_test_1.xml'. 00108 // There could be a race condition if two or more processes are calling this 00109 // function at the same time -- they could both pick the same filename. 00110 static FilePath GenerateUniqueFileName(const FilePath& directory, 00111 const FilePath& base_name, 00112 const char* extension); 00113 00114 // Returns true iff the path is NULL or "". 00115 bool IsEmpty() const { return c_str() == NULL || *c_str() == '\0'; } 00116 00117 // If input name has a trailing separator character, removes it and returns 00118 // the name, otherwise return the name string unmodified. 00119 // On Windows platform, uses \ as the separator, other platforms use /. 00120 FilePath RemoveTrailingPathSeparator() const; 00121 00122 // Returns a copy of the FilePath with the directory part removed. 00123 // Example: FilePath("path/to/file").RemoveDirectoryName() returns 00124 // FilePath("file"). If there is no directory part ("just_a_file"), it returns 00125 // the FilePath unmodified. If there is no file part ("just_a_dir/") it 00126 // returns an empty FilePath (""). 00127 // On Windows platform, '\' is the path separator, otherwise it is '/'. 00128 FilePath RemoveDirectoryName() const; 00129 00130 // RemoveFileName returns the directory path with the filename removed. 00131 // Example: FilePath("path/to/file").RemoveFileName() returns "path/to/". 00132 // If the FilePath is "a_file" or "/a_file", RemoveFileName returns 00133 // FilePath("./") or, on Windows, FilePath(".\\"). If the filepath does 00134 // not have a file, like "just/a/dir/", it returns the FilePath unmodified. 00135 // On Windows platform, '\' is the path separator, otherwise it is '/'. 00136 FilePath RemoveFileName() const; 00137 00138 // Returns a copy of the FilePath with the case-insensitive extension removed. 00139 // Example: FilePath("dir/file.exe").RemoveExtension("EXE") returns 00140 // FilePath("dir/file"). If a case-insensitive extension is not 00141 // found, returns a copy of the original FilePath. 00142 FilePath RemoveExtension(const char* extension) const; 00143 00144 // Creates directories so that path exists. Returns true if successful or if 00145 // the directories already exist; returns false if unable to create 00146 // directories for any reason. Will also return false if the FilePath does 00147 // not represent a directory (that is, it doesn't end with a path separator). 00148 bool CreateDirectoriesRecursively() const; 00149 00150 // Create the directory so that path exists. Returns true if successful or 00151 // if the directory already exists; returns false if unable to create the 00152 // directory for any reason, including if the parent directory does not 00153 // exist. Not named "CreateDirectory" because that's a macro on Windows. 00154 bool CreateFolder() const; 00155 00156 // Returns true if FilePath describes something in the file-system, 00157 // either a file, directory, or whatever, and that something exists. 00158 bool FileOrDirectoryExists() const; 00159 00160 // Returns true if pathname describes a directory in the file-system 00161 // that exists. 00162 bool DirectoryExists() const; 00163 00164 // Returns true if FilePath ends with a path separator, which indicates that 00165 // it is intended to represent a directory. Returns false otherwise. 00166 // This does NOT check that a directory (or file) actually exists. 00167 bool IsDirectory() const; 00168 00169 // Returns true if pathname describes a root directory. (Windows has one 00170 // root directory per disk drive.) 00171 bool IsRootDirectory() const; 00172 00173 // Returns true if pathname describes an absolute path. 00174 bool IsAbsolutePath() const; 00175 00176 private: 00177 // Replaces multiple consecutive separators with a single separator. 00178 // For example, "bar///foo" becomes "bar/foo". Does not eliminate other 00179 // redundancies that might be in a pathname involving "." or "..". 00180 // 00181 // A pathname with multiple consecutive separators may occur either through 00182 // user error or as a result of some scripts or APIs that generate a pathname 00183 // with a trailing separator. On other platforms the same API or script 00184 // may NOT generate a pathname with a trailing "/". Then elsewhere that 00185 // pathname may have another "/" and pathname components added to it, 00186 // without checking for the separator already being there. 00187 // The script language and operating system may allow paths like "foo//bar" 00188 // but some of the functions in FilePath will not handle that correctly. In 00189 // particular, RemoveTrailingPathSeparator() only removes one separator, and 00190 // it is called in CreateDirectoriesRecursively() assuming that it will change 00191 // a pathname from directory syntax (trailing separator) to filename syntax. 00192 // 00193 // On Windows this method also replaces the alternate path separator '/' with 00194 // the primary path separator '\\', so that for example "bar\\/\\foo" becomes 00195 // "bar\\foo". 00196 00197 void Normalize(); 00198 00199 // Returns a pointer to the last occurence of a valid path separator in 00200 // the FilePath. On Windows, for example, both '/' and '\' are valid path 00201 // separators. Returns NULL if no path separator was found. 00202 const char* FindLastPathSeparator() const; 00203 00204 String pathname_; 00205 }; // class FilePath 00206 00207 } // namespace internal 00208 } // namespace testing 00209 00210 #endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_