1848b8605Smrg// Copyright 2008, Google Inc.
2848b8605Smrg// All rights reserved.
3848b8605Smrg//
4848b8605Smrg// Redistribution and use in source and binary forms, with or without
5848b8605Smrg// modification, are permitted provided that the following conditions are
6848b8605Smrg// met:
7848b8605Smrg//
8848b8605Smrg//     * Redistributions of source code must retain the above copyright
9848b8605Smrg// notice, this list of conditions and the following disclaimer.
10848b8605Smrg//     * Redistributions in binary form must reproduce the above
11848b8605Smrg// copyright notice, this list of conditions and the following disclaimer
12848b8605Smrg// in the documentation and/or other materials provided with the
13848b8605Smrg// distribution.
14848b8605Smrg//     * Neither the name of Google Inc. nor the names of its
15848b8605Smrg// contributors may be used to endorse or promote products derived from
16848b8605Smrg// this software without specific prior written permission.
17848b8605Smrg//
18848b8605Smrg// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19848b8605Smrg// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20848b8605Smrg// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21848b8605Smrg// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22848b8605Smrg// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23848b8605Smrg// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24848b8605Smrg// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25848b8605Smrg// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26848b8605Smrg// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27848b8605Smrg// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28848b8605Smrg// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29848b8605Smrg//
30848b8605Smrg// Author: keith.ray@gmail.com (Keith Ray)
31848b8605Smrg//
32848b8605Smrg// Google Test filepath utilities
33848b8605Smrg//
34848b8605Smrg// This header file declares classes and functions used internally by
35848b8605Smrg// Google Test.  They are subject to change without notice.
36848b8605Smrg//
37848b8605Smrg// This file is #included in <gtest/internal/gtest-internal.h>.
38848b8605Smrg// Do not include this header file separately!
39848b8605Smrg
40848b8605Smrg#ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_
41848b8605Smrg#define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_
42848b8605Smrg
43848b8605Smrg#include "gtest/internal/gtest-string.h"
44848b8605Smrg
45848b8605Smrgnamespace testing {
46848b8605Smrgnamespace internal {
47848b8605Smrg
48848b8605Smrg// FilePath - a class for file and directory pathname manipulation which
49848b8605Smrg// handles platform-specific conventions (like the pathname separator).
50848b8605Smrg// Used for helper functions for naming files in a directory for xml output.
51848b8605Smrg// Except for Set methods, all methods are const or static, which provides an
52848b8605Smrg// "immutable value object" -- useful for peace of mind.
53848b8605Smrg// A FilePath with a value ending in a path separator ("like/this/") represents
54848b8605Smrg// a directory, otherwise it is assumed to represent a file. In either case,
55848b8605Smrg// it may or may not represent an actual file or directory in the file system.
56848b8605Smrg// Names are NOT checked for syntax correctness -- no checking for illegal
57848b8605Smrg// characters, malformed paths, etc.
58848b8605Smrg
59848b8605Smrgclass GTEST_API_ FilePath {
60848b8605Smrg public:
61848b8605Smrg  FilePath() : pathname_("") { }
62848b8605Smrg  FilePath(const FilePath& rhs) : pathname_(rhs.pathname_) { }
63848b8605Smrg
64848b8605Smrg  explicit FilePath(const std::string& pathname) : pathname_(pathname) {
65848b8605Smrg    Normalize();
66848b8605Smrg  }
67848b8605Smrg
68848b8605Smrg  FilePath& operator=(const FilePath& rhs) {
69848b8605Smrg    Set(rhs);
70848b8605Smrg    return *this;
71848b8605Smrg  }
72848b8605Smrg
73848b8605Smrg  void Set(const FilePath& rhs) {
74848b8605Smrg    pathname_ = rhs.pathname_;
75848b8605Smrg  }
76848b8605Smrg
77848b8605Smrg  const std::string& string() const { return pathname_; }
78848b8605Smrg  const char* c_str() const { return pathname_.c_str(); }
79848b8605Smrg
80848b8605Smrg  // Returns the current working directory, or "" if unsuccessful.
81848b8605Smrg  static FilePath GetCurrentDir();
82848b8605Smrg
83848b8605Smrg  // Given directory = "dir", base_name = "test", number = 0,
84848b8605Smrg  // extension = "xml", returns "dir/test.xml". If number is greater
85848b8605Smrg  // than zero (e.g., 12), returns "dir/test_12.xml".
86848b8605Smrg  // On Windows platform, uses \ as the separator rather than /.
87848b8605Smrg  static FilePath MakeFileName(const FilePath& directory,
88848b8605Smrg                               const FilePath& base_name,
89848b8605Smrg                               int number,
90848b8605Smrg                               const char* extension);
91848b8605Smrg
92848b8605Smrg  // Given directory = "dir", relative_path = "test.xml",
93848b8605Smrg  // returns "dir/test.xml".
94848b8605Smrg  // On Windows, uses \ as the separator rather than /.
95848b8605Smrg  static FilePath ConcatPaths(const FilePath& directory,
96848b8605Smrg                              const FilePath& relative_path);
97848b8605Smrg
98848b8605Smrg  // Returns a pathname for a file that does not currently exist. The pathname
99848b8605Smrg  // will be directory/base_name.extension or
100848b8605Smrg  // directory/base_name_<number>.extension if directory/base_name.extension
101848b8605Smrg  // already exists. The number will be incremented until a pathname is found
102848b8605Smrg  // that does not already exist.
103848b8605Smrg  // Examples: 'dir/foo_test.xml' or 'dir/foo_test_1.xml'.
104848b8605Smrg  // There could be a race condition if two or more processes are calling this
105848b8605Smrg  // function at the same time -- they could both pick the same filename.
106848b8605Smrg  static FilePath GenerateUniqueFileName(const FilePath& directory,
107848b8605Smrg                                         const FilePath& base_name,
108848b8605Smrg                                         const char* extension);
109848b8605Smrg
110848b8605Smrg  // Returns true iff the path is "".
111848b8605Smrg  bool IsEmpty() const { return pathname_.empty(); }
112848b8605Smrg
113848b8605Smrg  // If input name has a trailing separator character, removes it and returns
114848b8605Smrg  // the name, otherwise return the name string unmodified.
115848b8605Smrg  // On Windows platform, uses \ as the separator, other platforms use /.
116848b8605Smrg  FilePath RemoveTrailingPathSeparator() const;
117848b8605Smrg
118848b8605Smrg  // Returns a copy of the FilePath with the directory part removed.
119848b8605Smrg  // Example: FilePath("path/to/file").RemoveDirectoryName() returns
120848b8605Smrg  // FilePath("file"). If there is no directory part ("just_a_file"), it returns
121848b8605Smrg  // the FilePath unmodified. If there is no file part ("just_a_dir/") it
122848b8605Smrg  // returns an empty FilePath ("").
123848b8605Smrg  // On Windows platform, '\' is the path separator, otherwise it is '/'.
124848b8605Smrg  FilePath RemoveDirectoryName() const;
125848b8605Smrg
126848b8605Smrg  // RemoveFileName returns the directory path with the filename removed.
127848b8605Smrg  // Example: FilePath("path/to/file").RemoveFileName() returns "path/to/".
128848b8605Smrg  // If the FilePath is "a_file" or "/a_file", RemoveFileName returns
129848b8605Smrg  // FilePath("./") or, on Windows, FilePath(".\\"). If the filepath does
130848b8605Smrg  // not have a file, like "just/a/dir/", it returns the FilePath unmodified.
131848b8605Smrg  // On Windows platform, '\' is the path separator, otherwise it is '/'.
132848b8605Smrg  FilePath RemoveFileName() const;
133848b8605Smrg
134848b8605Smrg  // Returns a copy of the FilePath with the case-insensitive extension removed.
135848b8605Smrg  // Example: FilePath("dir/file.exe").RemoveExtension("EXE") returns
136848b8605Smrg  // FilePath("dir/file"). If a case-insensitive extension is not
137848b8605Smrg  // found, returns a copy of the original FilePath.
138848b8605Smrg  FilePath RemoveExtension(const char* extension) const;
139848b8605Smrg
140848b8605Smrg  // Creates directories so that path exists. Returns true if successful or if
141848b8605Smrg  // the directories already exist; returns false if unable to create
142848b8605Smrg  // directories for any reason. Will also return false if the FilePath does
143848b8605Smrg  // not represent a directory (that is, it doesn't end with a path separator).
144848b8605Smrg  bool CreateDirectoriesRecursively() const;
145848b8605Smrg
146848b8605Smrg  // Create the directory so that path exists. Returns true if successful or
147848b8605Smrg  // if the directory already exists; returns false if unable to create the
148848b8605Smrg  // directory for any reason, including if the parent directory does not
149848b8605Smrg  // exist. Not named "CreateDirectory" because that's a macro on Windows.
150848b8605Smrg  bool CreateFolder() const;
151848b8605Smrg
152848b8605Smrg  // Returns true if FilePath describes something in the file-system,
153848b8605Smrg  // either a file, directory, or whatever, and that something exists.
154848b8605Smrg  bool FileOrDirectoryExists() const;
155848b8605Smrg
156848b8605Smrg  // Returns true if pathname describes a directory in the file-system
157848b8605Smrg  // that exists.
158848b8605Smrg  bool DirectoryExists() const;
159848b8605Smrg
160848b8605Smrg  // Returns true if FilePath ends with a path separator, which indicates that
161848b8605Smrg  // it is intended to represent a directory. Returns false otherwise.
162848b8605Smrg  // This does NOT check that a directory (or file) actually exists.
163848b8605Smrg  bool IsDirectory() const;
164848b8605Smrg
165848b8605Smrg  // Returns true if pathname describes a root directory. (Windows has one
166848b8605Smrg  // root directory per disk drive.)
167848b8605Smrg  bool IsRootDirectory() const;
168848b8605Smrg
169848b8605Smrg  // Returns true if pathname describes an absolute path.
170848b8605Smrg  bool IsAbsolutePath() const;
171848b8605Smrg
172848b8605Smrg private:
173848b8605Smrg  // Replaces multiple consecutive separators with a single separator.
174848b8605Smrg  // For example, "bar///foo" becomes "bar/foo". Does not eliminate other
175848b8605Smrg  // redundancies that might be in a pathname involving "." or "..".
176848b8605Smrg  //
177848b8605Smrg  // A pathname with multiple consecutive separators may occur either through
178848b8605Smrg  // user error or as a result of some scripts or APIs that generate a pathname
179848b8605Smrg  // with a trailing separator. On other platforms the same API or script
180848b8605Smrg  // may NOT generate a pathname with a trailing "/". Then elsewhere that
181848b8605Smrg  // pathname may have another "/" and pathname components added to it,
182848b8605Smrg  // without checking for the separator already being there.
183848b8605Smrg  // The script language and operating system may allow paths like "foo//bar"
184848b8605Smrg  // but some of the functions in FilePath will not handle that correctly. In
185848b8605Smrg  // particular, RemoveTrailingPathSeparator() only removes one separator, and
186848b8605Smrg  // it is called in CreateDirectoriesRecursively() assuming that it will change
187848b8605Smrg  // a pathname from directory syntax (trailing separator) to filename syntax.
188848b8605Smrg  //
189848b8605Smrg  // On Windows this method also replaces the alternate path separator '/' with
190848b8605Smrg  // the primary path separator '\\', so that for example "bar\\/\\foo" becomes
191848b8605Smrg  // "bar\\foo".
192848b8605Smrg
193848b8605Smrg  void Normalize();
194848b8605Smrg
195848b8605Smrg  // Returns a pointer to the last occurence of a valid path separator in
196848b8605Smrg  // the FilePath. On Windows, for example, both '/' and '\' are valid path
197848b8605Smrg  // separators. Returns NULL if no path separator was found.
198848b8605Smrg  const char* FindLastPathSeparator() const;
199848b8605Smrg
200848b8605Smrg  std::string pathname_;
201848b8605Smrg};  // class FilePath
202848b8605Smrg
203848b8605Smrg}  // namespace internal
204848b8605Smrg}  // namespace testing
205848b8605Smrg
206848b8605Smrg#endif  // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_
207