-
Notifications
You must be signed in to change notification settings - Fork 0
/
ws_wordwrap.go
90 lines (75 loc) · 2.14 KB
/
ws_wordwrap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
MySQL UDF ws wordwrap
*/
package main
import (
/*
#cgo CFLAGS: -I/usr/include/mysql
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mysql.h>
#include <limits.h>
*/
"C"
"github.com/shigenobu/mysql_ws_wordwrap/func"
"strconv"
"unsafe"
)
// convert argc, argv into go structure
func argToGostrings(count C.uint, args **C.char, lengths *C.ulong) []string {
// https://github.com/golang/go/wiki/cgo#turning-c-arrays-into-go-slices
length := count
argslice := (*[1 << 30]*C.char)(unsafe.Pointer(args))[:length:length]
lengthsslice := (*[1 << 3]C.ulong)(unsafe.Pointer(lengths))[:length:length]
gostrings := make([]string, count)
for i, s := range argslice {
l := C.int(lengthsslice[i])
gostrings[i] = C.GoStringN(s, l)
}
return gostrings
}
// get pointer value
func getUintPointerValue(pointer *uint32, offset int) *C.uint {
// https://www.programminghunter.com/article/4762726307/
return (*C.uint)(unsafe.Pointer(uintptr(unsafe.Pointer(pointer)) + uintptr(offset)*uintptr(unsafe.Sizeof(C.uint(0)))))
}
//export ws_wordwrap_init
func ws_wordwrap_init(initid *C.UDF_INIT, args *C.UDF_ARGS, message *C.char) C.my_bool {
length := int(args.arg_count)
if length != 3 {
return 1
}
typeslice := (*[3]C.uint)(unsafe.Pointer(args.arg_type))[:length:length]
if typeslice[0] != C.STRING_RESULT {
return 1
}
if typeslice[1] != C.INT_RESULT {
return 1
}
if typeslice[2] != C.STRING_RESULT {
return 1
}
// resolve int value as string
*getUintPointerValue(args.arg_type, 1) = C.STRING_RESULT
return 0
}
//export ws_wordwrap
func ws_wordwrap(initid *C.UDF_INIT, args *C.UDF_ARGS, result *C.char, length *C.ulong, isNull *C.char, error *C.char) *C.char {
C.free(unsafe.Pointer(initid.ptr))
argsString := argToGostrings(args.arg_count, args.args, args.lengths)
// -----
// ここが処理本体
limit, _ := strconv.Atoi(argsString[1])
rslt := _func.Wordwrap(argsString[0], limit, argsString[2])
// -----
initid.ptr = C.CString(rslt)
*length = C.ulong(len(rslt))
return initid.ptr
}
//export ws_wordwrap_deinit
func ws_wordwrap_deinit(initid *C.UDF_INIT) {
C.free(unsafe.Pointer(initid.ptr))
}
func main() {
}