]> Repositorios git - scryer-prolog.git/commitdiff
Add rename_file/2 predicate to the files library
authorPaulo Moura <[email protected]>
Tue, 18 May 2021 17:08:30 +0000 (18:08 +0100)
committerPaulo Moura <[email protected]>
Tue, 18 May 2021 17:08:30 +0000 (18:08 +0100)
src/clause_types.rs
src/lib/files.pl
src/machine/system_calls.rs

index 4ceda2f055c1de6e3f9670917a4c5b0dc9641e0b..1b35f05db9964b3bb4eedadf24379128e5e65ccb 100644 (file)
@@ -175,6 +175,7 @@ pub(crate) enum SystemClauseType {
     MakeDirectory,
     MakeDirectoryPath,
     DeleteFile,
+       RenameFile,
     DeleteDirectory,
     WorkingDirectory,
     PathCanonical,
@@ -338,6 +339,7 @@ impl SystemClauseType {
             &SystemClauseType::MakeDirectory => clause_name!("$make_directory"),
             &SystemClauseType::MakeDirectoryPath => clause_name!("$make_directory_path"),
             &SystemClauseType::DeleteFile => clause_name!("$delete_file"),
+            &SystemClauseType::RenameFile => clause_name!("$rename_file"),
             &SystemClauseType::DeleteDirectory => clause_name!("$delete_directory"),
             &SystemClauseType::WorkingDirectory => clause_name!("$working_directory"),
             &SystemClauseType::PathCanonical => clause_name!("$path_canonical"),
@@ -749,6 +751,7 @@ impl SystemClauseType {
             ("$make_directory", 1) => Some(SystemClauseType::MakeDirectory),
             ("$make_directory_path", 1) => Some(SystemClauseType::MakeDirectoryPath),
             ("$delete_file", 1) => Some(SystemClauseType::DeleteFile),
+            ("$rename_file", 2) => Some(SystemClauseType::RenameFile),
             ("$delete_directory", 1) => Some(SystemClauseType::DeleteDirectory),
             ("$working_directory", 2) => Some(SystemClauseType::WorkingDirectory),
             ("$path_canonical", 2) => Some(SystemClauseType::PathCanonical),
index 1d9841c9daa6b2f5dd6545fe283046ef0ed7ca17..6989cb4697c7d1da7a14ec508c535f07e6080c86 100644 (file)
@@ -51,6 +51,7 @@
                   file_exists/1,
                   directory_exists/1,
                   delete_file/1,
+                                 rename_file/2,
                                  delete_directory/1,
                   make_directory/1,
                   make_directory_path/1,
@@ -101,6 +102,12 @@ delete_file(File) :-
         list_of_chars(File),
         '$delete_file'(File).
 
+rename_file(File, Renamed) :-
+        file_must_exist(File, rename_file/2),
+        list_of_chars(File),
+               list_of_chars(Renamed),
+        '$rename_file'(File, Renamed).
+
 delete_directory(Directory) :-
         directory_must_exist(Directory, delete_directory/1),
         list_of_chars(Directory),
index fbe76bdfcd0108636cbc4f3618526ecf71d76fa5..3fe7900acd1f3fd114a29bd095f88e8f269d8ab6 100644 (file)
@@ -900,6 +900,18 @@ impl MachineState {
                     }
                 }
             }
+            &SystemClauseType::RenameFile => {
+                let file = self.heap_pstr_iter(self[temp_v!(1)]).to_string();
+                let renamed = self.heap_pstr_iter(self[temp_v!(2)]).to_string();
+
+                match fs::rename(file, renamed) {
+                    Ok(_) => {}
+                    _ => {
+                        self.fail = true;
+                        return Ok(());
+                    }
+                }
+            }
             &SystemClauseType::DeleteDirectory => {
                 let directory = self.heap_pstr_iter(self[temp_v!(1)]).to_string();