mirror of
https://github.com/Mercury-Language/mercury.git
synced 2026-04-15 09:23:44 +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.
101 lines
2.7 KiB
Mathematica
101 lines
2.7 KiB
Mathematica
%---------------------------------------------------------------------------%
|
|
% vim: ft=mercury ts=4 sw=4 et
|
|
%---------------------------------------------------------------------------%
|
|
%
|
|
% Test writing and reading int8s with binary streams.
|
|
%
|
|
%---------------------------------------------------------------------------%
|
|
|
|
:- module write_binary_int8.
|
|
:- interface.
|
|
|
|
:- import_module io.
|
|
|
|
:- pred main(io::di, io::uo) is det.
|
|
|
|
:- implementation.
|
|
|
|
:- import_module int.
|
|
:- import_module int8.
|
|
:- import_module io.file.
|
|
:- import_module list.
|
|
|
|
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),
|
|
list.foldl(write_binary_int8(OutputFile), data, !IO),
|
|
io.close_binary_output(OutputFile, !IO),
|
|
|
|
io.open_binary_input(FileName, OpenInputResult, !IO),
|
|
(
|
|
OpenInputResult = ok(InputFile),
|
|
read_int8s(InputFile, [], ReadResult, !IO),
|
|
(
|
|
ReadResult = ok(RevUInt8s),
|
|
io.close_binary_input(InputFile, !IO),
|
|
list.reverse(RevUInt8s, UInt8s),
|
|
( if UInt8s = data then
|
|
io.print_line("PASSED", !IO)
|
|
else
|
|
io.print_line("FAILED", !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 read_int8s(io.binary_input_stream::in,
|
|
list(int8)::in, io.res(list(int8))::out, io::di, io::uo) is det.
|
|
|
|
read_int8s(InputFile, !.Us, Result, !IO) :-
|
|
io.read_binary_int8(InputFile, ReadResult, !IO),
|
|
(
|
|
ReadResult = ok(U),
|
|
!:Us = [U | !.Us],
|
|
read_int8s(InputFile, !.Us, Result, !IO)
|
|
;
|
|
ReadResult = eof,
|
|
Result = ok(!.Us)
|
|
;
|
|
ReadResult = error(IO_Error),
|
|
Result = error(IO_Error)
|
|
).
|
|
|
|
:- func data = list(int8).
|
|
|
|
data = Uint8s :-
|
|
int.fold_down(add_int8, -128, 127, [], Uint8s).
|
|
|
|
:- pred add_int8(int::in,
|
|
list(int8)::in, list(int8)::out) is det.
|
|
|
|
add_int8(I, !Us) :-
|
|
U = int8.cast_from_int(I),
|
|
!:Us = [U | !.Us].
|