void read_string(wxFileInputStream &file, wxDataInputStream &data, int len, std::string &ret)
{
	wxString str = wxT("");
	for (int i = 0; i < len; ++i)
	{
		if (file.Eof()) return;
		//str.Append(wxString::FromAscii(file.Peek()));
		ret += file.Peek();
		data.Read8();
	}
	//ret = str.ToAscii();
}
std::string read_until(wxFileInputStream &file, wxDataInputStream &data, char stop)
{
	std::string ret;
	if (file.Eof()) return ret;
	while (file.Peek() != stop)
	{
		ret += file.Peek();
		data.Read8();
		if (file.Eof()) return ret;
	}
	return ret;
}
int wx_bdecode(wxFileInputStream &file, wxDataInputStream &data, entry &ret, int depth)
{
	char myByte;
	wxString padding(wxT(""));
	if (depth >= 100)
	{
		return 0;
	}
	if (file.Eof())
	{
		return 0;
	}
	switch(file.Peek())
	{
		case 'i':
			{
				if (file.Peek() == 'i') data.Read8();
				ret = entry(entry::int_t);
				std::string val = read_until(file, data, 'e');
				ret.integer() = boost::lexical_cast<entry::integer_type>(val);
				if (file.Peek() == 'e') data.Read8();
				wxLogMessage(wxT("num: ")+wxString(val.c_str(), wxConvUTF8));
			}
			break;

		case 'l':
			{
				if (file.Peek() == 'l') data.Read8();
				ret = entry(entry::list_t);
				while (file.Peek() != 'e')
				{
					ret.list().push_back(entry());
					entry& list = ret.list().back();
					wx_bdecode(file, data, list, depth + 1);
				}
				if (file.Peek() == 'e') data.Read8();
			}
			break;

		case 'd':
			{
				if (file.Peek() == 'd') data.Read8();
				ret = entry(entry::dictionary_t);
				while (file.Peek() != 'e')
				{
					entry key;
					wx_bdecode(file, data, key, depth + 1);
					if (key.type() != entry::string_t)
					{
						return 0;
					}

					entry dict;
					wx_bdecode(file, data, dict, depth + 1);
					ret.dict().insert(std::pair<std::string, entry>(key.string(), dict));
				}
				if (file.Peek() == 'e')  data.Read8();
			}
			break;

		default:
			if(isdigit(file.Peek()))
			{
				ret = entry(entry::string_t);
				std::string len_s = read_until(file, data, ':');
				if (file.Peek() == ':') data.Read8();
				int len = std::atoi(len_s.c_str());
				read_string(file, data, len, ret.string());
			}
			else
			{
				return 0;
			}
			break;
	}// switch
}
Exemple #4
0
void read_mse1_card(Set& set, wxFileInputStream& f, wxTextInputStream& file) {
	CardP card(new Card(*set.game));
	while (!f.Eof()) {
		// read a line
		String line = file.ReadLine();
		if (line.empty()) continue;
		Char type = line.GetChar(0);
		line = line.substr(1);
		// interpret this line
		switch (type) {
			case 'A': {		// done
				set.cards.push_back(card);
				return;
			} case 'B': {	// name
				card->value(_("name")) = to_script(line);
				break;
			} case 'C': case 'D': { // image filename
				LocalFileName image_file = set.newFileName(_("image"),_("")); // a new unique name in the package
				if (wxCopyFile(line, set.nameOut(image_file), true)) {
					card->value(_("image")) = script_local_image_file(image_file);
				}
				break;
			} case 'E':	{	// super type
				card->value(_("super type")) = to_script(line);
				break;
			} case 'F': {	// sub type
				card->value(_("sub type")) = to_script(line);
				break;
			} case 'G': {	// casting cost
				card->value(_("casting cost")) = to_script(line);
				break;
			} case 'H': {	// rarity
				String rarity;
				if      (line == _("(U)")) rarity = _("uncommon");
				else if (line == _("(R)")) rarity = _("rare");
				else                       rarity = _("common");
				card->value(_("rarity")) = to_script(rarity);
				break;
			} case 'I': {	// power/thoughness
				size_t pos = line.find_first_of(_('/'));
				if (pos != String::npos) {
					card->value(_("power")) = to_script(line.substr(0, pos));
					card->value(_("toughness")) = to_script(line.substr(pos+1));
				}
				break;
			} case 'J': {	// rule text or part of text
				append_line(card->value(_("rule text")), line);
				break;
			} case 'K':	{	// flavor text or part of text
				append_line(card->value(_("flavor text")), line);
				break;
			} case 'L': {	// card color (if not default)
				// decode color
				String color;
				if      (line == _("1")) color = _("white");
				else if (line == _("2")) color = _("blue");
				else if (line == _("3")) color = _("black");
				else if (line == _("4")) color = _("red");
				else if (line == _("5")) color = _("green");
				else if (line == _("6")) color = _("colorless");
				else if (line == _("7")) color = _("land");
				else if (line == _("9")) color = _("multicolor");
				else                     color = _("colorless");
				card->value(_("card color")) = to_script(color);
				break;
			} default: {
				throw ParseError(_("Not a valid MSE1 file"));
			}
		}
	}
}