Exemplo n.º 1
0
// Returns the value stored at the given array position.
func numberVarGetValue(v *Variable, pos uint) (interface{}, error) {
	if v.dataFloats != nil {
		// log.Printf("getting pos=%d from %+v", pos, v.dataFloats)
		return v.dataFloats[pos], nil
	}
	if v.dataInts != nil {
		// log.Printf("getting pos=%d from %+v", pos, v.dataInts)
		return v.dataInts[pos], nil
	}

	num := (*C.OCINumber)(v.getHandle(pos))
	buf := make([]byte, 64)
	size := C.ub4(len(buf))
	if err := v.environment.CheckStatus(
		C.OCINumberToText(v.environment.errorHandle,
			num,
			(*C.oratext)(unsafe.Pointer(&v.environment.numberToStringFormatBuffer[0])),
			C.ub4(len(v.environment.numberToStringFormatBuffer)), nil, 0,
			&size, (*C.oratext)(&buf[0])),
		"NumberToText",
	); err != nil {
		var floatVal float64
		_ = v.environment.CheckStatus(
			C.OCINumberToReal(v.environment.errorHandle,
				num,
				C.sizeof_double, unsafe.Pointer(&floatVal)),
			"numberToFloat")
		log.Printf("floatVal=%f format=%q len=%d num=%p (%d) size=%p buf=%p",
			floatVal,
			v.environment.numberToStringFormatBuffer,
			len(v.environment.numberToStringFormatBuffer),
			num, *((*byte)(unsafe.Pointer(num))),
			&size, &buf[0])
		return 0, errgo.Notef(err, "want string (%f)", floatVal)
	}
	numS := strings.Replace(
		v.environment.FromEncodedString(buf[:int(size)]),
		",", ".", -1)
	if v.typ == NumberAsStringVarType {
		return numS, nil
	}

	if len(numS) <= 9 && strings.IndexByte(numS, '.') < 0 {
		intVal, err := numAsInt(v, num)
		return intVal, errgo.Mask(err)
	}
	return numS, nil
}
Exemplo n.º 2
0
// Returns the value stored at the given array position.
func numberVarGetValue(v *Variable, pos uint) (interface{}, error) {
	if v.dataFloats != nil {
		// log.Printf("getting pos=%d from %+v", pos, v.dataFloats)
		return v.dataFloats[pos], nil
	}
	if v.dataInts != nil {
		// log.Printf("getting pos=%d from %+v", pos, v.dataInts)
		return v.dataInts[pos], nil
	}
	if v.typ == NumberAsStringVarType {
		buf := make([]byte, 200)
		var size C.ub4
		if err := v.environment.CheckStatus(
			C.OCINumberToText(v.environment.errorHandle,
				(*C.OCINumber)(v.getHandle(pos)),
				(*C.oratext)(unsafe.Pointer(&v.environment.numberToStringFormatBuffer[0])),
				C.ub4(len(v.environment.numberToStringFormatBuffer)), nil, 0,
				&size, (*C.oratext)(&buf[0])),
			"NumberToText"); err != nil {
			return 0, err
		}
		return v.environment.FromEncodedString(buf[:int(size)]), nil
	}
	// log.Printf("v=%s IsInteger?%s", v.typ, v.typ.IsInteger())
	if v.typ.IsInteger() {
		intVal := int64(0)
		if err := v.environment.CheckStatus(
			C.OCINumberToInt(v.environment.errorHandle,
				(*C.OCINumber)(v.getHandle(pos)),
				C.sizeof_long, C.OCI_NUMBER_SIGNED, unsafe.Pointer(&intVal)),
			"numberToInt"); err != nil {
			return -1, err
		}
		if v.typ == BooleanVarType {
			return intVal > 0, nil
		}
		return intVal, nil
	}

	floatVal := float64(0)
	err := v.environment.CheckStatus(
		C.OCINumberToReal(v.environment.errorHandle,
			(*C.OCINumber)(v.getHandle(pos)),
			C.sizeof_double, unsafe.Pointer(&floatVal)),
		"numberToFloat")
	return floatVal, err
}