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


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Tue Jan 19 2021 03:06:12