note description: "Serialize/Deserialize data from a medium." legal: "See notice at end of class." status: "See notice at end of class." date: "$Date: 2017-09-25 15:53:33 +0000 (Mon, 25 Sep 2017) $" revision: "$Revision: 100789 $" class SED_MEDIUM_READER_WRITER create make, make_for_reading, make_for_writing, make_with_buffer feature {NONE} -- Initialization default_create -- Process instances of classes with no creation clause. -- (Default: do nothing.) -- (from ANY) do end make (a_medium: IO_MEDIUM) -- Initialize current to read or write from a_medium. require a_medium_not_void: a_medium /= Void a_medium_support_storable: a_medium.support_storable do make_with_buffer (a_medium, Default_buffer_size) ensure medium_set: medium = a_medium buffer_size_set: buffer_size = Default_buffer_size end make_for_reading (a_medium: IO_MEDIUM) -- Initialize current to read from a_medium. require a_medium_not_void: a_medium /= Void a_medium_open_for_reading: a_medium.is_open_read a_medium_support_storable: a_medium.support_storable do make (a_medium) set_for_reading ensure medium_set: medium = a_medium buffer_size_set: buffer_size = Default_buffer_size end make_for_writing (a_medium: IO_MEDIUM) -- Initialize current to write to a_medium. require a_medium_not_void: a_medium /= Void a_medium_open_for_writing: a_medium.is_open_write a_medium_support_storable: a_medium.support_storable do make (a_medium) set_for_writing ensure medium_set: medium = a_medium buffer_size_set: buffer_size = Default_buffer_size end make_with_buffer (a_medium: IO_MEDIUM; a_buffer_size: INTEGER_32) -- Initialize current to read or write from a_medium using a buffer of size a_buffer_size. -- buffer_size will be overriden during read operation by the value of buffer_size used -- when writing. require a_medium_not_void: a_medium /= Void a_buffer_size_non_negative: a_buffer_size >= 0 a_medium_support_storable: a_medium.support_storable do medium := a_medium buffer_size := a_buffer_size create buffer.make (a_buffer_size) ensure medium_set: medium = a_medium buffer_size_set: buffer_size = a_buffer_size buffer_size_set: buffer.count = a_buffer_size end feature -- Access generating_type: TYPE [detachable SED_MEDIUM_READER_WRITER] -- Type of current object -- (type of which it is a direct instance) -- (from ANY) external "built_in" ensure -- from ANY generating_type_not_void: Result /= Void end generator: STRING_8 -- Name of current object's generating class -- (base class of the type of which it is a direct instance) -- (from ANY) external "built_in" ensure -- from ANY generator_not_void: Result /= Void generator_not_empty: not Result.is_empty end read_boolean: BOOLEAN -- Read next boolean -- (from SED_READER_WRITER) require -- from SED_READER_WRITER is_ready: is_ready_for_reading do Result := read_natural_8 = 1 end read_character_32: CHARACTER_32 -- Read next 32-bits character -- (from SED_READER_WRITER) require -- from SED_READER_WRITER is_ready: is_ready_for_reading do Result := read_natural_32.to_character_32 end read_character_8: CHARACTER_8 -- Read next 8-bits character -- (from SED_READER_WRITER) require -- from SED_READER_WRITER is_ready: is_ready_for_reading do Result := read_natural_8.to_character_8 end read_compressed_integer_32: INTEGER_32 -- Read next compressed integer_32. -- See read_compressed_natural_32 for more details about encoding. -- (from SED_READER_WRITER) require -- from SED_READER_WRITER is_ready: is_ready_for_reading do Result := read_compressed_natural_32.as_integer_32 end read_compressed_natural_32: NATURAL_32 -- Read next compressed natural_32. -- Depending on first natural_8 value read, it will tell how much more we need to read: -- 1 - Of the form 0xxxxxxx (0x00) for values between 0 and 127 -- 2 - Of the form 10xxxxxx (0x80) for values between 128 and 16382 and one more to read. -- 3 - Of the form 110xxxxx (0xC0) for values between 16383 and 2097150 and 2 more to read. -- 4 - Of the form 1110xxxx (0xE0) for values between 2097151 and 268435454 and 3 more to read -- 5 - Otherwise a full natural_32 to read. -- (from SED_READER_WRITER) require -- from SED_READER_WRITER is_ready: is_ready_for_reading local l_nat8: NATURAL_8 do l_nat8 := read_natural_8 if l_nat8 & 128 = 0 then Result := l_nat8.to_natural_32 elseif l_nat8 & 192 = 128 then Result := ((l_nat8.as_natural_32 & 63) |<< 8) | read_natural_8.as_natural_32 elseif l_nat8 & 224 = 192 then Result := ((l_nat8.as_natural_32 & 31) |<< 16) | (read_natural_8.as_natural_32 |<< 8) | read_natural_8.as_natural_32 elseif l_nat8 & 240 = 224 then Result := ((l_nat8.as_natural_32 & 15) |<< 24) | (read_natural_8.as_natural_32 |<< 16) | (read_natural_8.as_natural_32 |<< 8) | read_natural_8.as_natural_32 else Result := read_natural_32 end end read_immutable_string_32: IMMUTABLE_STRING_32 -- Read next sequence of characters encoded in UTF-8 as an IMMUTABLE_STRING_32. -- (from SED_READER_WRITER) require -- from SED_READER_WRITER is_ready: is_ready_for_reading do create Result.make_from_string (read_string_32) ensure -- from SED_READER_WRITER read_string_8_not_void: Result /= Void end read_immutable_string_8: IMMUTABLE_STRING_8 -- Read next 8-bits sequence of character as an IMMUTABLE_STRING_8 -- (from SED_READER_WRITER) require -- from SED_READER_WRITER is_ready: is_ready_for_reading do create Result.make_from_string (read_string_8) ensure -- from SED_READER_WRITER read_immutable_string_8_not_void: Result /= Void end read_integer_16: INTEGER_16 -- Read next integer_16 -- (from SED_BINARY_READER_WRITER) require -- from SED_READER_WRITER is_ready: is_ready_for_reading do check correct_size: {PLATFORM}.natural_16_bytes = {PLATFORM}.integer_16_bytes end Result := read_natural_16.as_integer_16 end read_integer_32: INTEGER_32 -- Read next integer_32 -- (from SED_BINARY_READER_WRITER) require -- from SED_READER_WRITER is_ready: is_ready_for_reading do check correct_size: {PLATFORM}.natural_32_bytes = {PLATFORM}.integer_32_bytes end Result := read_natural_32.as_integer_32 end read_integer_64: INTEGER_64 -- Read next integer_64 -- (from SED_BINARY_READER_WRITER) require -- from SED_READER_WRITER is_ready: is_ready_for_reading do check correct_size: {PLATFORM}.natural_64_bytes = {PLATFORM}.integer_64_bytes end Result := read_natural_64.as_integer_64 end read_integer_8: INTEGER_8 -- Read next integer_8 -- (from SED_BINARY_READER_WRITER) require -- from SED_READER_WRITER is_ready: is_ready_for_reading do check correct_size: {PLATFORM}.natural_8_bytes = {PLATFORM}.integer_8_bytes end Result := read_natural_8.as_integer_8 end read_natural_16: NATURAL_16 -- Read next natural_16 -- (from SED_BINARY_READER_WRITER) require -- from SED_READER_WRITER is_ready: is_ready_for_reading local l_pos: INTEGER_32 do check_buffer ({PLATFORM}.natural_16_bytes) l_pos := buffer_position if is_little_endian_storable then Result := buffer.read_natural_16_le (l_pos) else Result := buffer.read_natural_16_be (l_pos) end l_pos := l_pos + {PLATFORM}.natural_16_bytes buffer_position := l_pos end read_natural_32: NATURAL_32 -- Read next natural_32 -- (from SED_BINARY_READER_WRITER) require -- from SED_READER_WRITER is_ready: is_ready_for_reading local l_pos: INTEGER_32 do check_buffer ({PLATFORM}.natural_32_bytes) l_pos := buffer_position if is_little_endian_storable then Result := buffer.read_natural_32_le (l_pos) else Result := buffer.read_natural_32_be (l_pos) end l_pos := l_pos + {PLATFORM}.natural_32_bytes buffer_position := l_pos end read_natural_64: NATURAL_64 -- Read next natural_64 -- (from SED_BINARY_READER_WRITER) require -- from SED_READER_WRITER is_ready: is_ready_for_reading local l_pos: INTEGER_32 do check_buffer ({PLATFORM}.natural_64_bytes) l_pos := buffer_position if is_little_endian_storable then Result := buffer.read_natural_64_le (l_pos) else Result := buffer.read_natural_64_be (l_pos) end l_pos := l_pos + {PLATFORM}.natural_64_bytes buffer_position := l_pos end read_natural_8: NATURAL_8 -- Read next natural_8 -- (from SED_BINARY_READER_WRITER) require -- from SED_READER_WRITER is_ready: is_ready_for_reading local l_pos: INTEGER_32 do check_buffer ({PLATFORM}.natural_8_bytes) l_pos := buffer_position Result := buffer.read_natural_8 (l_pos) l_pos := l_pos + {PLATFORM}.natural_8_bytes buffer_position := l_pos end read_pointer: POINTER -- Read next pointer -- (from SED_BINARY_READER_WRITER) require -- from SED_READER_WRITER is_ready: is_ready_for_reading local l_val32: NATURAL_32 l_val64: NATURAL_64 do if is_pointer_value_stored then check_buffer (stored_pointer_bytes) if {PLATFORM}.pointer_bytes = stored_pointer_bytes then if {PLATFORM}.pointer_bytes = {PLATFORM}.natural_32_bytes then l_val32 := read_natural_32; ($Result).memory_copy ($l_val32.to_pointer, {PLATFORM}.natural_32_bytes) elseif {PLATFORM}.pointer_bytes = {PLATFORM}.natural_64_bytes then l_val64 := read_natural_64; ($Result).memory_copy ($l_val64.to_pointer, {PLATFORM}.natural_64_bytes) else check not_supported: False end end elseif {PLATFORM}.pointer_bytes > stored_pointer_bytes then check expected_size: {PLATFORM}.pointer_bytes = {PLATFORM}.natural_64_bytes and stored_pointer_bytes = {PLATFORM}.natural_32_bytes end l_val32 := read_natural_32; ($Result + {PLATFORM}.natural_32_bytes).memory_copy ($l_val32.to_pointer, {PLATFORM}.natural_32_bytes) else check expected_size: {PLATFORM}.pointer_bytes = {PLATFORM}.natural_32_bytes and stored_pointer_bytes = {PLATFORM}.natural_64_bytes end l_val32 := read_natural_64.as_natural_32; ($Result).memory_copy ($l_val32.to_pointer, {PLATFORM}.natural_32_bytes) end end end read_real_32: REAL_32 -- Read next real_32 -- (from SED_BINARY_READER_WRITER) require -- from SED_READER_WRITER is_ready: is_ready_for_reading local l_pos: INTEGER_32 do check_buffer ({PLATFORM}.real_32_bytes) l_pos := buffer_position if is_little_endian_storable then Result := buffer.read_real_32_le (l_pos) else Result := buffer.read_real_32_be (l_pos) end l_pos := l_pos + {PLATFORM}.real_32_bytes buffer_position := l_pos end read_real_64: REAL_64 -- Read next real_64 -- (from SED_BINARY_READER_WRITER) require -- from SED_READER_WRITER is_ready: is_ready_for_reading local l_pos: INTEGER_32 do check_buffer ({PLATFORM}.real_64_bytes) l_pos := buffer_position if is_little_endian_storable then Result := buffer.read_real_64_le (l_pos) else Result := buffer.read_real_64_be (l_pos) end l_pos := l_pos + {PLATFORM}.real_64_bytes buffer_position := l_pos end read_string_32: STRING_32 -- Read next sequence of characters encoded in UTF-8. -- (from SED_READER_WRITER) require -- from SED_READER_WRITER is_ready: is_ready_for_reading local u: UTF_CONVERTER do Result := u.utf_8_string_8_to_string_32 (read_string_8) ensure -- from SED_READER_WRITER read_string_8_not_void: Result /= Void end read_string_8: STRING_8 -- Read next 8-bits sequence of character -- (from SED_READER_WRITER) require -- from SED_READER_WRITER is_ready: is_ready_for_reading local i, nb: INTEGER_32 do nb := read_compressed_natural_32.to_integer_32 create Result.make (nb) from i := 1 nb := nb + 1 until i = nb loop Result.append_character (read_character_8) i := i + 1 end ensure -- from SED_READER_WRITER read_string_8_not_void: Result /= Void end Reader_writer_version_6_6: NATURAL_32 = 1 -- Version of format used for reading/writing. feature -- Comparison frozen deep_equal (a: detachable ANY; b: like arg #1): BOOLEAN -- Are a and b either both void -- or attached to isomorphic object structures? -- (from ANY) do if a = Void then Result := b = Void else Result := b /= Void and then a.is_deep_equal (b) end ensure -- from ANY instance_free: class shallow_implies_deep: standard_equal (a, b) implies Result both_or_none_void: (a = Void) implies (Result = (b = Void)) same_type: (Result and (a /= Void)) implies (b /= Void and then a.same_type (b)) symmetric: Result implies deep_equal (b, a) end frozen equal (a: detachable ANY; b: like arg #1): BOOLEAN -- Are a and b either both void or attached -- to objects considered equal? -- (from ANY) do if a = Void then Result := b = Void else Result := b /= Void and then a.is_equal (b) end ensure -- from ANY instance_free: class definition: Result = (a = Void and b = Void) or else ((a /= Void and b /= Void) and then a.is_equal (b)) end frozen is_deep_equal alias "≡≡≡" (other: SED_MEDIUM_READER_WRITER): BOOLEAN -- Are Current and other attached to isomorphic object structures? -- (from ANY) require -- from ANY other_not_void: other /= Void external "built_in" ensure -- from ANY shallow_implies_deep: standard_is_equal (other) implies Result same_type: Result implies same_type (other) symmetric: Result implies other.is_deep_equal (Current) end is_equal (other: SED_MEDIUM_READER_WRITER): BOOLEAN -- Is other attached to an object considered -- equal to current object? -- (from ANY) require -- from ANY other_not_void: other /= Void external "built_in" ensure -- from ANY symmetric: Result implies other ~ Current consistent: standard_is_equal (other) implies Result end frozen standard_equal (a: detachable ANY; b: like arg #1): BOOLEAN -- Are a and b either both void or attached to -- field-by-field identical objects of the same type? -- Always uses default object comparison criterion. -- (from ANY) do if a = Void then Result := b = Void else Result := b /= Void and then a.standard_is_equal (b) end ensure -- from ANY instance_free: class definition: Result = (a = Void and b = Void) or else ((a /= Void and b /= Void) and then a.standard_is_equal (b)) end frozen standard_is_equal alias "≜" (other: SED_MEDIUM_READER_WRITER): BOOLEAN -- Is other attached to an object of the same type -- as current object, and field-by-field identical to it? -- (from ANY) require -- from ANY other_not_void: other /= Void external "built_in" ensure -- from ANY same_type: Result implies same_type (other) symmetric: Result implies other.standard_is_equal (Current) end feature -- Status report cleanup -- When a reading is stopped, perform the necessary cleanup to perform -- another reading or writing. require -- from SED_READER_WRITER is_ready: is_ready_for_reading do from until is_last_chunk loop read_buffer_from_medium end end conforms_to (other: ANY): BOOLEAN -- Does type of current object conform to type -- of other (as per Eiffel: The Language, chapter 13)? -- (from ANY) require -- from ANY other_not_void: other /= Void external "built_in" end is_for_reading: BOOLEAN -- Will current do a read operation? -- (from SED_BINARY_READER_WRITER) is_last_chunk: BOOLEAN -- Are we reading the last chunk? is_pointer_value_stored: BOOLEAN -- Is value of a POINTER stored? -- (from SED_READER_WRITER) is_ready_for_reading: BOOLEAN -- Is Current ready for future read operations? require -- from SED_READER_WRITER True do Result := is_for_reading and then medium.exists and then medium.is_open_read and then medium.support_storable and then medium.readable end is_ready_for_writing: BOOLEAN -- Is Current ready for future write operations? require -- from SED_READER_WRITER True do Result := not is_for_reading and then medium.exists and then medium.is_open_write and then medium.support_storable end same_type (other: ANY): BOOLEAN -- Is type of current object identical to type of other? -- (from ANY) require -- from ANY other_not_void: other /= Void external "built_in" ensure -- from ANY definition: Result = (conforms_to (other) and other.conforms_to (Current)) end feature -- Element change write_boolean (v: BOOLEAN) -- Write v. -- (from SED_READER_WRITER) require -- from SED_READER_WRITER is_ready: is_ready_for_writing do if v then write_natural_8 (1) else write_natural_8 (0) end end write_character_32 (v: CHARACTER_32) -- Write v. -- (from SED_READER_WRITER) require -- from SED_READER_WRITER is_ready: is_ready_for_writing do write_natural_32 (v.natural_32_code) end write_character_8 (v: CHARACTER_8) -- Write v. -- (from SED_READER_WRITER) require -- from SED_READER_WRITER is_ready: is_ready_for_writing do write_natural_8 (v.code.to_natural_8) end write_compressed_integer_32 (v: INTEGER_32) -- Write v as a compressed integer_32. -- See write_compressed_natural_32 for details about encoding. -- (from SED_READER_WRITER) require -- from SED_READER_WRITER is_ready: is_ready_for_writing do write_compressed_natural_32 (v.as_natural_32) end write_compressed_natural_32 (v: NATURAL_32) -- Write v as a compressed natural_32. -- Depending on value of v, it will tell how many natural_8 will -- be written. Below here is the actual encoding where x represents -- a meaningful bit for v and the last number in parenthesis is the -- marker value in hexadecimal which is added to the value of v. -- 1 - For values between 0 and 127, write 0xxxxxxx (0x00). -- 2 - For values between 128 and 16382, write 0x10xxxxxx xxxxxxxx (0xE0). -- 3 - For values between 16383 and 2097150, write 0x110xxxxx xxxxxxxx xxxxxxxx (0xC0). -- 4 - For values between 2097151 and 268435454, write 0x1110xxx xxxxxxxx xxxxxxxx xxxxxxxx (0xE0). -- 5 - Otherwise write v as a full natural_32. -- (from SED_READER_WRITER) require -- from SED_READER_WRITER is_ready: is_ready_for_writing do if v < 16384 then if v < 128 then write_natural_8 (v.as_natural_8) else write_natural_8 ((((v | 32768) & 65280) |>> 8).as_natural_8) write_natural_8 ((v & 255).as_natural_8) end elseif v < 2097152 then write_natural_8 ((((v | 12582912) & 16711680) |>> 16).as_natural_8) write_natural_8 (((v & 65280) |>> 8).as_natural_8) write_natural_8 ((v & 255).as_natural_8) elseif v < 268435456 then write_natural_8 ((((v | 3758096384) & 4278190080) |>> 24).as_natural_8) write_natural_8 (((v & 16711680) |>> 16).as_natural_8) write_natural_8 (((v & 65280) |>> 8).as_natural_8) write_natural_8 ((v & 255).as_natural_8) else write_natural_8 (240) write_natural_32 (v) end end write_immutable_string_8 (v: IMMUTABLE_STRING_8) obsolete "Use `write_string_8' instead. [2017-05-31]" -- Write v. -- (from SED_READER_WRITER) require -- from SED_READER_WRITER is_ready: is_ready_for_writing v_not_void: v /= Void do write_string_8 (v) end write_integer_16 (v: INTEGER_16) -- Write v. -- (from SED_BINARY_READER_WRITER) require -- from SED_READER_WRITER is_ready: is_ready_for_writing do check correct_size: {PLATFORM}.natural_16_bytes = {PLATFORM}.integer_16_bytes end write_natural_16 (v.as_natural_16) end write_integer_32 (v: INTEGER_32) -- Write v. -- (from SED_BINARY_READER_WRITER) require -- from SED_READER_WRITER is_ready: is_ready_for_writing do check correct_size: {PLATFORM}.natural_32_bytes = {PLATFORM}.integer_32_bytes end write_natural_32 (v.as_natural_32) end write_integer_64 (v: INTEGER_64) -- Write v. -- (from SED_BINARY_READER_WRITER) require -- from SED_READER_WRITER is_ready: is_ready_for_writing do check correct_size: {PLATFORM}.natural_64_bytes = {PLATFORM}.integer_64_bytes end write_natural_64 (v.as_natural_64) end write_integer_8 (v: INTEGER_8) -- Write v. -- (from SED_BINARY_READER_WRITER) require -- from SED_READER_WRITER is_ready: is_ready_for_writing do check correct_size: {PLATFORM}.natural_8_bytes = {PLATFORM}.integer_8_bytes end write_natural_8 (v.as_natural_8) end write_natural_16 (v: NATURAL_16) -- Write v. -- (from SED_BINARY_READER_WRITER) require -- from SED_READER_WRITER is_ready: is_ready_for_writing local l_pos: INTEGER_32 do check_buffer ({PLATFORM}.natural_16_bytes) l_pos := buffer_position if is_little_endian_storable then buffer.put_natural_16_le (v, l_pos) else buffer.put_natural_16_be (v, l_pos) end l_pos := l_pos + {PLATFORM}.natural_16_bytes buffer_position := l_pos end write_natural_32 (v: NATURAL_32) -- Write v. -- (from SED_BINARY_READER_WRITER) require -- from SED_READER_WRITER is_ready: is_ready_for_writing local l_pos: INTEGER_32 do check_buffer ({PLATFORM}.natural_32_bytes) l_pos := buffer_position if is_little_endian_storable then buffer.put_natural_32_le (v, l_pos) else buffer.put_natural_32_be (v, l_pos) end l_pos := l_pos + {PLATFORM}.natural_32_bytes buffer_position := l_pos end write_natural_64 (v: NATURAL_64) -- Write v. -- (from SED_BINARY_READER_WRITER) require -- from SED_READER_WRITER is_ready: is_ready_for_writing local l_pos: INTEGER_32 do check_buffer ({PLATFORM}.natural_64_bytes) l_pos := buffer_position if is_little_endian_storable then buffer.put_natural_64_le (v, l_pos) else buffer.put_natural_64_be (v, l_pos) end l_pos := l_pos + {PLATFORM}.natural_64_bytes buffer_position := l_pos end write_natural_8 (v: NATURAL_8) -- Write v. -- (from SED_BINARY_READER_WRITER) require -- from SED_READER_WRITER is_ready: is_ready_for_writing local l_pos: INTEGER_32 do check_buffer ({PLATFORM}.natural_8_bytes) l_pos := buffer_position; buffer.put_natural_8 (v, l_pos) l_pos := l_pos + {PLATFORM}.natural_8_bytes buffer_position := l_pos end write_pointer (v: POINTER) -- Write v. -- (from SED_BINARY_READER_WRITER) require -- from SED_READER_WRITER is_ready: is_ready_for_writing local l_val32: NATURAL_32 l_val64: NATURAL_64 do if is_pointer_value_stored then if {PLATFORM}.pointer_bytes = {PLATFORM}.natural_32_bytes then ($l_val32).memory_copy ($v.to_pointer, {PLATFORM}.natural_32_bytes) write_natural_32 (l_val32) elseif {PLATFORM}.pointer_bytes = {PLATFORM}.natural_64_bytes then ($l_val64).memory_copy ($v.to_pointer, {PLATFORM}.natural_64_bytes) write_natural_64 (l_val64) else check not_supported: False end end end end write_real_32 (v: REAL_32) -- Write v. -- (from SED_BINARY_READER_WRITER) require -- from SED_READER_WRITER is_ready: is_ready_for_writing local l_pos: INTEGER_32 do check_buffer ({PLATFORM}.real_32_bytes) l_pos := buffer_position if is_little_endian_storable then buffer.put_real_32_le (v, l_pos) else buffer.put_real_32_be (v, l_pos) end l_pos := l_pos + {PLATFORM}.real_32_bytes buffer_position := l_pos end write_real_64 (v: REAL_64) -- Write v. -- (from SED_BINARY_READER_WRITER) require -- from SED_READER_WRITER is_ready: is_ready_for_writing local l_pos: INTEGER_32 do check_buffer ({PLATFORM}.real_64_bytes) l_pos := buffer_position if is_little_endian_storable then buffer.put_real_64_le (v, l_pos) else buffer.put_real_64_be (v, l_pos) end l_pos := l_pos + {PLATFORM}.real_64_bytes buffer_position := l_pos end write_string_32 (v: READABLE_STRING_GENERAL) -- Write v in UTF-8 format. -- (from SED_READER_WRITER) require -- from SED_READER_WRITER is_ready: is_ready_for_writing v_not_void: v /= Void local u: UTF_CONVERTER do write_string_8 (u.utf_32_string_to_utf_8_string_8 (v)) end write_string_8 (v: READABLE_STRING_8) -- Write v. -- (from SED_READER_WRITER) require -- from SED_READER_WRITER is_ready: is_ready_for_writing v_not_void: v /= Void local i, nb: INTEGER_32 do write_compressed_natural_32 (v.count.to_natural_32) from i := 1 nb := v.count + 1 until i = nb loop write_character_8 (v.item (i)) i := i + 1 end end feature -- Duplication frozen clone (other: detachable ANY): like other obsolete "Use `twin' instead. [2017-05-31]" -- Void if other is void; otherwise new object -- equal to other -- -- For non-void other, clone calls copy; -- to change copying/cloning semantics, redefine copy. -- (from ANY) do if other /= Void then Result := other.twin end ensure -- from ANY instance_free: class equal: Result ~ other end copy (other: SED_MEDIUM_READER_WRITER) -- Update current object using fields of object attached -- to other, so as to yield equal objects. -- (from ANY) require -- from ANY other_not_void: other /= Void type_identity: same_type (other) external "built_in" ensure -- from ANY is_equal: Current ~ other end frozen deep_clone (other: detachable ANY): like other obsolete "Use `deep_twin' instead. [2017-05-31]" -- Void if other is void: otherwise, new object structure -- recursively duplicated from the one attached to other -- (from ANY) do if other /= Void then Result := other.deep_twin end ensure -- from ANY instance_free: class deep_equal: deep_equal (other, Result) end frozen deep_copy (other: SED_MEDIUM_READER_WRITER) -- Effect equivalent to that of: -- copy (other . deep_twin) -- (from ANY) require -- from ANY other_not_void: other /= Void do copy (other.deep_twin) ensure -- from ANY deep_equal: deep_equal (Current, other) end frozen deep_twin: SED_MEDIUM_READER_WRITER -- New object structure recursively duplicated from Current. -- (from ANY) external "built_in" ensure -- from ANY deep_twin_not_void: Result /= Void deep_equal: deep_equal (Current, Result) end frozen standard_clone (other: detachable ANY): like other obsolete "Use `standard_twin' instead. [2017-05-31]" -- Void if other is void; otherwise new object -- field-by-field identical to other. -- Always uses default copying semantics. -- (from ANY) do if other /= Void then Result := other.standard_twin end ensure -- from ANY instance_free: class equal: standard_equal (Result, other) end frozen standard_copy (other: SED_MEDIUM_READER_WRITER) -- Copy every field of other onto corresponding field -- of current object. -- (from ANY) require -- from ANY other_not_void: other /= Void type_identity: same_type (other) external "built_in" ensure -- from ANY is_standard_equal: standard_is_equal (other) end frozen standard_twin: SED_MEDIUM_READER_WRITER -- New object field-by-field identical to other. -- Always uses default copying semantics. -- (from ANY) external "built_in" ensure -- from ANY standard_twin_not_void: Result /= Void equal: standard_equal (Result, Current) end frozen twin: SED_MEDIUM_READER_WRITER -- New object equal to Current -- twin calls copy; to change copying/twinning semantics, redefine copy. -- (from ANY) external "built_in" ensure -- from ANY twin_not_void: Result /= Void is_equal: Result ~ Current end feature -- Basic operations frozen as_attached: attached SED_MEDIUM_READER_WRITER obsolete "Remove calls to this feature. [2017-05-31]" -- Attached version of Current. -- (Can be used during transitional period to convert -- non-void-safe classes to void-safe ones.) -- (from ANY) do Result := Current end frozen default: detachable SED_MEDIUM_READER_WRITER -- Default value of object's type -- (from ANY) do end frozen default_pointer: POINTER -- Default value of type POINTER -- (Avoid the need to write p.default for -- some p of type POINTER.) -- (from ANY) do ensure -- from ANY instance_free: class end default_rescue -- Process exception for routines with no Rescue clause. -- (Default: do nothing.) -- (from ANY) do end frozen do_nothing -- Execute a null action. -- (from ANY) do ensure -- from ANY instance_free: class end feature -- Access bits size boolean_bits: INTEGER_32 -- Number of bits in a value of type BOOLEAN -- (from PLATFORM) do Result := boolean_bytes * 8 ensure -- from PLATFORM instance_free: class end Character_32_bits: INTEGER_32 = 32 -- Number of bits in a value of type CHARACTER_32 -- (from PLATFORM) Character_8_bits: INTEGER_32 = 8 -- Number of bits in a value of type CHARACTER_8 -- (from PLATFORM) Integer_16_bits: INTEGER_32 = 16 -- Number of bits in a value of type INTEGER_16 -- (from PLATFORM) Integer_32_bits: INTEGER_32 = 32 -- Number of bits in a value of type INTEGER_32 -- (from PLATFORM) Integer_64_bits: INTEGER_32 = 64 -- Number of bits in a value of type INTEGER_64 -- (from PLATFORM) Integer_8_bits: INTEGER_32 = 8 -- Number of bits in a value of type INTEGER_8 -- (from PLATFORM) Natural_16_bits: INTEGER_32 = 16 -- Number of bits in a value of type NATURAL_16 -- (from PLATFORM) Natural_32_bits: INTEGER_32 = 32 -- Number of bits in a value of type NATURAL_32 -- (from PLATFORM) Natural_64_bits: INTEGER_32 = 64 -- Number of bits in a value of type NATURAL_64 -- (from PLATFORM) Natural_8_bits: INTEGER_32 = 8 -- Number of bits in a value of type NATURAL_8 -- (from PLATFORM) pointer_bits: INTEGER_32 -- Number of bits in a value of type POINTER -- (from PLATFORM) do Result := pointer_bytes * 8 ensure -- from PLATFORM instance_free: class end Real_32_bits: INTEGER_32 = 32 -- Number of bits in a value of type REAL_32 -- (from PLATFORM) Real_64_bits: INTEGER_32 = 64 -- Number of bits in a value of type REAL_64 -- (from PLATFORM) feature -- Access bytes size boolean_bytes: INTEGER_32 -- Number of bytes in a value of type BOOLEAN -- (from PLATFORM) external "built_in static" ensure -- from PLATFORM instance_free: class end Character_32_bytes: INTEGER_32 = 4 -- Number of bytes in a value of type CHARACTER_32 -- (from PLATFORM) Character_8_bytes: INTEGER_32 = 1 -- Number of bytes in a value of type CHARACTER_8 -- (from PLATFORM) Integer_16_bytes: INTEGER_32 = 2 -- Number of bytes in a value of type INTEGER_16 -- (from PLATFORM) Integer_32_bytes: INTEGER_32 = 4 -- Number of bytes in a value of type INTEGER_32 -- (from PLATFORM) Integer_64_bytes: INTEGER_32 = 8 -- Number of bytes in a value of type INTEGER_64 -- (from PLATFORM) Integer_8_bytes: INTEGER_32 = 1 -- Number of bytes in a value of type INTEGER_8 -- (from PLATFORM) Natural_16_bytes: INTEGER_32 = 2 -- Number of bytes in a value of type NATURAL_16 -- (from PLATFORM) Natural_32_bytes: INTEGER_32 = 4 -- Number of bytes in a value of type NATURAL_32 -- (from PLATFORM) Natural_64_bytes: INTEGER_32 = 8 -- Number of bytes in a value of type NATURAL_64 -- (from PLATFORM) Natural_8_bytes: INTEGER_32 = 1 -- Number of bytes in a value of type NATURAL_8 -- (from PLATFORM) pointer_bytes: INTEGER_32 -- Number of bytes in a value of type POINTER -- (from PLATFORM) external "built_in static" ensure -- from PLATFORM instance_free: class end Real_32_bytes: INTEGER_32 = 4 -- Number of bytes in a value of type REAL_32 -- (from PLATFORM) Real_64_bytes: INTEGER_32 = 8 -- Number of bytes in a value of type REAL_64 -- (from PLATFORM) feature {NONE} -- Buffer update check_buffer (n: INTEGER_32) -- If there is enough space in buffer to read n bytes, do nothing. -- Otherwise, read/write to medium to free some space. require -- from SED_BINARY_READER_WRITER n_non_negative: n >= 0 do if n + buffer_position > buffer_size then if is_for_reading then read_buffer_from_medium else flush_buffer_to_medium (False) end end end flush_buffer_to_medium (last_chunk: BOOLEAN) -- Write next chunk of data to medium. require is_ready: is_ready_for_writing do buffer.put_integer_32_be (buffer_position, size_position (True)); buffer.put_boolean (last_chunk, new_chunk_position); medium.put_managed_pointer (buffer, 0, buffer_position) write_chunk_header end read_buffer_from_medium -- Read next chunk of data. require is_ready: is_ready_for_reading local l_version: NATURAL_32 l_is_new_format: BOOLEAN l_failure: SERIALIZATION_FAILURE do medium.read_to_managed_pointer (buffer, 0, {PLATFORM}.integer_32_bytes) if medium.bytes_read = {PLATFORM}.integer_32_bytes then buffer_size := buffer.read_integer_32_be (0) is_last_chunk := True if buffer_size = -1 then l_is_new_format := True; medium.read_to_managed_pointer (buffer, version_position, {PLATFORM}.integer_32_bytes) l_version := buffer.read_natural_32_be (version_position) if l_version = Reader_writer_version_6_6 then medium.read_to_managed_pointer (buffer, size_position (True), {PLATFORM}.integer_32_bytes) buffer_size := buffer.read_integer_32_be (size_position (True)); medium.read_to_managed_pointer (buffer, new_chunk_position, {PLATFORM}.boolean_bytes) is_last_chunk := buffer.read_boolean (new_chunk_position) end else l_is_new_format := False end buffer_position := chunk_header_size (l_is_new_format) if buffer_size > buffer.count then buffer.resize (buffer_size) end if buffer_size > {PLATFORM}.integer_32_bytes then medium.read_to_managed_pointer (buffer, buffer_position, buffer_size - buffer_position) if medium.bytes_read /= buffer_size - buffer_position then buffer_position := 0 buffer_size := buffer.count create l_failure; l_failure.set_description ("Read less than expected number of bytes in buffer."); l_failure.raise end else buffer_position := 0 buffer_size := buffer.count create l_failure; l_failure.set_description ("Read less than 4 bytes in buffer's header."); l_failure.raise end else buffer_position := 0 buffer_size := buffer.count create l_failure; l_failure.set_description ("Cannot read buffer size from header."); l_failure.raise end end feature -- Header/Footer read_footer -- Action being executed after finishing to read data. -- (from SED_READER_WRITER) require -- from SED_READER_WRITER is_ready: is_ready_for_reading do end read_header -- Retrieve configuration of how data was stored. require -- from SED_READER_WRITER is_ready: is_ready_for_reading do read_buffer_from_medium is_little_endian_storable := read_boolean is_pointer_value_stored := read_boolean stored_pointer_bytes := read_integer_32 end write_chunk_header -- Store information about chunks do buffer_position := 0; buffer.put_integer_32_be (-1, 0) buffer_position := buffer_position + {PLATFORM}.integer_32_bytes; buffer.put_natural_32_be (Reader_writer_version_6_6, buffer_position) buffer_position := buffer_position + {PLATFORM}.natural_32_bytes buffer_position := buffer_position + {PLATFORM}.integer_32_bytes buffer_position := buffer_position + {PLATFORM}.boolean_bytes end write_footer -- Store last buffered data. require -- from SED_READER_WRITER is_ready: is_ready_for_writing do flush_buffer_to_medium (True) end write_header -- Store configuration on how data will be stored. require -- from SED_READER_WRITER is_ready: is_ready_for_writing do buffer_size := buffer.count check enough_space: buffer.count > {PLATFORM}.integer_32_bytes * 4 + {PLATFORM}.boolean_bytes * 2 end is_little_endian_storable := Is_little_endian write_chunk_header write_boolean (is_little_endian_storable) write_boolean (is_pointer_value_stored) write_integer_32 ({PLATFORM}.pointer_bytes) end feature {NONE} -- Implementation: Access buffer: MANAGED_POINTER -- Buffer to store/fetch data from medium -- (from SED_BINARY_READER_WRITER) chunk_header_size (a_is_new_format: BOOLEAN): INTEGER_32 -- Size of data do Result := {PLATFORM}.integer_32_bytes if a_is_new_format then Result := Result + {PLATFORM}.natural_32_bytes + {PLATFORM}.integer_32_bytes + {PLATFORM}.boolean_bytes end end medium: IO_MEDIUM -- Medium used for read/write operations new_chunk_position: INTEGER_32 -- Position where presence or not of a next chunk is stored. do Result := size_position (True) + {PLATFORM}.integer_32_bytes end size_position (a_is_new_format: BOOLEAN): INTEGER_32 -- Position where size information for current chunk is stored. do if a_is_new_format then Result := version_position + {PLATFORM}.natural_32_bytes else Result := 0 end end version_position: INTEGER_32 -- Position where information about version is stored do Result := size_position (False) + {PLATFORM}.integer_32_bytes end feature {NONE} -- Implementation: Status report buffer_position: INTEGER_32 -- Position in buffer for next read/write operation -- (from SED_BINARY_READER_WRITER) buffer_size: INTEGER_32 -- Size for buffer to reduce access to medium -- (from SED_BINARY_READER_WRITER) Default_buffer_size: INTEGER_32 = 262144 -- Default size for buffer -- (from SED_BINARY_READER_WRITER) is_little_endian_storable: BOOLEAN -- If is_for_reading, encoding used to write data. -- Otherwise encoding that will be used to write data. -- (from SED_BINARY_READER_WRITER) stored_pointer_bytes: INTEGER_32 -- Size of pointers in bytes used to write data. -- (from SED_BINARY_READER_WRITER) feature -- Obsoletes character_bits: INTEGER_32 obsolete "Use `character_8_bits' instead. [2017-05-31]" -- Number of bits in a value of type CHARACTER_8 -- (from PLATFORM) do Result := 8 ensure -- from PLATFORM instance_free: class end character_bytes: INTEGER_32 obsolete "Use `character_8_bytes' instead. [2017-05-31]" -- Number of bytes in a value of type CHARACTER_8 -- (from PLATFORM) external "built_in static" ensure -- from PLATFORM instance_free: class end double_bits: INTEGER_32 obsolete "Use `real_64_bits' instead. [2017-05-31]" -- Number of bits in a value of type REAL_64 -- (from PLATFORM) do Result := 64 ensure -- from PLATFORM instance_free: class end double_bytes: INTEGER_32 obsolete "Use `real_64_bytes' instead. [2017-05-31]" -- Number of bytes in a value of type REAL_64 -- (from PLATFORM) external "built_in static" ensure -- from PLATFORM instance_free: class end integer_bits: INTEGER_32 obsolete "Use `integer_32_bits' instead. [2017-05-31]" -- Number of bits in a value of type INTEGER_32 -- (from PLATFORM) do Result := 32 ensure -- from PLATFORM instance_free: class end integer_bytes: INTEGER_32 obsolete "Use `integer_32_bytes' instead. [2017-05-31]" -- Number of bytes in a value of type INTEGER_32 -- (from PLATFORM) external "built_in static" ensure -- from PLATFORM instance_free: class end maximum_character_code: INTEGER_32 obsolete "Use `{CHARACTER_8}.max_value' instead. [2017-05-31]" -- Largest supported code for CHARACTER_8 values -- (from PLATFORM) do Result := {CHARACTER_8}.max_value ensure -- from PLATFORM instance_free: class meaningful: Result >= 127 end maximum_integer: INTEGER_32 obsolete "Use `{INTEGER}.max_value' instead. [2017-05-31]" -- Largest supported value of type INTEGER_32 -- (from PLATFORM) do Result := {INTEGER_32}.max_value ensure -- from PLATFORM instance_free: class meaningful: Result >= 0 end minimum_character_code: INTEGER_32 obsolete "Use `{CHARACTER_8}.min_value' instead. [2017-05-31]" -- Smallest supported code for CHARACTER_8 values -- (from PLATFORM) do Result := {CHARACTER_8}.min_value ensure -- from PLATFORM instance_free: class meaningful: Result <= 0 end minimum_integer: INTEGER_32 obsolete "Use `{INTEGER}.min_value' instead. [2017-05-31]" -- Smallest supported value of type INTEGER_32 -- (from PLATFORM) do Result := {INTEGER_32}.min_value ensure -- from PLATFORM instance_free: class meaningful: Result <= 0 end real_bits: INTEGER_32 obsolete "Use `real_32_bits' instead. [2017-05-31]" -- Number of bits in a value of type REAL_32 -- (from PLATFORM) do Result := 32 ensure -- from PLATFORM instance_free: class end real_bytes: INTEGER_32 obsolete "Use `real_32_bytes' instead. [2017-05-31]" -- Number of bytes in a value of type REAL_32 -- (from PLATFORM) external "built_in static" ensure -- from PLATFORM instance_free: class end wide_character_bytes: INTEGER_32 obsolete "Use `character_32_bytes' instead. [2017-05-31]" -- Number of bytes in a value of type CHARACTER_32 -- (from PLATFORM) external "built_in static" ensure -- from PLATFORM instance_free: class end feature -- Output Io: STD_FILES -- Handle to standard file setup -- (from ANY) once create Result; Result.set_output_default ensure -- from ANY instance_free: class io_not_void: Result /= Void end out: STRING_8 -- New string containing terse printable representation -- of current object -- (from ANY) do Result := tagged_out ensure -- from ANY out_not_void: Result /= Void end print (o: detachable ANY) -- Write terse external representation of o -- on standard output. -- (from ANY) local s: READABLE_STRING_8 do if attached o then s := o.out if attached {READABLE_STRING_32} s as s32 then Io.put_string_32 (s32) elseif attached {READABLE_STRING_8} s as s8 then Io.put_string (s8) else Io.put_string_32 (s.as_string_32) end end ensure -- from ANY instance_free: class end frozen tagged_out: STRING_8 -- New string containing terse printable representation -- of current object -- (from ANY) external "built_in" ensure -- from ANY tagged_out_not_void: Result /= Void end feature -- Platform is_64_bits: BOOLEAN -- Is the current process runing in 64-bit mode -- (from PLATFORM) external "built_in static" ensure -- from PLATFORM instance_free: class end is_dotnet: BOOLEAN -- Are we targetting .NET? -- (from PLATFORM) external "built_in static" ensure -- from PLATFORM instance_free: class end Is_little_endian: BOOLEAN -- Is current platform a little endian one? -- (from PLATFORM) local l_nat16: NATURAL_16 l_nat8: NATURAL_8 l_first: NATURAL_8 once l_nat16 := 17185 l_nat8 := 33; ($l_first).memory_copy ($l_nat16.to_pointer, 1) Result := l_first = l_nat8 ensure -- from PLATFORM instance_free: class end is_mac: BOOLEAN -- Are we running on Mac OS? -- (from PLATFORM) external "built_in static" ensure -- from PLATFORM instance_free: class end is_scoop_capable: BOOLEAN -- Is current platform capable of SCOOP? -- (from PLATFORM) external "built_in static" ensure -- from PLATFORM instance_free: class end is_thread_capable: BOOLEAN -- Is current platform capable of multi-threading? -- (from PLATFORM) external "built_in static" ensure -- from PLATFORM instance_free: class end is_unix: BOOLEAN -- Are we running on a Unix like platform? -- (from PLATFORM) external "built_in static" ensure -- from PLATFORM instance_free: class end is_vms: BOOLEAN -- Are we running on VMS? -- (from PLATFORM) external "built_in static" ensure -- from PLATFORM instance_free: class end is_vxworks: BOOLEAN -- Are we running on VxWorks? -- (from PLATFORM) external "built_in static" ensure -- from PLATFORM instance_free: class end is_windows: BOOLEAN -- Are we running on Windows platform? -- (from PLATFORM) external "built_in static" ensure -- from PLATFORM instance_free: class end Operating_environment: OPERATING_ENVIRONMENT -- Objects available from the operating system -- (from ANY) once create Result ensure -- from ANY instance_free: class operating_environment_not_void: Result /= Void end feature {NONE} -- Retrieval frozen internal_correct_mismatch -- Called from runtime to perform a proper dynamic dispatch on correct_mismatch -- from MISMATCH_CORRECTOR. -- (from ANY) local l_msg: STRING_32 l_exc: EXCEPTIONS do if attached {MISMATCH_CORRECTOR} Current as l_corrector then l_corrector.correct_mismatch else create l_msg.make_from_string ("Mismatch: ".as_string_32) create l_exc; l_msg.append (generating_type.name_32); l_exc.raise_retrieval_exception (l_msg) end end feature -- Settings set_for_reading -- Set current for reading. -- (from SED_BINARY_READER_WRITER) do is_for_reading := True ensure -- from SED_BINARY_READER_WRITER is_for_reading: is_for_reading end set_for_writing -- Set current for writing. -- (from SED_BINARY_READER_WRITER) do is_for_reading := False ensure -- from SED_BINARY_READER_WRITER is_for_writing: not is_for_reading end set_is_pointer_value_stored (v: like is_pointer_value_stored) -- Set is_pointer_value_stored with v. -- (from SED_READER_WRITER) do is_pointer_value_stored := v ensure -- from SED_READER_WRITER is_pointer_value_stored_set: is_pointer_value_stored = v end invariant medium_not_void: medium /= Void -- from SED_BINARY_READER_WRITER buffer_not_void: buffer /= Void buffer_size_non_negative: buffer_size >= 0 buffer_position_non_negative: buffer_position >= 0 stored_pointer_bytes_non_negative: stored_pointer_bytes >= 0 -- from ANY reflexive_equality: standard_is_equal (Current) reflexive_conformance: conforms_to (Current) note library: "EiffelBase: Library of reusable components for Eiffel." copyright: "Copyright (c) 1984-2017, Eiffel Software and others" license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)" source: "[ Eiffel Software 5949 Hollister Ave., Goleta, CA 93117 USA Telephone 805-685-1006, Fax 805-685-6869 Website http://www.eiffel.com Customer support http://support.eiffel.com ]" end -- class SED_MEDIUM_READER_WRITER
Generated by ISE EiffelStudio