Compare commits

..

4 Commits

Author SHA1 Message Date
yuxh
37678f5397 兼容Linux平台 2021-11-03 10:09:48 +08:00
yuxh
55c60d656f 添加对Linux平台的支持 2021-11-03 00:08:12 +08:00
Donovan Jimenez
8960e66bd3 Merge pull request #24 from kowalczykp/master
Fix Panic in Go 1.6
2016-03-05 17:51:43 -05:00
pkowalczyk
072063d01b Fixes #23 using call_iconv wrapper 2016-03-05 19:56:42 +01:00

View File

@ -4,23 +4,21 @@ package iconv
#cgo darwin LDFLAGS: -liconv #cgo darwin LDFLAGS: -liconv
#cgo freebsd LDFLAGS: -liconv #cgo freebsd LDFLAGS: -liconv
#cgo windows LDFLAGS: -liconv #cgo windows LDFLAGS: -liconv
#cgo LDFLAGS: -liconv
#include <stdlib.h> #include <stdlib.h>
#include <iconv.h> #include <iconv.h>
#include <locale.h>
// called by init, seems to be necessary for TRANSLIT to work // As of GO 1.6 passing a pointer to Go pointer, will lead to panic
void initLocale() { // Therofore we use this wrapper function, to avoid passing **char directly from go
setlocale(LC_ALL, ""); size_t call_iconv(iconv_t ctx, char *in, size_t *size_in, char *out, size_t *size_out){
return iconv(ctx, &in, size_in, &out, size_out);
} }
*/ */
import "C" import "C"
import "syscall" import "syscall"
import "unsafe" import "unsafe"
func init() {
C.initLocale()
}
type Converter struct { type Converter struct {
context C.iconv_t context C.iconv_t
open bool open bool
@ -90,7 +88,7 @@ func (this *Converter) Convert(input []byte, output []byte) (bytesRead int, byte
inputPointer := (*C.char)(unsafe.Pointer(&input[0])) inputPointer := (*C.char)(unsafe.Pointer(&input[0]))
outputPointer := (*C.char)(unsafe.Pointer(&output[0])) outputPointer := (*C.char)(unsafe.Pointer(&output[0]))
_, err = C.iconv(this.context, &inputPointer, &inputLeft, &outputPointer, &outputLeft) _, err = C.call_iconv(this.context, inputPointer, &inputLeft, outputPointer, &outputLeft)
// update byte counters // update byte counters
bytesRead = len(input) - int(inputLeft) bytesRead = len(input) - int(inputLeft)
@ -99,13 +97,13 @@ func (this *Converter) Convert(input []byte, output []byte) (bytesRead int, byte
// inputPointer will be nil, outputPointer is generated as above // inputPointer will be nil, outputPointer is generated as above
outputPointer := (*C.char)(unsafe.Pointer(&output[0])) outputPointer := (*C.char)(unsafe.Pointer(&output[0]))
_, err = C.iconv(this.context, nil, &inputLeft, &outputPointer, &outputLeft) _, err = C.call_iconv(this.context, nil, &inputLeft, outputPointer, &outputLeft)
// update write byte counter // update write byte counter
bytesWritten = len(output) - int(outputLeft) bytesWritten = len(output) - int(outputLeft)
} else { } else {
// both input and output are zero length, do a shift state reset // both input and output are zero length, do a shift state reset
_, err = C.iconv(this.context, nil, &inputLeft, nil, &outputLeft) _, err = C.call_iconv(this.context, nil, &inputLeft, nil, &outputLeft)
} }
} else { } else {
err = syscall.EBADF err = syscall.EBADF