// convert a bdecode_node into an old skool entry
	void entry::operator=(bdecode_node const& e)
	{
		switch (e.type())
		{
			case bdecode_node::string_t:
				this->string() = e.string_value();
				break;
			case bdecode_node::int_t:
				this->integer() = e.int_value();
				break;
			case bdecode_node::dict_t:
			{
				dictionary_type& d = this->dict();
				for (int i = 0; i < e.dict_size(); ++i)
				{
					std::pair<std::string, bdecode_node> elem = e.dict_at(i);
					d[elem.first] = elem.second;
				}
				break;
			}
			case bdecode_node::list_t:
			{
				list_type& l = this->list();
				for (int i = 0; i < e.list_size(); ++i)
				{
					l.push_back(entry());
					l.back() = e.list_at(i);
				}
				break;
			}
			case bdecode_node::none_t:
				destruct();
				break;
		}
	}
Exemple #2
0
void render_variant(std::string& out, bdecode_node const& e)
{
	switch (e.type())
	{
		case bdecode_node::dict_t:
			print_dict(out);
			for (int i = 0; i < e.dict_size(); ++i)
			{
				std::pair<lt::string_view, bdecode_node> item = e.dict_at(i);
				const bool duplicate = g_seed == 1;
				const bool skipped = g_seed == 2;
				g_seed -= 2;
				if (duplicate)
				{
					print_string(out, item.first.to_string());
					render_variant(out, item.second);
				}
				if (!skipped)
				{
					print_string(out, item.first.to_string());
					render_variant(out, item.second);
				}

				render_arbitrary_item(out);
			}
			print_terminate(out);
			break;
		case bdecode_node::list_t:
			print_list(out);
			for (int i = 0; i < e.list_size(); ++i)
			{
				const bool duplicate = g_seed == 1;
				const bool skipped = g_seed == 2;
				g_seed -= 2;
				if (duplicate) render_variant(out, e.list_at(i));

				render_arbitrary_item(out);

				if (!skipped) render_variant(out, e.list_at(i));
			}
			print_terminate(out);
			break;
		case bdecode_node::int_t:
			print_int(out, e.int_value());
			break;
		case bdecode_node::string_t:
			print_string(out, e.string_value().to_string());
			break;
		default:
			abort();
	}
}
Exemple #3
0
	std::string print_entry(bdecode_node const& e
		, bool single_line, int indent)
	{
		char indent_str[200];
		using std::memset;
		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 bdecode_node::none_t: return "none";
			case bdecode_node::int_t:
			{
				char str[100];
				std::snprintf(str, sizeof(str), "%" PRId64, e.int_value());
				return str;
			}
			case bdecode_node::string_t:
			{
				print_string(ret, e.string_value(), single_line);
				return ret;
			}
			case bdecode_node::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 bdecode_node::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<string_view, bdecode_node> ent = e.dict_at(i);
					print_string(ret, ent.first, true);
					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;
	}