michael@0: // Copyright (c) 2008 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #include michael@0: michael@0: #include "base/file_path.h" michael@0: #include "base/logging.h" michael@0: michael@0: // These includes are just for the *Hack functions, and should be removed michael@0: // when those functions are removed. michael@0: #include "base/string_piece.h" michael@0: #include "base/string_util.h" michael@0: #include "base/sys_string_conversions.h" michael@0: michael@0: #if defined(FILE_PATH_USES_WIN_SEPARATORS) michael@0: const FilePath::CharType FilePath::kSeparators[] = FILE_PATH_LITERAL("\\/"); michael@0: #else // FILE_PATH_USES_WIN_SEPARATORS michael@0: const FilePath::CharType FilePath::kSeparators[] = FILE_PATH_LITERAL("/"); michael@0: #endif // FILE_PATH_USES_WIN_SEPARATORS michael@0: michael@0: const FilePath::CharType FilePath::kCurrentDirectory[] = FILE_PATH_LITERAL("."); michael@0: const FilePath::CharType FilePath::kParentDirectory[] = FILE_PATH_LITERAL(".."); michael@0: michael@0: const FilePath::CharType FilePath::kExtensionSeparator = FILE_PATH_LITERAL('.'); michael@0: michael@0: michael@0: namespace { michael@0: michael@0: // If this FilePath contains a drive letter specification, returns the michael@0: // position of the last character of the drive letter specification, michael@0: // otherwise returns npos. This can only be true on Windows, when a pathname michael@0: // begins with a letter followed by a colon. On other platforms, this always michael@0: // returns npos. michael@0: FilePath::StringType::size_type FindDriveLetter( michael@0: const FilePath::StringType& path) { michael@0: #if defined(FILE_PATH_USES_DRIVE_LETTERS) michael@0: // This is dependent on an ASCII-based character set, but that's a michael@0: // reasonable assumption. iswalpha can be too inclusive here. michael@0: if (path.length() >= 2 && path[1] == L':' && michael@0: ((path[0] >= L'A' && path[0] <= L'Z') || michael@0: (path[0] >= L'a' && path[0] <= L'z'))) { michael@0: return 1; michael@0: } michael@0: #endif // FILE_PATH_USES_DRIVE_LETTERS michael@0: return FilePath::StringType::npos; michael@0: } michael@0: michael@0: bool IsPathAbsolute(const FilePath::StringType& path) { michael@0: #if defined(FILE_PATH_USES_DRIVE_LETTERS) michael@0: FilePath::StringType::size_type letter = FindDriveLetter(path); michael@0: if (letter != FilePath::StringType::npos) { michael@0: // Look for a separator right after the drive specification. michael@0: return path.length() > letter + 1 && michael@0: FilePath::IsSeparator(path[letter + 1]); michael@0: } michael@0: // Look for a pair of leading separators. michael@0: return path.length() > 1 && michael@0: FilePath::IsSeparator(path[0]) && FilePath::IsSeparator(path[1]); michael@0: #else // FILE_PATH_USES_DRIVE_LETTERS michael@0: // Look for a separator in the first position. michael@0: return path.length() > 0 && FilePath::IsSeparator(path[0]); michael@0: #endif // FILE_PATH_USES_DRIVE_LETTERS michael@0: } michael@0: michael@0: } // namespace michael@0: michael@0: bool FilePath::IsSeparator(CharType character) { michael@0: for (size_t i = 0; i < arraysize(kSeparators) - 1; ++i) { michael@0: if (character == kSeparators[i]) { michael@0: return true; michael@0: } michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: // libgen's dirname and basename aren't guaranteed to be thread-safe and aren't michael@0: // guaranteed to not modify their input strings, and in fact are implemented michael@0: // differently in this regard on different platforms. Don't use them, but michael@0: // adhere to their behavior. michael@0: FilePath FilePath::DirName() const { michael@0: FilePath new_path(path_); michael@0: new_path.StripTrailingSeparatorsInternal(); michael@0: michael@0: // The drive letter, if any, always needs to remain in the output. If there michael@0: // is no drive letter, as will always be the case on platforms which do not michael@0: // support drive letters, letter will be npos, or -1, so the comparisons and michael@0: // resizes below using letter will still be valid. michael@0: StringType::size_type letter = FindDriveLetter(new_path.path_); michael@0: michael@0: StringType::size_type last_separator = michael@0: new_path.path_.find_last_of(kSeparators, StringType::npos, michael@0: arraysize(kSeparators) - 1); michael@0: if (last_separator == StringType::npos) { michael@0: // path_ is in the current directory. michael@0: new_path.path_.resize(letter + 1); michael@0: } else if (last_separator == letter + 1) { michael@0: // path_ is in the root directory. michael@0: new_path.path_.resize(letter + 2); michael@0: } else if (last_separator == letter + 2 && michael@0: IsSeparator(new_path.path_[letter + 1])) { michael@0: // path_ is in "//" (possibly with a drive letter); leave the double michael@0: // separator intact indicating alternate root. michael@0: new_path.path_.resize(letter + 3); michael@0: } else if (last_separator != 0) { michael@0: // path_ is somewhere else, trim the basename. michael@0: new_path.path_.resize(last_separator); michael@0: } michael@0: michael@0: new_path.StripTrailingSeparatorsInternal(); michael@0: if (!new_path.path_.length()) michael@0: new_path.path_ = kCurrentDirectory; michael@0: michael@0: return new_path; michael@0: } michael@0: michael@0: FilePath FilePath::BaseName() const { michael@0: FilePath new_path(path_); michael@0: new_path.StripTrailingSeparatorsInternal(); michael@0: michael@0: // The drive letter, if any, is always stripped. michael@0: StringType::size_type letter = FindDriveLetter(new_path.path_); michael@0: if (letter != StringType::npos) { michael@0: new_path.path_.erase(0, letter + 1); michael@0: } michael@0: michael@0: // Keep everything after the final separator, but if the pathname is only michael@0: // one character and it's a separator, leave it alone. michael@0: StringType::size_type last_separator = michael@0: new_path.path_.find_last_of(kSeparators, StringType::npos, michael@0: arraysize(kSeparators) - 1); michael@0: if (last_separator != StringType::npos && michael@0: last_separator < new_path.path_.length() - 1) { michael@0: new_path.path_.erase(0, last_separator + 1); michael@0: } michael@0: michael@0: return new_path; michael@0: } michael@0: michael@0: FilePath::StringType FilePath::Extension() const { michael@0: // BaseName() calls StripTrailingSeparators, so cases like /foo.baz/// work. michael@0: StringType base = BaseName().value(); michael@0: michael@0: // Special case "." and ".." michael@0: if (base == kCurrentDirectory || base == kParentDirectory) michael@0: return StringType(); michael@0: michael@0: const StringType::size_type last_dot = base.rfind(kExtensionSeparator); michael@0: if (last_dot == StringType::npos) michael@0: return StringType(); michael@0: return StringType(base, last_dot); michael@0: } michael@0: michael@0: FilePath FilePath::RemoveExtension() const { michael@0: StringType ext = Extension(); michael@0: // It's important to check Extension() since that verifies that the michael@0: // kExtensionSeparator actually appeared in the last path component. michael@0: if (ext.empty()) michael@0: return FilePath(path_); michael@0: // Since Extension() verified that the extension is in fact in the last path michael@0: // component, this substr will effectively strip trailing separators. michael@0: const StringType::size_type last_dot = path_.rfind(kExtensionSeparator); michael@0: return FilePath(path_.substr(0, last_dot)); michael@0: } michael@0: michael@0: FilePath FilePath::InsertBeforeExtension(const StringType& suffix) const { michael@0: if (suffix.empty()) michael@0: return FilePath(path_); michael@0: michael@0: if (path_.empty()) michael@0: return FilePath(); michael@0: michael@0: StringType base = BaseName().value(); michael@0: if (base.empty()) michael@0: return FilePath(); michael@0: if (*(base.end() - 1) == kExtensionSeparator) { michael@0: // Special case "." and ".." michael@0: if (base == kCurrentDirectory || base == kParentDirectory) { michael@0: return FilePath(); michael@0: } michael@0: } michael@0: michael@0: StringType ext = Extension(); michael@0: StringType ret = RemoveExtension().value(); michael@0: ret.append(suffix); michael@0: ret.append(ext); michael@0: return FilePath(ret); michael@0: } michael@0: michael@0: FilePath FilePath::ReplaceExtension(const StringType& extension) const { michael@0: if (path_.empty()) michael@0: return FilePath(); michael@0: michael@0: StringType base = BaseName().value(); michael@0: if (base.empty()) michael@0: return FilePath(); michael@0: if (*(base.end() - 1) == kExtensionSeparator) { michael@0: // Special case "." and ".." michael@0: if (base == kCurrentDirectory || base == kParentDirectory) { michael@0: return FilePath(); michael@0: } michael@0: } michael@0: michael@0: FilePath no_ext = RemoveExtension(); michael@0: // If the new extension is "" or ".", then just remove the current extension. michael@0: if (extension.empty() || extension == StringType(1, kExtensionSeparator)) michael@0: return no_ext; michael@0: michael@0: StringType str = no_ext.value(); michael@0: if (extension[0] != kExtensionSeparator) michael@0: str.append(1, kExtensionSeparator); michael@0: str.append(extension); michael@0: return FilePath(str); michael@0: } michael@0: michael@0: FilePath FilePath::Append(const StringType& component) const { michael@0: DCHECK(!IsPathAbsolute(component)); michael@0: if (path_.compare(kCurrentDirectory) == 0) { michael@0: // Append normally doesn't do any normalization, but as a special case, michael@0: // when appending to kCurrentDirectory, just return a new path for the michael@0: // component argument. Appending component to kCurrentDirectory would michael@0: // serve no purpose other than needlessly lengthening the path, and michael@0: // it's likely in practice to wind up with FilePath objects containing michael@0: // only kCurrentDirectory when calling DirName on a single relative path michael@0: // component. michael@0: return FilePath(component); michael@0: } michael@0: michael@0: FilePath new_path(path_); michael@0: new_path.StripTrailingSeparatorsInternal(); michael@0: michael@0: // Don't append a separator if the path is empty (indicating the current michael@0: // directory) or if the path component is empty (indicating nothing to michael@0: // append). michael@0: if (component.length() > 0 && new_path.path_.length() > 0) { michael@0: michael@0: // Don't append a separator if the path still ends with a trailing michael@0: // separator after stripping (indicating the root directory). michael@0: if (!IsSeparator(new_path.path_[new_path.path_.length() - 1])) { michael@0: michael@0: // Don't append a separator if the path is just a drive letter. michael@0: if (FindDriveLetter(new_path.path_) + 1 != new_path.path_.length()) { michael@0: new_path.path_.append(1, kSeparators[0]); michael@0: } michael@0: } michael@0: } michael@0: michael@0: new_path.path_.append(component); michael@0: return new_path; michael@0: } michael@0: michael@0: FilePath FilePath::Append(const FilePath& component) const { michael@0: return Append(component.value()); michael@0: } michael@0: michael@0: FilePath FilePath::AppendASCII(const std::string& component) const { michael@0: DCHECK(IsStringASCII(component)); michael@0: #if defined(OS_WIN) michael@0: return Append(ASCIIToWide(component)); michael@0: #elif defined(OS_POSIX) michael@0: return Append(component); michael@0: #endif michael@0: } michael@0: michael@0: bool FilePath::IsAbsolute() const { michael@0: return IsPathAbsolute(path_); michael@0: } michael@0: michael@0: #if defined(OS_POSIX) michael@0: // See file_path.h for a discussion of the encoding of paths on POSIX michael@0: // platforms. These *Hack() functions are not quite correct, but they're michael@0: // only temporary while we fix the remainder of the code. michael@0: // Remember to remove the #includes at the top when you remove these. michael@0: michael@0: // static michael@0: FilePath FilePath::FromWStringHack(const std::wstring& wstring) { michael@0: return FilePath(base::SysWideToNativeMB(wstring)); michael@0: } michael@0: std::wstring FilePath::ToWStringHack() const { michael@0: return base::SysNativeMBToWide(path_); michael@0: } michael@0: #elif defined(OS_WIN) michael@0: // static michael@0: FilePath FilePath::FromWStringHack(const std::wstring& wstring) { michael@0: return FilePath(wstring); michael@0: } michael@0: std::wstring FilePath::ToWStringHack() const { michael@0: return path_; michael@0: } michael@0: #endif michael@0: michael@0: void FilePath::OpenInputStream(std::ifstream& stream) const { michael@0: stream.open( michael@0: #ifndef __MINGW32__ michael@0: path_.c_str(), michael@0: #else michael@0: base::SysWideToNativeMB(path_).c_str(), michael@0: #endif michael@0: std::ios::in | std::ios::binary); michael@0: } michael@0: michael@0: FilePath FilePath::StripTrailingSeparators() const { michael@0: FilePath new_path(path_); michael@0: new_path.StripTrailingSeparatorsInternal(); michael@0: michael@0: return new_path; michael@0: } michael@0: michael@0: void FilePath::StripTrailingSeparatorsInternal() { michael@0: // If there is no drive letter, start will be 1, which will prevent stripping michael@0: // the leading separator if there is only one separator. If there is a drive michael@0: // letter, start will be set appropriately to prevent stripping the first michael@0: // separator following the drive letter, if a separator immediately follows michael@0: // the drive letter. michael@0: StringType::size_type start = FindDriveLetter(path_) + 2; michael@0: michael@0: StringType::size_type last_stripped = StringType::npos; michael@0: for (StringType::size_type pos = path_.length(); michael@0: pos > start && IsSeparator(path_[pos - 1]); michael@0: --pos) { michael@0: // If the string only has two separators and they're at the beginning, michael@0: // don't strip them, unless the string began with more than two separators. michael@0: if (pos != start + 1 || last_stripped == start + 2 || michael@0: !IsSeparator(path_[start - 1])) { michael@0: path_.resize(pos - 1); michael@0: last_stripped = pos; michael@0: } michael@0: } michael@0: }