1
0
Fork 0
mirror of https://github.com/vbatts/go-mtree.git synced 2024-10-31 22:36:38 +00:00
go-mtree/cvis/vis.go
Vincent Batts 08b1000418
vis: adding a pure golang Vis()
The current Vis() and Unvis() are using the C implementation from
MTREE(8).

But that means that cgo is used, which is not always desired.

Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
2016-12-07 16:12:28 -05:00

28 lines
743 B
Go

package cvis
// #include "vis.h"
// #include <stdlib.h>
import "C"
import (
"fmt"
"math"
"unsafe"
)
// Vis is a wrapper around the C implementation
func Vis(src string, flags int) (string, error) {
// dst needs to be 4 times the length of str, must check appropriate size
if uint32(len(src)*4+1) >= math.MaxUint32/4 {
return "", fmt.Errorf("failed to encode: %q", src)
}
dst := string(make([]byte, 4*len(src)+1))
cDst, cSrc := C.CString(dst), C.CString(src)
defer C.free(unsafe.Pointer(cDst))
defer C.free(unsafe.Pointer(cSrc))
C.strvis(cDst, cSrc, C.int(flags))
return C.GoString(cDst), nil
}
// DefaultVisFlags are the common flags used in mtree string encoding
var DefaultVisFlags = C.VIS_WHITE | C.VIS_OCTAL | C.VIS_GLOB