Пример #1
0
func buildCArgs(params []driver.Value) **C.char {
	sparams := make([]string, len(params))
	isnulls := make([]bool, len(params))
	for i, v := range params {
		var str string
		switch v := v.(type) {
		case []byte:
			str = "\\x" + hex.EncodeToString(v)
		case bool:
			if v {
				str = "t"
			} else {
				str = "f"
			}
		case time.Time:
			str = v.Format(timeFormat)
		case nil:
			isnulls[i] = true
		default:
			str = fmt.Sprint(v)
		}

		sparams[i] = str
	}
	cparams := C.makeCharArray(C.int(len(sparams)))
	for i, s := range sparams {
		if isnulls[i] {
			C.setArrayString(cparams, nil, C.int(i))
		} else {
			C.setArrayString(cparams, C.CString(s), C.int(i))
		}
	}
	return cparams
}
Пример #2
0
func makeNullTerminatedArgs(args []string) **C.char {
	cparams := C.makeCharArray(C.int(len(args) + 1))
	for i, s := range args {
		C.setArrayString(cparams, C.CString(s), C.int(i))
	}
	C.setArrayString(cparams, nil, C.int(len(args)))
	return cparams
}
Пример #3
0
func makeNullTerminatedArgs(args []string) **C.char {
	cparams := C.makeCharArray(C.size_t(len(args) + 1))
	if cparams == nil {
		return nil
	}

	for i := 0; i < len(args); i++ {
		C.setArrayString(cparams, C.CString(args[i]), C.size_t(i))
	}
	C.setArrayString(cparams, nil, C.size_t(len(args)))

	return cparams
}
Пример #4
0
func buildCArgs(params ...interface{}) **C.char {
	sparams := make([]string, len(params))
	for i, v := range params {
		var str string
		switch v := v.(type) {
		case []byte:
			str = "\\x" + hex.EncodeToString(v)
		case bool:
			if v {
				str = "t"
			} else {
				str = "f"
			}
		case *time.Time:
			str = v.Format(timeFormat)
		default:
			str = fmt.Sprint(v)
		}

		sparams[i] = str
	}
	cparams := C.makeCharArray(C.int(len(sparams)))
	for i, s := range sparams {
		C.setArrayString(cparams, C.CString(s), C.int(i))
	}
	return cparams
}
Пример #5
0
// convert database/sql/driver arguments into libpq-style char**
func buildCArgs(args []driver.Value) (**C.char, error) {
	carray := getCharArrayFromPool(len(args))

	for i, v := range args {
		var str string
		switch v := v.(type) {
		case int64:
			str = strconv.FormatInt(v, 10)
		case float64:
			str = strconv.FormatFloat(v, 'E', -1, 64)
		case bool:
			if v {
				str = "t"
			} else {
				str = "f"
			}
		case []byte:
			str = `\x` + hex.EncodeToString(v)
		case string:
			str = v
		case time.Time:
			str = v.Format(timeFormat)
		case nil:
			str = "NULL"
		default:
			returnCharArrayToPool(len(args), carray)
			return nil, errors.New("libpq: unsupported type")
		}

		C.setArrayString(carray, C.CString(str), C.int(i))
	}

	return carray, nil
}
Пример #6
0
func CreateProgram(context *Context, sources []string) *Program {
	runtime.LockOSThread()
	var program Program
	var error C.cl_int

	csources := C.makeCharArray(C.int(len(sources)))
	clenghts := make([]C.size_t, len(sources))

	defer C.freeCharArray(csources, C.int(len(sources)))

	for i := 0; i < len(sources); i++ {
		C.setArrayString(csources, C.CString(sources[i]), C.int(i))
		clenghts[i] = C.size_t(len(sources[i]))
		fmt.Printf("Program log:%d %s\n", clenghts[i], sources[i])
	}

	program.item = C.clCreateProgramWithSource(
		context.item,
		C.cl_uint(len(sources)),
		csources,
		&clenghts[0],
		&error)

	fmt.Printf("Program error:%d\n", error)

	return &program
}
Пример #7
0
func AcquirePlanes(planes []string, callback PlanesAvailable, ref interface{}) int {
	cAircrafts := C.makeCharArray(C.int(len(planes)))
	defer C.freeCharArray(cAircrafts, C.int(len(planes)))
	for i, s := range planes {
		C.setArrayString(cAircrafts, C.CString(s), C.int(i))
	}
	reg:=&regInfo{callback,ref}
	return int(C.XPLMAcquirePlanes(cAircrafts,C.XPLMPlanesAvailable_f(unsafe.Pointer(C.planesAvailableCallback)),unsafe.Pointer(reg)))
}
Пример #8
0
// Generic channel shuffling: copy src to dst, but with channels in the order specified by
// ChannelOpts.Order[0..nchannels-1]. Does not support in-place operation.
//
// See ChannelOpts docs for more details on the options.
func Channels(dst, src *ImageBuf, nchannels int, opts ...*ChannelOpts) error {
	var (
		order    *C.int32_t
		values   *C.float
		newNames **C.char
		shuffle  C.bool = C.bool(false)
	)

	var opt *ChannelOpts
	if len(opts) > 0 {
		opt = opts[len(opts)-1]
	}

	if opt != nil {
		shuffle = C.bool(opt.ShuffleNames)

		if opt.Order != nil {
			if len(opt.Order) < nchannels {
				return fmt.Errorf("ChannelOpts.Order length %d is less than nchannels %d",
					len(opt.Order), nchannels)
			}
			order = (*C.int32_t)(unsafe.Pointer(&opt.Order[0]))
		}

		if opt.Values != nil {
			if len(opt.Values) < nchannels {
				return fmt.Errorf("ChannelOpts.Values length %d is less than nchannels %d",
					len(opt.Values), nchannels)
			}
			values = (*C.float)(unsafe.Pointer(&opt.Values[0]))
		}

		if opt.NewNames != nil {
			if len(opt.NewNames) < nchannels {
				return fmt.Errorf("ChannelOpts.NewNames length %d is less than nchannels %d",
					len(opt.NewNames), nchannels)
			}
			nameSize := len(opt.NewNames)
			newNames = C.makeCharArray(C.int(nameSize))
			defer C.freeCharArray(newNames, C.int(nameSize))
			for i, s := range opt.NewNames {
				C.setArrayString(newNames, C.CString(s), C.int(i))
			}
		}
	}

	ok := C.channels(dst.ptr, src.ptr, C.int(nchannels), order, values, newNames, shuffle)
	if !bool(ok) {
		return dst.LastError()
	}
	return nil
}
Пример #9
0
func MoveToTrash(filePath string) (string, error) {
	files := []string{filePath}
	C_files := C.makeCharArray(C.int(len(files)))
	defer C.freeCharArray(C_files, C.int(len(files)))
	for i, s := range files {
		C.setArrayString(C_files, C.CString(s), C.int(i))
	}

	success := C.RecycleFiles(C_files, C.int(len(files)), C.int(0))
	if success != 1 {
		return "", errors.New("file could not be recycled")
	}
	return "", nil
}
Пример #10
0
func (node *Node) AddRemotes(addrs []string) (err error) {
	if len(addrs) == 0 {
		return fmt.Errorf("list of remotes is empty")
	}

	cargs := C.makeCharArray(C.int(len(addrs)))
	defer C.freeCharArray(cargs, C.int(len(addrs)))
	for i, s := range addrs {
		C.setArrayString(cargs, C.CString(s), C.int(i))
	}

	err = nil
	c_err := C.node_add_remote_array(node.node, cargs, C.int(len(addrs)))
	if c_err < 0 {
		err = syscall.Errno(-c_err)
	}
	return
}
Пример #11
0
// Simple is the 'simple' interface.
//  If port is 0, the default remctl port is used
//  If principal is "", the default principal of "host/<hostname>" is used
//
// For more control of how the call is made, use the more 'complex'
// interface.
func Simple(host string, port uint16, principal string, command []string) (*RemctlResult, error) {
	var res *C.struct_remctl_result

	host_c := C.CString(host)
	defer C.free(unsafe.Pointer(host_c))

	var principal_c *_Ctype_char
	if principal != "" {
		principal_c = C.CString(principal)
		defer C.free(unsafe.Pointer(principal_c))
	}

	command_len := len(command)
	command_c := C.makeCharArray(C.int(command_len))
	defer C.freeCharArray(command_c, C.int(command_len))
	for i, s := range command {
		C.setArrayString(command_c, C.CString(s), C.int(i))
	}

	res, err := C.remctl(host_c, C.ushort(port), principal_c, command_c)

	if res == nil {
		return nil, err
	} else {
		defer C.remctl_result_free((*C.struct_remctl_result)(unsafe.Pointer(res)))
		result := &RemctlResult{}
		stdout_len := (C.int)(res.stdout_len)
		stderr_len := (C.int)(res.stderr_len)
		result.Stdout = C.GoStringN(res.stdout_buf, stdout_len)
		result.Stderr = C.GoStringN(res.stderr_buf, stderr_len)
		result.Status = (int)(res.status)
		if res.error != nil {
			return nil, errors.New(C.GoString(res.error))
		}

		return result, nil
	}
}
Пример #12
0
func buildCArgs(params []driver.Value) **C.char {
	sparams := make([]interface{}, len(params))
	for i, v := range params {
		var str interface{}
		switch v := v.(type) {
		case []byte:
			str = "\\x" + hex.EncodeToString(v)
		case bool:
			if v {
				str = "t"
			} else {
				str = "f"
			}
		case time.Time:
			str = v.Format(timeFormat)
		case nil:
			str = nil
		default:
			str = fmt.Sprint(v)
		}

		sparams[i] = str
	}
	cparams := C.makeCharArray(C.int(len(sparams)))
	converter := func(str interface{}) *C.char {
		if str == nil {
			return nil
		}
		return C.CString(str.(string))
	}

	for i, s := range sparams {
		C.setArrayString(cparams, converter(s), C.int(i))
	}
	return cparams
}