Пример #1
0
/* used for Struct serialization where types are known ahead of time */
static int dump_num(
    LWES_TYPE type,
    VALUE val,
    LWES_BYTE_P buf,
    size_t *off)
{
    if (type != LWES_TYPE_IP_ADDR && TYPE(val) == T_STRING)
        val = rb_funcall(val, id_to_i, 0, 0);

    switch (type) {
    case LWES_TYPE_U_INT_16:
        return dump_uint16(val, buf, off);
    case LWES_TYPE_INT_16:
        return dump_int16(val, buf, off);
    case LWES_TYPE_U_INT_32:
        return dump_uint32(val, buf, off);
    case LWES_TYPE_INT_32:
        return dump_int32(val, buf, off);
    case LWES_TYPE_U_INT_64:
        return dump_uint64(val, buf, off);
    case LWES_TYPE_INT_64:
        return dump_int64(val, buf, off);
    case LWES_TYPE_IP_ADDR:
        return dump_ip_addr(val, buf, off);
    default:
        rb_raise(rb_eRuntimeError,
                 "unknown LWES attribute type: 0x%02x", type);
    }
    assert("you should never get here (dump_num)");
    return -1;
}
Пример #2
0
void dump_int64_smart(uint64_t value, FILE *stream)
{
	uint64_t val = value;
	const char *c = "";

	if(value > 1000000000)
	{
		val = value / 1000000000;
		c = "G";
	}
	else if(value > 1000000)
	{
		val = value / 1000000;
		c = "M";
	}
	else if(value > 1000)
	{
		val = value / 1000;
		c = "K";
	}

	dump_int64(val, stream);
	fputs(c, stream);
}