Blame nss/external_tests/google_test/gtest/include/gtest/internal/gtest-filepath.h

Packit 40b132
// Copyright 2008, Google Inc.
Packit 40b132
// All rights reserved.
Packit 40b132
//
Packit 40b132
// Redistribution and use in source and binary forms, with or without
Packit 40b132
// modification, are permitted provided that the following conditions are
Packit 40b132
// met:
Packit 40b132
//
Packit 40b132
//     * Redistributions of source code must retain the above copyright
Packit 40b132
// notice, this list of conditions and the following disclaimer.
Packit 40b132
//     * Redistributions in binary form must reproduce the above
Packit 40b132
// copyright notice, this list of conditions and the following disclaimer
Packit 40b132
// in the documentation and/or other materials provided with the
Packit 40b132
// distribution.
Packit 40b132
//     * Neither the name of Google Inc. nor the names of its
Packit 40b132
// contributors may be used to endorse or promote products derived from
Packit 40b132
// this software without specific prior written permission.
Packit 40b132
//
Packit 40b132
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit 40b132
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit 40b132
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit 40b132
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit 40b132
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit 40b132
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit 40b132
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit 40b132
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit 40b132
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit 40b132
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit 40b132
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 40b132
//
Packit 40b132
// Author: keith.ray@gmail.com (Keith Ray)
Packit 40b132
//
Packit 40b132
// Google Test filepath utilities
Packit 40b132
//
Packit 40b132
// This header file declares classes and functions used internally by
Packit 40b132
// Google Test.  They are subject to change without notice.
Packit 40b132
//
Packit 40b132
// This file is #included in <gtest/internal/gtest-internal.h>.
Packit 40b132
// Do not include this header file separately!
Packit 40b132
Packit 40b132
#ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_
Packit 40b132
#define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_
Packit 40b132
Packit 40b132
#include "gtest/internal/gtest-string.h"
Packit 40b132
Packit 40b132
namespace testing {
Packit 40b132
namespace internal {
Packit 40b132
Packit 40b132
// FilePath - a class for file and directory pathname manipulation which
Packit 40b132
// handles platform-specific conventions (like the pathname separator).
Packit 40b132
// Used for helper functions for naming files in a directory for xml output.
Packit 40b132
// Except for Set methods, all methods are const or static, which provides an
Packit 40b132
// "immutable value object" -- useful for peace of mind.
Packit 40b132
// A FilePath with a value ending in a path separator ("like/this/") represents
Packit 40b132
// a directory, otherwise it is assumed to represent a file. In either case,
Packit 40b132
// it may or may not represent an actual file or directory in the file system.
Packit 40b132
// Names are NOT checked for syntax correctness -- no checking for illegal
Packit 40b132
// characters, malformed paths, etc.
Packit 40b132
Packit 40b132
class GTEST_API_ FilePath {
Packit 40b132
 public:
Packit 40b132
  FilePath() : pathname_("") { }
Packit 40b132
  FilePath(const FilePath& rhs) : pathname_(rhs.pathname_) { }
Packit 40b132
Packit 40b132
  explicit FilePath(const std::string& pathname) : pathname_(pathname) {
Packit 40b132
    Normalize();
Packit 40b132
  }
Packit 40b132
Packit 40b132
  FilePath& operator=(const FilePath& rhs) {
Packit 40b132
    Set(rhs);
Packit 40b132
    return *this;
Packit 40b132
  }
Packit 40b132
Packit 40b132
  void Set(const FilePath& rhs) {
Packit 40b132
    pathname_ = rhs.pathname_;
Packit 40b132
  }
Packit 40b132
Packit 40b132
  const std::string& string() const { return pathname_; }
Packit 40b132
  const char* c_str() const { return pathname_.c_str(); }
Packit 40b132
Packit 40b132
  // Returns the current working directory, or "" if unsuccessful.
Packit 40b132
  static FilePath GetCurrentDir();
Packit 40b132
Packit 40b132
  // Given directory = "dir", base_name = "test", number = 0,
Packit 40b132
  // extension = "xml", returns "dir/test.xml". If number is greater
Packit 40b132
  // than zero (e.g., 12), returns "dir/test_12.xml".
Packit 40b132
  // On Windows platform, uses \ as the separator rather than /.
Packit 40b132
  static FilePath MakeFileName(const FilePath& directory,
Packit 40b132
                               const FilePath& base_name,
Packit 40b132
                               int number,
Packit 40b132
                               const char* extension);
Packit 40b132
Packit 40b132
  // Given directory = "dir", relative_path = "test.xml",
Packit 40b132
  // returns "dir/test.xml".
Packit 40b132
  // On Windows, uses \ as the separator rather than /.
Packit 40b132
  static FilePath ConcatPaths(const FilePath& directory,
Packit 40b132
                              const FilePath& relative_path);
Packit 40b132
Packit 40b132
  // Returns a pathname for a file that does not currently exist. The pathname
Packit 40b132
  // will be directory/base_name.extension or
Packit 40b132
  // directory/base_name_<number>.extension if directory/base_name.extension
Packit 40b132
  // already exists. The number will be incremented until a pathname is found
Packit 40b132
  // that does not already exist.
Packit 40b132
  // Examples: 'dir/foo_test.xml' or 'dir/foo_test_1.xml'.
Packit 40b132
  // There could be a race condition if two or more processes are calling this
Packit 40b132
  // function at the same time -- they could both pick the same filename.
Packit 40b132
  static FilePath GenerateUniqueFileName(const FilePath& directory,
Packit 40b132
                                         const FilePath& base_name,
Packit 40b132
                                         const char* extension);
Packit 40b132
Packit 40b132
  // Returns true iff the path is "".
Packit 40b132
  bool IsEmpty() const { return pathname_.empty(); }
Packit 40b132
Packit 40b132
  // If input name has a trailing separator character, removes it and returns
Packit 40b132
  // the name, otherwise return the name string unmodified.
Packit 40b132
  // On Windows platform, uses \ as the separator, other platforms use /.
Packit 40b132
  FilePath RemoveTrailingPathSeparator() const;
Packit 40b132
Packit 40b132
  // Returns a copy of the FilePath with the directory part removed.
Packit 40b132
  // Example: FilePath("path/to/file").RemoveDirectoryName() returns
Packit 40b132
  // FilePath("file"). If there is no directory part ("just_a_file"), it returns
Packit 40b132
  // the FilePath unmodified. If there is no file part ("just_a_dir/") it
Packit 40b132
  // returns an empty FilePath ("").
Packit 40b132
  // On Windows platform, '\' is the path separator, otherwise it is '/'.
Packit 40b132
  FilePath RemoveDirectoryName() const;
Packit 40b132
Packit 40b132
  // RemoveFileName returns the directory path with the filename removed.
Packit 40b132
  // Example: FilePath("path/to/file").RemoveFileName() returns "path/to/".
Packit 40b132
  // If the FilePath is "a_file" or "/a_file", RemoveFileName returns
Packit 40b132
  // FilePath("./") or, on Windows, FilePath(".\\"). If the filepath does
Packit 40b132
  // not have a file, like "just/a/dir/", it returns the FilePath unmodified.
Packit 40b132
  // On Windows platform, '\' is the path separator, otherwise it is '/'.
Packit 40b132
  FilePath RemoveFileName() const;
Packit 40b132
Packit 40b132
  // Returns a copy of the FilePath with the case-insensitive extension removed.
Packit 40b132
  // Example: FilePath("dir/file.exe").RemoveExtension("EXE") returns
Packit 40b132
  // FilePath("dir/file"). If a case-insensitive extension is not
Packit 40b132
  // found, returns a copy of the original FilePath.
Packit 40b132
  FilePath RemoveExtension(const char* extension) const;
Packit 40b132
Packit 40b132
  // Creates directories so that path exists. Returns true if successful or if
Packit 40b132
  // the directories already exist; returns false if unable to create
Packit 40b132
  // directories for any reason. Will also return false if the FilePath does
Packit 40b132
  // not represent a directory (that is, it doesn't end with a path separator).
Packit 40b132
  bool CreateDirectoriesRecursively() const;
Packit 40b132
Packit 40b132
  // Create the directory so that path exists. Returns true if successful or
Packit 40b132
  // if the directory already exists; returns false if unable to create the
Packit 40b132
  // directory for any reason, including if the parent directory does not
Packit 40b132
  // exist. Not named "CreateDirectory" because that's a macro on Windows.
Packit 40b132
  bool CreateFolder() const;
Packit 40b132
Packit 40b132
  // Returns true if FilePath describes something in the file-system,
Packit 40b132
  // either a file, directory, or whatever, and that something exists.
Packit 40b132
  bool FileOrDirectoryExists() const;
Packit 40b132
Packit 40b132
  // Returns true if pathname describes a directory in the file-system
Packit 40b132
  // that exists.
Packit 40b132
  bool DirectoryExists() const;
Packit 40b132
Packit 40b132
  // Returns true if FilePath ends with a path separator, which indicates that
Packit 40b132
  // it is intended to represent a directory. Returns false otherwise.
Packit 40b132
  // This does NOT check that a directory (or file) actually exists.
Packit 40b132
  bool IsDirectory() const;
Packit 40b132
Packit 40b132
  // Returns true if pathname describes a root directory. (Windows has one
Packit 40b132
  // root directory per disk drive.)
Packit 40b132
  bool IsRootDirectory() const;
Packit 40b132
Packit 40b132
  // Returns true if pathname describes an absolute path.
Packit 40b132
  bool IsAbsolutePath() const;
Packit 40b132
Packit 40b132
 private:
Packit 40b132
  // Replaces multiple consecutive separators with a single separator.
Packit 40b132
  // For example, "bar///foo" becomes "bar/foo". Does not eliminate other
Packit 40b132
  // redundancies that might be in a pathname involving "." or "..".
Packit 40b132
  //
Packit 40b132
  // A pathname with multiple consecutive separators may occur either through
Packit 40b132
  // user error or as a result of some scripts or APIs that generate a pathname
Packit 40b132
  // with a trailing separator. On other platforms the same API or script
Packit 40b132
  // may NOT generate a pathname with a trailing "/". Then elsewhere that
Packit 40b132
  // pathname may have another "/" and pathname components added to it,
Packit 40b132
  // without checking for the separator already being there.
Packit 40b132
  // The script language and operating system may allow paths like "foo//bar"
Packit 40b132
  // but some of the functions in FilePath will not handle that correctly. In
Packit 40b132
  // particular, RemoveTrailingPathSeparator() only removes one separator, and
Packit 40b132
  // it is called in CreateDirectoriesRecursively() assuming that it will change
Packit 40b132
  // a pathname from directory syntax (trailing separator) to filename syntax.
Packit 40b132
  //
Packit 40b132
  // On Windows this method also replaces the alternate path separator '/' with
Packit 40b132
  // the primary path separator '\\', so that for example "bar\\/\\foo" becomes
Packit 40b132
  // "bar\\foo".
Packit 40b132
Packit 40b132
  void Normalize();
Packit 40b132
Packit 40b132
  // Returns a pointer to the last occurence of a valid path separator in
Packit 40b132
  // the FilePath. On Windows, for example, both '/' and '\' are valid path
Packit 40b132
  // separators. Returns NULL if no path separator was found.
Packit 40b132
  const char* FindLastPathSeparator() const;
Packit 40b132
Packit 40b132
  std::string pathname_;
Packit 40b132
};  // class FilePath
Packit 40b132
Packit 40b132
}  // namespace internal
Packit 40b132
}  // namespace testing
Packit 40b132
Packit 40b132
#endif  // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_