This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/library/lineutils.yap

113 lines
2.0 KiB
Plaintext
Raw Normal View History

:- module(line_utils,
2008-09-01 04:44:54 +01:00
[search_for/2,
search_for/3,
scan_natural/3,
scan_integer/3,
split/3,
glue/3,
2008-09-01 04:44:54 +01:00
copy_line/2,
filter/3,
file_filter/3,
2008-09-04 16:58:45 +01:00
process/2
]).
:- meta_predicate filter(+,+,:), file_filter(+,+,:), process(+,:).
:- use_module(library(lists),
[member/2,
append/3]).
:- use_module(library(readutil),
[read_line_to_codes/2]).
2008-09-01 04:44:54 +01:00
search_for(C,L) :-
search_for(C, L, []).
search_for(C) --> [C], !.
search_for(C) --> [_],
search_for(C).
scan_integer(N) -->
"-", !,
scan_natural(0, N0),
N is -N0.
scan_integer(N) -->
scan_natural(0, N).
2008-09-01 04:44:54 +01:00
scan_natural(N) -->
scan_natural(0, N).
scan_natural(N0,N) -->
[C],
{C >= 0'0, C =< 0'9 }, !,
2008-09-01 04:44:54 +01:00
{ N1 is N0*10+(C-0'0) }, %'
get_natural(N1,N).
scan_natural(N,N) --> [].
split(String, SplitCodes, Strings) :-
split_at_blank(SplitCodes, Strings, String, []).
split_at_blank(SplitCodes, More) -->
[C],
{ member(C, SplitCodes) }, !,
split_at_blank(SplitCodes, More).
split_at_blank(SplitCodes, [[C|New]| More]) -->
[C], !,
split(SplitCodes, New, More).
split_at_blank(_, []) --> [].
split(SplitCodes, [], More) -->
[C],
{ member(C, SplitCodes) }, !,
split_at_blank(SplitCodes, More).
split(SplitCodes, [C|New], Set) -->
[C], !,
split(SplitCodes, New, Set).
split(_, [], []) --> [].
glue([], _, []).
glue([A], _, A) :- !.
glue([H|T], [B|_], Merged) :-
append(H, [B|Rest], Merged),
glue(T, [B], Rest).
copy_line(StreamInp, StreamOut) :-
read_line_to_codes(StreamInp, Line),
format(StreamOut, '~s~n', [Line]).
filter(StreamInp, StreamOut, Command) :-
repeat,
read_line_to_codes(StreamInp, Line),
(
Line == end_of_file
->
true
;
call(Command, Line, NewLine),
format(StreamOut, '~s~n', [NewLine]),
fail
).
process(StreamInp, Command) :-
repeat,
read_line_to_codes(StreamInp, Line),
(
Line == end_of_file
->
true
;
call(Command, Line),
fail
).
file_filter(Inp, Out, Command) :-
open(Inp, read, StreamInp),
open(Out, write, StreamOut),
filter(StreamInp, StreamOut, Command),
close(StreamInp),
close(StreamOut).