1fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Copyright 2008, Google Inc.
2fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// All rights reserved.
3fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
4fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Redistribution and use in source and binary forms, with or without
5fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// modification, are permitted provided that the following conditions are
6fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// met:
7fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
8fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//     * Redistributions of source code must retain the above copyright
9fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// notice, this list of conditions and the following disclaimer.
10fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//     * Redistributions in binary form must reproduce the above
11fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// copyright notice, this list of conditions and the following disclaimer
12fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// in the documentation and/or other materials provided with the
13fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// distribution.
14fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//     * Neither the name of Google Inc. nor the names of its
15fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// contributors may be used to endorse or promote products derived from
16fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// this software without specific prior written permission.
17fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
18fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
30fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Author: keith.ray@gmail.com (Keith Ray)
31fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
32fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Google Test filepath utilities
33fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
34fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// This header file declares classes and functions used internally by
35fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Google Test.  They are subject to change without notice.
36fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
37fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// This file is #included in <gtest/internal/gtest-internal.h>.
38fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Do not include this header file separately!
39fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
40fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_
41fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_
42fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
43fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#include <gtest/internal/gtest-string.h>
44fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
45fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savillenamespace testing {
46fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savillenamespace internal {
47fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
48fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// FilePath - a class for file and directory pathname manipulation which
49fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// handles platform-specific conventions (like the pathname separator).
50fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Used for helper functions for naming files in a directory for xml output.
51fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Except for Set methods, all methods are const or static, which provides an
52fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// "immutable value object" -- useful for peace of mind.
53fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// A FilePath with a value ending in a path separator ("like/this/") represents
54fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// a directory, otherwise it is assumed to represent a file. In either case,
55fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// it may or may not represent an actual file or directory in the file system.
56fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Names are NOT checked for syntax correctness -- no checking for illegal
57fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// characters, malformed paths, etc.
58fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
59fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleclass FilePath {
60fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville public:
61fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  FilePath() : pathname_("") { }
62fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  FilePath(const FilePath& rhs) : pathname_(rhs.pathname_) { }
63fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
64fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  explicit FilePath(const char* pathname) : pathname_(pathname) {
65fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    Normalize();
66fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  }
67fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
68fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  explicit FilePath(const String& pathname) : pathname_(pathname) {
69fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    Normalize();
70fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  }
71fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
72fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  FilePath& operator=(const FilePath& rhs) {
73fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    Set(rhs);
74fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    return *this;
75fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  }
76fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
77fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  void Set(const FilePath& rhs) {
78fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    pathname_ = rhs.pathname_;
79fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  }
80fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
81fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  String ToString() const { return pathname_; }
82fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  const char* c_str() const { return pathname_.c_str(); }
83fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
84fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Returns the current working directory, or "" if unsuccessful.
85fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  static FilePath GetCurrentDir();
86fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
87fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Given directory = "dir", base_name = "test", number = 0,
88fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // extension = "xml", returns "dir/test.xml". If number is greater
89fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // than zero (e.g., 12), returns "dir/test_12.xml".
90fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // On Windows platform, uses \ as the separator rather than /.
91fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  static FilePath MakeFileName(const FilePath& directory,
92fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville                               const FilePath& base_name,
93fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville                               int number,
94fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville                               const char* extension);
95fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
96fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Given directory = "dir", relative_path = "test.xml",
97fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // returns "dir/test.xml".
98fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // On Windows, uses \ as the separator rather than /.
99fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  static FilePath ConcatPaths(const FilePath& directory,
100fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville                              const FilePath& relative_path);
101fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
102fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Returns a pathname for a file that does not currently exist. The pathname
103fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // will be directory/base_name.extension or
104fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // directory/base_name_<number>.extension if directory/base_name.extension
105fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // already exists. The number will be incremented until a pathname is found
106fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // that does not already exist.
107fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Examples: 'dir/foo_test.xml' or 'dir/foo_test_1.xml'.
108fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // There could be a race condition if two or more processes are calling this
109fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // function at the same time -- they could both pick the same filename.
110fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  static FilePath GenerateUniqueFileName(const FilePath& directory,
111fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville                                         const FilePath& base_name,
112fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville                                         const char* extension);
113fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
114fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Returns true iff the path is NULL or "".
115fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  bool IsEmpty() const { return c_str() == NULL || *c_str() == '\0'; }
116fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
117fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // If input name has a trailing separator character, removes it and returns
118fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // the name, otherwise return the name string unmodified.
119fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // On Windows platform, uses \ as the separator, other platforms use /.
120fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  FilePath RemoveTrailingPathSeparator() const;
121fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
122fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Returns a copy of the FilePath with the directory part removed.
123fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Example: FilePath("path/to/file").RemoveDirectoryName() returns
124fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // FilePath("file"). If there is no directory part ("just_a_file"), it returns
125fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // the FilePath unmodified. If there is no file part ("just_a_dir/") it
126fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // returns an empty FilePath ("").
127fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // On Windows platform, '\' is the path separator, otherwise it is '/'.
128fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  FilePath RemoveDirectoryName() const;
129fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
130fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // RemoveFileName returns the directory path with the filename removed.
131fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Example: FilePath("path/to/file").RemoveFileName() returns "path/to/".
132fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // If the FilePath is "a_file" or "/a_file", RemoveFileName returns
133fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // FilePath("./") or, on Windows, FilePath(".\\"). If the filepath does
134fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // not have a file, like "just/a/dir/", it returns the FilePath unmodified.
135fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // On Windows platform, '\' is the path separator, otherwise it is '/'.
136fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  FilePath RemoveFileName() const;
137fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
138fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Returns a copy of the FilePath with the case-insensitive extension removed.
139fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Example: FilePath("dir/file.exe").RemoveExtension("EXE") returns
140fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // FilePath("dir/file"). If a case-insensitive extension is not
141fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // found, returns a copy of the original FilePath.
142fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  FilePath RemoveExtension(const char* extension) const;
143fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
144fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Creates directories so that path exists. Returns true if successful or if
145fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // the directories already exist; returns false if unable to create
146fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // directories for any reason. Will also return false if the FilePath does
147fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // not represent a directory (that is, it doesn't end with a path separator).
148fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  bool CreateDirectoriesRecursively() const;
149fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
150fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Create the directory so that path exists. Returns true if successful or
151fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // if the directory already exists; returns false if unable to create the
152fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // directory for any reason, including if the parent directory does not
153fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // exist. Not named "CreateDirectory" because that's a macro on Windows.
154fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  bool CreateFolder() const;
155fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
156fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Returns true if FilePath describes something in the file-system,
157fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // either a file, directory, or whatever, and that something exists.
158fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  bool FileOrDirectoryExists() const;
159fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
160fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Returns true if pathname describes a directory in the file-system
161fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // that exists.
162fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  bool DirectoryExists() const;
163fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
164fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Returns true if FilePath ends with a path separator, which indicates that
165fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // it is intended to represent a directory. Returns false otherwise.
166fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // This does NOT check that a directory (or file) actually exists.
167fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  bool IsDirectory() const;
168fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
169fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Returns true if pathname describes a root directory. (Windows has one
170fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // root directory per disk drive.)
171fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  bool IsRootDirectory() const;
172fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
173fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Returns true if pathname describes an absolute path.
174fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  bool IsAbsolutePath() const;
175fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
176fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville private:
177fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Replaces multiple consecutive separators with a single separator.
178fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // For example, "bar///foo" becomes "bar/foo". Does not eliminate other
179fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // redundancies that might be in a pathname involving "." or "..".
180fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  //
181fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // A pathname with multiple consecutive separators may occur either through
182fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // user error or as a result of some scripts or APIs that generate a pathname
183fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // with a trailing separator. On other platforms the same API or script
184fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // may NOT generate a pathname with a trailing "/". Then elsewhere that
185fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // pathname may have another "/" and pathname components added to it,
186fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // without checking for the separator already being there.
187fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // The script language and operating system may allow paths like "foo//bar"
188fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // but some of the functions in FilePath will not handle that correctly. In
189fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // particular, RemoveTrailingPathSeparator() only removes one separator, and
190fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // it is called in CreateDirectoriesRecursively() assuming that it will change
191fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // a pathname from directory syntax (trailing separator) to filename syntax.
192fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
193fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  void Normalize();
194fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
195fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  String pathname_;
196fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville};  // class FilePath
197fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
198fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville}  // namespace internal
199fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville}  // namespace testing
200fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
201fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#endif  // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_
202