18b92989c89bec8632aa47dc58dc162f199d62edcJames Zern// Copyright 2008, Google Inc.
28b92989c89bec8632aa47dc58dc162f199d62edcJames Zern// All rights reserved.
38b92989c89bec8632aa47dc58dc162f199d62edcJames Zern//
48b92989c89bec8632aa47dc58dc162f199d62edcJames Zern// Redistribution and use in source and binary forms, with or without
58b92989c89bec8632aa47dc58dc162f199d62edcJames Zern// modification, are permitted provided that the following conditions are
68b92989c89bec8632aa47dc58dc162f199d62edcJames Zern// met:
78b92989c89bec8632aa47dc58dc162f199d62edcJames Zern//
88b92989c89bec8632aa47dc58dc162f199d62edcJames Zern//     * Redistributions of source code must retain the above copyright
98b92989c89bec8632aa47dc58dc162f199d62edcJames Zern// notice, this list of conditions and the following disclaimer.
108b92989c89bec8632aa47dc58dc162f199d62edcJames Zern//     * Redistributions in binary form must reproduce the above
118b92989c89bec8632aa47dc58dc162f199d62edcJames Zern// copyright notice, this list of conditions and the following disclaimer
128b92989c89bec8632aa47dc58dc162f199d62edcJames Zern// in the documentation and/or other materials provided with the
138b92989c89bec8632aa47dc58dc162f199d62edcJames Zern// distribution.
148b92989c89bec8632aa47dc58dc162f199d62edcJames Zern//     * Neither the name of Google Inc. nor the names of its
158b92989c89bec8632aa47dc58dc162f199d62edcJames Zern// contributors may be used to endorse or promote products derived from
168b92989c89bec8632aa47dc58dc162f199d62edcJames Zern// this software without specific prior written permission.
178b92989c89bec8632aa47dc58dc162f199d62edcJames Zern//
188b92989c89bec8632aa47dc58dc162f199d62edcJames Zern// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
198b92989c89bec8632aa47dc58dc162f199d62edcJames Zern// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
208b92989c89bec8632aa47dc58dc162f199d62edcJames Zern// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
218b92989c89bec8632aa47dc58dc162f199d62edcJames Zern// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
228b92989c89bec8632aa47dc58dc162f199d62edcJames Zern// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
238b92989c89bec8632aa47dc58dc162f199d62edcJames Zern// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
248b92989c89bec8632aa47dc58dc162f199d62edcJames Zern// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
258b92989c89bec8632aa47dc58dc162f199d62edcJames Zern// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
268b92989c89bec8632aa47dc58dc162f199d62edcJames Zern// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
278b92989c89bec8632aa47dc58dc162f199d62edcJames Zern// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
288b92989c89bec8632aa47dc58dc162f199d62edcJames Zern// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
298b92989c89bec8632aa47dc58dc162f199d62edcJames Zern//
308b92989c89bec8632aa47dc58dc162f199d62edcJames Zern// Author: keith.ray@gmail.com (Keith Ray)
318b92989c89bec8632aa47dc58dc162f199d62edcJames Zern//
328b92989c89bec8632aa47dc58dc162f199d62edcJames Zern// Google Test filepath utilities
338b92989c89bec8632aa47dc58dc162f199d62edcJames Zern//
348b92989c89bec8632aa47dc58dc162f199d62edcJames Zern// This header file declares classes and functions used internally by
358b92989c89bec8632aa47dc58dc162f199d62edcJames Zern// Google Test.  They are subject to change without notice.
368b92989c89bec8632aa47dc58dc162f199d62edcJames Zern//
378b92989c89bec8632aa47dc58dc162f199d62edcJames Zern// This file is #included in <gtest/internal/gtest-internal.h>.
388b92989c89bec8632aa47dc58dc162f199d62edcJames Zern// Do not include this header file separately!
398b92989c89bec8632aa47dc58dc162f199d62edcJames Zern
408b92989c89bec8632aa47dc58dc162f199d62edcJames Zern#ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_
418b92989c89bec8632aa47dc58dc162f199d62edcJames Zern#define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_
428b92989c89bec8632aa47dc58dc162f199d62edcJames Zern
438b92989c89bec8632aa47dc58dc162f199d62edcJames Zern#include "gtest/internal/gtest-string.h"
448b92989c89bec8632aa47dc58dc162f199d62edcJames Zern
458b92989c89bec8632aa47dc58dc162f199d62edcJames Zernnamespace testing {
468b92989c89bec8632aa47dc58dc162f199d62edcJames Zernnamespace internal {
478b92989c89bec8632aa47dc58dc162f199d62edcJames Zern
488b92989c89bec8632aa47dc58dc162f199d62edcJames Zern// FilePath - a class for file and directory pathname manipulation which
498b92989c89bec8632aa47dc58dc162f199d62edcJames Zern// handles platform-specific conventions (like the pathname separator).
508b92989c89bec8632aa47dc58dc162f199d62edcJames Zern// Used for helper functions for naming files in a directory for xml output.
518b92989c89bec8632aa47dc58dc162f199d62edcJames Zern// Except for Set methods, all methods are const or static, which provides an
528b92989c89bec8632aa47dc58dc162f199d62edcJames Zern// "immutable value object" -- useful for peace of mind.
538b92989c89bec8632aa47dc58dc162f199d62edcJames Zern// A FilePath with a value ending in a path separator ("like/this/") represents
548b92989c89bec8632aa47dc58dc162f199d62edcJames Zern// a directory, otherwise it is assumed to represent a file. In either case,
558b92989c89bec8632aa47dc58dc162f199d62edcJames Zern// it may or may not represent an actual file or directory in the file system.
568b92989c89bec8632aa47dc58dc162f199d62edcJames Zern// Names are NOT checked for syntax correctness -- no checking for illegal
578b92989c89bec8632aa47dc58dc162f199d62edcJames Zern// characters, malformed paths, etc.
588b92989c89bec8632aa47dc58dc162f199d62edcJames Zern
598b92989c89bec8632aa47dc58dc162f199d62edcJames Zernclass GTEST_API_ FilePath {
608b92989c89bec8632aa47dc58dc162f199d62edcJames Zern public:
618b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  FilePath() : pathname_("") { }
628b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  FilePath(const FilePath& rhs) : pathname_(rhs.pathname_) { }
638b92989c89bec8632aa47dc58dc162f199d62edcJames Zern
648b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  explicit FilePath(const std::string& pathname) : pathname_(pathname) {
658b92989c89bec8632aa47dc58dc162f199d62edcJames Zern    Normalize();
668b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  }
678b92989c89bec8632aa47dc58dc162f199d62edcJames Zern
688b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  FilePath& operator=(const FilePath& rhs) {
698b92989c89bec8632aa47dc58dc162f199d62edcJames Zern    Set(rhs);
708b92989c89bec8632aa47dc58dc162f199d62edcJames Zern    return *this;
718b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  }
728b92989c89bec8632aa47dc58dc162f199d62edcJames Zern
738b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  void Set(const FilePath& rhs) {
748b92989c89bec8632aa47dc58dc162f199d62edcJames Zern    pathname_ = rhs.pathname_;
758b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  }
768b92989c89bec8632aa47dc58dc162f199d62edcJames Zern
778b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  const std::string& string() const { return pathname_; }
788b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  const char* c_str() const { return pathname_.c_str(); }
798b92989c89bec8632aa47dc58dc162f199d62edcJames Zern
808b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // Returns the current working directory, or "" if unsuccessful.
818b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  static FilePath GetCurrentDir();
828b92989c89bec8632aa47dc58dc162f199d62edcJames Zern
838b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // Given directory = "dir", base_name = "test", number = 0,
848b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // extension = "xml", returns "dir/test.xml". If number is greater
858b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // than zero (e.g., 12), returns "dir/test_12.xml".
868b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // On Windows platform, uses \ as the separator rather than /.
878b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  static FilePath MakeFileName(const FilePath& directory,
888b92989c89bec8632aa47dc58dc162f199d62edcJames Zern                               const FilePath& base_name,
898b92989c89bec8632aa47dc58dc162f199d62edcJames Zern                               int number,
908b92989c89bec8632aa47dc58dc162f199d62edcJames Zern                               const char* extension);
918b92989c89bec8632aa47dc58dc162f199d62edcJames Zern
928b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // Given directory = "dir", relative_path = "test.xml",
938b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // returns "dir/test.xml".
948b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // On Windows, uses \ as the separator rather than /.
958b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  static FilePath ConcatPaths(const FilePath& directory,
968b92989c89bec8632aa47dc58dc162f199d62edcJames Zern                              const FilePath& relative_path);
978b92989c89bec8632aa47dc58dc162f199d62edcJames Zern
988b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // Returns a pathname for a file that does not currently exist. The pathname
998b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // will be directory/base_name.extension or
1008b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // directory/base_name_<number>.extension if directory/base_name.extension
1018b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // already exists. The number will be incremented until a pathname is found
1028b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // that does not already exist.
1038b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // Examples: 'dir/foo_test.xml' or 'dir/foo_test_1.xml'.
1048b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // There could be a race condition if two or more processes are calling this
1058b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // function at the same time -- they could both pick the same filename.
1068b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  static FilePath GenerateUniqueFileName(const FilePath& directory,
1078b92989c89bec8632aa47dc58dc162f199d62edcJames Zern                                         const FilePath& base_name,
1088b92989c89bec8632aa47dc58dc162f199d62edcJames Zern                                         const char* extension);
1098b92989c89bec8632aa47dc58dc162f199d62edcJames Zern
1108b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // Returns true iff the path is "".
1118b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  bool IsEmpty() const { return pathname_.empty(); }
1128b92989c89bec8632aa47dc58dc162f199d62edcJames Zern
1138b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // If input name has a trailing separator character, removes it and returns
1148b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // the name, otherwise return the name string unmodified.
1158b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // On Windows platform, uses \ as the separator, other platforms use /.
1168b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  FilePath RemoveTrailingPathSeparator() const;
1178b92989c89bec8632aa47dc58dc162f199d62edcJames Zern
1188b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // Returns a copy of the FilePath with the directory part removed.
1198b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // Example: FilePath("path/to/file").RemoveDirectoryName() returns
1208b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // FilePath("file"). If there is no directory part ("just_a_file"), it returns
1218b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // the FilePath unmodified. If there is no file part ("just_a_dir/") it
1228b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // returns an empty FilePath ("").
1238b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // On Windows platform, '\' is the path separator, otherwise it is '/'.
1248b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  FilePath RemoveDirectoryName() const;
1258b92989c89bec8632aa47dc58dc162f199d62edcJames Zern
1268b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // RemoveFileName returns the directory path with the filename removed.
1278b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // Example: FilePath("path/to/file").RemoveFileName() returns "path/to/".
1288b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // If the FilePath is "a_file" or "/a_file", RemoveFileName returns
1298b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // FilePath("./") or, on Windows, FilePath(".\\"). If the filepath does
1308b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // not have a file, like "just/a/dir/", it returns the FilePath unmodified.
1318b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // On Windows platform, '\' is the path separator, otherwise it is '/'.
1328b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  FilePath RemoveFileName() const;
1338b92989c89bec8632aa47dc58dc162f199d62edcJames Zern
1348b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // Returns a copy of the FilePath with the case-insensitive extension removed.
1358b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // Example: FilePath("dir/file.exe").RemoveExtension("EXE") returns
1368b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // FilePath("dir/file"). If a case-insensitive extension is not
1378b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // found, returns a copy of the original FilePath.
1388b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  FilePath RemoveExtension(const char* extension) const;
1398b92989c89bec8632aa47dc58dc162f199d62edcJames Zern
1408b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // Creates directories so that path exists. Returns true if successful or if
1418b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // the directories already exist; returns false if unable to create
1428b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // directories for any reason. Will also return false if the FilePath does
1438b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // not represent a directory (that is, it doesn't end with a path separator).
1448b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  bool CreateDirectoriesRecursively() const;
1458b92989c89bec8632aa47dc58dc162f199d62edcJames Zern
1468b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // Create the directory so that path exists. Returns true if successful or
1478b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // if the directory already exists; returns false if unable to create the
1488b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // directory for any reason, including if the parent directory does not
1498b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // exist. Not named "CreateDirectory" because that's a macro on Windows.
1508b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  bool CreateFolder() const;
1518b92989c89bec8632aa47dc58dc162f199d62edcJames Zern
1528b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // Returns true if FilePath describes something in the file-system,
1538b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // either a file, directory, or whatever, and that something exists.
1548b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  bool FileOrDirectoryExists() const;
1558b92989c89bec8632aa47dc58dc162f199d62edcJames Zern
1568b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // Returns true if pathname describes a directory in the file-system
1578b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // that exists.
1588b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  bool DirectoryExists() const;
1598b92989c89bec8632aa47dc58dc162f199d62edcJames Zern
1608b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // Returns true if FilePath ends with a path separator, which indicates that
1618b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // it is intended to represent a directory. Returns false otherwise.
1628b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // This does NOT check that a directory (or file) actually exists.
1638b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  bool IsDirectory() const;
1648b92989c89bec8632aa47dc58dc162f199d62edcJames Zern
1658b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // Returns true if pathname describes a root directory. (Windows has one
1668b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // root directory per disk drive.)
1678b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  bool IsRootDirectory() const;
1688b92989c89bec8632aa47dc58dc162f199d62edcJames Zern
1698b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // Returns true if pathname describes an absolute path.
1708b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  bool IsAbsolutePath() const;
1718b92989c89bec8632aa47dc58dc162f199d62edcJames Zern
1728b92989c89bec8632aa47dc58dc162f199d62edcJames Zern private:
1738b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // Replaces multiple consecutive separators with a single separator.
1748b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // For example, "bar///foo" becomes "bar/foo". Does not eliminate other
1758b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // redundancies that might be in a pathname involving "." or "..".
1768b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  //
1778b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // A pathname with multiple consecutive separators may occur either through
1788b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // user error or as a result of some scripts or APIs that generate a pathname
1798b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // with a trailing separator. On other platforms the same API or script
1808b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // may NOT generate a pathname with a trailing "/". Then elsewhere that
1818b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // pathname may have another "/" and pathname components added to it,
1828b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // without checking for the separator already being there.
1838b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // The script language and operating system may allow paths like "foo//bar"
1848b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // but some of the functions in FilePath will not handle that correctly. In
1858b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // particular, RemoveTrailingPathSeparator() only removes one separator, and
1868b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // it is called in CreateDirectoriesRecursively() assuming that it will change
1878b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // a pathname from directory syntax (trailing separator) to filename syntax.
1888b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  //
1898b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // On Windows this method also replaces the alternate path separator '/' with
1908b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // the primary path separator '\\', so that for example "bar\\/\\foo" becomes
1918b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // "bar\\foo".
1928b92989c89bec8632aa47dc58dc162f199d62edcJames Zern
1938b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  void Normalize();
1948b92989c89bec8632aa47dc58dc162f199d62edcJames Zern
1958b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // Returns a pointer to the last occurence of a valid path separator in
1968b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // the FilePath. On Windows, for example, both '/' and '\' are valid path
1978b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  // separators. Returns NULL if no path separator was found.
1988b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  const char* FindLastPathSeparator() const;
1998b92989c89bec8632aa47dc58dc162f199d62edcJames Zern
2008b92989c89bec8632aa47dc58dc162f199d62edcJames Zern  std::string pathname_;
2018b92989c89bec8632aa47dc58dc162f199d62edcJames Zern};  // class FilePath
2028b92989c89bec8632aa47dc58dc162f199d62edcJames Zern
2038b92989c89bec8632aa47dc58dc162f199d62edcJames Zern}  // namespace internal
2048b92989c89bec8632aa47dc58dc162f199d62edcJames Zern}  // namespace testing
2058b92989c89bec8632aa47dc58dc162f199d62edcJames Zern
2068b92989c89bec8632aa47dc58dc162f199d62edcJames Zern#endif  // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_
207