113481Sgiacomo.travaglini@arm.com// Copyright 2008, Google Inc.
213481Sgiacomo.travaglini@arm.com// All rights reserved.
313481Sgiacomo.travaglini@arm.com//
413481Sgiacomo.travaglini@arm.com// Redistribution and use in source and binary forms, with or without
513481Sgiacomo.travaglini@arm.com// modification, are permitted provided that the following conditions are
613481Sgiacomo.travaglini@arm.com// met:
713481Sgiacomo.travaglini@arm.com//
813481Sgiacomo.travaglini@arm.com//     * Redistributions of source code must retain the above copyright
913481Sgiacomo.travaglini@arm.com// notice, this list of conditions and the following disclaimer.
1013481Sgiacomo.travaglini@arm.com//     * Redistributions in binary form must reproduce the above
1113481Sgiacomo.travaglini@arm.com// copyright notice, this list of conditions and the following disclaimer
1213481Sgiacomo.travaglini@arm.com// in the documentation and/or other materials provided with the
1313481Sgiacomo.travaglini@arm.com// distribution.
1413481Sgiacomo.travaglini@arm.com//     * Neither the name of Google Inc. nor the names of its
1513481Sgiacomo.travaglini@arm.com// contributors may be used to endorse or promote products derived from
1613481Sgiacomo.travaglini@arm.com// this software without specific prior written permission.
1713481Sgiacomo.travaglini@arm.com//
1813481Sgiacomo.travaglini@arm.com// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1913481Sgiacomo.travaglini@arm.com// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2013481Sgiacomo.travaglini@arm.com// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2113481Sgiacomo.travaglini@arm.com// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2213481Sgiacomo.travaglini@arm.com// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2313481Sgiacomo.travaglini@arm.com// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2413481Sgiacomo.travaglini@arm.com// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2513481Sgiacomo.travaglini@arm.com// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2613481Sgiacomo.travaglini@arm.com// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2713481Sgiacomo.travaglini@arm.com// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2813481Sgiacomo.travaglini@arm.com// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2913481Sgiacomo.travaglini@arm.com//
3013481Sgiacomo.travaglini@arm.com// Author: keith.ray@gmail.com (Keith Ray)
3113481Sgiacomo.travaglini@arm.com//
3213481Sgiacomo.travaglini@arm.com// Google Test filepath utilities
3313481Sgiacomo.travaglini@arm.com//
3413481Sgiacomo.travaglini@arm.com// This header file declares classes and functions used internally by
3513481Sgiacomo.travaglini@arm.com// Google Test.  They are subject to change without notice.
3613481Sgiacomo.travaglini@arm.com//
3713481Sgiacomo.travaglini@arm.com// This file is #included in <gtest/internal/gtest-internal.h>.
3813481Sgiacomo.travaglini@arm.com// Do not include this header file separately!
3913481Sgiacomo.travaglini@arm.com
4013481Sgiacomo.travaglini@arm.com#ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_
4113481Sgiacomo.travaglini@arm.com#define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_
4213481Sgiacomo.travaglini@arm.com
4313481Sgiacomo.travaglini@arm.com#include "gtest/internal/gtest-string.h"
4413481Sgiacomo.travaglini@arm.com
4513481Sgiacomo.travaglini@arm.comnamespace testing {
4613481Sgiacomo.travaglini@arm.comnamespace internal {
4713481Sgiacomo.travaglini@arm.com
4813481Sgiacomo.travaglini@arm.com// FilePath - a class for file and directory pathname manipulation which
4913481Sgiacomo.travaglini@arm.com// handles platform-specific conventions (like the pathname separator).
5013481Sgiacomo.travaglini@arm.com// Used for helper functions for naming files in a directory for xml output.
5113481Sgiacomo.travaglini@arm.com// Except for Set methods, all methods are const or static, which provides an
5213481Sgiacomo.travaglini@arm.com// "immutable value object" -- useful for peace of mind.
5313481Sgiacomo.travaglini@arm.com// A FilePath with a value ending in a path separator ("like/this/") represents
5413481Sgiacomo.travaglini@arm.com// a directory, otherwise it is assumed to represent a file. In either case,
5513481Sgiacomo.travaglini@arm.com// it may or may not represent an actual file or directory in the file system.
5613481Sgiacomo.travaglini@arm.com// Names are NOT checked for syntax correctness -- no checking for illegal
5713481Sgiacomo.travaglini@arm.com// characters, malformed paths, etc.
5813481Sgiacomo.travaglini@arm.com
5913481Sgiacomo.travaglini@arm.comclass GTEST_API_ FilePath {
6013481Sgiacomo.travaglini@arm.com public:
6113481Sgiacomo.travaglini@arm.com  FilePath() : pathname_("") { }
6213481Sgiacomo.travaglini@arm.com  FilePath(const FilePath& rhs) : pathname_(rhs.pathname_) { }
6313481Sgiacomo.travaglini@arm.com
6413481Sgiacomo.travaglini@arm.com  explicit FilePath(const std::string& pathname) : pathname_(pathname) {
6513481Sgiacomo.travaglini@arm.com    Normalize();
6613481Sgiacomo.travaglini@arm.com  }
6713481Sgiacomo.travaglini@arm.com
6813481Sgiacomo.travaglini@arm.com  FilePath& operator=(const FilePath& rhs) {
6913481Sgiacomo.travaglini@arm.com    Set(rhs);
7013481Sgiacomo.travaglini@arm.com    return *this;
7113481Sgiacomo.travaglini@arm.com  }
7213481Sgiacomo.travaglini@arm.com
7313481Sgiacomo.travaglini@arm.com  void Set(const FilePath& rhs) {
7413481Sgiacomo.travaglini@arm.com    pathname_ = rhs.pathname_;
7513481Sgiacomo.travaglini@arm.com  }
7613481Sgiacomo.travaglini@arm.com
7713481Sgiacomo.travaglini@arm.com  const std::string& string() const { return pathname_; }
7813481Sgiacomo.travaglini@arm.com  const char* c_str() const { return pathname_.c_str(); }
7913481Sgiacomo.travaglini@arm.com
8013481Sgiacomo.travaglini@arm.com  // Returns the current working directory, or "" if unsuccessful.
8113481Sgiacomo.travaglini@arm.com  static FilePath GetCurrentDir();
8213481Sgiacomo.travaglini@arm.com
8313481Sgiacomo.travaglini@arm.com  // Given directory = "dir", base_name = "test", number = 0,
8413481Sgiacomo.travaglini@arm.com  // extension = "xml", returns "dir/test.xml". If number is greater
8513481Sgiacomo.travaglini@arm.com  // than zero (e.g., 12), returns "dir/test_12.xml".
8613481Sgiacomo.travaglini@arm.com  // On Windows platform, uses \ as the separator rather than /.
8713481Sgiacomo.travaglini@arm.com  static FilePath MakeFileName(const FilePath& directory,
8813481Sgiacomo.travaglini@arm.com                               const FilePath& base_name,
8913481Sgiacomo.travaglini@arm.com                               int number,
9013481Sgiacomo.travaglini@arm.com                               const char* extension);
9113481Sgiacomo.travaglini@arm.com
9213481Sgiacomo.travaglini@arm.com  // Given directory = "dir", relative_path = "test.xml",
9313481Sgiacomo.travaglini@arm.com  // returns "dir/test.xml".
9413481Sgiacomo.travaglini@arm.com  // On Windows, uses \ as the separator rather than /.
9513481Sgiacomo.travaglini@arm.com  static FilePath ConcatPaths(const FilePath& directory,
9613481Sgiacomo.travaglini@arm.com                              const FilePath& relative_path);
9713481Sgiacomo.travaglini@arm.com
9813481Sgiacomo.travaglini@arm.com  // Returns a pathname for a file that does not currently exist. The pathname
9913481Sgiacomo.travaglini@arm.com  // will be directory/base_name.extension or
10013481Sgiacomo.travaglini@arm.com  // directory/base_name_<number>.extension if directory/base_name.extension
10113481Sgiacomo.travaglini@arm.com  // already exists. The number will be incremented until a pathname is found
10213481Sgiacomo.travaglini@arm.com  // that does not already exist.
10313481Sgiacomo.travaglini@arm.com  // Examples: 'dir/foo_test.xml' or 'dir/foo_test_1.xml'.
10413481Sgiacomo.travaglini@arm.com  // There could be a race condition if two or more processes are calling this
10513481Sgiacomo.travaglini@arm.com  // function at the same time -- they could both pick the same filename.
10613481Sgiacomo.travaglini@arm.com  static FilePath GenerateUniqueFileName(const FilePath& directory,
10713481Sgiacomo.travaglini@arm.com                                         const FilePath& base_name,
10813481Sgiacomo.travaglini@arm.com                                         const char* extension);
10913481Sgiacomo.travaglini@arm.com
11013481Sgiacomo.travaglini@arm.com  // Returns true iff the path is "".
11113481Sgiacomo.travaglini@arm.com  bool IsEmpty() const { return pathname_.empty(); }
11213481Sgiacomo.travaglini@arm.com
11313481Sgiacomo.travaglini@arm.com  // If input name has a trailing separator character, removes it and returns
11413481Sgiacomo.travaglini@arm.com  // the name, otherwise return the name string unmodified.
11513481Sgiacomo.travaglini@arm.com  // On Windows platform, uses \ as the separator, other platforms use /.
11613481Sgiacomo.travaglini@arm.com  FilePath RemoveTrailingPathSeparator() const;
11713481Sgiacomo.travaglini@arm.com
11813481Sgiacomo.travaglini@arm.com  // Returns a copy of the FilePath with the directory part removed.
11913481Sgiacomo.travaglini@arm.com  // Example: FilePath("path/to/file").RemoveDirectoryName() returns
12013481Sgiacomo.travaglini@arm.com  // FilePath("file"). If there is no directory part ("just_a_file"), it returns
12113481Sgiacomo.travaglini@arm.com  // the FilePath unmodified. If there is no file part ("just_a_dir/") it
12213481Sgiacomo.travaglini@arm.com  // returns an empty FilePath ("").
12313481Sgiacomo.travaglini@arm.com  // On Windows platform, '\' is the path separator, otherwise it is '/'.
12413481Sgiacomo.travaglini@arm.com  FilePath RemoveDirectoryName() const;
12513481Sgiacomo.travaglini@arm.com
12613481Sgiacomo.travaglini@arm.com  // RemoveFileName returns the directory path with the filename removed.
12713481Sgiacomo.travaglini@arm.com  // Example: FilePath("path/to/file").RemoveFileName() returns "path/to/".
12813481Sgiacomo.travaglini@arm.com  // If the FilePath is "a_file" or "/a_file", RemoveFileName returns
12913481Sgiacomo.travaglini@arm.com  // FilePath("./") or, on Windows, FilePath(".\\"). If the filepath does
13013481Sgiacomo.travaglini@arm.com  // not have a file, like "just/a/dir/", it returns the FilePath unmodified.
13113481Sgiacomo.travaglini@arm.com  // On Windows platform, '\' is the path separator, otherwise it is '/'.
13213481Sgiacomo.travaglini@arm.com  FilePath RemoveFileName() const;
13313481Sgiacomo.travaglini@arm.com
13413481Sgiacomo.travaglini@arm.com  // Returns a copy of the FilePath with the case-insensitive extension removed.
13513481Sgiacomo.travaglini@arm.com  // Example: FilePath("dir/file.exe").RemoveExtension("EXE") returns
13613481Sgiacomo.travaglini@arm.com  // FilePath("dir/file"). If a case-insensitive extension is not
13713481Sgiacomo.travaglini@arm.com  // found, returns a copy of the original FilePath.
13813481Sgiacomo.travaglini@arm.com  FilePath RemoveExtension(const char* extension) const;
13913481Sgiacomo.travaglini@arm.com
14013481Sgiacomo.travaglini@arm.com  // Creates directories so that path exists. Returns true if successful or if
14113481Sgiacomo.travaglini@arm.com  // the directories already exist; returns false if unable to create
14213481Sgiacomo.travaglini@arm.com  // directories for any reason. Will also return false if the FilePath does
14313481Sgiacomo.travaglini@arm.com  // not represent a directory (that is, it doesn't end with a path separator).
14413481Sgiacomo.travaglini@arm.com  bool CreateDirectoriesRecursively() const;
14513481Sgiacomo.travaglini@arm.com
14613481Sgiacomo.travaglini@arm.com  // Create the directory so that path exists. Returns true if successful or
14713481Sgiacomo.travaglini@arm.com  // if the directory already exists; returns false if unable to create the
14813481Sgiacomo.travaglini@arm.com  // directory for any reason, including if the parent directory does not
14913481Sgiacomo.travaglini@arm.com  // exist. Not named "CreateDirectory" because that's a macro on Windows.
15013481Sgiacomo.travaglini@arm.com  bool CreateFolder() const;
15113481Sgiacomo.travaglini@arm.com
15213481Sgiacomo.travaglini@arm.com  // Returns true if FilePath describes something in the file-system,
15313481Sgiacomo.travaglini@arm.com  // either a file, directory, or whatever, and that something exists.
15413481Sgiacomo.travaglini@arm.com  bool FileOrDirectoryExists() const;
15513481Sgiacomo.travaglini@arm.com
15613481Sgiacomo.travaglini@arm.com  // Returns true if pathname describes a directory in the file-system
15713481Sgiacomo.travaglini@arm.com  // that exists.
15813481Sgiacomo.travaglini@arm.com  bool DirectoryExists() const;
15913481Sgiacomo.travaglini@arm.com
16013481Sgiacomo.travaglini@arm.com  // Returns true if FilePath ends with a path separator, which indicates that
16113481Sgiacomo.travaglini@arm.com  // it is intended to represent a directory. Returns false otherwise.
16213481Sgiacomo.travaglini@arm.com  // This does NOT check that a directory (or file) actually exists.
16313481Sgiacomo.travaglini@arm.com  bool IsDirectory() const;
16413481Sgiacomo.travaglini@arm.com
16513481Sgiacomo.travaglini@arm.com  // Returns true if pathname describes a root directory. (Windows has one
16613481Sgiacomo.travaglini@arm.com  // root directory per disk drive.)
16713481Sgiacomo.travaglini@arm.com  bool IsRootDirectory() const;
16813481Sgiacomo.travaglini@arm.com
16913481Sgiacomo.travaglini@arm.com  // Returns true if pathname describes an absolute path.
17013481Sgiacomo.travaglini@arm.com  bool IsAbsolutePath() const;
17113481Sgiacomo.travaglini@arm.com
17213481Sgiacomo.travaglini@arm.com private:
17313481Sgiacomo.travaglini@arm.com  // Replaces multiple consecutive separators with a single separator.
17413481Sgiacomo.travaglini@arm.com  // For example, "bar///foo" becomes "bar/foo". Does not eliminate other
17513481Sgiacomo.travaglini@arm.com  // redundancies that might be in a pathname involving "." or "..".
17613481Sgiacomo.travaglini@arm.com  //
17713481Sgiacomo.travaglini@arm.com  // A pathname with multiple consecutive separators may occur either through
17813481Sgiacomo.travaglini@arm.com  // user error or as a result of some scripts or APIs that generate a pathname
17913481Sgiacomo.travaglini@arm.com  // with a trailing separator. On other platforms the same API or script
18013481Sgiacomo.travaglini@arm.com  // may NOT generate a pathname with a trailing "/". Then elsewhere that
18113481Sgiacomo.travaglini@arm.com  // pathname may have another "/" and pathname components added to it,
18213481Sgiacomo.travaglini@arm.com  // without checking for the separator already being there.
18313481Sgiacomo.travaglini@arm.com  // The script language and operating system may allow paths like "foo//bar"
18413481Sgiacomo.travaglini@arm.com  // but some of the functions in FilePath will not handle that correctly. In
18513481Sgiacomo.travaglini@arm.com  // particular, RemoveTrailingPathSeparator() only removes one separator, and
18613481Sgiacomo.travaglini@arm.com  // it is called in CreateDirectoriesRecursively() assuming that it will change
18713481Sgiacomo.travaglini@arm.com  // a pathname from directory syntax (trailing separator) to filename syntax.
18813481Sgiacomo.travaglini@arm.com  //
18913481Sgiacomo.travaglini@arm.com  // On Windows this method also replaces the alternate path separator '/' with
19013481Sgiacomo.travaglini@arm.com  // the primary path separator '\\', so that for example "bar\\/\\foo" becomes
19113481Sgiacomo.travaglini@arm.com  // "bar\\foo".
19213481Sgiacomo.travaglini@arm.com
19313481Sgiacomo.travaglini@arm.com  void Normalize();
19413481Sgiacomo.travaglini@arm.com
19513481Sgiacomo.travaglini@arm.com  // Returns a pointer to the last occurence of a valid path separator in
19613481Sgiacomo.travaglini@arm.com  // the FilePath. On Windows, for example, both '/' and '\' are valid path
19713481Sgiacomo.travaglini@arm.com  // separators. Returns NULL if no path separator was found.
19813481Sgiacomo.travaglini@arm.com  const char* FindLastPathSeparator() const;
19913481Sgiacomo.travaglini@arm.com
20013481Sgiacomo.travaglini@arm.com  std::string pathname_;
20113481Sgiacomo.travaglini@arm.com};  // class FilePath
20213481Sgiacomo.travaglini@arm.com
20313481Sgiacomo.travaglini@arm.com}  // namespace internal
20413481Sgiacomo.travaglini@arm.com}  // namespace testing
20513481Sgiacomo.travaglini@arm.com
20613481Sgiacomo.travaglini@arm.com#endif  // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_
207