OLD | NEW |
1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/file_path.h" | 5 #include "base/file_path.h" |
6 | 6 |
| 7 // These includes are just for the *Hack functions, and should be removed |
| 8 // when those functions are removed. |
| 9 #include "base/string_piece.h" |
| 10 #include "base/sys_string_conversions.h" |
| 11 |
7 #if defined(FILE_PATH_USES_WIN_SEPARATORS) | 12 #if defined(FILE_PATH_USES_WIN_SEPARATORS) |
8 const FilePath::CharType FilePath::kSeparators[] = FILE_PATH_LITERAL("\\/"); | 13 const FilePath::CharType FilePath::kSeparators[] = FILE_PATH_LITERAL("\\/"); |
9 #else // FILE_PATH_USES_WIN_SEPARATORS | 14 #else // FILE_PATH_USES_WIN_SEPARATORS |
10 const FilePath::CharType FilePath::kSeparators[] = FILE_PATH_LITERAL("/"); | 15 const FilePath::CharType FilePath::kSeparators[] = FILE_PATH_LITERAL("/"); |
11 #endif // FILE_PATH_USES_WIN_SEPARATORS | 16 #endif // FILE_PATH_USES_WIN_SEPARATORS |
12 | 17 |
13 const FilePath::CharType FilePath::kCurrentDirectory[] = FILE_PATH_LITERAL("."); | 18 const FilePath::CharType FilePath::kCurrentDirectory[] = FILE_PATH_LITERAL("."); |
14 const FilePath::CharType FilePath::kParentDirectory[] = FILE_PATH_LITERAL(".."); | 19 const FilePath::CharType FilePath::kParentDirectory[] = FILE_PATH_LITERAL(".."); |
15 | 20 |
16 // Returns true if |character| is in kSeparators. | 21 // Returns true if |character| is in kSeparators. |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 return path_.length() > letter + 1 && IsSeparator(path_[letter + 1]); | 150 return path_.length() > letter + 1 && IsSeparator(path_[letter + 1]); |
146 } | 151 } |
147 // Look for a pair of leading separators. | 152 // Look for a pair of leading separators. |
148 return path_.length() > 1 && IsSeparator(path_[0]) && IsSeparator(path_[1]); | 153 return path_.length() > 1 && IsSeparator(path_[0]) && IsSeparator(path_[1]); |
149 #else // FILE_PATH_USES_DRIVE_LETTERS | 154 #else // FILE_PATH_USES_DRIVE_LETTERS |
150 // Look for a separator in the first position. | 155 // Look for a separator in the first position. |
151 return path_.length() > 0 && IsSeparator(path_[0]); | 156 return path_.length() > 0 && IsSeparator(path_[0]); |
152 #endif // FILE_PATH_USES_DRIVE_LETTERS | 157 #endif // FILE_PATH_USES_DRIVE_LETTERS |
153 } | 158 } |
154 | 159 |
| 160 #if defined(OS_POSIX) |
| 161 // See file_path.h for a discussion of the encoding of paths on POSIX |
| 162 // platforms. These *Hack() functions are not quite correct, but they're |
| 163 // only temporary while we fix the remainder of the code. |
| 164 // Remember to remove the #includes at the top when you remove these. |
| 165 |
| 166 // static |
| 167 FilePath FilePath::FromWStringHack(const std::wstring& wstring) { |
| 168 return FilePath(base::SysWideToNativeMB(wstring)); |
| 169 } |
| 170 std::wstring FilePath::ToWStringHack() const { |
| 171 return base::SysNativeMBToWide(path_); |
| 172 } |
| 173 #elif defined(OS_WIN) |
| 174 // static |
| 175 FilePath FilePath::FromWStringHack(const std::wstring& wstring) { |
| 176 return FilePath(wstring); |
| 177 } |
| 178 std::wstring FilePath::ToWStringHack() const { |
| 179 return path_; |
| 180 } |
| 181 #endif |
| 182 |
155 void FilePath::StripTrailingSeparators() { | 183 void FilePath::StripTrailingSeparators() { |
156 // If there is no drive letter, start will be 1, which will prevent stripping | 184 // If there is no drive letter, start will be 1, which will prevent stripping |
157 // the leading separator if there is only one separator. If there is a drive | 185 // the leading separator if there is only one separator. If there is a drive |
158 // letter, start will be set appropriately to prevent stripping the first | 186 // letter, start will be set appropriately to prevent stripping the first |
159 // separator following the drive letter, if a separator immediately follows | 187 // separator following the drive letter, if a separator immediately follows |
160 // the drive letter. | 188 // the drive letter. |
161 StringType::size_type start = FindDriveLetter() + 2; | 189 StringType::size_type start = FindDriveLetter() + 2; |
162 | 190 |
163 StringType::size_type last_stripped = StringType::npos; | 191 StringType::size_type last_stripped = StringType::npos; |
164 for (StringType::size_type pos = path_.length(); | 192 for (StringType::size_type pos = path_.length(); |
165 pos > start && IsSeparator(path_[pos - 1]); | 193 pos > start && IsSeparator(path_[pos - 1]); |
166 --pos) { | 194 --pos) { |
167 // If the string only has two separators and they're at the beginning, | 195 // If the string only has two separators and they're at the beginning, |
168 // don't strip them, unless the string began with more than two separators. | 196 // don't strip them, unless the string began with more than two separators. |
169 if (pos != start + 1 || last_stripped == start + 2 || | 197 if (pos != start + 1 || last_stripped == start + 2 || |
170 !IsSeparator(path_[start - 1])) { | 198 !IsSeparator(path_[start - 1])) { |
171 path_.resize(pos - 1); | 199 path_.resize(pos - 1); |
172 last_stripped = pos; | 200 last_stripped = pos; |
173 } | 201 } |
174 } | 202 } |
175 } | 203 } |
OLD | NEW |