Allow 8 bit select in custom inserter
[llvm/msp430.git] / utils / unittest / googletest / gtest-filepath.cc
blob640c27c313c3d6b6f60a24c2d7e52c38ceeb0a88
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.
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.
30 // Authors: keith.ray@gmail.com (Keith Ray)
32 #include <gtest/internal/gtest-filepath.h>
33 #include <gtest/internal/gtest-port.h>
35 #include <stdlib.h>
37 #ifdef _WIN32_WCE
38 #include <windows.h>
39 #elif defined(GTEST_OS_WINDOWS)
40 #include <direct.h>
41 #include <io.h>
42 #include <sys/stat.h>
43 #elif defined(GTEST_OS_SYMBIAN)
44 // Symbian OpenC has PATH_MAX in sys/syslimits.h
45 #include <sys/syslimits.h>
46 #include <unistd.h>
47 #else
48 #include <limits.h>
49 #include <sys/stat.h>
50 #include <unistd.h>
51 #endif // _WIN32_WCE or _WIN32
53 #ifdef GTEST_OS_WINDOWS
54 #define GTEST_PATH_MAX_ _MAX_PATH
55 #elif defined(PATH_MAX)
56 #define GTEST_PATH_MAX_ PATH_MAX
57 #elif defined(_XOPEN_PATH_MAX)
58 #define GTEST_PATH_MAX_ _XOPEN_PATH_MAX
59 #else
60 #define GTEST_PATH_MAX_ _POSIX_PATH_MAX
61 #endif // GTEST_OS_WINDOWS
63 #include <gtest/internal/gtest-string.h>
65 namespace testing {
66 namespace internal {
68 #ifdef GTEST_OS_WINDOWS
69 const char kPathSeparator = '\\';
70 const char kPathSeparatorString[] = "\\";
71 #ifdef _WIN32_WCE
72 // Windows CE doesn't have a current directory. You should not use
73 // the current directory in tests on Windows CE, but this at least
74 // provides a reasonable fallback.
75 const char kCurrentDirectoryString[] = "\\";
76 // Windows CE doesn't define INVALID_FILE_ATTRIBUTES
77 const DWORD kInvalidFileAttributes = 0xffffffff;
78 #else
79 const char kCurrentDirectoryString[] = ".\\";
80 #endif // _WIN32_WCE
81 #else
82 const char kPathSeparator = '/';
83 const char kPathSeparatorString[] = "/";
84 const char kCurrentDirectoryString[] = "./";
85 #endif // GTEST_OS_WINDOWS
87 // Returns the current working directory, or "" if unsuccessful.
88 FilePath FilePath::GetCurrentDir() {
89 #ifdef _WIN32_WCE
90 // Windows CE doesn't have a current directory, so we just return
91 // something reasonable.
92 return FilePath(kCurrentDirectoryString);
93 #elif defined(GTEST_OS_WINDOWS)
94 char cwd[GTEST_PATH_MAX_ + 1] = {};
95 return FilePath(_getcwd(cwd, sizeof(cwd)) == NULL ? "" : cwd);
96 #else
97 char cwd[GTEST_PATH_MAX_ + 1] = {};
98 return FilePath(getcwd(cwd, sizeof(cwd)) == NULL ? "" : cwd);
99 #endif
102 // Returns a copy of the FilePath with the case-insensitive extension removed.
103 // Example: FilePath("dir/file.exe").RemoveExtension("EXE") returns
104 // FilePath("dir/file"). If a case-insensitive extension is not
105 // found, returns a copy of the original FilePath.
106 FilePath FilePath::RemoveExtension(const char* extension) const {
107 String dot_extension(String::Format(".%s", extension));
108 if (pathname_.EndsWithCaseInsensitive(dot_extension.c_str())) {
109 return FilePath(String(pathname_.c_str(), pathname_.GetLength() - 4));
111 return *this;
114 // Returns a copy of the FilePath with the directory part removed.
115 // Example: FilePath("path/to/file").RemoveDirectoryName() returns
116 // FilePath("file"). If there is no directory part ("just_a_file"), it returns
117 // the FilePath unmodified. If there is no file part ("just_a_dir/") it
118 // returns an empty FilePath ("").
119 // On Windows platform, '\' is the path separator, otherwise it is '/'.
120 FilePath FilePath::RemoveDirectoryName() const {
121 const char* const last_sep = strrchr(c_str(), kPathSeparator);
122 return last_sep ? FilePath(String(last_sep + 1)) : *this;
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 FilePath::RemoveFileName() const {
132 const char* const last_sep = strrchr(c_str(), kPathSeparator);
133 return FilePath(last_sep ? String(c_str(), last_sep + 1 - c_str())
134 : String(kCurrentDirectoryString));
137 // Helper functions for naming files in a directory for xml output.
139 // Given directory = "dir", base_name = "test", number = 0,
140 // extension = "xml", returns "dir/test.xml". If number is greater
141 // than zero (e.g., 12), returns "dir/test_12.xml".
142 // On Windows platform, uses \ as the separator rather than /.
143 FilePath FilePath::MakeFileName(const FilePath& directory,
144 const FilePath& base_name,
145 int number,
146 const char* extension) {
147 FilePath dir(directory.RemoveTrailingPathSeparator());
148 if (number == 0) {
149 return FilePath(String::Format("%s%c%s.%s", dir.c_str(), kPathSeparator,
150 base_name.c_str(), extension));
152 return FilePath(String::Format("%s%c%s_%d.%s", dir.c_str(), kPathSeparator,
153 base_name.c_str(), number, extension));
156 // Returns true if pathname describes something findable in the file-system,
157 // either a file, directory, or whatever.
158 bool FilePath::FileOrDirectoryExists() const {
159 #ifdef GTEST_OS_WINDOWS
160 #ifdef _WIN32_WCE
161 LPCWSTR unicode = String::AnsiToUtf16(pathname_.c_str());
162 const DWORD attributes = GetFileAttributes(unicode);
163 delete [] unicode;
164 return attributes != kInvalidFileAttributes;
165 #else
166 struct _stat file_stat = {};
167 return _stat(pathname_.c_str(), &file_stat) == 0;
168 #endif // _WIN32_WCE
169 #else
170 struct stat file_stat = {};
171 return stat(pathname_.c_str(), &file_stat) == 0;
172 #endif // GTEST_OS_WINDOWS
175 // Returns true if pathname describes a directory in the file-system
176 // that exists.
177 bool FilePath::DirectoryExists() const {
178 bool result = false;
179 #ifdef GTEST_OS_WINDOWS
180 // Don't strip off trailing separator if path is a root directory on
181 // Windows (like "C:\\").
182 const FilePath& path(IsRootDirectory() ? *this :
183 RemoveTrailingPathSeparator());
184 #ifdef _WIN32_WCE
185 LPCWSTR unicode = String::AnsiToUtf16(path.c_str());
186 const DWORD attributes = GetFileAttributes(unicode);
187 delete [] unicode;
188 if ((attributes != kInvalidFileAttributes) &&
189 (attributes & FILE_ATTRIBUTE_DIRECTORY)) {
190 result = true;
192 #else
193 struct _stat file_stat = {};
194 result = _stat(path.c_str(), &file_stat) == 0 &&
195 (_S_IFDIR & file_stat.st_mode) != 0;
196 #endif // _WIN32_WCE
197 #else
198 struct stat file_stat = {};
199 result = stat(pathname_.c_str(), &file_stat) == 0 &&
200 S_ISDIR(file_stat.st_mode);
201 #endif // GTEST_OS_WINDOWS
202 return result;
205 // Returns true if pathname describes a root directory. (Windows has one
206 // root directory per disk drive.)
207 bool FilePath::IsRootDirectory() const {
208 #ifdef GTEST_OS_WINDOWS
209 const char* const name = pathname_.c_str();
210 return pathname_.GetLength() == 3 &&
211 ((name[0] >= 'a' && name[0] <= 'z') ||
212 (name[0] >= 'A' && name[0] <= 'Z')) &&
213 name[1] == ':' &&
214 name[2] == kPathSeparator;
215 #else
216 return pathname_ == kPathSeparatorString;
217 #endif
220 // Returns a pathname for a file that does not currently exist. The pathname
221 // will be directory/base_name.extension or
222 // directory/base_name_<number>.extension if directory/base_name.extension
223 // already exists. The number will be incremented until a pathname is found
224 // that does not already exist.
225 // Examples: 'dir/foo_test.xml' or 'dir/foo_test_1.xml'.
226 // There could be a race condition if two or more processes are calling this
227 // function at the same time -- they could both pick the same filename.
228 FilePath FilePath::GenerateUniqueFileName(const FilePath& directory,
229 const FilePath& base_name,
230 const char* extension) {
231 FilePath full_pathname;
232 int number = 0;
233 do {
234 full_pathname.Set(MakeFileName(directory, base_name, number++, extension));
235 } while (full_pathname.FileOrDirectoryExists());
236 return full_pathname;
239 // Returns true if FilePath ends with a path separator, which indicates that
240 // it is intended to represent a directory. Returns false otherwise.
241 // This does NOT check that a directory (or file) actually exists.
242 bool FilePath::IsDirectory() const {
243 return pathname_.EndsWith(kPathSeparatorString);
246 // Create directories so that path exists. Returns true if successful or if
247 // the directories already exist; returns false if unable to create directories
248 // for any reason.
249 bool FilePath::CreateDirectoriesRecursively() const {
250 if (!this->IsDirectory()) {
251 return false;
254 if (pathname_.GetLength() == 0 || this->DirectoryExists()) {
255 return true;
258 const FilePath parent(this->RemoveTrailingPathSeparator().RemoveFileName());
259 return parent.CreateDirectoriesRecursively() && this->CreateFolder();
262 // Create the directory so that path exists. Returns true if successful or
263 // if the directory already exists; returns false if unable to create the
264 // directory for any reason, including if the parent directory does not
265 // exist. Not named "CreateDirectory" because that's a macro on Windows.
266 bool FilePath::CreateFolder() const {
267 #ifdef GTEST_OS_WINDOWS
268 #ifdef _WIN32_WCE
269 FilePath removed_sep(this->RemoveTrailingPathSeparator());
270 LPCWSTR unicode = String::AnsiToUtf16(removed_sep.c_str());
271 int result = CreateDirectory(unicode, NULL) ? 0 : -1;
272 delete [] unicode;
273 #else
274 int result = _mkdir(pathname_.c_str());
275 #endif // !WIN32_WCE
276 #else
277 int result = mkdir(pathname_.c_str(), 0777);
278 #endif // _WIN32
279 if (result == -1) {
280 return this->DirectoryExists(); // An error is OK if the directory exists.
282 return true; // No error.
285 // If input name has a trailing separator character, remove it and return the
286 // name, otherwise return the name string unmodified.
287 // On Windows platform, uses \ as the separator, other platforms use /.
288 FilePath FilePath::RemoveTrailingPathSeparator() const {
289 return pathname_.EndsWith(kPathSeparatorString)
290 ? FilePath(String(pathname_.c_str(), pathname_.GetLength() - 1))
291 : *this;
294 // Normalize removes any redundant separators that might be in the pathname.
295 // For example, "bar///foo" becomes "bar/foo". Does not eliminate other
296 // redundancies that might be in a pathname involving "." or "..".
297 void FilePath::Normalize() {
298 if (pathname_.c_str() == NULL) {
299 pathname_ = "";
300 return;
302 const char* src = pathname_.c_str();
303 char* const dest = new char[pathname_.GetLength() + 1];
304 char* dest_ptr = dest;
305 memset(dest_ptr, 0, pathname_.GetLength() + 1);
307 while (*src != '\0') {
308 *dest_ptr++ = *src;
309 if (*src != kPathSeparator)
310 src++;
311 else
312 while (*src == kPathSeparator)
313 src++;
315 *dest_ptr = '\0';
316 pathname_ = dest;
317 delete[] dest;
320 } // namespace internal
321 } // namespace testing