Beispiel #1
0
func ConvertGoTiltStructToC(ts TiltState) *C.freenect_raw_tilt_state {
	c_ts := C.create_tilt_state()
	c_ts.accelerometer_x = C.int16_t(ts.Accelerometer_x)
	c_ts.accelerometer_y = C.int16_t(ts.Accelerometer_y)
	c_ts.accelerometer_z = C.int16_t(ts.Accelerometer_z)
	c_ts.tilt_angle = C.int8_t(ts.Tilt_angle)
	c_ts.tilt_status = C.freenect_tilt_status_code(ts.Tilt_status)
	return c_ts
}
Beispiel #2
0
//Set the replication of the specified file to the supplied value.
//path: The path of the file.
//Returns nil on success, or error.
func (fs *Fs) SetReplication(path string, replication int16) error {
	p := C.CString(path)
	defer C.free(unsafe.Pointer(p))
	ret, err := C.hdfsSetReplication(fs.cptr, p, C.int16_t(replication))
	if err != nil && ret == C.int(-1) {
		return err
	}
	return nil
}
Beispiel #3
0
func setRetPointer(p unsafe.Pointer, v reflect.Value) {
	switch v.Kind() {
	case reflect.Int:
		*((*C.int)(p)) = C.int(v.Int())

	case reflect.Int8:
		*((*C.int8_t)(p)) = C.int8_t(v.Int())

	case reflect.Int16:
		*((*C.int16_t)(p)) = C.int16_t(v.Int())

	case reflect.Int32:
		*((*C.int32_t)(p)) = C.int32_t(v.Int())

	case reflect.Int64:
		*((*C.int64_t)(p)) = C.int64_t(v.Int())

	case reflect.Uint:
		*((*C.uint)(p)) = C.uint(v.Uint())

	case reflect.Uint8:
		*((*C.uint8_t)(p)) = C.uint8_t(v.Uint())

	case reflect.Uint16:
		*((*C.uint16_t)(p)) = C.uint16_t(v.Uint())

	case reflect.Uint32:
		*((*C.uint32_t)(p)) = C.uint32_t(v.Uint())

	case reflect.Uint64:
		*((*C.uint64_t)(p)) = C.uint64_t(v.Uint())

	case reflect.Uintptr:
		*((*C.size_t)(p)) = C.size_t(v.Uint())

	case reflect.Float32:
		*((*C.float)(p)) = C.float(v.Float())

	case reflect.Float64:
		*((*C.double)(p)) = C.double(v.Float())

	case reflect.String:
		*((**C.char)(p)) = C.CString(v.String())

	case reflect.UnsafePointer:
		*((*unsafe.Pointer)(p)) = unsafe.Pointer(v.Pointer())
	}
}
Beispiel #4
0
func (t object_map) Run(w http.ResponseWriter, r *http.Request, fn func()) {
	var req map[string]*json.RawMessage
	err := json.NewDecoder(r.Body).Decode(&req)

	if err != nil {
		http.Error(w, "Bad request", 400)
		return
	}

	for key, value := range req {
		dst, ok := t[key]

		if !ok {
			http.Error(w, "Unknown key: "+key, 400)
			return
		}

		var err error

		switch dst.Type {
		case "*main._Ctype_char":
			var s string
			err = json.Unmarshal(*value, &s)
			tmp := C.CString(s)
			*(**C.char)(dst.Offset) = tmp
			defer C.free(unsafe.Pointer(tmp))
		case "main._Ctype_uint32_t":
			var i uint32
			err = json.Unmarshal(*value, &i)
			*(*C.uint32_t)(dst.Offset) = C.uint32_t(i)
		case "main._Ctype_uint16_t":
			var i uint16
			err = json.Unmarshal(*value, &i)
			*(*C.uint16_t)(dst.Offset) = C.uint16_t(i)
		case "main._Ctype_uint8_t":
			var i uint8
			err = json.Unmarshal(*value, &i)
			*(*C.uint8_t)(dst.Offset) = C.uint8_t(i)
		case "main._Ctype_int32_t":
			var i int32
			err = json.Unmarshal(*value, &i)
			*(*C.int32_t)(dst.Offset) = C.int32_t(i)
		case "main._Ctype_int16_t":
			var i int16
			err = json.Unmarshal(*value, &i)
			*(*C.int16_t)(dst.Offset) = C.int16_t(i)
		case "main._Ctype_int8_t":
			var i int8
			err = json.Unmarshal(*value, &i)
			*(*C.int8_t)(dst.Offset) = C.int8_t(i)
		case "main._Ctype_time_t":
			var i uint64
			err = json.Unmarshal(*value, &i)
			*(*C.time_t)(dst.Offset) = C.time_t(i)
		case "*main._Ctype_uint32_t":
			var ai []uint32
			err = json.Unmarshal(*value, &ai)
			tmp := C.sluw_alloc_uint32_t(C.int(len(ai)))
			defer C.free(unsafe.Pointer(tmp))
			for i := 0; i < len(ai); i++ {
				C.sluw_set_uint32_t(tmp, C.uint32_t(ai[i]), C.int(i))
			}
			*(**C.uint32_t)(dst.Offset) = tmp
		case "*main._Ctype_int32_t":
			var ai []int32
			err = json.Unmarshal(*value, &ai)
			tmp := C.sluw_alloc_int32_t(C.int(len(ai)))
			defer C.free(unsafe.Pointer(tmp))
			for i := 0; i < len(ai); i++ {
				C.sluw_set_int32_t(tmp, C.int32_t(ai[i]), C.int(i))
			}
			*(**C.int32_t)(dst.Offset) = tmp
		case "**main._Ctype_char":
			var as []string
			err = json.Unmarshal(*value, &as)
			tmp := C.sluw_alloc_chars(C.int(len(as)))
			defer C.free(unsafe.Pointer(tmp))
			for i := 0; i < len(as); i++ {
				tmp2 := C.CString(as[i])
				defer C.free(unsafe.Pointer(tmp2))
				C.sluw_set_chars(tmp, tmp2, C.int(i))
			}
			*(***C.char)(dst.Offset) = (**C.char)(tmp)
		default:
			log.Println(key, reflect.TypeOf(dst), "not supported")
		}

		if err != nil {
			http.Error(w, "Bad value for key: "+key, 400)
			return
		}
	}

	fn()
}
Beispiel #5
0
func marshal(v interface{}, data *C.pn_data_t) {
	switch v := v.(type) {
	case nil:
		C.pn_data_put_null(data)
	case bool:
		C.pn_data_put_bool(data, C.bool(v))
	case int8:
		C.pn_data_put_byte(data, C.int8_t(v))
	case int16:
		C.pn_data_put_short(data, C.int16_t(v))
	case int32:
		C.pn_data_put_int(data, C.int32_t(v))
	case int64:
		C.pn_data_put_long(data, C.int64_t(v))
	case int:
		if unsafe.Sizeof(0) == 8 {
			C.pn_data_put_long(data, C.int64_t(v))
		} else {
			C.pn_data_put_int(data, C.int32_t(v))
		}
	case uint8:
		C.pn_data_put_ubyte(data, C.uint8_t(v))
	case uint16:
		C.pn_data_put_ushort(data, C.uint16_t(v))
	case uint32:
		C.pn_data_put_uint(data, C.uint32_t(v))
	case uint64:
		C.pn_data_put_ulong(data, C.uint64_t(v))
	case uint:
		if unsafe.Sizeof(0) == 8 {
			C.pn_data_put_ulong(data, C.uint64_t(v))
		} else {
			C.pn_data_put_uint(data, C.uint32_t(v))
		}
	case float32:
		C.pn_data_put_float(data, C.float(v))
	case float64:
		C.pn_data_put_double(data, C.double(v))
	case string:
		C.pn_data_put_string(data, pnBytes([]byte(v)))
	case []byte:
		C.pn_data_put_binary(data, pnBytes(v))
	case Binary:
		C.pn_data_put_binary(data, pnBytes([]byte(v)))
	case Symbol:
		C.pn_data_put_symbol(data, pnBytes([]byte(v)))
	case Map: // Special map type
		C.pn_data_put_map(data)
		C.pn_data_enter(data)
		for key, val := range v {
			marshal(key, data)
			marshal(val, data)
		}
		C.pn_data_exit(data)
	default:
		switch reflect.TypeOf(v).Kind() {
		case reflect.Map:
			putMap(data, v)
		case reflect.Slice:
			putList(data, v)
		default:
			panic(fmt.Errorf("cannot marshal %s to AMQP", reflect.TypeOf(v)))
		}
	}
	err := dataError("marshal", data)
	if err != nil {
		panic(err)
	}
	return
}
Beispiel #6
0
func makeArgValue(v reflect.Value) unsafe.Pointer {
	switch v.Kind() {
	case reflect.Int:
		x := C.int(v.Int())
		return unsafe.Pointer(&x)

	case reflect.Int8:
		x := C.int8_t(v.Int())
		return unsafe.Pointer(&x)

	case reflect.Int16:
		x := C.int16_t(v.Int())
		return unsafe.Pointer(&x)

	case reflect.Int32:
		x := C.int32_t(v.Int())
		return unsafe.Pointer(&x)

	case reflect.Int64:
		x := C.int64_t(v.Int())
		return unsafe.Pointer(&x)

	case reflect.Uint:
		x := C.uint(v.Uint())
		return unsafe.Pointer(&x)

	case reflect.Uint8:
		x := C.uint8_t(v.Uint())
		return unsafe.Pointer(&x)

	case reflect.Uint16:
		x := C.uint16_t(v.Uint())
		return unsafe.Pointer(&x)

	case reflect.Uint32:
		x := C.uint32_t(v.Uint())
		return unsafe.Pointer(&x)

	case reflect.Uint64:
		x := C.uint64_t(v.Uint())
		return unsafe.Pointer(&x)

	case reflect.Uintptr:
		x := C.size_t(v.Uint())
		return unsafe.Pointer(&x)

	case reflect.Float32:
		x := C.float(v.Float())
		return unsafe.Pointer(&x)

	case reflect.Float64:
		x := C.double(v.Float())
		return unsafe.Pointer(&x)

	case reflect.String:
		x := C.CString(v.String())
		return unsafe.Pointer(&x)

	case reflect.UnsafePointer:
		x := v.Pointer()
		return unsafe.Pointer(&x)

	case reflect.Ptr:
		x := v.Pointer()
		return unsafe.Pointer(&x)

	case reflect.Slice:
		x := v.Pointer()
		return unsafe.Pointer(&x)

	case reflect.Interface:
		if v.IsNil() {
			x := unsafe.Pointer(nil)
			return unsafe.Pointer(&x)
		}
	}

	unsupportedArgType(v)
	return nil
}
Beispiel #7
0
func makeRetValue(v reflect.Value) unsafe.Pointer {
	if !v.IsValid() {
		return nil
	}

	switch v = v.Elem(); v.Kind() {
	case reflect.Int:
		x := C.int(v.Int())
		return unsafe.Pointer(&x)

	case reflect.Int8:
		x := C.int8_t(v.Int())
		return unsafe.Pointer(&x)

	case reflect.Int16:
		x := C.int16_t(v.Int())
		return unsafe.Pointer(&x)

	case reflect.Int32:
		x := C.int32_t(v.Int())
		return unsafe.Pointer(&x)

	case reflect.Int64:
		x := C.int64_t(v.Int())
		return unsafe.Pointer(&x)

	case reflect.Uint8:
		x := C.uint8_t(v.Uint())
		return unsafe.Pointer(&x)

	case reflect.Uint16:
		x := C.uint16_t(v.Uint())
		return unsafe.Pointer(&x)

	case reflect.Uint32:
		x := C.uint32_t(v.Uint())
		return unsafe.Pointer(&x)

	case reflect.Uint64:
		x := C.uint64_t(v.Uint())
		return unsafe.Pointer(&x)

	case reflect.Uintptr:
		x := C.size_t(v.Uint())
		return unsafe.Pointer(&x)

	case reflect.Float32:
		x := C.float(v.Float())
		return unsafe.Pointer(&x)

	case reflect.Float64:
		x := C.double(v.Float())
		return unsafe.Pointer(&x)

	case reflect.String:
		x := unsafe.Pointer(nil)
		return unsafe.Pointer(&x)

	case reflect.UnsafePointer:
		x := unsafe.Pointer(nil)
		return unsafe.Pointer(&x)

	case reflect.Ptr:
		x := unsafe.Pointer(nil)
		return unsafe.Pointer(&x)
	}

	unsupportedRetType(v)
	return nil
}
Beispiel #8
0
func (f *File) Fwrite16be(w int16) int16 {
	return int16(C.al_fwrite16be((*C.ALLEGRO_FILE)(unsafe.Pointer(f)), C.int16_t(w)))
}
Beispiel #9
0
func BuildValue(format string, values ...interface{}) (Object, error) {
	cValues := make([]C.ArgValue, len(values))
	for i, value := range values {
		switch v := value.(type) {
		case string:
			s := C.CString(v)
			defer C.free(unsafe.Pointer(s))
			cValues[i]._type = &C.ffi_type_pointer
			cValues[i].value = unsafe.Pointer(&s)
		case Object:
			b := v.Base()
			cValues[i]._type = &C.ffi_type_pointer
			cValues[i].value = unsafe.Pointer(&b)
		case int:
			iv := C.int(v)
			cValues[i]._type = &C.ffi_type_sint
			cValues[i].value = unsafe.Pointer(&iv)
		case int8:
			iv := C.int8_t(v)
			cValues[i]._type = &C.ffi_type_sint8
			cValues[i].value = unsafe.Pointer(&iv)
		case int16:
			iv := C.int16_t(v)
			cValues[i]._type = &C.ffi_type_sint16
			cValues[i].value = unsafe.Pointer(&iv)
		case int32:
			iv := C.int32_t(v)
			cValues[i]._type = &C.ffi_type_sint32
			cValues[i].value = unsafe.Pointer(&iv)
		case int64:
			iv := C.int64_t(v)
			cValues[i]._type = &C.ffi_type_sint64
			cValues[i].value = unsafe.Pointer(&iv)
		case uint:
			iv := C.uint(v)
			cValues[i]._type = &C.ffi_type_uint
			cValues[i].value = unsafe.Pointer(&iv)
		case uint8:
			iv := C.uint8_t(v)
			cValues[i]._type = &C.ffi_type_uint8
			cValues[i].value = unsafe.Pointer(&iv)
		case uint16:
			iv := C.uint16_t(v)
			cValues[i]._type = &C.ffi_type_uint16
			cValues[i].value = unsafe.Pointer(&iv)
		case uint32:
			iv := C.uint32_t(v)
			cValues[i]._type = &C.ffi_type_uint32
			cValues[i].value = unsafe.Pointer(&iv)
		case uint64:
			iv := C.uint64_t(v)
			cValues[i]._type = &C.ffi_type_uint64
			cValues[i].value = unsafe.Pointer(&iv)
		case float32:
			fv := C.float(v)
			cValues[i]._type = &C.ffi_type_float
			cValues[i].value = unsafe.Pointer(&fv)
		case float64:
			fv := C.double(v)
			cValues[i]._type = &C.ffi_type_double
			cValues[i].value = unsafe.Pointer(&fv)
		default:
			return nil, TypeError.Err("Unsupported type: %T", v)
		}
	}
	f := C.CString(format)
	defer C.free(unsafe.Pointer(f))
	ret := C.doBuildValue(f, &cValues[0], C.int(len(cValues)))
	if ret == nil {
		return nil, exception()
	}
	return newObject(ret), nil
}