1% file_path_name_ext.pro -- a utility to construct or deconstruct file, path, name and extension 2:- module(filename,[ 3 file_path_name_ext/4, 4 file_path_name_ext/5, 5 lastchar/2, 6 firstchar/2 7]).
Note: In alignment with URI formats, filenames may end with #, typically in cases where
the filename is used as a prefix for entities. In this case, the # is stripped
from the name and ignored.
44file_path_name_ext(File,Path,Name,Ext):- file_path_name_ext(File,_,Path,Name,Ext). 45file_path_name_ext(Filename,DExt,Path,Name,Ext):- 46 nonvar(Filename), 47 absolute_file_name(Filename,Full,[expand(true)]), 48 ( lastchar(Full,'#') -> After=1; After=0), % maybe remove '#' 49 ( atom_prefix(Full,'file://') -> Before=7; Before=0), % maybe remove 'file://' 50 sub_string(Full,Before,_,After,File), 51 file_name_extension(PathName,X,File), 52 file_base_name(File,BaseName), 53 file_base_name(PathName,Name), 54 ( X = '' % check if extension is empty, but default extension exists 55 -> ( (Name= BaseName, ground(DExt)) -> Ext= DExt; Ext= X ) 56 ; Ext = X 57 ), 58 file_directory_name(File,P), 59 atom_concat(P,'/',Path). 60 61file_path_name_ext(Filename,DExt,Path,Name,Ex):- 62 var(Filename), 63 ( (atom(Name),lastchar(Name,'#')) % maybe remove '#' 64 -> sub_string(Name,0,_,1,Nam) 65 ; Nam = Name 66 ), 67 ( Ex=DExt; true ), % maybe use default extension 68 ( firstchar(Ex,'.') % maybe remove extra '.' from extension 69 -> sub_string(Ex,1,_,0,Ext) 70 ; Ext=Ex 71 ), 72 ( working_directory(Path,Path); true), 73 ( lastchar(Path,'/') % maybe remove extra slash from path 74 -> sub_string(Path,0,_,1,FPath) 75 ; FPath=Path 76 ),!, 77 expand_file_search_path(FPath,FullPath), 78 atomic_list_concat([FullPath,'/',Nam,'.',Ext],Filename).
92lastchar(String,Char):-
93 atom_string(Text,String),
94 sub_atom(Text,_,1,0,Char).
108firstchar(String,Char):-
109 atom_string(Text,String),
110 sub_atom(Text,0,1,_,Char)