package api
import (
"fmt"
"unsafe"
)
type ExternalPlugin struct {
libHandle uintptr
pluginPtr unsafe.Pointer
name string
vtable *PluginVTable
fileSystem *ExternalFileSystem
}
type PluginVTable struct {
PluginNew func() unsafe.Pointer
PluginFree func(unsafe.Pointer)
PluginName func(unsafe.Pointer) *byte
PluginValidate func(unsafe.Pointer, *byte) *byte
PluginInitialize func(unsafe.Pointer, *byte) *byte
PluginShutdown func(unsafe.Pointer) *byte
PluginGetReadme func(unsafe.Pointer) *byte
FSCreate func(unsafe.Pointer, *byte) *byte
FSMkdir func(unsafe.Pointer, *byte, uint32) *byte
FSRemove func(unsafe.Pointer, *byte) *byte
FSRemoveAll func(unsafe.Pointer, *byte) *byte
FSRead func(unsafe.Pointer, *byte, int64, int64, *int) *byte
FSWrite func(unsafe.Pointer, *byte, *byte, int, int64, uint32) int64
FSReadDir func(unsafe.Pointer, *byte, *int) *FileInfoArray
FSStat func(unsafe.Pointer, *byte) *FileInfoC
FSRename func(unsafe.Pointer, *byte, *byte) *byte
FSChmod func(unsafe.Pointer, *byte, uint32) *byte
}
type FileInfoC struct {
Name *byte
Size int64
Mode uint32
ModTime int64
IsDir int32
MetaName *byte
MetaType *byte
MetaContent *byte
}
type FileInfoArray struct {
Items *FileInfoC
Count int
}
type ExternalFileSystem struct {
pluginPtr unsafe.Pointer
vtable *PluginVTable
}
func CString(s string) *byte {
if s == "" {
return nil
}
b := append([]byte(s), 0)
return &b[0]
}
func GoString(cstr *byte) string {
if cstr == nil {
return ""
}
var length int
for {
ptr := unsafe.Pointer(uintptr(unsafe.Pointer(cstr)) + uintptr(length))
if *(*byte)(ptr) == 0 {
break
}
length++
}
if length == 0 {
return ""
}
return string(unsafe.Slice(cstr, length))
}
func GoError(errStr *byte) error {
if errStr == nil {
return nil
}
msg := GoString(errStr)
if msg == "" {
return nil
}
return fmt.Errorf("%s", msg)
}