file_name_on_path

This commit is contained in:
Vítor Santos Costa 2011-06-22 12:29:01 +01:00
parent 0b6b471359
commit a1e3f0daa2

View File

@ -6,6 +6,24 @@
% True if file Alias points to Dir. Multiple solutions are
% generated with the longest directory first.
%% file_name_on_path(+File:atom, -OnPath) is det.
%
% True if OnPath a description of File based on the file search
% path. This performs the inverse of absolute_file_name/3.
prolog:file_name_on_path(Path, ShortId) :-
( prolog:file_alias_path(Alias, Dir),
atom_concat(Dir, Local, Path)
-> ( Alias == '.'
-> ShortId = Local
; file_name_extension(Base, pl, Local)
-> ShortId =.. [Alias, Base]
; ShortId =.. [Alias, Local]
)
; ShortId = Path
).
:- dynamic
alias_cache/2.
@ -25,7 +43,7 @@ build_alias_cache :-
search_path(Alias, Dir, AliasLen, DirLen), Ts),
sort(Ts, List0),
reverse(List0, List),
forall(member(t(_, _, Alias, Dir), List),
forall(lists:member(t(_, _, Alias, Dir), List),
assert(alias_cache(Alias, Dir))).
search_path('.', Here, 999, DirLen) :-
@ -53,3 +71,11 @@ ensure_slash(Dir0, Dir) :-
atom_concat(Dir0, /, Dir).
reverse(List, Reversed) :-
reverse(List, [], Reversed).
reverse([], Reversed, Reversed).
reverse([Head|Tail], Sofar, Reversed) :-
reverse(Tail, [Head|Sofar], Reversed).