Ejemplo n.º 1
0
Archivo: util.go Proyecto: j14159/gocmc
func cstrToStr(cstr C.CFStringRef) (goString string) {
	len := C.CFStringGetLength(cstr)
	buf := make([]byte, len*2)
	cbuf := (*C.char)(unsafe.Pointer(&buf[0]))
	C.CFStringGetCString(cstr, cbuf, len*2, C.CFStringGetSystemEncoding())
	return string(buf)
}
Ejemplo n.º 2
0
func fromCFString(cstr C.CFStringRef) string {
	defer C.CFRelease(C.CFTypeRef(cstr))

	var (
		buf  []C.char
		ok   C.Boolean
		size uint = 1024
	)
	for ok == C.FALSE {
		buf = make([]C.char, size)
		ok = C.CFStringGetCString(cstr, &buf[0],
			C.CFIndex(len(buf)), C.kCFStringEncodingUTF8)
		size *= 2
	}
	return C.GoString(&buf[0])
}
Ejemplo n.º 3
0
func getCFDictValueUTF8String(dict C.CFDictionaryRef, key C.CFTypeRef) (string, error) {
	valCFStringRef, err := getCFDictValueCFStringRef(dict, key)
	if err != nil {
		return "", err
	}
	log.Debugf("valCFStringRef: %#v", valCFStringRef)
	if valCFStringRef == nil {
		return "", errors.New("getCFDictValueUTF8String: Nil value")
	}

	strLen := C.CFStringGetLength(valCFStringRef)
	log.Debugf("strLen: %d", strLen)
	charUTF8Len := C.CFStringGetMaximumSizeForEncoding(strLen, C.kCFStringEncodingUTF8) + 1
	log.Debugf("charUTF8Len: %d", charUTF8Len)
	cstrBytes := make([]byte, charUTF8Len, charUTF8Len)
	if C.Boolean(0) == C.CFStringGetCString(valCFStringRef, (*C.char)(unsafe.Pointer(&cstrBytes[0])), charUTF8Len, C.kCFStringEncodingUTF8) {
		return "", errors.New("getCFDictValueUTF8String: CFStringGetCString: failed to convert value to string")
	}
	return C.GoString((*C.char)(unsafe.Pointer(&cstrBytes[0]))), nil
}