2003-10-23 01:00:57 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2003, Arvid Norberg
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2007-03-17 18:15:16 +01:00
|
|
|
#include "libtorrent/pch.hpp"
|
|
|
|
|
2004-03-27 23:02:31 +01:00
|
|
|
#include <algorithm>
|
2007-11-18 01:56:43 +01:00
|
|
|
#include <iostream>
|
2006-01-06 22:48:49 +01:00
|
|
|
#include <iomanip>
|
2003-10-23 01:00:57 +02:00
|
|
|
#include "libtorrent/entry.hpp"
|
2005-11-01 19:30:39 +01:00
|
|
|
#include "libtorrent/config.hpp"
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2003-11-09 19:17:09 +01:00
|
|
|
#if defined(_MSC_VER)
|
2003-10-23 01:00:57 +02:00
|
|
|
namespace std
|
|
|
|
{
|
|
|
|
using ::isprint;
|
|
|
|
}
|
|
|
|
#define for if (false) {} else for
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
template <class T>
|
|
|
|
void call_destructor(T* o)
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(o);
|
2003-10-23 01:00:57 +02:00
|
|
|
o->~T();
|
|
|
|
}
|
2004-03-17 13:14:44 +01:00
|
|
|
|
|
|
|
struct compare_string
|
|
|
|
{
|
|
|
|
compare_string(char const* s): m_str(s) {}
|
|
|
|
|
|
|
|
bool operator()(
|
|
|
|
std::pair<std::string
|
|
|
|
, libtorrent::entry> const& e) const
|
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
return m_str && e.first == m_str;
|
2004-03-17 13:14:44 +01:00
|
|
|
}
|
|
|
|
char const* m_str;
|
|
|
|
};
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
|
2004-01-07 01:48:02 +01:00
|
|
|
namespace libtorrent
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2005-08-12 14:40:58 +02:00
|
|
|
namespace detail
|
|
|
|
{
|
2005-11-01 19:30:39 +01:00
|
|
|
TORRENT_EXPORT char const* integer_to_str(char* buf, int size, entry::integer_type val)
|
2005-08-12 14:40:58 +02:00
|
|
|
{
|
|
|
|
int sign = 0;
|
|
|
|
if (val < 0)
|
|
|
|
{
|
|
|
|
sign = 1;
|
|
|
|
val = -val;
|
|
|
|
}
|
|
|
|
buf[--size] = '\0';
|
|
|
|
if (val == 0) buf[--size] = '0';
|
|
|
|
for (; size > sign && val != 0;)
|
|
|
|
{
|
|
|
|
buf[--size] = '0' + char(val % 10);
|
|
|
|
val /= 10;
|
|
|
|
}
|
|
|
|
if (sign) buf[--size] = '-';
|
|
|
|
return buf + size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-03-17 13:14:44 +01:00
|
|
|
entry& entry::operator[](char const* key)
|
|
|
|
{
|
2005-10-13 23:50:59 +02:00
|
|
|
dictionary_type::iterator i = dict().find(key);
|
2004-03-17 13:14:44 +01:00
|
|
|
if (i != dict().end()) return i->second;
|
|
|
|
dictionary_type::iterator ret = dict().insert(
|
2005-10-13 23:50:59 +02:00
|
|
|
dict().begin()
|
2004-03-17 13:14:44 +01:00
|
|
|
, std::make_pair(std::string(key), entry()));
|
|
|
|
return ret->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
entry& entry::operator[](std::string const& key)
|
|
|
|
{
|
|
|
|
return (*this)[key.c_str()];
|
|
|
|
}
|
|
|
|
|
|
|
|
entry* entry::find_key(char const* key)
|
|
|
|
{
|
|
|
|
dictionary_type::iterator i = std::find_if(
|
|
|
|
dict().begin()
|
|
|
|
, dict().end()
|
|
|
|
, compare_string(key));
|
|
|
|
if (i == dict().end()) return 0;
|
|
|
|
return &i->second;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
entry const* entry::find_key(char const* key) const
|
|
|
|
{
|
2005-10-13 23:50:59 +02:00
|
|
|
dictionary_type::const_iterator i = dict().find(key);
|
2004-03-17 13:14:44 +01:00
|
|
|
if (i == dict().end()) return 0;
|
|
|
|
return &i->second;
|
|
|
|
}
|
|
|
|
|
2007-11-02 03:02:52 +01:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
2004-03-17 13:14:44 +01:00
|
|
|
const entry& entry::operator[](char const* key) const
|
|
|
|
{
|
2005-10-13 23:50:59 +02:00
|
|
|
dictionary_type::const_iterator i = dict().find(key);
|
|
|
|
if (i == dict().end()) throw type_error(
|
|
|
|
(std::string("key not found: ") + key).c_str());
|
2004-03-17 13:14:44 +01:00
|
|
|
return i->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
const entry& entry::operator[](std::string const& key) const
|
|
|
|
{
|
|
|
|
return (*this)[key.c_str()];
|
|
|
|
}
|
2007-11-02 03:02:52 +01:00
|
|
|
#endif
|
2004-01-07 01:48:02 +01:00
|
|
|
|
2008-01-03 04:10:25 +01:00
|
|
|
entry::entry(): m_type(undefined_t)
|
|
|
|
{
|
|
|
|
#ifndef NDEBUG
|
|
|
|
m_type_queried = true;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
entry::entry(data_type t): m_type(t)
|
|
|
|
{
|
|
|
|
construct(t);
|
|
|
|
#ifndef NDEBUG
|
|
|
|
m_type_queried = true;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
entry::entry(const entry& e)
|
|
|
|
{
|
|
|
|
copy(e);
|
|
|
|
#ifndef NDEBUG
|
|
|
|
m_type_queried = e.m_type_queried;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
entry::entry(dictionary_type const& v)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2007-12-28 20:07:28 +01:00
|
|
|
#ifndef NDEBUG
|
|
|
|
m_type_queried = true;
|
|
|
|
#endif
|
2004-01-07 01:48:02 +01:00
|
|
|
new(data) dictionary_type(v);
|
|
|
|
m_type = dictionary_t;
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
entry::entry(string_type const& v)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2007-12-28 20:07:28 +01:00
|
|
|
#ifndef NDEBUG
|
|
|
|
m_type_queried = true;
|
|
|
|
#endif
|
2004-01-07 01:48:02 +01:00
|
|
|
new(data) string_type(v);
|
|
|
|
m_type = string_t;
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
entry::entry(list_type const& v)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2007-12-28 20:07:28 +01:00
|
|
|
#ifndef NDEBUG
|
|
|
|
m_type_queried = true;
|
|
|
|
#endif
|
2004-01-07 01:48:02 +01:00
|
|
|
new(data) list_type(v);
|
|
|
|
m_type = list_t;
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
entry::entry(integer_type const& v)
|
2004-01-07 01:48:02 +01:00
|
|
|
{
|
2007-12-28 20:07:28 +01:00
|
|
|
#ifndef NDEBUG
|
|
|
|
m_type_queried = true;
|
|
|
|
#endif
|
2004-01-07 01:48:02 +01:00
|
|
|
new(data) integer_type(v);
|
|
|
|
m_type = int_t;
|
|
|
|
}
|
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
void entry::operator=(dictionary_type const& v)
|
2004-01-07 01:48:02 +01:00
|
|
|
{
|
|
|
|
destruct();
|
|
|
|
new(data) dictionary_type(v);
|
|
|
|
m_type = dictionary_t;
|
2007-12-28 20:07:28 +01:00
|
|
|
#ifndef NDEBUG
|
|
|
|
m_type_queried = true;
|
|
|
|
#endif
|
2004-01-07 01:48:02 +01:00
|
|
|
}
|
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
void entry::operator=(string_type const& v)
|
2004-01-07 01:48:02 +01:00
|
|
|
{
|
|
|
|
destruct();
|
|
|
|
new(data) string_type(v);
|
|
|
|
m_type = string_t;
|
2007-12-28 20:07:28 +01:00
|
|
|
#ifndef NDEBUG
|
|
|
|
m_type_queried = true;
|
|
|
|
#endif
|
2004-01-07 01:48:02 +01:00
|
|
|
}
|
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
void entry::operator=(list_type const& v)
|
2004-01-07 01:48:02 +01:00
|
|
|
{
|
|
|
|
destruct();
|
|
|
|
new(data) list_type(v);
|
|
|
|
m_type = list_t;
|
2007-12-28 20:07:28 +01:00
|
|
|
#ifndef NDEBUG
|
|
|
|
m_type_queried = true;
|
|
|
|
#endif
|
2004-01-07 01:48:02 +01:00
|
|
|
}
|
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
void entry::operator=(integer_type const& v)
|
2004-01-07 01:48:02 +01:00
|
|
|
{
|
|
|
|
destruct();
|
|
|
|
new(data) integer_type(v);
|
|
|
|
m_type = int_t;
|
2007-12-28 20:07:28 +01:00
|
|
|
#ifndef NDEBUG
|
|
|
|
m_type_queried = true;
|
|
|
|
#endif
|
2004-01-07 01:48:02 +01:00
|
|
|
}
|
|
|
|
|
2005-08-11 13:06:52 +02:00
|
|
|
bool entry::operator==(entry const& e) const
|
|
|
|
{
|
|
|
|
if (m_type != e.m_type) return false;
|
|
|
|
|
|
|
|
switch(m_type)
|
|
|
|
{
|
|
|
|
case int_t:
|
|
|
|
return integer() == e.integer();
|
|
|
|
case string_t:
|
|
|
|
return string() == e.string();
|
|
|
|
case list_t:
|
|
|
|
return list() == e.list();
|
|
|
|
case dictionary_t:
|
|
|
|
return dict() == e.dict();
|
|
|
|
default:
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_type == undefined_t);
|
2005-08-11 13:06:52 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2004-01-07 01:48:02 +01:00
|
|
|
|
|
|
|
void entry::construct(data_type t)
|
|
|
|
{
|
|
|
|
m_type = t;
|
|
|
|
switch(m_type)
|
|
|
|
{
|
|
|
|
case int_t:
|
|
|
|
new(data) integer_type;
|
|
|
|
break;
|
|
|
|
case string_t:
|
|
|
|
new(data) string_type;
|
|
|
|
break;
|
|
|
|
case list_t:
|
|
|
|
new(data) list_type;
|
|
|
|
break;
|
|
|
|
case dictionary_t:
|
|
|
|
new (data) dictionary_type;
|
|
|
|
break;
|
|
|
|
default:
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_type == undefined_t);
|
2004-01-07 01:48:02 +01:00
|
|
|
m_type = undefined_t;
|
|
|
|
}
|
2007-12-28 20:07:28 +01:00
|
|
|
#ifndef NDEBUG
|
|
|
|
m_type_queried = true;
|
|
|
|
#endif
|
2004-01-07 01:48:02 +01:00
|
|
|
}
|
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
void entry::copy(entry const& e)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2007-12-28 20:07:28 +01:00
|
|
|
m_type = e.type();
|
2004-01-07 01:48:02 +01:00
|
|
|
switch(m_type)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2004-01-07 01:48:02 +01:00
|
|
|
case int_t:
|
|
|
|
new(data) integer_type(e.integer());
|
|
|
|
break;
|
|
|
|
case string_t:
|
|
|
|
new(data) string_type(e.string());
|
|
|
|
break;
|
|
|
|
case list_t:
|
|
|
|
new(data) list_type(e.list());
|
|
|
|
break;
|
|
|
|
case dictionary_t:
|
|
|
|
new (data) dictionary_type(e.dict());
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
m_type = undefined_t;
|
|
|
|
}
|
2007-12-28 20:07:28 +01:00
|
|
|
#ifndef NDEBUG
|
|
|
|
m_type_queried = true;
|
|
|
|
#endif
|
2004-01-07 01:48:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void entry::destruct()
|
|
|
|
{
|
|
|
|
switch(m_type)
|
|
|
|
{
|
|
|
|
case int_t:
|
|
|
|
call_destructor(reinterpret_cast<integer_type*>(data));
|
|
|
|
break;
|
|
|
|
case string_t:
|
|
|
|
call_destructor(reinterpret_cast<string_type*>(data));
|
|
|
|
break;
|
|
|
|
case list_t:
|
|
|
|
call_destructor(reinterpret_cast<list_type*>(data));
|
|
|
|
break;
|
|
|
|
case dictionary_t:
|
|
|
|
call_destructor(reinterpret_cast<dictionary_type*>(data));
|
|
|
|
break;
|
|
|
|
default:
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_type == undefined_t);
|
2004-01-07 01:48:02 +01:00
|
|
|
break;
|
|
|
|
}
|
2007-12-28 20:07:28 +01:00
|
|
|
#ifndef NDEBUG
|
|
|
|
m_type_queried = false;
|
|
|
|
#endif
|
2004-01-07 01:48:02 +01:00
|
|
|
}
|
|
|
|
|
2007-06-10 22:46:09 +02:00
|
|
|
void entry::swap(entry& e)
|
|
|
|
{
|
|
|
|
// not implemented
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(false);
|
2007-06-10 22:46:09 +02:00
|
|
|
}
|
|
|
|
|
2004-01-07 01:48:02 +01:00
|
|
|
void entry::print(std::ostream& os, int indent) const
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(indent >= 0);
|
2004-01-07 01:48:02 +01:00
|
|
|
for (int i = 0; i < indent; ++i) os << " ";
|
|
|
|
switch (m_type)
|
|
|
|
{
|
|
|
|
case int_t:
|
|
|
|
os << integer() << "\n";
|
|
|
|
break;
|
|
|
|
case string_t:
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2004-01-07 01:48:02 +01:00
|
|
|
bool binary_string = false;
|
|
|
|
for (std::string::const_iterator i = string().begin(); i != string().end(); ++i)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2004-01-07 01:48:02 +01:00
|
|
|
if (!std::isprint(static_cast<unsigned char>(*i)))
|
|
|
|
{
|
|
|
|
binary_string = true;
|
|
|
|
break;
|
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
2004-01-07 01:48:02 +01:00
|
|
|
if (binary_string)
|
|
|
|
{
|
|
|
|
os.unsetf(std::ios_base::dec);
|
|
|
|
os.setf(std::ios_base::hex);
|
|
|
|
for (std::string::const_iterator i = string().begin(); i != string().end(); ++i)
|
2006-01-06 22:48:49 +01:00
|
|
|
os << std::setfill('0') << std::setw(2)
|
|
|
|
<< static_cast<unsigned int>((unsigned char)*i);
|
2004-01-07 01:48:02 +01:00
|
|
|
os.unsetf(std::ios_base::hex);
|
|
|
|
os.setf(std::ios_base::dec);
|
|
|
|
os << "\n";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
os << string() << "\n";
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case list_t:
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2004-01-07 01:48:02 +01:00
|
|
|
os << "list\n";
|
|
|
|
for (list_type::const_iterator i = list().begin(); i != list().end(); ++i)
|
|
|
|
{
|
|
|
|
i->print(os, indent+1);
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case dictionary_t:
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2004-01-07 01:48:02 +01:00
|
|
|
os << "dictionary\n";
|
|
|
|
for (dictionary_type::const_iterator i = dict().begin(); i != dict().end(); ++i)
|
|
|
|
{
|
|
|
|
for (int j = 0; j < indent+1; ++j) os << " ";
|
|
|
|
os << "[" << i->first << "]";
|
2006-08-01 17:27:08 +02:00
|
|
|
if (i->second.type() != entry::string_t
|
|
|
|
&& i->second.type() != entry::int_t)
|
|
|
|
os << "\n";
|
2004-01-07 01:48:02 +01:00
|
|
|
else os << " ";
|
|
|
|
i->second.print(os, indent+2);
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
default:
|
|
|
|
os << "<uninitialized>\n";
|
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
}
|
2005-10-13 23:50:59 +02:00
|
|
|
|