iconv-go/iconv.go
Donovan Jimenez 82db0fae9a Initial iconv go package, supports:
* string conversion
 * byte slice conversion
 * Reader conversion
2011-01-14 18:34:30 -05:00

47 lines
942 B
Go

package iconv
// #include <errno.h>
import "C"
import (
"os"
)
// allows us to check for iconv specific errors
type Error os.Error
var (
EILSEQ Error = os.Errno(int(C.EILSEQ))
E2BIG Error = os.Errno(int(C.E2BIG))
)
func Convert(input []byte, output []byte, fromEncoding string, toEncoding string) (bytesRead int, bytesWritten int, err os.Error) {
// create a new converter
converter, err := NewConverter(fromEncoding, toEncoding)
if err == nil {
// call Convert
bytesRead, bytesWritten, err = converter.Convert(input, output)
// close the converter
converter.Close()
}
return
}
func ConvertString(input string, fromEncoding string, toEncoding string) (output string, err os.Error) {
// create a new converter
converter, err := NewConverter(fromEncoding, toEncoding)
if err == nil {
// convert the string
output, err = converter.ConvertString(input)
// close the converter
converter.Close()
}
return
}