Merge pull request #630 from triska/delete_file

ADDED: delete_file/1, addressing a remaining aspect of #511
This commit is contained in:
Mark Thom
2020-07-14 20:26:51 -03:00
committed by GitHub
3 changed files with 56 additions and 1 deletions

View File

@@ -176,6 +176,8 @@ pub enum SystemClauseType {
FileExists,
DirectoryExists,
MakeDirectory,
DeleteFile,
WorkingDirectory,
DeleteAttribute,
DeleteHeadAttribute,
DynamicModuleResolution(usize),
@@ -341,6 +343,8 @@ impl SystemClauseType {
&SystemClauseType::FileExists => clause_name!("$file_exists"),
&SystemClauseType::DirectoryExists => clause_name!("$directory_exists"),
&SystemClauseType::MakeDirectory => clause_name!("$make_directory"),
&SystemClauseType::DeleteFile => clause_name!("$delete_file"),
&SystemClauseType::WorkingDirectory => clause_name!("$working_directory"),
&SystemClauseType::REPL(REPLCodePtr::CompileBatch) => clause_name!("$compile_batch"),
&SystemClauseType::REPL(REPLCodePtr::UseModule) => clause_name!("$use_module"),
&SystemClauseType::REPL(REPLCodePtr::UseQualifiedModule) => {
@@ -675,6 +679,8 @@ impl SystemClauseType {
("$file_exists", 1) => Some(SystemClauseType::FileExists),
("$directory_exists", 1) => Some(SystemClauseType::DirectoryExists),
("$make_directory", 1) => Some(SystemClauseType::MakeDirectory),
("$delete_file", 1) => Some(SystemClauseType::DeleteFile),
("$working_directory", 2) => Some(SystemClauseType::WorkingDirectory),
("$use_module", 1) => Some(SystemClauseType::REPL(REPLCodePtr::UseModule)),
("$use_module_from_file", 1) =>
Some(SystemClauseType::REPL(REPLCodePtr::UseModuleFromFile)),

View File

@@ -50,7 +50,9 @@
file_size/2,
file_exists/1,
directory_exists/1,
make_directory/1]).
delete_file/1,
make_directory/1,
working_directory/2]).
:- use_module(library(error)).
:- use_module(library(lists)).
@@ -80,3 +82,12 @@ directory_exists(Directory) :-
make_directory(Directory) :-
list_of_chars(Directory),
'$make_directory'(Directory).
delete_file(File) :-
list_of_chars(File),
'$delete_file'(File).
working_directory(Dir0, Dir) :-
can_be(list, Dir0),
can_be(list, Dir),
'$working_directory'(Dir0, Dir).

View File

@@ -880,6 +880,44 @@ impl MachineState {
}
}
}
&SystemClauseType::DeleteFile => {
let file = self.heap_pstr_iter(self[temp_v!(1)]).to_string();
match fs::remove_file(file) {
Ok(_) => { }
_ => { self.fail = true;
return Ok(());
}
}
}
&SystemClauseType::WorkingDirectory => {
if let Ok(dir) = env::current_dir() {
let current =
match dir.to_str() {
Some(d) => { d }
_ => { let stub = MachineError::functor_stub(clause_name!("working_directory"), 2);
let err = MachineError::representation_error(RepFlag::Character);
let err = self.error_form(err, stub);
return Err(err);
}
};
let chars = self.heap.put_complete_string(current);
self.unify(self[temp_v!(1)], chars);
let next = self.heap_pstr_iter(self[temp_v!(2)]).to_string();
match env::set_current_dir(std::path::Path::new(&next)) {
Ok(_) => { }
_ => { self.fail = true;
return Ok(());
}
}
} else {
self.fail = true;
return Ok(());
}
}
&SystemClauseType::AtEndOfExpansion => {
if self.cp == LocalCodePtr::TopLevel(0, 0) {
self.at_end_of_expansion = true;