func newGeohash(geohash string) *C.CCGeohashStruct { cgeohash := C.CString(geohash) cg := &C.CCGeohashStruct{} C.memset(unsafe.Pointer(&(cg.hash[0])), '0', MaxGeohashLength) // move this on C side C.strncpy(&(cg.hash[0]), cgeohash, C.size_t(len(geohash))) C.init_from_hash(cg) return cg }
// SetFormat sets the image format for encoding this image. // See http://www.graphicsmagick.org for a list of supported // formats. func (in *Info) SetFormat(format string) { if format == "" { in.info.magick[0] = 0 } else { s := C.CString(format) defer C.free(unsafe.Pointer(s)) C.strncpy(&in.info.magick[0], s, C.MaxTextExtent) } }
func (p *PropertyStore) SetPropertyString(name string, value string) { szName := C.CString(name) cValue := C.CString(value) defer C.free(unsafe.Pointer(szName)) defer C.free(unsafe.Pointer(cValue)) var st C.struct_aiString C.strncpy(&st.data[0], cValue, C.MAXLEN) C.aiSetImportPropertyString((*C.struct_aiPropertyStore)(p), szName, &st) }
// Encode writes the image to the given io.Writer, encoding // it according to the info parameter. Please, see the // Info type for the available encoding options. func (im *Image) Encode(w io.Writer, info *Info) error { var ex C.ExceptionInfo C.GetExceptionInfo(&ex) defer C.DestroyExceptionInfo(&ex) if info == nil { info = NewInfo() } /* ImageToBlob copies the format from the image into the image info. Overwrite if required and then restore */ im.mu.Lock() var format *C.char copied := false if info.info.magick[0] != 0 { copied = true if im.image.magick[0] != 0 { format = C.strdup(&im.image.magick[0]) } C.strncpy(&im.image.magick[0], &info.info.magick[0], C.MaxTextExtent) } var s C.size_t mem := imageToBlob(info, im, &s, &ex) if copied { /* Restore image format */ if format != nil { C.strncpy(&im.image.magick[0], format, C.MaxTextExtent) C.free(unsafe.Pointer(format)) } else { C.memset(unsafe.Pointer(&im.image.magick[0]), 0, C.MaxTextExtent) } } im.mu.Unlock() if mem == nil { return exError(&ex, "encoding") } b := goBytes(mem, int(s)) w.Write(b) C.free(mem) return nil }
func main() { // To get the string into GoString, we need a *char // but charArray isn't null terminated, so don't do this! myString := C.GoString((*C.char)(unsafe.Pointer(&C.charArray))) fmt.Println(myString) // Assigning to a char array requires constant sized arrays C.charArray = *(*[16]C.char)(unsafe.Pointer(&[16]byte{'f', 'r', 'o', 'm', ' ', 'g', 'o'})) fmt.Println(C.GoString((*C.char)(unsafe.Pointer(&C.charArray)))) // Using strncpy still needs more unsafe pointers type assertions C.strncpy((*C.char)(unsafe.Pointer(&C.charArray)), C.CString("my string"), 9) fmt.Println(C.GoString((*C.char)(unsafe.Pointer(&C.charArray)))) }
// OpenSerial connects to the transceiver and returns a ready to use Rig. // // Caller must remember to Close the Rig after use. func OpenSerial(model RigModel, path string, baudrate int) (*SerialRig, error) { rig := C.rig_init(C.rig_model_t(model)) if rig == nil { return nil, ErrUnknownModel } // Set baudrate C.setBaudRate(rig, C.int(baudrate)) // Set path to tty C.strncpy(&rig.state.rigport.pathname[0], C.CString(path), C.FILPATHLEN-1) err := codeToError(C.rig_open(rig)) if err != nil { return nil, fmt.Errorf("Unable to open rig: %s", err) } return &SerialRig{*rig}, nil }
func CreateTun(name string) (*Tun, error) { f, err := os.OpenFile(TUN_DEV, os.O_RDWR, 0) if err != nil { return nil, err } cbuf := (*C.char)(C.malloc(C.IFNAMSIZ)) defer C.free(unsafe.Pointer(cbuf)) C.memset(unsafe.Pointer(cbuf), 0, C.IFNAMSIZ) c_name := C.CString(name) defer C.free(unsafe.Pointer(c_name)) C.strncpy(cbuf, c_name, C.IFNAMSIZ-1) if C.create_tun(C.int(f.Fd()), cbuf) < 0 { return nil, fmt.Errorf("create tun fail") } return &Tun{C.GoString(cbuf), f}, nil }
func test6506() { // nothing to run, just make sure this compiles var x C.size_t C.calloc(x, x) C.malloc(x) C.realloc(nil, x) C.memcpy(nil, nil, x) C.memcmp(nil, nil, x) C.memmove(nil, nil, x) C.strncpy(nil, nil, x) C.strncmp(nil, nil, x) C.strncat(nil, nil, x) x = C.strxfrm(nil, nil, x) C.memchr(nil, 0, x) x = C.strcspn(nil, nil) x = C.strspn(nil, nil) C.memset(nil, 0, x) x = C.strlen(nil) _ = x }
//export goSummary func goSummary(summaryString *C.char, summaryMaxLength C.size_t, value unsafe.Pointer) { str := summaryCallback0(value) C.strncpy(summaryString, C.CString(str), summaryMaxLength) }