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 //
31 // Google Test filepath utilities
32 //
33 // This header file declares classes and functions used internally by
34 // Google Test.  They are subject to change without notice.
35 //
36 // This file is #included in gtest/internal/gtest-internal.h.
37 // Do not include this header file separately!
38 
39 #ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_
40 #define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_
41 
42 #include "gtest/internal/gtest-string.h"
43 
44 namespace testing {
45 namespace internal {
46 
47 // FilePath - a class for file and directory pathname manipulation which
48 // handles platform-specific conventions (like the pathname separator).
49 // Used for helper functions for naming files in a directory for xml output.
50 // Except for Set methods, all methods are const or static, which provides an
51 // "immutable value object" -- useful for peace of mind.
52 // A FilePath with a value ending in a path separator ("like/this/") represents
53 // a directory, otherwise it is assumed to represent a file. In either case,
54 // it may or may not represent an actual file or directory in the file system.
55 // Names are NOT checked for syntax correctness -- no checking for illegal
56 // characters, malformed paths, etc.
57 
58 class GTEST_API_ FilePath {
59  public:
FilePath()60   FilePath() : pathname_("") { }
FilePath(const FilePath & rhs)61   FilePath(const FilePath& rhs) : pathname_(rhs.pathname_) { }
62 
FilePath(const std::string & pathname)63   explicit FilePath(const std::string& pathname) : pathname_(pathname) {
64     Normalize();
65   }
66 
67   FilePath& operator=(const FilePath& rhs) {
68     Set(rhs);
69     return *this;
70   }
71 
Set(const FilePath & rhs)72   void Set(const FilePath& rhs) {
73     pathname_ = rhs.pathname_;
74   }
75 
string()76   const std::string& string() const { return pathname_; }
c_str()77   const char* c_str() const { return pathname_.c_str(); }
78 
79   // Returns the current working directory, or "" if unsuccessful.
80   static FilePath GetCurrentDir();
81 
82   // Given directory = "dir", base_name = "test", number = 0,
83   // extension = "xml", returns "dir/test.xml". If number is greater
84   // than zero (e.g., 12), returns "dir/test_12.xml".
85   // On Windows platform, uses \ as the separator rather than /.
86   static FilePath MakeFileName(const FilePath& directory,
87                                const FilePath& base_name,
88                                int number,
89                                const char* extension);
90 
91   // Given directory = "dir", relative_path = "test.xml",
92   // returns "dir/test.xml".
93   // On Windows, uses \ as the separator rather than /.
94   static FilePath ConcatPaths(const FilePath& directory,
95                               const FilePath& relative_path);
96 
97   // Returns a pathname for a file that does not currently exist. The pathname
98   // will be directory/base_name.extension or
99   // directory/base_name_<number>.extension if directory/base_name.extension
100   // already exists. The number will be incremented until a pathname is found
101   // that does not already exist.
102   // Examples: 'dir/foo_test.xml' or 'dir/foo_test_1.xml'.
103   // There could be a race condition if two or more processes are calling this
104   // function at the same time -- they could both pick the same filename.
105   static FilePath GenerateUniqueFileName(const FilePath& directory,
106                                          const FilePath& base_name,
107                                          const char* extension);
108 
109   // Returns true iff the path is "".
IsEmpty()110   bool IsEmpty() const { return pathname_.empty(); }
111 
112   // If input name has a trailing separator character, removes it and returns
113   // the name, otherwise return the name string unmodified.
114   // On Windows platform, uses \ as the separator, other platforms use /.
115   FilePath RemoveTrailingPathSeparator() const;
116 
117   // Returns a copy of the FilePath with the directory part removed.
118   // Example: FilePath("path/to/file").RemoveDirectoryName() returns
119   // FilePath("file"). If there is no directory part ("just_a_file"), it returns
120   // the FilePath unmodified. If there is no file part ("just_a_dir/") it
121   // returns an empty FilePath ("").
122   // On Windows platform, '\' is the path separator, otherwise it is '/'.
123   FilePath RemoveDirectoryName() const;
124 
125   // RemoveFileName returns the directory path with the filename removed.
126   // Example: FilePath("path/to/file").RemoveFileName() returns "path/to/".
127   // If the FilePath is "a_file" or "/a_file", RemoveFileName returns
128   // FilePath("./") or, on Windows, FilePath(".\\"). If the filepath does
129   // not have a file, like "just/a/dir/", it returns the FilePath unmodified.
130   // On Windows platform, '\' is the path separator, otherwise it is '/'.
131   FilePath RemoveFileName() const;
132 
133   // Returns a copy of the FilePath with the case-insensitive extension removed.
134   // Example: FilePath("dir/file.exe").RemoveExtension("EXE") returns
135   // FilePath("dir/file"). If a case-insensitive extension is not
136   // found, returns a copy of the original FilePath.
137   FilePath RemoveExtension(const char* extension) const;
138 
139   // Creates directories so that path exists. Returns true if successful or if
140   // the directories already exist; returns false if unable to create
141   // directories for any reason. Will also return false if the FilePath does
142   // not represent a directory (that is, it doesn't end with a path separator).
143   bool CreateDirectoriesRecursively() const;
144 
145   // Create the directory so that path exists. Returns true if successful or
146   // if the directory already exists; returns false if unable to create the
147   // directory for any reason, including if the parent directory does not
148   // exist. Not named "CreateDirectory" because that's a macro on Windows.
149   bool CreateFolder() const;
150 
151   // Returns true if FilePath describes something in the file-system,
152   // either a file, directory, or whatever, and that something exists.
153   bool FileOrDirectoryExists() const;
154 
155   // Returns true if pathname describes a directory in the file-system
156   // that exists.
157   bool DirectoryExists() const;
158 
159   // Returns true if FilePath ends with a path separator, which indicates that
160   // it is intended to represent a directory. Returns false otherwise.
161   // This does NOT check that a directory (or file) actually exists.
162   bool IsDirectory() const;
163 
164   // Returns true if pathname describes a root directory. (Windows has one
165   // root directory per disk drive.)
166   bool IsRootDirectory() const;
167 
168   // Returns true if pathname describes an absolute path.
169   bool IsAbsolutePath() const;
170 
171  private:
172   // Replaces multiple consecutive separators with a single separator.
173   // For example, "bar///foo" becomes "bar/foo". Does not eliminate other
174   // redundancies that might be in a pathname involving "." or "..".
175   //
176   // A pathname with multiple consecutive separators may occur either through
177   // user error or as a result of some scripts or APIs that generate a pathname
178   // with a trailing separator. On other platforms the same API or script
179   // may NOT generate a pathname with a trailing "/". Then elsewhere that
180   // pathname may have another "/" and pathname components added to it,
181   // without checking for the separator already being there.
182   // The script language and operating system may allow paths like "foo//bar"
183   // but some of the functions in FilePath will not handle that correctly. In
184   // particular, RemoveTrailingPathSeparator() only removes one separator, and
185   // it is called in CreateDirectoriesRecursively() assuming that it will change
186   // a pathname from directory syntax (trailing separator) to filename syntax.
187   //
188   // On Windows this method also replaces the alternate path separator '/' with
189   // the primary path separator '\\', so that for example "bar\\/\\foo" becomes
190   // "bar\\foo".
191 
192   void Normalize();
193 
194   // Returns a pointer to the last occurence of a valid path separator in
195   // the FilePath. On Windows, for example, both '/' and '\' are valid path
196   // separators. Returns NULL if no path separator was found.
197   const char* FindLastPathSeparator() const;
198 
199   std::string pathname_;
200 };  // class FilePath
201 
202 }  // namespace internal
203 }  // namespace testing
204 
205 #endif  // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_
206