file operations in stdlib
This commit is contained in:
parent
1802bce604
commit
de3e3e5d4e
3 changed files with 177 additions and 1 deletions
|
|
@ -19,10 +19,33 @@ package stdlib
|
|||
|
||||
import (
|
||||
"os"
|
||||
"io/ioutil"
|
||||
"gitlab.com/whom/shs/ast"
|
||||
"gitlab.com/whom/shs/log"
|
||||
)
|
||||
|
||||
/* Take a path, return the absolute path
|
||||
* does not verify that the absolute path is correct
|
||||
* currently only supports paths using forward slashes
|
||||
*
|
||||
* TODO: handle ~
|
||||
*/
|
||||
func AbsPath(arg string) string {
|
||||
if arg[0] != '/' {
|
||||
dir, err := os.Getwd()
|
||||
if err != nil {
|
||||
log.Log(log.ERR,
|
||||
"Couldnt get working directory: " + err.Error(),
|
||||
"path")
|
||||
return arg
|
||||
}
|
||||
|
||||
return dir + "/" + arg
|
||||
}
|
||||
|
||||
return arg
|
||||
}
|
||||
|
||||
func cd(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
|
||||
in = in.Eval(ft, vt, true)
|
||||
|
||||
|
|
@ -44,3 +67,128 @@ func cd(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
|
|||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func fexists(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
|
||||
in = in.Eval(ft, vt, false)
|
||||
if in == nil || (in.Tag != ast.NUMBER && in.Tag != ast.STRING) {
|
||||
log.Log(log.ERR,
|
||||
"argument to fexists must be a string or number",
|
||||
"fexists")
|
||||
return nil
|
||||
}
|
||||
|
||||
filename := in.Value()
|
||||
out := ast.TRUE
|
||||
|
||||
if _, err := os.Stat(AbsPath(filename)); err != nil {
|
||||
log.Log(log.DEBUG,
|
||||
"couldnt stat file: " + err.Error(),
|
||||
"fexists")
|
||||
out = ast.FALSE
|
||||
}
|
||||
|
||||
ret := &ast.Token{Tag: ast.BOOL}
|
||||
ret.Set(out)
|
||||
return ret
|
||||
}
|
||||
|
||||
func fread(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
|
||||
in = in.Eval(ft, vt, false)
|
||||
exists := fexists(in, vt, ft) // some waste, extra use of Eval
|
||||
if exists == nil || exists.Tag != ast.BOOL || exists.Value() == ast.FALSE {
|
||||
log.Log(log.ERR,
|
||||
"error calling fexists or file doesnt exist",
|
||||
"fread")
|
||||
return nil
|
||||
}
|
||||
|
||||
fname := in.Value()
|
||||
text, err := ioutil.ReadFile(fname)
|
||||
if err != nil {
|
||||
log.Log(log.ERR,
|
||||
"error reading file" + err.Error(),
|
||||
"fread")
|
||||
return nil
|
||||
}
|
||||
|
||||
ret := &ast.Token{Tag: ast.STRING}
|
||||
ret.Set(string(text))
|
||||
return ret
|
||||
}
|
||||
|
||||
func fwrite(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
|
||||
in = in.Eval(ft, vt, false)
|
||||
if in == nil || in.Tag == ast.SYMBOL || in.Tag == ast.LIST {
|
||||
log.Log(log.ERR,
|
||||
"first argument must be a filename",
|
||||
"fwrite")
|
||||
return nil
|
||||
}
|
||||
|
||||
text := in.Next
|
||||
if text == nil || text.Tag == ast.SYMBOL || text.Tag == ast.LIST {
|
||||
log.Log(log.ERR,
|
||||
"second argument must be stringable",
|
||||
"fwrite")
|
||||
return nil
|
||||
}
|
||||
|
||||
err := ioutil.WriteFile(
|
||||
AbsPath(in.Value()),
|
||||
[]byte(text.Value()),
|
||||
0644)
|
||||
if err != nil {
|
||||
log.Log(log.ERR,
|
||||
"error writing file: " + err.Error(),
|
||||
"fwrite")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func fappend(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
|
||||
in = in.Eval(ft, vt, false)
|
||||
if in == nil || in.Tag == ast.SYMBOL || in.Tag == ast.LIST {
|
||||
log.Log(log.ERR,
|
||||
"first argument must be a filename",
|
||||
"fappend")
|
||||
return nil
|
||||
}
|
||||
|
||||
text := in.Next
|
||||
if text == nil || text.Tag == ast.SYMBOL || text.Tag == ast.LIST {
|
||||
log.Log(log.ERR,
|
||||
"second argument must be stringable",
|
||||
"fappend")
|
||||
return nil
|
||||
}
|
||||
|
||||
exists := fexists(in, vt, ft)
|
||||
if exists.Value() == ast.FALSE {
|
||||
log.Log(log.ERR,
|
||||
"file "+in.Value()+" does not exist",
|
||||
"fappend")
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
f, err := os.OpenFile(
|
||||
AbsPath(in.Value()),
|
||||
os.O_APPEND|os.O_CREATE|os.O_WRONLY,
|
||||
0644)
|
||||
if err != nil {
|
||||
log.Log(log.ERR,
|
||||
"couldnt open file for append: " + err.Error(),
|
||||
"fappend")
|
||||
return nil
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
if _, err := f.WriteString(text.Value()); err != nil {
|
||||
log.Log(log.ERR,
|
||||
"error appending to file: " + err.Error(),
|
||||
"fappend")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue