From 051581c84b32559cc520aefdfe18dff073afdee1 Mon Sep 17 00:00:00 2001 From: William Bell Date: Thu, 30 May 2024 00:25:16 +0100 Subject: [PATCH] add copy and move functions --- src/file.go | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.go | 4 ++-- 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/src/file.go b/src/file.go index 5ce5944..2db19e9 100644 --- a/src/file.go +++ b/src/file.go @@ -12,8 +12,73 @@ import ( var ArFile = Map(anymap{ "read": builtinFunc{"read", ArRead}, "write": builtinFunc{"write", ArWrite}, + "move": builtinFunc{"move", ARmoveFile}, + "copy": builtinFunc{"copy", ARcopyFile}, }) +func ARmoveFile(args ...any) (any, ArErr) { + if len(args) != 2 { + return ArObject{}, ArErr{TYPE: "Runtime Error", message: "move takes 2 arguments, got " + fmt.Sprint(len(args)), EXISTS: true} + } + if typeof(args[0]) != "string" { + return ArObject{}, ArErr{TYPE: "Runtime Error", message: "move takes a string not type '" + typeof(args[0]) + "'", EXISTS: true} + } + if typeof(args[1]) != "string" { + return ArObject{}, ArErr{TYPE: "Runtime Error", message: "move takes a string not type '" + typeof(args[1]) + "'", EXISTS: true} + } + args[0] = ArValidToAny(args[0]) + args[1] = ArValidToAny(args[1]) + err := os.Rename(args[0].(string), args[1].(string)) + if err != nil { + return ArObject{}, ArErr{TYPE: "Runtime Error", message: err.Error(), EXISTS: true} + } + return nil, ArErr{} +} + +func copyFile(src, dst string) (int64, error) { + sourceFileStat, err := os.Stat(src) + if err != nil { + return 0, err + } + + if !sourceFileStat.Mode().IsRegular() { + return 0, fmt.Errorf("%s is not a regular file", src) + } + + source, err := os.Open(src) + if err != nil { + return 0, err + } + defer source.Close() + + destination, err := os.Create(dst) + if err != nil { + return 0, err + } + defer destination.Close() + nBytes, err := io.Copy(destination, source) + return nBytes, err +} + +func ARcopyFile(args ...any) (any, ArErr) { + if len(args) != 2 { + return ArObject{}, ArErr{TYPE: "Runtime Error", message: "copy takes 2 arguments, got " + fmt.Sprint(len(args)), EXISTS: true} + } + if typeof(args[0]) != "string" { + return ArObject{}, ArErr{TYPE: "Runtime Error", message: "copy takes a string not type '" + typeof(args[0]) + "'", EXISTS: true} + } + if typeof(args[1]) != "string" { + return ArObject{}, ArErr{TYPE: "Runtime Error", message: "copy takes a string not type '" + typeof(args[1]) + "'", EXISTS: true} + } + args[0] = ArValidToAny(args[0]) + args[1] = ArValidToAny(args[1]) + _, err := copyFile(args[0].(string), args[1].(string)) + if err != nil { + return ArObject{}, ArErr{TYPE: "Runtime Error", message: err.Error(), EXISTS: true} + } + return nil, ArErr{} +} + func readtext(file *os.File) (string, error) { var buf bytes.Buffer _, err := io.Copy(&buf, file) diff --git a/src/main.go b/src/main.go index 900ddcd..ba422c0 100644 --- a/src/main.go +++ b/src/main.go @@ -10,8 +10,8 @@ var Args = os.Args[1:] type stack = []ArObject -const VERSION = "3.0.5" -const VERSION_NUM = 3 +const VERSION = "3.0.6" +const VERSION_NUM = 4 func newscope() ArObject { return Map(anymap{})