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 testing/base/internal/gtest-internal.h
38// Do not include this header file separately!
39
40#ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_
41#define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_
42
43#include <gtest/internal/gtest-string.h>
44
45namespace testing {
46namespace internal {
47
48// FilePath - a class for file and directory pathname manipulation which
49// handles platform-specific conventions (like the pathname separator).
50// Used for helper functions for naming files in a directory for xml output.
51// Except for Set methods, all methods are const or static, which provides an
52// "immutable value object" -- useful for peace of mind.
53// A FilePath with a value ending in a path separator ("like/this/") represents
54// a directory, otherwise it is assumed to represent a file. In either case,
55// it may or may not represent an actual file or directory in the file system.
56// Names are NOT checked for syntax correctness -- no checking for illegal
57// characters, malformed paths, etc.
58
59class FilePath {
60 public:
61  FilePath() : pathname_("") { }
62  FilePath(const FilePath& rhs) : pathname_(rhs.pathname_) { }
63  explicit FilePath(const char* pathname) : pathname_(pathname) { }
64  explicit FilePath(const String& pathname) : pathname_(pathname) { }
65
66  void Set(const FilePath& rhs) {
67    pathname_ = rhs.pathname_;
68  }
69
70  String ToString() const { return pathname_; }
71  const char* c_str() const { return pathname_.c_str(); }
72
73  // Given directory = "dir", base_name = "test", number = 0,
74  // extension = "xml", returns "dir/test.xml". If number is greater
75  // than zero (e.g., 12), returns "dir/test_12.xml".
76  // On Windows platform, uses \ as the separator rather than /.
77  static FilePath MakeFileName(const FilePath& directory,
78                               const FilePath& base_name,
79                               int number,
80                               const char* extension);
81
82  // Returns a pathname for a file that does not currently exist. The pathname
83  // will be directory/base_name.extension or
84  // directory/base_name_<number>.extension if directory/base_name.extension
85  // already exists. The number will be incremented until a pathname is found
86  // that does not already exist.
87  // Examples: 'dir/foo_test.xml' or 'dir/foo_test_1.xml'.
88  // There could be a race condition if two or more processes are calling this
89  // function at the same time -- they could both pick the same filename.
90  static FilePath GenerateUniqueFileName(const FilePath& directory,
91                                         const FilePath& base_name,
92                                         const char* extension);
93
94  // If input name has a trailing separator character, removes it and returns
95  // the name, otherwise return the name string unmodified.
96  // On Windows platform, uses \ as the separator, other platforms use /.
97  FilePath RemoveTrailingPathSeparator() const;
98
99  // Returns a copy of the FilePath with the directory part removed.
100  // Example: FilePath("path/to/file").RemoveDirectoryName() returns
101  // FilePath("file"). If there is no directory part ("just_a_file"), it returns
102  // the FilePath unmodified. If there is no file part ("just_a_dir/") it
103  // returns an empty FilePath ("").
104  // On Windows platform, '\' is the path separator, otherwise it is '/'.
105  FilePath RemoveDirectoryName() const;
106
107  // RemoveFileName returns the directory path with the filename removed.
108  // Example: FilePath("path/to/file").RemoveFileName() returns "path/to/".
109  // If the FilePath is "a_file" or "/a_file", RemoveFileName returns
110  // FilePath("./") or, on Windows, FilePath(".\\"). If the filepath does
111  // not have a file, like "just/a/dir/", it returns the FilePath unmodified.
112  // On Windows platform, '\' is the path separator, otherwise it is '/'.
113  FilePath RemoveFileName() const;
114
115  // Returns a copy of the FilePath with the case-insensitive extension removed.
116  // Example: FilePath("dir/file.exe").RemoveExtension("EXE") returns
117  // FilePath("dir/file"). If a case-insensitive extension is not
118  // found, returns a copy of the original FilePath.
119  FilePath RemoveExtension(const char* extension) const;
120
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
127  // Create the directory so that path exists. Returns true if successful or
128  // if the directory already exists; returns false if unable to create the
129  // directory for any reason, including if the parent directory does not
130  // exist. Not named "CreateDirectory" because that's a macro on Windows.
131  bool CreateFolder() const;
132
133  // Returns true if FilePath describes something in the file-system,
134  // either a file, directory, or whatever, and that something exists.
135  bool FileOrDirectoryExists() const;
136
137  // Returns true if pathname describes a directory in the file-system
138  // that exists.
139  bool DirectoryExists() const;
140
141  // Returns true if FilePath ends with a path separator, which indicates that
142  // it is intended to represent a directory. Returns false otherwise.
143  // This does NOT check that a directory (or file) actually exists.
144  bool IsDirectory() const;
145
146 private:
147  String pathname_;
148
149  // Don't implement operator= because it is banned by the style guide.
150  FilePath& operator=(const FilePath& rhs);
151};  // class FilePath
152
153}  // namespace internal
154}  // namespace testing
155
156#endif  // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_
157