delete_file should be SICStus compatible

This commit is contained in:
Vitor Santos Costa 2013-02-12 16:21:26 -06:00
parent 268bca2330
commit 9fb63762dc
3 changed files with 74 additions and 2 deletions

View File

@ -46,6 +46,8 @@
#define PLVERSION YAP_VERSION
#define PLNAME "yap"
#define SWIP "swi_"
/* try not to pollute the SWI space */
#ifdef P
#undef P

View File

@ -88,7 +88,77 @@ check_int(I, Inp) :-
throw(error(type_error(integer,I),Inp)).
% file operations
% file operations
delete_file(IFile) :-
true_file_name(IFile, File),
delete_file(File, off, on, off).
delete_file(IFile, Opts) :-
true_file_name(IFile, File),
process_delete_file_opts(Opts, Dir, Recurse, Ignore, delete_file(File,Opts)),
delete_file(File, Dir, Recurse, Ignore).
process_delete_file_opts(V, _, _, _, T) :- var(V), !,
throw(error(instantiation_error,T)).
process_delete_file_opts([], off, off, off, _) :- !.
process_delete_file_opts([V|_], _, _, _, T) :- var(V), !,
throw(error(instantiation_error,T)).
process_delete_file_opts([directory|Opts], on, Recurse, Ignore, T) :- !,
process_delete_file_opts(Opts, _, Recurse, Ignore, T).
process_delete_file_opts([recursive|Opts], Dir, on, Ignore, T) :- !,
process_delete_file_opts(Opts, Dir, _, Ignore, T).
process_delete_file_opts([ignore|Opts], Dir, Recurse, on, T) :- !,
process_delete_file_opts(Opts, Dir, Recurse, _, T).
process_delete_file_opts(Opts, _, _, _, T) :-
throw(error(domain_error(delete_file_option,Opts),T)).
delete_file(IFile, Dir, Recurse, Ignore) :-
true_file_name(IFile, File),
file_property(File, Type, _, _, _Permissions, _, Ignore),
delete_file(Type, File, Dir, Recurse, Ignore).
delete_file(N, File, _Dir, _Recurse, Ignore) :- number(N), !, % error.
handle_system_error(N, Ignore, delete_file(File)).
delete_file(directory, File, Dir, Recurse, Ignore) :-
delete_directory(Dir, File, Recurse, Ignore).
delete_file(_, File, _Dir, _Recurse, Ignore) :-
unlink_file(File, Ignore).
unlink_file(IFile, Ignore) :-
true_file_name(IFile, File),
unlink(File, N),
handle_system_error(N, Ignore, delete_file(File)).
delete_directory(on, File, _Recurse, Ignore) :-
rm_directory(File, Ignore).
delete_directory(off, File, Recurse, Ignore) :-
delete_directory(Recurse, File, Ignore).
rm_directory(File, Ignore) :-
rmdir(File, Error),
handle_system_error(Error, Ignore, delete_file(File)).
delete_directory(on, File, Ignore) :-
directory_files(File, FileList, Ignore),
path_separator(D),
atom_concat(File, D, FileP),
delete_dirfiles(FileList, FileP, Ignore),
rmdir(File, Ignore).
delete_dirfiles([], _, _).
delete_dirfiles(['.'|Fs], File, Ignore) :- !,
delete_dirfiles(Fs, File, Ignore).
delete_dirfiles(['..'|Fs], File, Ignore) :- !,
delete_dirfiles(Fs, File, Ignore).
delete_dirfiles([F|Fs], File, Ignore) :-
atom_concat(File,F,TrueF),
delete_file(TrueF, off, on, Ignore),
delete_dirfiles(Fs, File, Ignore).
directory_files(File, FileList, Ignore) :-
list_directory(File, FileList, Error),
handle_system_error(Error, Ignore, directory_files(File, FileList)).
handle_system_error(Error, _Ignore, _G) :- var(Error), !.
handle_system_error(Error, off, G) :- atom(Error), !,

View File

@ -828,7 +828,7 @@ PRED_IMPL("tmp_file_stream", 3, tmp_file_stream, 0)
static
PRED_IMPL("delete_file", 1, delete_file, 0)
PRED_IMPL("swi_delete_file", 1, delete_file, 0)
{ PRED_LD
char *n;
atom_t aname;
@ -1125,7 +1125,7 @@ BeginPredDefs(files)
PRED_DEF("exists_directory", 1, exists_directory, 0)
PRED_DEF("tmp_file", 2, tmp_file, 0)
PRED_DEF("tmp_file_stream", 3, tmp_file_stream, 0)
PRED_DEF("delete_file", 1, delete_file, 0)
PRED_DEF("swi_delete_file", 1, delete_file, 0)
PRED_DEF("delete_directory", 1, delete_directory, 0)
PRED_DEF("make_directory", 1, make_directory, 0)
PRED_DEF("same_file", 2, same_file, 0)