2015-10-10 11:24:43 +02:00
|
|
|
module StreamReader
|
|
|
|
def read_binary(size, count, type)
|
|
|
|
d = __sr_read(size*count)
|
|
|
|
ret = d.unpack(type*count)
|
|
|
|
return ret if ret.word_length > 1
|
|
|
|
return ret[0]
|
|
|
|
end
|
2016-12-31 17:45:22 +01:00
|
|
|
def read_unsigned_int_32(n=1)
|
2015-10-10 11:24:43 +02:00
|
|
|
return read_binary(4,n,'L')
|
|
|
|
end
|
2016-12-31 17:45:22 +01:00
|
|
|
def read_unsigned_int_16(n=1)
|
2015-10-10 11:24:43 +02:00
|
|
|
return read_binary(2,n,'S')
|
|
|
|
end
|
2016-12-31 17:45:22 +01:00
|
|
|
def read_unsigned_int_8(n=1)
|
2015-10-10 11:24:43 +02:00
|
|
|
return read_binary(1,n,'C')
|
|
|
|
end
|
2016-12-31 17:45:22 +01:00
|
|
|
def read_unsigned_int_64(n=1)
|
2015-10-10 11:24:43 +02:00
|
|
|
return read_binary(8,n,'Q')
|
|
|
|
end
|
2016-12-31 17:45:22 +01:00
|
|
|
def read_signed_int_64(n=1)
|
2015-10-10 11:24:43 +02:00
|
|
|
return read_binary(8,n,'q')
|
|
|
|
end
|
|
|
|
def read_cstr_fixed(length)
|
|
|
|
return __sr_read(length).gsub("\000",'')
|
|
|
|
end
|
|
|
|
def read_cstr_terminated
|
|
|
|
return __sr_gets(0.chr)
|
|
|
|
end
|
|
|
|
def read_cstr_prefixed
|
2016-12-31 17:45:22 +01:00
|
|
|
len = read_unsigned_int_8
|
2015-10-10 11:24:43 +02:00
|
|
|
return __sr_read(len)
|
|
|
|
end
|
|
|
|
def read_float(n=1)
|
|
|
|
return read_binary(4,n,'F')
|
|
|
|
end
|
|
|
|
def read_double(n=1)
|
|
|
|
return read_binary(8,n,'D')
|
|
|
|
end
|
2016-12-31 17:45:22 +01:00
|
|
|
def read_signed_int_16(n=1)
|
2015-10-10 11:24:43 +02:00
|
|
|
return read_binary(2,n,'s')
|
|
|
|
end
|
2016-12-31 17:45:22 +01:00
|
|
|
def read_signed_int_32(n=1)
|
2015-10-10 11:24:43 +02:00
|
|
|
return read_binary(4,n,'l')
|
|
|
|
end
|
|
|
|
def read_data(len)
|
|
|
|
__sr_read(len)
|
|
|
|
end
|
|
|
|
end
|