gtest-filepath.h 9.4 KB

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