builtin-os-file.go: changed read and write function names; added fileReadTextAll
This commit is contained in:
parent
f41dba069e
commit
d066344af8
@ -68,7 +68,7 @@ func createFileFunc(ctx ExprContext, name string, args []any) (result any, err e
|
||||
result = &osWriter{fh: fh, writer: bufio.NewWriter(fh)}
|
||||
}
|
||||
} else {
|
||||
err = errMissingFilePath("createFile")
|
||||
err = errMissingFilePath(name)
|
||||
}
|
||||
return
|
||||
}
|
||||
@ -80,7 +80,7 @@ func openFileFunc(ctx ExprContext, name string, args []any) (result any, err err
|
||||
result = &osReader{fh: fh, reader: bufio.NewReader(fh)}
|
||||
}
|
||||
} else {
|
||||
err = errMissingFilePath("openFile")
|
||||
err = errMissingFilePath(name)
|
||||
}
|
||||
return
|
||||
}
|
||||
@ -92,7 +92,7 @@ func appendFileFunc(ctx ExprContext, name string, args []any) (result any, err e
|
||||
result = &osWriter{fh: fh, writer: bufio.NewWriter(fh)}
|
||||
}
|
||||
} else {
|
||||
err = errMissingFilePath("appendFile")
|
||||
err = errMissingFilePath(name)
|
||||
}
|
||||
return
|
||||
}
|
||||
@ -118,13 +118,13 @@ func closeFileFunc(ctx ExprContext, name string, args []any) (result any, err er
|
||||
}
|
||||
}
|
||||
if err == nil && (handle == nil || invalidFileHandle != nil) {
|
||||
err = errInvalidFileHandle("closeFileFunc", handle)
|
||||
err = errInvalidFileHandle(name, handle)
|
||||
}
|
||||
result = err == nil
|
||||
return
|
||||
}
|
||||
|
||||
func writeFileFunc(ctx ExprContext, name string, args []any) (result any, err error) {
|
||||
func fileWriteTextFunc(ctx ExprContext, name string, args []any) (result any, err error) {
|
||||
var handle osHandle
|
||||
var invalidFileHandle any
|
||||
var ok bool
|
||||
@ -142,12 +142,12 @@ func writeFileFunc(ctx ExprContext, name string, args []any) (result any, err er
|
||||
}
|
||||
|
||||
if err == nil && (handle == nil || invalidFileHandle != nil) {
|
||||
err = errInvalidFileHandle("writeFileFunc", invalidFileHandle)
|
||||
err = errInvalidFileHandle(name, invalidFileHandle)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func readFileFunc(ctx ExprContext, name string, args []any) (result any, err error) {
|
||||
func fileReadTextFunc(ctx ExprContext, name string, args []any) (result any, err error) {
|
||||
var handle osHandle
|
||||
var invalidFileHandle any
|
||||
var ok bool
|
||||
@ -165,23 +165,50 @@ func readFileFunc(ctx ExprContext, name string, args []any) (result any, err err
|
||||
limit = s[0]
|
||||
}
|
||||
|
||||
if v, err = r.reader.ReadString(limit); err == nil {
|
||||
if len(v) > 0 && v[len(v)-1] == limit {
|
||||
v, err = r.reader.ReadString(limit)
|
||||
if err == io.EOF {
|
||||
err = nil
|
||||
}
|
||||
if len(v) > 0 {
|
||||
if v[len(v)-1] == limit {
|
||||
result = v[0 : len(v)-1]
|
||||
} else {
|
||||
result = v
|
||||
}
|
||||
}
|
||||
if err == io.EOF {
|
||||
err = nil
|
||||
}
|
||||
} else {
|
||||
invalidFileHandle = handle
|
||||
}
|
||||
}
|
||||
|
||||
if err == nil && (handle == nil || invalidFileHandle != nil) {
|
||||
err = errInvalidFileHandle("readFileFunc", invalidFileHandle)
|
||||
err = errInvalidFileHandle(name, invalidFileHandle)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func fileReadTextAllFunc(ctx ExprContext, name string, args []any) (result any, err error) {
|
||||
var handle osHandle
|
||||
var invalidFileHandle any
|
||||
var ok bool
|
||||
|
||||
result = nil
|
||||
if handle, ok = args[0].(osHandle); !ok || args[0] == nil {
|
||||
invalidFileHandle = args[0]
|
||||
}
|
||||
|
||||
if handle != nil {
|
||||
if r, ok := handle.(*osReader); ok {
|
||||
var b []byte
|
||||
b, err = io.ReadAll(r.reader)
|
||||
result = string(b)
|
||||
} else {
|
||||
invalidFileHandle = handle
|
||||
}
|
||||
}
|
||||
|
||||
if err == nil && (handle == nil || invalidFileHandle != nil) {
|
||||
err = errInvalidFileHandle(name, invalidFileHandle)
|
||||
}
|
||||
return
|
||||
}
|
||||
@ -190,21 +217,30 @@ func ImportOsFuncs(ctx ExprContext) {
|
||||
ctx.RegisterFunc("fileOpen", NewGolangFunctor(openFileFunc), TypeHandle, []ExprFuncParam{
|
||||
NewFuncParam(ParamFilepath),
|
||||
})
|
||||
|
||||
ctx.RegisterFunc("fileAppend", NewGolangFunctor(appendFileFunc), TypeHandle, []ExprFuncParam{
|
||||
NewFuncParam(ParamFilepath),
|
||||
})
|
||||
|
||||
ctx.RegisterFunc("fileCreate", NewGolangFunctor(createFileFunc), TypeHandle, []ExprFuncParam{
|
||||
NewFuncParam(ParamFilepath),
|
||||
})
|
||||
ctx.RegisterFunc("fileWrite", NewGolangFunctor(writeFileFunc), TypeInt, []ExprFuncParam{
|
||||
|
||||
ctx.RegisterFunc("fileClose", NewGolangFunctor(closeFileFunc), TypeBoolean, []ExprFuncParam{
|
||||
NewFuncParam(TypeHandle),
|
||||
})
|
||||
|
||||
ctx.RegisterFunc("fileWriteText", NewGolangFunctor(fileWriteTextFunc), TypeInt, []ExprFuncParam{
|
||||
NewFuncParam(TypeHandle),
|
||||
NewFuncParamFlagDef(TypeItem, PfDefault|PfRepeat, ""),
|
||||
})
|
||||
ctx.RegisterFunc("fileRead", NewGolangFunctor(readFileFunc), TypeString, []ExprFuncParam{
|
||||
|
||||
ctx.RegisterFunc("fileReadText", NewGolangFunctor(fileReadTextFunc), TypeString, []ExprFuncParam{
|
||||
NewFuncParam(TypeHandle),
|
||||
NewFuncParamFlagDef("limitCh", PfDefault, "\n"),
|
||||
})
|
||||
ctx.RegisterFunc("fileClose", NewGolangFunctor(closeFileFunc), TypeBoolean, []ExprFuncParam{
|
||||
|
||||
ctx.RegisterFunc("fileReadTextAll", NewGolangFunctor(fileReadTextAllFunc), TypeString, []ExprFuncParam{
|
||||
NewFuncParam(TypeHandle),
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user