This source file includes following definitions.
- GetInstance
- contains
- containsNone
- IsFilenameLegal
- ReplaceIllegalCharactersInPath
- LocaleAwareCompareFilenames
- NormalizeFileNameEncoding
#include "base/i18n/file_util_icu.h"
#include "base/files/file_path.h"
#include "base/i18n/icu_string_conversions.h"
#include "base/i18n/string_compare.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/singleton.h"
#include "base/strings/string_util.h"
#include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "third_party/icu/source/common/unicode/uniset.h"
#include "third_party/icu/source/i18n/unicode/coll.h"
using base::string16;
namespace {
class IllegalCharacters {
public:
static IllegalCharacters* GetInstance() {
return Singleton<IllegalCharacters>::get();
}
bool contains(UChar32 ucs4) {
return !!set->contains(ucs4);
}
bool containsNone(const string16 &s) {
return !!set->containsNone(icu::UnicodeString(s.c_str(), s.size()));
}
private:
friend class Singleton<IllegalCharacters>;
friend struct DefaultSingletonTraits<IllegalCharacters>;
IllegalCharacters();
~IllegalCharacters() { }
scoped_ptr<icu::UnicodeSet> set;
DISALLOW_COPY_AND_ASSIGN(IllegalCharacters);
};
IllegalCharacters::IllegalCharacters() {
UErrorCode status = U_ZERO_ERROR;
#if defined(WCHAR_T_IS_UTF16)
set.reset(new icu::UnicodeSet(icu::UnicodeString(
L"[[\"*/:<>?\\\\|][:Cc:][:Cf:] - [\u200c\u200d]]"), status));
#else
set.reset(new icu::UnicodeSet(UNICODE_STRING_SIMPLE(
"[[\"*/:<>?\\\\|][:Cc:][:Cf:] - [\\u200c\\u200d]]").unescape(),
status));
#endif
DCHECK(U_SUCCESS(status));
set->add(0xFDD0, 0xFDEF);
for (int i = 0; i <= 0x10; ++i) {
int plane_base = 0x10000 * i;
set->add(plane_base + 0xFFFE, plane_base + 0xFFFF);
}
set->freeze();
}
}
namespace file_util {
bool IsFilenameLegal(const string16& file_name) {
return IllegalCharacters::GetInstance()->containsNone(file_name);
}
void ReplaceIllegalCharactersInPath(base::FilePath::StringType* file_name,
char replace_char) {
DCHECK(file_name);
DCHECK(!(IllegalCharacters::GetInstance()->contains(replace_char)));
base::TrimWhitespace(*file_name, base::TRIM_ALL, file_name);
IllegalCharacters* illegal = IllegalCharacters::GetInstance();
int cursor = 0;
while (cursor < static_cast<int>(file_name->size())) {
int char_begin = cursor;
uint32 code_point;
#if defined(OS_MACOSX)
U8_NEXT(file_name->data(), cursor, static_cast<int>(file_name->length()),
code_point);
#elif defined(OS_WIN)
U16_NEXT(file_name->data(), cursor, static_cast<int>(file_name->length()),
code_point);
#elif defined(OS_POSIX)
unsigned char cur_char = static_cast<unsigned char>((*file_name)[cursor++]);
if (cur_char >= 0x80)
continue;
code_point = cur_char;
#else
NOTREACHED();
#endif
if (illegal->contains(code_point)) {
file_name->replace(char_begin, cursor - char_begin, 1, replace_char);
cursor = char_begin + 1;
}
}
}
bool LocaleAwareCompareFilenames(const base::FilePath& a,
const base::FilePath& b) {
UErrorCode error_code = U_ZERO_ERROR;
scoped_ptr<icu::Collator> collator(icu::Collator::createInstance(error_code));
DCHECK(U_SUCCESS(error_code));
collator->setStrength(icu::Collator::TERTIARY);
#if defined(OS_WIN)
return base::i18n::CompareString16WithCollator(collator.get(),
base::WideToUTF16(a.value()), base::WideToUTF16(b.value())) == UCOL_LESS;
#elif defined(OS_POSIX)
return base::i18n::CompareString16WithCollator(
collator.get(),
base::WideToUTF16(base::SysNativeMBToWide(a.value().c_str())),
base::WideToUTF16(base::SysNativeMBToWide(b.value().c_str()))
) == UCOL_LESS;
#else
#error Not implemented on your system
#endif
}
void NormalizeFileNameEncoding(base::FilePath* file_name) {
#if defined(OS_CHROMEOS)
std::string normalized_str;
if (base::ConvertToUtf8AndNormalize(file_name->BaseName().value(),
base::kCodepageUTF8,
&normalized_str)) {
*file_name = file_name->DirName().Append(base::FilePath(normalized_str));
}
#endif
}
}