Ejemplo n.º 1
0
	// 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;
		}
	}
Ejemplo n.º 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();
	}
}
Ejemplo n.º 3
0
settings_pack load_pack_from_dict(bdecode_node const& settings)
{
    settings_pack pack;

    for (int i = 0; i < settings.dict_size(); ++i)
    {
        string_view key;
        bdecode_node val;
        std::tie(key, val) = settings.dict_at(i);
        switch (val.type())
        {
        case bdecode_node::dict_t:
        case bdecode_node::list_t:
            continue;
        case bdecode_node::int_t:
        {
            bool found = false;
            for (std::size_t k = 0; k < int_settings.size(); ++k)
            {
                if (key != int_settings[k].name) continue;
                pack.set_int(settings_pack::int_type_base + int(k), int(val.int_value()));
                found = true;
                break;
            }
            if (found) continue;
            for (std::size_t k = 0; k < bool_settings.size(); ++k)
            {
                if (key != bool_settings[k].name) continue;
                pack.set_bool(settings_pack::bool_type_base + int(k), val.int_value() != 0);
                break;
            }
        }
        break;
        case bdecode_node::string_t:
            for (std::size_t k = 0; k < str_settings.size(); ++k)
            {
                if (key != str_settings[k].name) continue;
                pack.set_str(settings_pack::string_type_base + int(k), val.string_value().to_string());
                break;
            }
            break;
        case bdecode_node::none_t:
            break;
        }
    }
    return pack;
}
Ejemplo n.º 4
0
	boost::shared_ptr<settings_pack> load_pack_from_dict(bdecode_node const& settings)
	{
		boost::shared_ptr<settings_pack> pack = boost::make_shared<settings_pack>();

		for (int i = 0; i < settings.dict_size(); ++i)
		{
			std::string key;
			bdecode_node val;
			std::tie(key, val) = settings.dict_at(i);
			switch (val.type())
			{
				case bdecode_node::dict_t:
				case bdecode_node::list_t:
					continue;
				case bdecode_node::int_t:
				{
					bool found = false;
					for (int k = 0; k < sizeof(int_settings)/sizeof(int_settings[0]); ++k)
					{
						if (key != int_settings[k].name) continue;
						pack->set_int(settings_pack::int_type_base + k, val.int_value());
						found = true;
						break;
					}
					if (found) continue;
					for (int k = 0; k < sizeof(bool_settings)/sizeof(bool_settings[0]); ++k)
					{
						if (key != bool_settings[k].name) continue;
						pack->set_bool(settings_pack::bool_type_base + k, val.int_value() != 0);
						break;
					}
				}
				break;
			case bdecode_node::string_t:
				for (int k = 0; k < sizeof(str_settings)/sizeof(str_settings[0]); ++k)
				{
					if (key != str_settings[k].name) continue;
					pack->set_str(settings_pack::string_type_base + k, val.string_value());
					break;
				}
				break;
			case bdecode_node::none_t:
				break;
			}
		}
		return pack;
	}
Ejemplo n.º 5
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];
				snprintf(str, sizeof(str), "%" PRId64, e.int_value());
				return str;
			}
			case bdecode_node::string_t:
			{
				print_string(ret, e.string_ptr(), e.string_length(), 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<std::string, bdecode_node> ent = e.dict_at(i);
					print_string(ret, ent.first.c_str(), ent.first.size(), 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;
	}