Merge: Manage errors on reading binary data
[nit.git] / lib / binary / binary.nit
index 28ddf32..f4be1eb 100644 (file)
@@ -45,10 +45,13 @@ in "C" `{
        #include <endian.h>
 
        // Android compatibility
+       #ifndef be32toh
+               #define be32toh(val) betoh32(val)
+               #define le32toh(val) letoh32(val)
+       #endif
+
        #ifndef be64toh
                #define be64toh(val) betoh64(val)
-       #endif
-       #ifndef le64toh
                #define le64toh(val) letoh64(val)
        #endif
 `}
@@ -148,11 +151,15 @@ redef abstract class Reader
        super BinaryStream
 
        # Read a single byte and return `true` if its value is different than 0
+       #
+       # Returns `false` when an error is pending (`last_error != null`).
        fun read_bool: Bool do return read_byte != 0
 
        # Get an `Array` of 8 `Bool` by reading a single byte
        #
        # To be used with `BinaryWriter::write_bits`.
+       #
+       # Returns an array of `false` when an error is pending (`last_error != null`).
        fun read_bits: Array[Bool]
        do
                var int = read_byte
@@ -163,12 +170,14 @@ redef abstract class Reader
        # Read a null terminated string
        #
        # To be used with `Writer::write_string`.
+       #
+       # Returns a truncated string when an error is pending (`last_error != null`).
        fun read_string: String
        do
                var buf = new FlatBuffer
                loop
                        var byte = read_byte
-                       if byte == 0x00 then return buf.to_s
+                       if byte == null or byte == 0x00 then return buf.to_s
                        buf.chars.add byte.ascii
                end
        end
@@ -176,6 +185,8 @@ redef abstract class Reader
        # Read the length as a 64 bits integer, then the content of the block
        #
        # To be used with `Writer::write_block`.
+       #
+       # Returns a truncated string when an error is pending (`last_error != null`).
        fun read_block: String
        do
                var length = read_int64
@@ -187,6 +198,8 @@ redef abstract class Reader
        #
        # Using this format may result in a loss of precision as it uses less bits
        # than Nit `Float`.
+       #
+       # Returns `0.0` when an error is pending (`last_error != null`).
        fun read_float: Float
        do
                if last_error != null then return 0.0
@@ -223,6 +236,8 @@ redef abstract class Reader
        `}
 
        # Read a floating point on 64 bits and return it as a `Float`
+       #
+       # Returns `0.0` when an error is pending (`last_error != null`).
        fun read_double: Float
        do
                if last_error != null then return 0.0
@@ -271,6 +286,8 @@ redef abstract class Reader
        #
        # Using this format may result in a loss of precision as the length of a
        # Nit `Int` may be less than 64 bits on some platforms.
+       #
+       # Returns `0` when an error is pending (`last_error != null`).
        fun read_int64: Int
        do
                if last_error != null then return 0
@@ -325,7 +342,7 @@ redef class Int
                        uint64_t conv;
                } u;
 
-               u.val = recv;
+               u.val = self;
 
                if (big_endian)
                        u.conv = htobe64(u.conv);
@@ -344,7 +361,7 @@ redef class Float
                        uint32_t conv;
                } u;
 
-               u.val = recv;
+               u.val = self;
 
                if (big_endian)
                        u.conv = htobe32(u.conv);
@@ -361,7 +378,7 @@ redef class Float
                        uint64_t conv;
                } u;
 
-               u.val = recv;
+               u.val = self;
 
                if (big_endian)
                        u.conv = htobe64(u.conv);