prefers name.utf-8 and path.utf-8 if they exists

This commit is contained in:
Arvid Norberg 2005-08-17 01:35:37 +00:00
parent 77956fb5a6
commit cc1d4dfdde
2 changed files with 43 additions and 18 deletions

View File

@ -44,22 +44,24 @@ wchar_t decode_utf8(InputIterator &iter, InputIterator last)
{
wchar_t ret;
if(((*iter)&0x80) == 0) {
ret=*iter++;
if (((*iter)&0x80) == 0) // one byte
{
ret = *iter++;
}
else if(((*iter)&0x20) == 0) {
ret=(
(((wchar_t)((*iter++)&0x1F)) << 6) |
decode_utf8_mb(iter, last)
);
else if (((*iter) & 0xe0) == 0xc0) // two bytes
{
wchar_t byte1 = (*iter++) & 0x1f;
wchar_t byte2 = decode_utf8_mb(iter, last);
ret = (byte1 << 6) | byte2;
}
else if(((*iter)&0x10) == 0) {
ret=(
(((wchar_t)((*iter++)&0x0F)) << 12) |
(decode_utf8_mb(iter, last) << 6) |
decode_utf8_mb(iter, last)
);
else if (((*iter) & 0xf0) == 0xe0) // three bytes
{
wchar_t byte1 = (*iter++) & 0x1f;
wchar_t byte2 = decode_utf8_mb(iter, last);
wchar_t byte3 = decode_utf8_mb(iter, last);
ret = (byte1 << 12) | (byte2 << 6) | byte3;
}
// TODO: support surrogate pairs
else throw std::runtime_error("UTF-8 not convertable to UTF-16");
return ret;

View File

@ -67,9 +67,24 @@ namespace
{
target.size = dict["length"].integer();
target.path = root_dir;
const entry::list_type& list = dict["path"].list();
for (entry::list_type::const_iterator i = list.begin();
i != list.end(); ++i)
// prefer the name.utf-8
// because if it exists, it is more
// likely to be correctly encoded
const entry::list_type* list = 0;
if (entry const* p = dict.find_key("path.utf-8"))
{
list = &p->list();
}
else
{
list = &dict["path"].list();
}
for (entry::list_type::const_iterator i = list->begin();
i != list->end(); ++i)
{
if (i->string() != "..")
target.path /= i->string();
@ -187,7 +202,15 @@ namespace libtorrent
m_piece_length = (int)info["piece length"].integer();
// extract file name (or the directory name if it's a multifile libtorrent)
if (entry const* e = info.find_key("name.utf-8"))
{
m_name = e->string();
}
else
{
m_name = info["name"].string();
}
path tmp = m_name;
if (tmp.is_complete()) throw std::runtime_error("torrent contains "
"a file with an absolute path: '" + m_name + "'");