update scalar field access
This commit is contained in:
@ -71,105 +71,58 @@ static PyObject* pyField_fldinfo(pyField *self)
|
|||||||
return Py_BuildValue("hhk", dbf, fsize, nelm);
|
return Py_BuildValue("hhk", dbf, fsize, nelm);
|
||||||
}
|
}
|
||||||
|
|
||||||
union dbfvalue {
|
|
||||||
char dv_sval[MAX_STRING_SIZE];
|
|
||||||
epicsUInt32 dv_uval;
|
|
||||||
epicsInt32 dv_ival;
|
|
||||||
double dv_fval;
|
|
||||||
};
|
|
||||||
|
|
||||||
static PyObject* pyField_getval(pyField *self)
|
static PyObject* pyField_getval(pyField *self)
|
||||||
{
|
{
|
||||||
union dbfvalue buf;
|
|
||||||
long nReq = 1;
|
|
||||||
short reqtype;
|
|
||||||
|
|
||||||
switch(self->addr.field_type)
|
switch(self->addr.field_type)
|
||||||
{
|
{
|
||||||
case DBF_CHAR:
|
#define OP(FTYPE, CTYPE, FN) case DBF_##FTYPE: return FN(*(CTYPE*)self->addr.pfield)
|
||||||
case DBF_UCHAR:
|
OP(CHAR, epicsInt8, PyInt_FromLong);
|
||||||
case DBF_SHORT:
|
OP(UCHAR, epicsUInt8, PyInt_FromLong);
|
||||||
case DBF_USHORT:
|
OP(SHORT, epicsInt16, PyInt_FromLong);
|
||||||
case DBF_ENUM:
|
OP(USHORT,epicsUInt16, PyInt_FromLong);
|
||||||
case DBF_LONG:
|
OP(LONG, epicsInt32, PyInt_FromLong);
|
||||||
reqtype = DBF_LONG;
|
OP(ULONG, epicsUInt32, PyInt_FromLong);
|
||||||
break;
|
OP(FLOAT, epicsFloat32,PyFloat_FromDouble);
|
||||||
case DBF_ULONG:
|
OP(DOUBLE,epicsFloat64,PyFloat_FromDouble);
|
||||||
reqtype = DBF_ULONG;
|
#undef OP
|
||||||
break;
|
|
||||||
case DBF_FLOAT:
|
|
||||||
case DBF_DOUBLE:
|
|
||||||
reqtype = DBF_DOUBLE;
|
|
||||||
break;
|
|
||||||
case DBF_STRING:
|
case DBF_STRING:
|
||||||
reqtype = DBF_STRING;
|
return PyString_FromString((char*)self->addr.pfield);
|
||||||
break;
|
|
||||||
default:
|
default:
|
||||||
PyErr_SetString(PyExc_ValueError, "Access to this field type is not supported");
|
PyErr_SetNone(PyExc_TypeError);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(dbGet(&self->addr, reqtype, buf.dv_sval, NULL, &nReq, NULL)) {
|
|
||||||
PyErr_SetString(PyExc_ValueError, "Error getting field value");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch(reqtype) {
|
|
||||||
case DBF_LONG:
|
|
||||||
return PyInt_FromLong(buf.dv_ival);
|
|
||||||
case DBF_ULONG:
|
|
||||||
return PyInt_FromLong(buf.dv_uval);
|
|
||||||
case DBF_DOUBLE:
|
|
||||||
return PyInt_FromLong(buf.dv_fval);
|
|
||||||
case DBF_STRING:
|
|
||||||
return PyString_FromString(buf.dv_sval);
|
|
||||||
default:
|
|
||||||
Py_RETURN_NONE;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static PyObject* pyField_putval(pyField *self, PyObject* args)
|
static PyObject* pyField_putval(pyField *self, PyObject* args)
|
||||||
{
|
{
|
||||||
PyObject *val;
|
PyObject *val;
|
||||||
|
|
||||||
union dbfvalue buf;
|
|
||||||
short puttype;
|
|
||||||
|
|
||||||
if(!PyArg_ParseTuple(args, "O", &val))
|
if(!PyArg_ParseTuple(args, "O", &val))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if(PyFloat_Check(val)) {
|
switch(self->addr.field_type)
|
||||||
double v = PyFloat_AsDouble(val);
|
{
|
||||||
buf.dv_fval = v;
|
#define OP(FTYPE, CTYPE, FN) case DBF_##FTYPE: *(CTYPE*)self->addr.pfield = FN(val); break
|
||||||
puttype = DBF_DOUBLE;
|
OP(CHAR, epicsInt8, PyInt_AsLong);
|
||||||
|
OP(UCHAR, epicsUInt8, PyInt_AsLong);
|
||||||
} else if(PyInt_Check(val)) {
|
OP(SHORT, epicsInt16, PyInt_AsLong);
|
||||||
long v = PyInt_AsLong(val);
|
OP(USHORT,epicsUInt16, PyInt_AsLong);
|
||||||
if(v>0x7fffffffL) {
|
OP(LONG, epicsInt32, PyInt_AsLong);
|
||||||
buf.dv_uval = v;
|
OP(ULONG, epicsUInt32, PyInt_AsLong);
|
||||||
puttype = DBF_ULONG;
|
OP(FLOAT, epicsFloat32,PyFloat_AsDouble);
|
||||||
} else {
|
OP(DOUBLE,epicsFloat64,PyFloat_AsDouble);
|
||||||
buf.dv_ival = v;
|
#undef OP
|
||||||
puttype = DBF_LONG;
|
case DBF_STRING: {
|
||||||
|
char *fld = PyString_AsString(val);
|
||||||
|
strncpy(self->addr.pfield, fld, MAX_STRING_SIZE);
|
||||||
|
fld = self->addr.pfield;
|
||||||
|
fld[MAX_STRING_SIZE-1]='\0';
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
default:
|
||||||
} else if(PyString_Check(val)) {
|
PyErr_SetNone(PyExc_TypeError);
|
||||||
const char *v = PyString_AsString(val);
|
|
||||||
strncpy(buf.dv_sval, v, MAX_STRING_SIZE);
|
|
||||||
buf.dv_sval[MAX_STRING_SIZE-1] = '\0';
|
|
||||||
puttype = DBF_STRING;
|
|
||||||
|
|
||||||
} else {
|
|
||||||
PyErr_SetString(PyExc_TypeError, "Unable to convert put type");
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(dbPut(&self->addr, puttype, buf.dv_sval, 1)){
|
|
||||||
PyErr_SetString(PyExc_ValueError, "Error putting field value");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
Py_RETURN_NONE;
|
Py_RETURN_NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user