mirror of
https://github.com/Mercury-Language/mercury.git
synced 2026-04-15 17:33:38 +00:00
library/io.file.m:
library/io.m:
Move two sections of io.m, the "file handling predicates" section
and the "handling temporary files" section to the new submodule io.file.m.
Leave behind in io.m "forwarding predicates", predicates that do nothing
except call the moved predicates in io.file.m, to provide backward
compatibility. But do mark the forwarding predicates as obsolete,
to tell people to update their (at their leisure, since the obsoleteness
warning can be turned off).
Also leave behind in io.m the definitions of the two types used
by some parameters of some of the moved predicates. Document the reason
why this is done.
library/MODULES_DOC:
List the new module among the documented modules.
NEWS:
Announce the changes.
browser/browse.m:
browser/interactive_query.m:
browser/listing.m:
compiler/analysis.file.m:
compiler/compile_target_code.m:
compiler/export.m:
compiler/fact_table.m:
compiler/file_util.m:
compiler/handle_options.m:
compiler/make.build.m:
compiler/make.module_dep_file.m:
compiler/make.module_target.m:
compiler/make.program_target.m:
compiler/make.util.m:
compiler/mercury_compile_main.m:
compiler/module_cmds.m:
compiler/parse_module.m:
compiler/passes_aux.m:
compiler/prog_event.m:
compiler/recompilation.check.m:
compiler/write_deps_file.m:
compiler/write_module_interface_files.m:
deep_profiler/conf.m:
deep_profiler/mdprof_cgi.m:
library/dir.m:
mdbcomp/program_representation.m:
ssdb/ssdb.m:
Call the file operation predicates directly in io.file.m, not indirectly
through io.m.
In two modules, add a #include of fcntl.h in C code. These modules contain
C code that needs this #include, but until now, they got it via a copy
in an automatically generated C header file of a foreign_decl pragma
in io.m that contained that #include. This diff moves that foreign_decl
to io.file.m, removing that crutch.
tests/debugger/browser_test.m:
tests/hard_coded/bit_buffer_test.m:
tests/hard_coded/bitmap_test.m:
tests/hard_coded/construct_bug.m:
tests/hard_coded/dir_fold.m:
tests/hard_coded/dir_test.m:
tests/hard_coded/read_binary_int16.m:
tests/hard_coded/read_binary_int32.m:
tests/hard_coded/read_binary_int64.m:
tests/hard_coded/read_binary_uint16.m:
tests/hard_coded/read_binary_uint32.m:
tests/hard_coded/read_binary_uint64.m:
tests/hard_coded/read_bitmap_size.m:
tests/hard_coded/remove_file.m:
tests/hard_coded/write_binary.m:
tests/hard_coded/write_binary_int8.m:
tests/hard_coded/write_binary_multibyte_int.m:
tests/hard_coded/write_binary_uint8.m:
Call the file operation predicates directly in io.file.m, not indirectly
through io.m.
107 lines
3.6 KiB
Mathematica
107 lines
3.6 KiB
Mathematica
%---------------------------------------------------------------------------%
|
|
% vim: ft=mercury ts=4 sw=4 et
|
|
%---------------------------------------------------------------------------%
|
|
%
|
|
% Test writing of int16, uint16, int32, uint32, int64 and uint64 to binary file
|
|
% streams.
|
|
|
|
:- module write_binary_multibyte_int.
|
|
:- interface.
|
|
|
|
:- import_module io.
|
|
|
|
:- pred main(io::di, io::uo) is det.
|
|
|
|
%---------------------------------------------------------------------------%
|
|
%---------------------------------------------------------------------------%
|
|
|
|
:- implementation.
|
|
|
|
:- import_module io.file.
|
|
:- import_module list.
|
|
:- import_module string.
|
|
|
|
main(!IO) :-
|
|
io.file.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.write_binary_int64_le(OutputFile, 0x0f0e0d0c0b0a_i64, !IO),
|
|
io.write_binary_int64_be(OutputFile, 0x0f0e0d0c0b0a_i64, !IO),
|
|
io.write_binary_uint64_le(OutputFile, 0xffeeddccbbaa_u64, !IO),
|
|
io.write_binary_uint64_be(OutputFile, 0xffeeddccbbaa_u64, !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, "\n", 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.file.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.
|
|
%---------------------------------------------------------------------------%
|