print_entry should not be exported

This commit is contained in:
Arvid Norberg 2013-07-19 22:26:07 +00:00
parent b27ff4bd00
commit b7816ff281
2 changed files with 141 additions and 3 deletions

View File

@ -97,6 +97,145 @@ int load_file(std::string const& filename, std::vector<char>& v, libtorrent::err
return 0;
}
int line_longer_than(libtorrent::lazy_entry const& e, int limit)
{
using namespace libtorrent;
int line_len = 0;
switch (e.type())
{
case lazy_entry::list_t:
line_len += 4;
if (line_len > limit) return -1;
for (int i = 0; i < e.list_size(); ++i)
{
int ret = line_longer_than(*e.list_at(i), limit - line_len);
if (ret == -1) return -1;
line_len += ret + 2;
}
break;
case lazy_entry::dict_t:
line_len += 4;
if (line_len > limit) return -1;
for (int i = 0; i < e.dict_size(); ++i)
{
line_len += 4 + e.dict_at(i).first.size();
if (line_len > limit) return -1;
int ret = line_longer_than(*e.dict_at(i).second, limit - line_len);
if (ret == -1) return -1;
line_len += ret + 1;
}
break;
case lazy_entry::string_t:
line_len += 3 + e.string_length();
break;
case lazy_entry::int_t:
{
size_type val = e.int_value();
while (val > 0)
{
++line_len;
val /= 10;
}
line_len += 2;
}
break;
case lazy_entry::none_t:
line_len += 4;
break;
}
if (line_len > limit) return -1;
return line_len;
}
std::string print_entry(libtorrent::lazy_entry const& e, bool single_line = false, int indent = 0)
{
using namespace libtorrent;
char indent_str[200];
memset(indent_str, ' ', 200);
indent_str[0] = ',';
indent_str[1] = '\n';
indent_str[199] = 0;
if (indent < 197 && indent >= 0) indent_str[indent+2] = 0;
std::string ret;
switch (e.type())
{
case lazy_entry::none_t: return "none";
case lazy_entry::int_t:
{
char str[100];
snprintf(str, sizeof(str), "%"PRId64, e.int_value());
return str;
}
case lazy_entry::string_t:
{
bool printable = true;
char const* str = e.string_ptr();
for (int i = 0; i < e.string_length(); ++i)
{
using namespace std;
if (is_print((unsigned char)str[i])) continue;
printable = false;
break;
}
ret += "'";
if (printable)
{
ret += e.string_value();
ret += "'";
return ret;
}
for (int i = 0; i < e.string_length(); ++i)
{
char tmp[5];
snprintf(tmp, sizeof(tmp), "%02x", (unsigned char)str[i]);
ret += tmp;
}
ret += "'";
return ret;
}
case lazy_entry::list_t:
{
ret += '[';
bool one_liner = line_longer_than(e, 200) != -1 || single_line;
if (!one_liner) ret += indent_str + 1;
for (int i = 0; i < e.list_size(); ++i)
{
if (i == 0 && one_liner) ret += " ";
ret += ::print_entry(*e.list_at(i), single_line, indent + 2);
if (i < e.list_size() - 1) ret += (one_liner?", ":indent_str);
else ret += (one_liner?" ":indent_str+1);
}
ret += "]";
return ret;
}
case lazy_entry::dict_t:
{
ret += "{";
bool one_liner = line_longer_than(e, 200) != -1 || single_line;
if (!one_liner) ret += indent_str+1;
for (int i = 0; i < e.dict_size(); ++i)
{
if (i == 0 && one_liner) ret += " ";
std::pair<std::string, lazy_entry const*> ent = e.dict_at(i);
ret += "'";
ret += ent.first;
ret += "': ";
ret += ::print_entry(*ent.second, single_line, indent + 2);
if (i < e.dict_size() - 1) ret += (one_liner?", ":indent_str);
else ret += (one_liner?" ":indent_str+1);
}
ret += "}";
return ret;
}
}
return ret;
}
int main(int argc, char* argv[])
{
using namespace libtorrent;
@ -134,7 +273,7 @@ int main(int argc, char* argv[])
ret = lazy_bdecode(&buf[0], &buf[0] + buf.size(), e, ec, &pos
, depth_limit, item_limit);
printf("\n\n----- raw info -----\n\n%s\n", print_entry(e).c_str());
printf("\n\n----- raw info -----\n\n%s\n", ::print_entry(e).c_str());
if (ret != 0)
{

View File

@ -279,8 +279,7 @@ namespace libtorrent
lazy_entry val;
};
// TODO: 3 does this need to be a public function?
TORRENT_EXPORT std::string print_entry(lazy_entry const& e
TORRENT_EXTRA_EXPORT std::string print_entry(lazy_entry const& e
, bool single_line = false, int indent = 0);
}