mirror of
https://github.com/Mercury-Language/mercury.git
synced 2026-04-16 01:43:35 +00:00
Add predicates for writing 16- and 32-bit integers to binary file streams in
native, little and big endian byte order to the standard library.
library/io.m:
Add the new predicates.
library/uint16.m:
library/uint32.m:
Add a functions for casting from the signed to unsigned versions
of these types.
runtime/mercury_int.h:
New header intended to hold things related to the implementation
of the integer types in C grades. Currently it contains the
macros defining the byte reverse operations for 16- and 32-bit
types. (It will eventually contain macros for (un)boxing 64-bit
integer types on 32-bit platforms as well.)
runtime/mercury_imp.h:
runtime/Mmakefile:
Add the new header.
tests/hard_coded/Mmakefile:
tests/write_binary_multibyte_int.{m,exp}:
Add a test for the new predicates.
101 lines
3.2 KiB
Mathematica
101 lines
3.2 KiB
Mathematica
%---------------------------------------------------------------------------%
|
|
% vim: ft=mercury ts=4 sw=4 et
|
|
%---------------------------------------------------------------------------%
|
|
%
|
|
% Test writing of int16, uint16, in32 and uint32 to binary file streams.
|
|
|
|
:- module write_binary_multibyte_int.
|
|
:- interface.
|
|
|
|
:- import_module io.
|
|
|
|
:- pred main(io::di, io::uo) is det.
|
|
|
|
%---------------------------------------------------------------------------%
|
|
%---------------------------------------------------------------------------%
|
|
|
|
:- implementation.
|
|
|
|
:- import_module list.
|
|
:- import_module string.
|
|
|
|
main(!IO) :-
|
|
io.make_temp_file(MakeTempResult, !IO),
|
|
(
|
|
MakeTempResult = ok(TempFileName),
|
|
main_2(TempFileName, !IO)
|
|
;
|
|
MakeTempResult = error(Msg),
|
|
io.print_line(Msg, !IO),
|
|
io.set_exit_status(1, !IO)
|
|
).
|
|
|
|
:- pred main_2(string::in, io::di, io::uo) is det.
|
|
|
|
main_2(FileName, !IO) :-
|
|
io.open_binary_output(FileName, OpenOutputResult, !IO),
|
|
(
|
|
OpenOutputResult = ok(OutputFile),
|
|
io.write_binary_int16_le(OutputFile, 0x0b0a_i16, !IO),
|
|
io.write_binary_int16_be(OutputFile, 0x0b0a_i16, !IO),
|
|
io.write_binary_uint16_le(OutputFile, 0xbbaa_u16, !IO),
|
|
io.write_binary_uint16_be(OutputFile, 0xbbaa_u16, !IO),
|
|
io.write_binary_int32_le(OutputFile, 0x0d0c0b0a_i32, !IO),
|
|
io.write_binary_int32_be(OutputFile, 0x0d0c0b0a_i32, !IO),
|
|
io.write_binary_uint32_le(OutputFile, 0xddccbbaa_u32, !IO),
|
|
io.write_binary_uint32_be(OutputFile, 0xddccbbaa_u32, !IO),
|
|
io.close_binary_output(OutputFile, !IO),
|
|
|
|
io.open_binary_input(FileName, OpenInputResult, !IO),
|
|
(
|
|
OpenInputResult = ok(InputFile),
|
|
read_bytes(InputFile, [], ReadResult, !IO),
|
|
(
|
|
ReadResult = ok(Bytes),
|
|
io.close_binary_input(InputFile, !IO),
|
|
io.write_list(Bytes, " ", print_byte, !IO),
|
|
io.nl(!IO)
|
|
;
|
|
ReadResult = error(IO_Error),
|
|
io.error_message(IO_Error, Msg),
|
|
io.print_line(Msg, !IO)
|
|
)
|
|
;
|
|
OpenInputResult = error(IO_Error),
|
|
io.error_message(IO_Error, Msg),
|
|
io.print_line(Msg, !IO)
|
|
)
|
|
;
|
|
OpenOutputResult = error(IO_Error),
|
|
io.error_message(IO_Error, Msg),
|
|
io.print_line(Msg, !IO)
|
|
),
|
|
io.remove_file(FileName, _, !IO).
|
|
|
|
:- pred print_byte(int::in, io::di, io::uo) is det.
|
|
|
|
print_byte(Byte, !IO) :-
|
|
io.format("0x%0.2x", [i(Byte)], !IO).
|
|
|
|
:- pred read_bytes(io.binary_input_stream::in,
|
|
list(int)::in, io.res(list(int))::out, io::di, io::uo) is det.
|
|
|
|
read_bytes(InputFile, !.Bytes, Result, !IO) :-
|
|
io.read_byte(InputFile, ReadResult, !IO),
|
|
(
|
|
ReadResult = ok(Byte),
|
|
!:Bytes = [Byte | !.Bytes],
|
|
read_bytes(InputFile, !.Bytes, Result, !IO)
|
|
;
|
|
ReadResult = eof,
|
|
list.reverse(!Bytes),
|
|
Result = ok(!.Bytes)
|
|
;
|
|
ReadResult = error(IO_Error),
|
|
Result = error(IO_Error)
|
|
).
|
|
|
|
%---------------------------------------------------------------------------%
|
|
:- end_module write_binary_multibyte_int.
|
|
%---------------------------------------------------------------------------%
|