media/webrtc/trunk/testing/gtest/src/gtest-filepath.cc

Thu, 15 Jan 2015 15:55:04 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:55:04 +0100
branch
TOR_BUG_9701
changeset 9
a63d609f5ebe
permissions
-rw-r--r--

Back out 97036ab72558 which inappropriately compared turds to third parties.

michael@0 1 // Copyright 2008, Google Inc.
michael@0 2 // All rights reserved.
michael@0 3 //
michael@0 4 // Redistribution and use in source and binary forms, with or without
michael@0 5 // modification, are permitted provided that the following conditions are
michael@0 6 // met:
michael@0 7 //
michael@0 8 // * Redistributions of source code must retain the above copyright
michael@0 9 // notice, this list of conditions and the following disclaimer.
michael@0 10 // * Redistributions in binary form must reproduce the above
michael@0 11 // copyright notice, this list of conditions and the following disclaimer
michael@0 12 // in the documentation and/or other materials provided with the
michael@0 13 // distribution.
michael@0 14 // * Neither the name of Google Inc. nor the names of its
michael@0 15 // contributors may be used to endorse or promote products derived from
michael@0 16 // this software without specific prior written permission.
michael@0 17 //
michael@0 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
michael@0 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@0 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@0 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 29 //
michael@0 30 // Authors: keith.ray@gmail.com (Keith Ray)
michael@0 31
michael@0 32 #include "gtest/internal/gtest-filepath.h"
michael@0 33 #include "gtest/internal/gtest-port.h"
michael@0 34
michael@0 35 #include <stdlib.h>
michael@0 36
michael@0 37 #if GTEST_OS_WINDOWS_MOBILE
michael@0 38 # include <windows.h>
michael@0 39 #elif GTEST_OS_WINDOWS
michael@0 40 # include <direct.h>
michael@0 41 # include <io.h>
michael@0 42 #elif GTEST_OS_SYMBIAN
michael@0 43 // Symbian OpenC has PATH_MAX in sys/syslimits.h
michael@0 44 # include <sys/syslimits.h>
michael@0 45 #else
michael@0 46 # include <limits.h>
michael@0 47 # include <climits> // Some Linux distributions define PATH_MAX here.
michael@0 48 #endif // GTEST_OS_WINDOWS_MOBILE
michael@0 49
michael@0 50 #if GTEST_OS_WINDOWS
michael@0 51 # define GTEST_PATH_MAX_ _MAX_PATH
michael@0 52 #elif defined(PATH_MAX)
michael@0 53 # define GTEST_PATH_MAX_ PATH_MAX
michael@0 54 #elif defined(_XOPEN_PATH_MAX)
michael@0 55 # define GTEST_PATH_MAX_ _XOPEN_PATH_MAX
michael@0 56 #else
michael@0 57 # define GTEST_PATH_MAX_ _POSIX_PATH_MAX
michael@0 58 #endif // GTEST_OS_WINDOWS
michael@0 59
michael@0 60 #include "gtest/internal/gtest-string.h"
michael@0 61
michael@0 62 namespace testing {
michael@0 63 namespace internal {
michael@0 64
michael@0 65 #if GTEST_OS_WINDOWS
michael@0 66 // On Windows, '\\' is the standard path separator, but many tools and the
michael@0 67 // Windows API also accept '/' as an alternate path separator. Unless otherwise
michael@0 68 // noted, a file path can contain either kind of path separators, or a mixture
michael@0 69 // of them.
michael@0 70 const char kPathSeparator = '\\';
michael@0 71 const char kAlternatePathSeparator = '/';
michael@0 72 const char kPathSeparatorString[] = "\\";
michael@0 73 const char kAlternatePathSeparatorString[] = "/";
michael@0 74 # if GTEST_OS_WINDOWS_MOBILE
michael@0 75 // Windows CE doesn't have a current directory. You should not use
michael@0 76 // the current directory in tests on Windows CE, but this at least
michael@0 77 // provides a reasonable fallback.
michael@0 78 const char kCurrentDirectoryString[] = "\\";
michael@0 79 // Windows CE doesn't define INVALID_FILE_ATTRIBUTES
michael@0 80 const DWORD kInvalidFileAttributes = 0xffffffff;
michael@0 81 # else
michael@0 82 const char kCurrentDirectoryString[] = ".\\";
michael@0 83 # endif // GTEST_OS_WINDOWS_MOBILE
michael@0 84 #else
michael@0 85 const char kPathSeparator = '/';
michael@0 86 const char kPathSeparatorString[] = "/";
michael@0 87 const char kCurrentDirectoryString[] = "./";
michael@0 88 #endif // GTEST_OS_WINDOWS
michael@0 89
michael@0 90 // Returns whether the given character is a valid path separator.
michael@0 91 static bool IsPathSeparator(char c) {
michael@0 92 #if GTEST_HAS_ALT_PATH_SEP_
michael@0 93 return (c == kPathSeparator) || (c == kAlternatePathSeparator);
michael@0 94 #else
michael@0 95 return c == kPathSeparator;
michael@0 96 #endif
michael@0 97 }
michael@0 98
michael@0 99 // Returns the current working directory, or "" if unsuccessful.
michael@0 100 FilePath FilePath::GetCurrentDir() {
michael@0 101 #if GTEST_OS_WINDOWS_MOBILE
michael@0 102 // Windows CE doesn't have a current directory, so we just return
michael@0 103 // something reasonable.
michael@0 104 return FilePath(kCurrentDirectoryString);
michael@0 105 #elif GTEST_OS_WINDOWS
michael@0 106 char cwd[GTEST_PATH_MAX_ + 1] = { '\0' };
michael@0 107 return FilePath(_getcwd(cwd, sizeof(cwd)) == NULL ? "" : cwd);
michael@0 108 #else
michael@0 109 char cwd[GTEST_PATH_MAX_ + 1] = { '\0' };
michael@0 110 return FilePath(getcwd(cwd, sizeof(cwd)) == NULL ? "" : cwd);
michael@0 111 #endif // GTEST_OS_WINDOWS_MOBILE
michael@0 112 }
michael@0 113
michael@0 114 // Returns a copy of the FilePath with the case-insensitive extension removed.
michael@0 115 // Example: FilePath("dir/file.exe").RemoveExtension("EXE") returns
michael@0 116 // FilePath("dir/file"). If a case-insensitive extension is not
michael@0 117 // found, returns a copy of the original FilePath.
michael@0 118 FilePath FilePath::RemoveExtension(const char* extension) const {
michael@0 119 String dot_extension(String::Format(".%s", extension));
michael@0 120 if (pathname_.EndsWithCaseInsensitive(dot_extension.c_str())) {
michael@0 121 return FilePath(String(pathname_.c_str(), pathname_.length() - 4));
michael@0 122 }
michael@0 123 return *this;
michael@0 124 }
michael@0 125
michael@0 126 // Returns a pointer to the last occurence of a valid path separator in
michael@0 127 // the FilePath. On Windows, for example, both '/' and '\' are valid path
michael@0 128 // separators. Returns NULL if no path separator was found.
michael@0 129 const char* FilePath::FindLastPathSeparator() const {
michael@0 130 const char* const last_sep = strrchr(c_str(), kPathSeparator);
michael@0 131 #if GTEST_HAS_ALT_PATH_SEP_
michael@0 132 const char* const last_alt_sep = strrchr(c_str(), kAlternatePathSeparator);
michael@0 133 // Comparing two pointers of which only one is NULL is undefined.
michael@0 134 if (last_alt_sep != NULL &&
michael@0 135 (last_sep == NULL || last_alt_sep > last_sep)) {
michael@0 136 return last_alt_sep;
michael@0 137 }
michael@0 138 #endif
michael@0 139 return last_sep;
michael@0 140 }
michael@0 141
michael@0 142 // Returns a copy of the FilePath with the directory part removed.
michael@0 143 // Example: FilePath("path/to/file").RemoveDirectoryName() returns
michael@0 144 // FilePath("file"). If there is no directory part ("just_a_file"), it returns
michael@0 145 // the FilePath unmodified. If there is no file part ("just_a_dir/") it
michael@0 146 // returns an empty FilePath ("").
michael@0 147 // On Windows platform, '\' is the path separator, otherwise it is '/'.
michael@0 148 FilePath FilePath::RemoveDirectoryName() const {
michael@0 149 const char* const last_sep = FindLastPathSeparator();
michael@0 150 return last_sep ? FilePath(String(last_sep + 1)) : *this;
michael@0 151 }
michael@0 152
michael@0 153 // RemoveFileName returns the directory path with the filename removed.
michael@0 154 // Example: FilePath("path/to/file").RemoveFileName() returns "path/to/".
michael@0 155 // If the FilePath is "a_file" or "/a_file", RemoveFileName returns
michael@0 156 // FilePath("./") or, on Windows, FilePath(".\\"). If the filepath does
michael@0 157 // not have a file, like "just/a/dir/", it returns the FilePath unmodified.
michael@0 158 // On Windows platform, '\' is the path separator, otherwise it is '/'.
michael@0 159 FilePath FilePath::RemoveFileName() const {
michael@0 160 const char* const last_sep = FindLastPathSeparator();
michael@0 161 String dir;
michael@0 162 if (last_sep) {
michael@0 163 dir = String(c_str(), last_sep + 1 - c_str());
michael@0 164 } else {
michael@0 165 dir = kCurrentDirectoryString;
michael@0 166 }
michael@0 167 return FilePath(dir);
michael@0 168 }
michael@0 169
michael@0 170 // Helper functions for naming files in a directory for xml output.
michael@0 171
michael@0 172 // Given directory = "dir", base_name = "test", number = 0,
michael@0 173 // extension = "xml", returns "dir/test.xml". If number is greater
michael@0 174 // than zero (e.g., 12), returns "dir/test_12.xml".
michael@0 175 // On Windows platform, uses \ as the separator rather than /.
michael@0 176 FilePath FilePath::MakeFileName(const FilePath& directory,
michael@0 177 const FilePath& base_name,
michael@0 178 int number,
michael@0 179 const char* extension) {
michael@0 180 String file;
michael@0 181 if (number == 0) {
michael@0 182 file = String::Format("%s.%s", base_name.c_str(), extension);
michael@0 183 } else {
michael@0 184 file = String::Format("%s_%d.%s", base_name.c_str(), number, extension);
michael@0 185 }
michael@0 186 return ConcatPaths(directory, FilePath(file));
michael@0 187 }
michael@0 188
michael@0 189 // Given directory = "dir", relative_path = "test.xml", returns "dir/test.xml".
michael@0 190 // On Windows, uses \ as the separator rather than /.
michael@0 191 FilePath FilePath::ConcatPaths(const FilePath& directory,
michael@0 192 const FilePath& relative_path) {
michael@0 193 if (directory.IsEmpty())
michael@0 194 return relative_path;
michael@0 195 const FilePath dir(directory.RemoveTrailingPathSeparator());
michael@0 196 return FilePath(String::Format("%s%c%s", dir.c_str(), kPathSeparator,
michael@0 197 relative_path.c_str()));
michael@0 198 }
michael@0 199
michael@0 200 // Returns true if pathname describes something findable in the file-system,
michael@0 201 // either a file, directory, or whatever.
michael@0 202 bool FilePath::FileOrDirectoryExists() const {
michael@0 203 #if GTEST_OS_WINDOWS_MOBILE
michael@0 204 LPCWSTR unicode = String::AnsiToUtf16(pathname_.c_str());
michael@0 205 const DWORD attributes = GetFileAttributes(unicode);
michael@0 206 delete [] unicode;
michael@0 207 return attributes != kInvalidFileAttributes;
michael@0 208 #else
michael@0 209 posix::StatStruct file_stat;
michael@0 210 return posix::Stat(pathname_.c_str(), &file_stat) == 0;
michael@0 211 #endif // GTEST_OS_WINDOWS_MOBILE
michael@0 212 }
michael@0 213
michael@0 214 // Returns true if pathname describes a directory in the file-system
michael@0 215 // that exists.
michael@0 216 bool FilePath::DirectoryExists() const {
michael@0 217 bool result = false;
michael@0 218 #if GTEST_OS_WINDOWS
michael@0 219 // Don't strip off trailing separator if path is a root directory on
michael@0 220 // Windows (like "C:\\").
michael@0 221 const FilePath& path(IsRootDirectory() ? *this :
michael@0 222 RemoveTrailingPathSeparator());
michael@0 223 #else
michael@0 224 const FilePath& path(*this);
michael@0 225 #endif
michael@0 226
michael@0 227 #if GTEST_OS_WINDOWS_MOBILE
michael@0 228 LPCWSTR unicode = String::AnsiToUtf16(path.c_str());
michael@0 229 const DWORD attributes = GetFileAttributes(unicode);
michael@0 230 delete [] unicode;
michael@0 231 if ((attributes != kInvalidFileAttributes) &&
michael@0 232 (attributes & FILE_ATTRIBUTE_DIRECTORY)) {
michael@0 233 result = true;
michael@0 234 }
michael@0 235 #else
michael@0 236 posix::StatStruct file_stat;
michael@0 237 result = posix::Stat(path.c_str(), &file_stat) == 0 &&
michael@0 238 posix::IsDir(file_stat);
michael@0 239 #endif // GTEST_OS_WINDOWS_MOBILE
michael@0 240
michael@0 241 return result;
michael@0 242 }
michael@0 243
michael@0 244 // Returns true if pathname describes a root directory. (Windows has one
michael@0 245 // root directory per disk drive.)
michael@0 246 bool FilePath::IsRootDirectory() const {
michael@0 247 #if GTEST_OS_WINDOWS
michael@0 248 // TODO(wan@google.com): on Windows a network share like
michael@0 249 // \\server\share can be a root directory, although it cannot be the
michael@0 250 // current directory. Handle this properly.
michael@0 251 return pathname_.length() == 3 && IsAbsolutePath();
michael@0 252 #else
michael@0 253 return pathname_.length() == 1 && IsPathSeparator(pathname_.c_str()[0]);
michael@0 254 #endif
michael@0 255 }
michael@0 256
michael@0 257 // Returns true if pathname describes an absolute path.
michael@0 258 bool FilePath::IsAbsolutePath() const {
michael@0 259 const char* const name = pathname_.c_str();
michael@0 260 #if GTEST_OS_WINDOWS
michael@0 261 return pathname_.length() >= 3 &&
michael@0 262 ((name[0] >= 'a' && name[0] <= 'z') ||
michael@0 263 (name[0] >= 'A' && name[0] <= 'Z')) &&
michael@0 264 name[1] == ':' &&
michael@0 265 IsPathSeparator(name[2]);
michael@0 266 #else
michael@0 267 return IsPathSeparator(name[0]);
michael@0 268 #endif
michael@0 269 }
michael@0 270
michael@0 271 // Returns a pathname for a file that does not currently exist. The pathname
michael@0 272 // will be directory/base_name.extension or
michael@0 273 // directory/base_name_<number>.extension if directory/base_name.extension
michael@0 274 // already exists. The number will be incremented until a pathname is found
michael@0 275 // that does not already exist.
michael@0 276 // Examples: 'dir/foo_test.xml' or 'dir/foo_test_1.xml'.
michael@0 277 // There could be a race condition if two or more processes are calling this
michael@0 278 // function at the same time -- they could both pick the same filename.
michael@0 279 FilePath FilePath::GenerateUniqueFileName(const FilePath& directory,
michael@0 280 const FilePath& base_name,
michael@0 281 const char* extension) {
michael@0 282 FilePath full_pathname;
michael@0 283 int number = 0;
michael@0 284 do {
michael@0 285 full_pathname.Set(MakeFileName(directory, base_name, number++, extension));
michael@0 286 } while (full_pathname.FileOrDirectoryExists());
michael@0 287 return full_pathname;
michael@0 288 }
michael@0 289
michael@0 290 // Returns true if FilePath ends with a path separator, which indicates that
michael@0 291 // it is intended to represent a directory. Returns false otherwise.
michael@0 292 // This does NOT check that a directory (or file) actually exists.
michael@0 293 bool FilePath::IsDirectory() const {
michael@0 294 return !pathname_.empty() &&
michael@0 295 IsPathSeparator(pathname_.c_str()[pathname_.length() - 1]);
michael@0 296 }
michael@0 297
michael@0 298 // Create directories so that path exists. Returns true if successful or if
michael@0 299 // the directories already exist; returns false if unable to create directories
michael@0 300 // for any reason.
michael@0 301 bool FilePath::CreateDirectoriesRecursively() const {
michael@0 302 if (!this->IsDirectory()) {
michael@0 303 return false;
michael@0 304 }
michael@0 305
michael@0 306 if (pathname_.length() == 0 || this->DirectoryExists()) {
michael@0 307 return true;
michael@0 308 }
michael@0 309
michael@0 310 const FilePath parent(this->RemoveTrailingPathSeparator().RemoveFileName());
michael@0 311 return parent.CreateDirectoriesRecursively() && this->CreateFolder();
michael@0 312 }
michael@0 313
michael@0 314 // Create the directory so that path exists. Returns true if successful or
michael@0 315 // if the directory already exists; returns false if unable to create the
michael@0 316 // directory for any reason, including if the parent directory does not
michael@0 317 // exist. Not named "CreateDirectory" because that's a macro on Windows.
michael@0 318 bool FilePath::CreateFolder() const {
michael@0 319 #if GTEST_OS_WINDOWS_MOBILE
michael@0 320 FilePath removed_sep(this->RemoveTrailingPathSeparator());
michael@0 321 LPCWSTR unicode = String::AnsiToUtf16(removed_sep.c_str());
michael@0 322 int result = CreateDirectory(unicode, NULL) ? 0 : -1;
michael@0 323 delete [] unicode;
michael@0 324 #elif GTEST_OS_WINDOWS
michael@0 325 int result = _mkdir(pathname_.c_str());
michael@0 326 #else
michael@0 327 int result = mkdir(pathname_.c_str(), 0777);
michael@0 328 #endif // GTEST_OS_WINDOWS_MOBILE
michael@0 329
michael@0 330 if (result == -1) {
michael@0 331 return this->DirectoryExists(); // An error is OK if the directory exists.
michael@0 332 }
michael@0 333 return true; // No error.
michael@0 334 }
michael@0 335
michael@0 336 // If input name has a trailing separator character, remove it and return the
michael@0 337 // name, otherwise return the name string unmodified.
michael@0 338 // On Windows platform, uses \ as the separator, other platforms use /.
michael@0 339 FilePath FilePath::RemoveTrailingPathSeparator() const {
michael@0 340 return IsDirectory()
michael@0 341 ? FilePath(String(pathname_.c_str(), pathname_.length() - 1))
michael@0 342 : *this;
michael@0 343 }
michael@0 344
michael@0 345 // Removes any redundant separators that might be in the pathname.
michael@0 346 // For example, "bar///foo" becomes "bar/foo". Does not eliminate other
michael@0 347 // redundancies that might be in a pathname involving "." or "..".
michael@0 348 // TODO(wan@google.com): handle Windows network shares (e.g. \\server\share).
michael@0 349 void FilePath::Normalize() {
michael@0 350 if (pathname_.c_str() == NULL) {
michael@0 351 pathname_ = "";
michael@0 352 return;
michael@0 353 }
michael@0 354 const char* src = pathname_.c_str();
michael@0 355 char* const dest = new char[pathname_.length() + 1];
michael@0 356 char* dest_ptr = dest;
michael@0 357 memset(dest_ptr, 0, pathname_.length() + 1);
michael@0 358
michael@0 359 while (*src != '\0') {
michael@0 360 *dest_ptr = *src;
michael@0 361 if (!IsPathSeparator(*src)) {
michael@0 362 src++;
michael@0 363 } else {
michael@0 364 #if GTEST_HAS_ALT_PATH_SEP_
michael@0 365 if (*dest_ptr == kAlternatePathSeparator) {
michael@0 366 *dest_ptr = kPathSeparator;
michael@0 367 }
michael@0 368 #endif
michael@0 369 while (IsPathSeparator(*src))
michael@0 370 src++;
michael@0 371 }
michael@0 372 dest_ptr++;
michael@0 373 }
michael@0 374 *dest_ptr = '\0';
michael@0 375 pathname_ = dest;
michael@0 376 delete[] dest;
michael@0 377 }
michael@0 378
michael@0 379 } // namespace internal
michael@0 380 } // namespace testing

mercurial