mirror of
https://github.com/Mercury-Language/mercury.git
synced 2025-12-12 04:14:06 +00:00
Estimated hours taken: 10
library/io.m:
library/io.nu.nl:
Implement io__read_line_as_string/{3,4}.
Also sneaked in here are some trivial whitespace fixes in some
of the pragma c_code which did not comply with our coding
standards (to do with type casting).
samples/cat.m:
samples/sort.m:
Use io__read_line_as_string.
tests/general/Mmakefile:
tests/general/read_line_as_string.exp:
tests/general/read_line_as_string.m:
Test case for io__read_line_as_string.
NEWS:
Mention the above change.
120 lines
2.4 KiB
Mathematica
120 lines
2.4 KiB
Mathematica
%-----------------------------------------------------------------------------%
|
|
%
|
|
% A simple sorting program.
|
|
%
|
|
% It works on text files, considering each line to be a record.
|
|
% The entire line is considered to be the sort key.
|
|
%
|
|
% The algorithm used is simple insertion sort.
|
|
%
|
|
% This source file is hereby placed in the public domain. -fjh (the author).
|
|
%
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- module sort.
|
|
|
|
:- interface.
|
|
:- import_module io.
|
|
|
|
:- pred main(io__state::di, io__state::uo) is det.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- implementation.
|
|
:- import_module string, list, char, require, std_util.
|
|
|
|
main -->
|
|
io__command_line_arguments(Args),
|
|
(
|
|
{ Args = [] },
|
|
handle_args(no, no),
|
|
sort
|
|
;
|
|
{ Args = [Input] },
|
|
handle_args(yes(Input), no),
|
|
sort
|
|
;
|
|
{ Args = [Input, Output] },
|
|
handle_args(yes(Input), yes(Output)),
|
|
sort
|
|
;
|
|
{ Args = [_, _, _ | _] },
|
|
io__write_string("Usage: sort [Input [Output]]\\n")
|
|
).
|
|
|
|
:- pred handle_args(maybe(string), maybe(string), io__state, io__state).
|
|
:- mode handle_args(in, in, di, uo) is det.
|
|
|
|
handle_args(InArg, OutArg) -->
|
|
(
|
|
{ InArg = yes(InFilename) },
|
|
io__see(InFilename, InResult),
|
|
(
|
|
{ InResult = ok }
|
|
;
|
|
{ InResult = error(InError) },
|
|
{ io__error_message(InError, InMsg) },
|
|
{ error(InMsg) }
|
|
)
|
|
;
|
|
{ InArg = no }
|
|
),
|
|
(
|
|
{ OutArg = yes(OutFilename) },
|
|
io__tell(OutFilename, OutResult),
|
|
(
|
|
{ OutResult = ok }
|
|
;
|
|
{ OutResult = error(OutError) },
|
|
{ io__error_message(OutError, OutMsg) },
|
|
{ error(OutMsg) }
|
|
)
|
|
;
|
|
{ OutArg = no }
|
|
).
|
|
|
|
:- pred sort(io__state, io__state).
|
|
:- mode sort(di, uo) is det.
|
|
|
|
sort -->
|
|
sort_2([]).
|
|
|
|
:- pred sort_2(list(string), io__state, io__state).
|
|
:- mode sort_2(in, di, uo) is det.
|
|
|
|
sort_2(Lines0) -->
|
|
io__read_line_as_string(Result),
|
|
(
|
|
{ Result = error(Error) },
|
|
{ io__error_message(Error, Msg) },
|
|
{ error(Msg) }
|
|
;
|
|
{ Result = eof },
|
|
sort_output(Lines0)
|
|
;
|
|
{ Result = ok(Line) },
|
|
{ insert(Lines0, Line, Lines1) },
|
|
sort_2(Lines1)
|
|
).
|
|
|
|
:- pred insert(list(T), T, list(T)).
|
|
:- mode insert(in, in, out) is det.
|
|
|
|
insert([], I, [I]).
|
|
insert([H | T], I, L) :-
|
|
compare(R, I, H),
|
|
( R = (<) ->
|
|
L = [I, H | T]
|
|
;
|
|
insert(T, I, NT),
|
|
L = [H | NT]
|
|
).
|
|
|
|
:- pred sort_output(list(string), io__state, io__state).
|
|
:- mode sort_output(in, di, uo) is det.
|
|
|
|
sort_output([]) --> [].
|
|
sort_output([Line | Lines]) -->
|
|
io__write_string(Line),
|
|
sort_output(Lines).
|