| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-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_util.h" | 5 #include "base/file_util.h" |
| 6 | 6 |
| 7 #include <fstream> | 7 #include <fstream> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 if (illegal_characters.contains(wstr[i])) { | 187 if (illegal_characters.contains(wstr[i])) { |
| 188 (*file_name)[i] = replace_char; | 188 (*file_name)[i] = replace_char; |
| 189 } | 189 } |
| 190 ++i; | 190 ++i; |
| 191 } | 191 } |
| 192 #else | 192 #else |
| 193 #error wchar_t* should be either UTF-16 or UTF-32 | 193 #error wchar_t* should be either UTF-16 or UTF-32 |
| 194 #endif | 194 #endif |
| 195 } | 195 } |
| 196 | 196 |
| 197 // Appends the extension to file adding a '.' if extension doesn't contain one. |
| 198 // This does nothing if extension is empty or '.'. This is used internally by |
| 199 // ReplaceExtension. |
| 200 static void AppendExtension(const std::wstring& extension, |
| 201 std::wstring* file) { |
| 202 if (!extension.empty() && extension != L".") { |
| 203 if (extension[0] != L'.') |
| 204 file->append(L"."); |
| 205 file->append(extension); |
| 206 } |
| 207 } |
| 208 |
| 197 void ReplaceExtension(std::wstring* file_name, const std::wstring& extension) { | 209 void ReplaceExtension(std::wstring* file_name, const std::wstring& extension) { |
| 198 const std::wstring::size_type last_dot = file_name->rfind(L'.'); | 210 const std::wstring::size_type last_dot = file_name->rfind(L'.'); |
| 211 if (last_dot == std::wstring::npos) { |
| 212 // No extension, just append the supplied extension. |
| 213 AppendExtension(extension, file_name); |
| 214 return; |
| 215 } |
| 216 const std::wstring::size_type last_separator = |
| 217 file_name->rfind(kPathSeparator); |
| 218 if (last_separator != std::wstring::npos && last_dot < last_separator) { |
| 219 // File name doesn't have extension, but one of the directories does; don't |
| 220 // replace it, just append the supplied extension. For example |
| 221 // 'c:\tmp.bar\foo'. |
| 222 AppendExtension(extension, file_name); |
| 223 return; |
| 224 } |
| 199 std::wstring result = file_name->substr(0, last_dot); | 225 std::wstring result = file_name->substr(0, last_dot); |
| 200 if (!extension.empty() && extension != L".") { | 226 AppendExtension(extension, &result); |
| 201 if (extension.at(0) != L'.') | |
| 202 result.append(L"."); | |
| 203 result.append(extension); | |
| 204 } | |
| 205 file_name->swap(result); | 227 file_name->swap(result); |
| 206 } | 228 } |
| 207 | 229 |
| 208 bool ContentsEqual(const std::wstring& filename1, | 230 bool ContentsEqual(const std::wstring& filename1, |
| 209 const std::wstring& filename2) { | 231 const std::wstring& filename2) { |
| 210 // We open the file in binary format even if they are text files because | 232 // We open the file in binary format even if they are text files because |
| 211 // we are just comparing that bytes are exactly same in both files and not | 233 // we are just comparing that bytes are exactly same in both files and not |
| 212 // doing anything smart with text formatting. | 234 // doing anything smart with text formatting. |
| 213 #if defined(OS_WIN) | 235 #if defined(OS_WIN) |
| 214 std::ifstream file1(filename1.c_str(), std::ios::in | std::ios::binary); | 236 std::ifstream file1(filename1.c_str(), std::ios::in | std::ios::binary); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 while ((len = fread(buf, 1, sizeof(buf), file)) > 0) { | 285 while ((len = fread(buf, 1, sizeof(buf), file)) > 0) { |
| 264 contents->append(buf, len); | 286 contents->append(buf, len); |
| 265 } | 287 } |
| 266 fclose(file); | 288 fclose(file); |
| 267 | 289 |
| 268 return true; | 290 return true; |
| 269 } | 291 } |
| 270 | 292 |
| 271 } // namespace | 293 } // namespace |
| 272 | 294 |
| OLD | NEW |