Ejemplo n.º 1
0
bool level::load(const unsigned long id, const size sz, const bool wrap_mode, const string& state)
{
	assert(id >= 1 && id <= PW_MAX_LEVEL_NUMBER);
	assert(!state.empty());

	_id = id;
	sprintf(_id_text, "%08u", static_cast<unsigned int>(_id));
	_wrap_mode = wrap_mode;
	_size_type = sz;
	_size_cell = size_from_type(sz);
	const size_t cell_count = _size_cell * _size_cell;
	_cells.resize(cell_count);

	if (state.length() != cell_count * 2)
		return false;

	for (size_t i = 0; i < cell_count; ++i) {
		_cells[i].reset();

		unsigned char cell_state;
		cell_state = C2H(state[i * 2]);
		cell_state = cell_state << 4;
		cell_state |= C2H(state[i * 2 + 1]);

		if (!_cells[i].load(cell_state))
			return false;
	}

	for (unsigned short y = 0; y < _size_cell; ++y) {
		for (unsigned short x = 0; x < _size_cell; ++x) {
			cell& c = cell_at(x, y);
			if (c.cell_type() == cell::ct_sender) {
				_senderX = x;
				_senderY = y;
				break;
			}
		}
	}

	define_connect_status();
	_solved = false;

	return true;
}
Ejemplo n.º 2
0
/*
 * bintohex
 *
 * Converts the given binary data (srcbuf) to
 * its equivalent hex chars (hexbuf).
 *
 * hexlen should be at least twice as srclen.
 * if hexbuf is not big enough returns 0.
 * otherwise returns number of valid chars in
 * hexbuf which is srclen * 2.
 */
size_t
bintohex(const char *srcbuf, size_t srclen,
    char *hexbuf, size_t hexlen)
{
	size_t outlen;
	char c;

	outlen = srclen << 1;

	if (hexlen < outlen)
		return (0);

	while (srclen-- > 0) {
		c = *srcbuf++;
		*hexbuf++ = C2H(c & 0xF);
		*hexbuf++ = C2H((c >> 4) & 0xF);
	}

	return (outlen);
}