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 <stdio.h> | 7 #include <stdio.h> |
8 | 8 |
9 #include <fstream> | 9 #include <fstream> |
10 | 10 |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 path_str.append(&FilePath::kSeparators[0], 1); | 72 path_str.append(&FilePath::kSeparators[0], 1); |
73 | 73 |
74 return true; | 74 return true; |
75 } | 75 } |
76 | 76 |
77 void TrimTrailingSeparator(std::wstring* dir) { | 77 void TrimTrailingSeparator(std::wstring* dir) { |
78 while (dir->length() > 1 && EndsWithSeparator(dir)) | 78 while (dir->length() > 1 && EndsWithSeparator(dir)) |
79 dir->resize(dir->length() - 1); | 79 dir->resize(dir->length() - 1); |
80 } | 80 } |
81 | 81 |
82 FilePath::StringType GetFileExtensionFromPath(const FilePath& path) { | 82 std::wstring GetFileExtensionFromPath(const std::wstring& path) { |
83 FilePath::StringType file_name = path.BaseName().value(); | 83 std::wstring file_name = GetFilenameFromPath(path); |
84 const FilePath::StringType::size_type last_dot = | 84 std::wstring::size_type last_dot = file_name.rfind(L'.'); |
85 file_name.rfind(kExtensionSeparator); | 85 return std::wstring(last_dot == std::wstring::npos ? |
86 return FilePath::StringType(last_dot == FilePath::StringType::npos ? | 86 L"" : |
87 FILE_PATH_LITERAL("") : | 87 file_name, last_dot+1); |
88 file_name, last_dot+1); | |
89 } | 88 } |
90 | 89 |
91 std::wstring GetFilenameWithoutExtensionFromPath(const std::wstring& path) { | 90 std::wstring GetFilenameWithoutExtensionFromPath(const std::wstring& path) { |
92 std::wstring file_name = GetFilenameFromPath(path); | 91 std::wstring file_name = GetFilenameFromPath(path); |
93 std::wstring::size_type last_dot = file_name.rfind(L'.'); | 92 std::wstring::size_type last_dot = file_name.rfind(L'.'); |
94 return file_name.substr(0, last_dot); | 93 return file_name.substr(0, last_dot); |
95 } | 94 } |
96 | 95 |
97 void InsertBeforeExtension(FilePath* path, const FilePath::StringType& suffix) { | 96 void InsertBeforeExtension(FilePath* path, const FilePath::StringType& suffix) { |
98 FilePath::StringType& value = | 97 FilePath::StringType& value = |
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
368 bool EndsWithSeparator(const std::wstring& path) { | 367 bool EndsWithSeparator(const std::wstring& path) { |
369 return EndsWithSeparator(FilePath::FromWStringHack(path)); | 368 return EndsWithSeparator(FilePath::FromWStringHack(path)); |
370 } | 369 } |
371 bool GetCurrentDirectory(std::wstring* path_str) { | 370 bool GetCurrentDirectory(std::wstring* path_str) { |
372 FilePath path; | 371 FilePath path; |
373 if (!GetCurrentDirectory(&path)) | 372 if (!GetCurrentDirectory(&path)) |
374 return false; | 373 return false; |
375 *path_str = path.ToWStringHack(); | 374 *path_str = path.ToWStringHack(); |
376 return true; | 375 return true; |
377 } | 376 } |
378 std::wstring GetFileExtensionFromPath(const std::wstring& path) { | |
379 FilePath::StringType extension = | |
380 GetFileExtensionFromPath(FilePath::FromWStringHack(path)); | |
381 #if defined(OS_WIN) | |
382 return extension; | |
383 #elif defined(OS_POSIX) | |
384 return UTF8ToWide(extension); | |
385 #endif | |
386 } | |
387 bool GetFileInfo(const std::wstring& file_path, FileInfo* results) { | 377 bool GetFileInfo(const std::wstring& file_path, FileInfo* results) { |
388 return GetFileInfo(FilePath::FromWStringHack(file_path), results); | 378 return GetFileInfo(FilePath::FromWStringHack(file_path), results); |
389 } | 379 } |
390 std::wstring GetFilenameFromPath(const std::wstring& path) { | 380 std::wstring GetFilenameFromPath(const std::wstring& path) { |
391 if (path.empty() || EndsWithSeparator(path)) | 381 if (path.empty() || EndsWithSeparator(path)) |
392 return std::wstring(); | 382 return std::wstring(); |
393 | 383 |
394 return FilePath::FromWStringHack(path).BaseName().ToWStringHack(); | 384 #if defined(OS_WIN) |
| 385 return FilePath::FromWStringHack(path).BaseName(); |
| 386 #elif defined(OS_POSIX) |
| 387 return UTF8ToWide(FilePath::FromWStringHack(path).BaseName()); |
| 388 #endif |
395 } | 389 } |
396 bool GetFileSize(const std::wstring& file_path, int64* file_size) { | 390 bool GetFileSize(const std::wstring& file_path, int64* file_size) { |
397 return GetFileSize(FilePath::FromWStringHack(file_path), file_size); | 391 return GetFileSize(FilePath::FromWStringHack(file_path), file_size); |
398 } | 392 } |
399 bool GetTempDir(std::wstring* path_str) { | 393 bool GetTempDir(std::wstring* path_str) { |
400 FilePath path; | 394 FilePath path; |
401 if (!GetTempDir(&path)) | 395 if (!GetTempDir(&path)) |
402 return false; | 396 return false; |
403 *path_str = path.ToWStringHack(); | 397 *path_str = path.ToWStringHack(); |
404 return true; | 398 return true; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
439 FilePath directory = path.DirName(); | 433 FilePath directory = path.DirName(); |
440 // If there is no separator, we will get back kCurrentDirectory. | 434 // If there is no separator, we will get back kCurrentDirectory. |
441 // In this case, clear dir. | 435 // In this case, clear dir. |
442 if (directory == path || directory.value() == FilePath::kCurrentDirectory) | 436 if (directory == path || directory.value() == FilePath::kCurrentDirectory) |
443 dir->clear(); | 437 dir->clear(); |
444 else | 438 else |
445 *dir = directory.ToWStringHack(); | 439 *dir = directory.ToWStringHack(); |
446 } | 440 } |
447 } // namespace | 441 } // namespace |
448 | 442 |
OLD | NEW |