From 0322982392a1e05273cb4667b33b0030f311f1f4 Mon Sep 17 00:00:00 2001 From: Matej Sekoranja Date: Fri, 22 Nov 2013 10:30:29 +0100 Subject: [PATCH] ported utils to new pvData array API, pvput now uses pvData convert --- testApp/remote/eget.cpp | 63 +++-- testApp/remote/pvput.cpp | 469 +++---------------------------------- testApp/remote/pvutils.cpp | 5 +- 3 files changed, 68 insertions(+), 469 deletions(-) diff --git a/testApp/remote/eget.cpp b/testApp/remote/eget.cpp index b5a4dc3..be4dfba 100644 --- a/testApp/remote/eget.cpp +++ b/testApp/remote/eget.cpp @@ -180,7 +180,7 @@ void formatNTEnum(std::ostream& o, PVStructurePtr const & pvStruct) } } -size_t getLongestString(vector const & array) +size_t getLongestString(shared_vector const & array) { size_t max = 0; size_t len = array.size(); @@ -213,7 +213,7 @@ size_t getLongestString(PVScalarArrayPtr const & array) // labels are optional // if provided labels.size() must equals columnData.size() void formatTable(std::ostream& o, - vector const & labels, + shared_vector const & labels, vector const & columnData, bool showHeader, bool transpose) { @@ -385,12 +385,8 @@ void formatNTTable(std::ostream& o, PVStructurePtr const & pvStruct) columnData.push_back(array); } - // get labels - StringArrayData labelsData; - labels->get(0, numColumns, labelsData); - bool showHeader = (mode != TerseMode); - formatTable(o, labelsData.data, columnData, showHeader, transpose); + formatTable(o, labels->view(), columnData, showHeader, transpose); } @@ -416,10 +412,9 @@ void formatNTMatrix(std::ostream& o, PVStructurePtr const & pvStruct) return; } - IntArrayData data; - dim->get(0, dims, data); - rows = data.data[0]; - cols = (dims == 2) ? data.data[1] : 1; + PVIntArray::const_svector data = dim->view(); + rows = data[0]; + cols = (dims == 2) ? data[1] : 1; if (rows <= 0 || cols <= 0) { @@ -534,12 +529,11 @@ void formatNTNameValue(std::ostream& o, PVStructurePtr const & pvStruct) size_t numColumns = name->getLength(); // get names - StringArrayData nameData; - name->get(0, name->getLength(), nameData); + PVStringArray::const_svector nameData = name->view(); // get max column name size bool showHeader = (mode != TerseMode); - size_t maxLabelColumnLength = showHeader ? getLongestString(nameData.data) : 0; + size_t maxLabelColumnLength = showHeader ? getLongestString(nameData) : 0; size_t maxColumnLength = getLongestString(array); @@ -569,7 +563,7 @@ void formatNTNameValue(std::ostream& o, PVStructurePtr const & pvStruct) { if (separator == ' ') { - int width = std::max(nameData.data[i].size()+padding, maxColumnLength); + int width = std::max(nameData[i].size()+padding, maxColumnLength); o << std::setw(width) << std::right; // non-compact o << std::setw(maxColumnLength) << std::right; } @@ -578,7 +572,7 @@ void formatNTNameValue(std::ostream& o, PVStructurePtr const & pvStruct) o << separator; } - o << nameData.data[i]; + o << nameData[i]; } o << std::endl; } @@ -588,7 +582,7 @@ void formatNTNameValue(std::ostream& o, PVStructurePtr const & pvStruct) { if (separator == ' ') { - int width = std::max(nameData.data[i].size()+padding, maxColumnLength); + int width = std::max(nameData[i].size()+padding, maxColumnLength); o << std::setw(width) << std::right; // non-compact o << std::setw(maxColumnLength) << std::right; } @@ -617,7 +611,7 @@ void formatNTNameValue(std::ostream& o, PVStructurePtr const & pvStruct) { o << std::setw(maxLabelColumnLength) << std::left; } - o << nameData.data[i]; + o << nameData[i]; } if (separator == ' ') @@ -718,20 +712,19 @@ void formatNTImage(std::ostream& /*o*/, PVStructurePtr const & pvStruct) // dim[] = { rows, columns } or // dim[] = { 3, rows, columns } - IntArrayData data; + PVIntArray::const_svector data = dim->view(); size_t dims = dim->getLength(); - dim->get(0, dims, data); size_t imageSize; if ((cm == 0 || cm == 1) && dims == 2) { - cols = data.data[0]; - rows = data.data[1]; + cols = data[0]; + rows = data[1]; imageSize = cols * rows; } else if (cm == 2 && dims == 3) { - cols = data.data[1]; - rows = data.data[2]; + cols = data[1]; + rows = data[2]; imageSize = cols * rows * 3; } else @@ -760,12 +753,11 @@ void formatNTImage(std::ostream& /*o*/, PVStructurePtr const & pvStruct) return; } - ByteArrayData img; - array->get(0, array->getLength(), img); + PVByteArray::const_svector img = array->view(); /* size_t len = static_cast(rows*cols); for (size_t i = 0; i < len; i++) - o << img.data[i]; + o << img[i]; */ //eget -s testImage | gnuplot -e "set size ratio -1; set palette grey; set cbrange [0:255]; plot '-' binary array=(512,384) flipy format='%uchar' with image" @@ -793,7 +785,7 @@ void formatNTImage(std::ostream& /*o*/, PVStructurePtr const & pvStruct) fprintf(gnuplotPipe, "plot '-' binary array=(%u,%u) flipy format='%%uchar' with rgbimage\n", cols, rows); for (size_t i = 0; i < imageSize; i++) - fprintf(gnuplotPipe, "%c", img.data[i]); + fprintf(gnuplotPipe, "%c", img[i]); } else { @@ -805,7 +797,7 @@ void formatNTImage(std::ostream& /*o*/, PVStructurePtr const & pvStruct) fprintf(gnuplotPipe, "plot '-' binary array=(%u,%u) flipy format='%%uchar' with image\n", cols, rows); for (size_t i = 0; i < imageSize; i++) - fprintf(gnuplotPipe, "%c", img.data[i]); + fprintf(gnuplotPipe, "%c", img[i]); } fflush(gnuplotPipe); @@ -927,7 +919,7 @@ static String emptyString; // only in ValueOnlyMode // NOTE: names might be empty -void printValues(vector const & names, vector const & values) +void printValues(shared_vector const & names, vector const & values) { size_t len = values.size(); @@ -950,10 +942,11 @@ void printValues(vector const & names, vector(getPVDataCreate()->createPVScalarArray(pvString)); - StringArray values; - values.reserve(1); + + PVStringArray::svector values; values.push_back(getConvert()->toString(scalar)); - stringArray->put(0, values.size(), values, 0); + stringArray->replace(freeze(values)); + scalarArrays.push_back(stringArray); } } @@ -1690,7 +1683,7 @@ int main (int argc, char *argv[]) vector collectedValues; collectedValues.reserve(nPvs); - vector collectedNames; + shared_vector collectedNames; collectedNames.reserve(nPvs); // for now a simple iterating sync implementation, guarantees order @@ -1778,7 +1771,7 @@ int main (int argc, char *argv[]) } if (collectValues) - printValues(collectedNames, collectedValues); + printValues(freeze(collectedNames), collectedValues); if (allOK && monitor) { diff --git a/testApp/remote/pvput.cpp b/testApp/remote/pvput.cpp index 8d81cc2..648ec0c 100644 --- a/testApp/remote/pvput.cpp +++ b/testApp/remote/pvput.cpp @@ -26,414 +26,10 @@ using namespace std::tr1; using namespace epics::pvData; using namespace epics::pvAccess; -// TODO add >> operator support to PVField -// code copied from Convert.cpp and added error handling, and more strict boolean convert - -void fromString(PVScalarPtr const & pvScalar, String const & from) -{ - ScalarConstPtr scalar = pvScalar->getScalar(); - ScalarType scalarType = scalar->getScalarType(); - switch(scalarType) { - case pvBoolean: { - PVBooleanPtr pv = static_pointer_cast(pvScalar); - bool isTrue = (from.compare("true")==0 || from.compare("1")==0); - bool isFalse = (from.compare("false")==0 || from.compare("0")==0); - if (!(isTrue || isFalse)) - throw runtime_error("failed to parse field " + pvScalar->getFieldName() + " (boolean) from string value '" + from + "'"); - pv->put(isTrue == true); - return; - } - case pvByte : { - PVBytePtr pv = static_pointer_cast(pvScalar); - int ival; - int result = sscanf(from.c_str(),"%d",&ival); - if (result != 1) - throw runtime_error("failed to parse field " + pvScalar->getFieldName() + " (byte) from string value '" + from + "'"); - int8 value = ival; - pv->put(value); - return; - } - case pvShort : { - PVShortPtr pv = static_pointer_cast(pvScalar); - int ival; - int result = sscanf(from.c_str(),"%d",&ival); - if (result != 1) - throw runtime_error("failed to parse field " + pvScalar->getFieldName() + " (short) from string value '" + from + "'"); - int16 value = ival; - pv->put(value); - return; - } - case pvInt : { - PVIntPtr pv = static_pointer_cast(pvScalar); - int ival; - int result = sscanf(from.c_str(),"%d",&ival); - if (result != 1) - throw runtime_error("failed to parse field " + pvScalar->getFieldName() + " (int) from string value '" + from + "'"); - int32 value = ival; - pv->put(value); - return; - } - case pvLong : { - PVLongPtr pv = static_pointer_cast(pvScalar); - int64 ival; - int result = sscanf(from.c_str(),"%lld",(long long *)&ival); - if (result != 1) - throw runtime_error("failed to parse field " + pvScalar->getFieldName() + " (long) from string value '" + from + "'"); - int64 value = ival; - pv->put(value); - return; - } - case pvUByte : { - PVUBytePtr pv = static_pointer_cast(pvScalar); - unsigned int ival; - int result = sscanf(from.c_str(),"%u",&ival); - if (result != 1) - throw runtime_error("failed to parse field " + pvScalar->getFieldName() + " (ubyte) from string value '" + from + "'"); - uint8 value = ival; - pv->put(value); - return; - } - case pvUShort : { - PVUShortPtr pv = static_pointer_cast(pvScalar); - unsigned int ival; - int result = sscanf(from.c_str(),"%u",&ival); - if (result != 1) - throw runtime_error("failed to parse field " + pvScalar->getFieldName() + " (ushort) from string value '" + from + "'"); - uint16 value = ival; - pv->put(value); - return; - } - case pvUInt : { - PVUIntPtr pv = static_pointer_cast(pvScalar); - unsigned int ival; - int result = sscanf(from.c_str(),"%u",&ival); - if (result != 1) - throw runtime_error("failed to parse field " + pvScalar->getFieldName() + " (uint) from string value '" + from + "'"); - uint32 value = ival; - pv->put(value); - return; - } - case pvULong : { - PVULongPtr pv = static_pointer_cast(pvScalar); - unsigned long long ival; - int result = sscanf(from.c_str(),"%llu",(long long unsigned int *)&ival); - if (result != 1) - throw runtime_error("failed to parse field " + pvScalar->getFieldName() + " (ulong) from string value '" + from + "'"); - uint64 value = ival; - pv->put(value); - return; - } - case pvFloat : { - PVFloatPtr pv = static_pointer_cast(pvScalar); - float value; - int result = sscanf(from.c_str(),"%f",&value); - if (result != 1) - throw runtime_error("failed to parse field " + pvScalar->getFieldName() + " (float) from string value '" + from + "'"); - pv->put(value); - return; - } - case pvDouble : { - PVDoublePtr pv = static_pointer_cast(pvScalar); - double value; - int result = sscanf(from.c_str(),"%lf",&value); - if (result != 1) - throw runtime_error("failed to parse field " + pvScalar->getFieldName() + " (double) from string value '" + from + "'"); - pv->put(value); - return; - } - case pvString: { - PVStringPtr value = static_pointer_cast(pvScalar); - value->put(from); - return; - } - } - String message("fromString unknown scalarType "); - ScalarTypeFunc::toString(&message,scalarType); - throw std::logic_error(message); -} - - -size_t convertFromStringArray(PVScalarArray *pv, - size_t offset, size_t len,const StringArray & from, size_t fromOffset) -{ - ScalarType elemType = pv->getScalarArray()->getElementType(); - size_t ntransfered = 0; - switch (elemType) { - case pvBoolean: { - PVBooleanArray *pvdata = static_cast(pv); - boolean data[1]; - while (len > 0) { - String fromString = from[fromOffset]; - bool isTrue = (fromString.compare("true")==0 || fromString.compare("1")==0); - bool isFalse = (fromString.compare("false")==0 || fromString.compare("0")==0); - if (!(isTrue || isFalse)) - { - char soffset[64]; - sprintf(soffset, "%u", (unsigned int)offset); - throw runtime_error("failed to parse field " + pv->getFieldName() + " (boolean array at index " + soffset + ") from string value '" + fromString + "'"); - } - data[0] = isTrue == true; - if (pvdata->put(offset, 1, data, 0) == 0) - return ntransfered; - --len; - ++ntransfered; - ++offset; - ++fromOffset; - } - return ntransfered; - } - case pvByte: { - PVByteArray *pvdata = static_cast(pv); - int8 data[1]; - while (len > 0) { - String fromString = from[fromOffset]; - int ival; - int result = sscanf(fromString.c_str(),"%d",&ival); - if (result != 1) - { - char soffset[64]; - sprintf(soffset, "%u", (unsigned int)offset); - throw runtime_error("failed to parse field " + pv->getFieldName() + " (byte array at index " + soffset + ") from string value '" + fromString + "'"); - } - data[0] = ival; - if (pvdata->put(offset, 1, data, 0) == 0) - return ntransfered; - --len; - ++ntransfered; - ++offset; - ++fromOffset; - } - return ntransfered; - } - case pvShort: { - PVShortArray *pvdata = static_cast(pv); - int16 data[1]; - while (len > 0) { - String fromString = from[fromOffset]; - int ival; - int result = sscanf(fromString.c_str(),"%d",&ival); - if (result != 1) - { - char soffset[64]; - sprintf(soffset, "%u", (unsigned int)offset); - throw runtime_error("failed to parse field " + pv->getFieldName() + " (short array at index " + soffset + ") from string value '" + fromString + "'"); - } - data[0] = ival; - if (pvdata->put(offset, 1, data, 0) == 0) - return ntransfered; - --len; - ++ntransfered; - ++offset; - ++fromOffset; - } - return ntransfered; - } - case pvInt: { - PVIntArray *pvdata = static_cast(pv); - int32 data[1]; - while (len > 0) { - String fromString = from[fromOffset]; - int ival; - int result = sscanf(fromString.c_str(),"%d",&ival); - if (result != 1) - { - char soffset[64]; - sprintf(soffset, "%u", (unsigned int)offset); - throw runtime_error("failed to parse field " + pv->getFieldName() + " (int array at index " + soffset + ") from string value '" + fromString + "'"); - } - data[0] = ival; - if (pvdata->put(offset, 1, data, 0) == 0) - return ntransfered; - --len; - ++ntransfered; - ++offset; - ++fromOffset; - } - return ntransfered; - } - case pvLong: { - PVLongArray *pvdata = static_cast(pv); - int64 data[1]; - while (len > 0) { - String fromString = from[fromOffset]; - int64 ival; - int result = sscanf(fromString.c_str(),"%lld",(long long int *)&ival); - if (result != 1) - { - char soffset[64]; - sprintf(soffset, "%u", (unsigned int)offset); - throw runtime_error("failed to parse field " + pv->getFieldName() + " (long array at index " + soffset + ") from string value '" + fromString + "'"); - } - data[0] = ival; - if (pvdata->put(offset, 1, data, 0) == 0) - return ntransfered; - --len; - ++ntransfered; - ++offset; - ++fromOffset; - } - return ntransfered; - } - case pvUByte: { - PVUByteArray *pvdata = static_cast(pv); - uint8 data[1]; - while (len > 0) { - String fromString = from[fromOffset]; - unsigned int ival; - int result = sscanf(fromString.c_str(),"%u",&ival); - if (result != 1) - { - char soffset[64]; - sprintf(soffset, "%u", (unsigned int)offset); - throw runtime_error("failed to parse field " + pv->getFieldName() + " (ubyte array at index " + soffset + ") from string value '" + fromString + "'"); - } - data[0] = ival; - if (pvdata->put(offset, 1, data, 0) == 0) - return ntransfered; - --len; - ++ntransfered; - ++offset; - ++fromOffset; - } - return ntransfered; - } - case pvUShort: { - PVUShortArray *pvdata = static_cast(pv); - uint16 data[1]; - while (len > 0) { - String fromString = from[fromOffset]; - unsigned int ival; - int result = sscanf(fromString.c_str(),"%u",&ival); - if (result != 1) - { - char soffset[64]; - sprintf(soffset, "%u", (unsigned int)offset); - throw runtime_error("failed to parse field " + pv->getFieldName() + " (ushort array at index " + soffset + ") from string value '" + fromString + "'"); - } - data[0] = ival; - if (pvdata->put(offset, 1, data, 0) == 0) - return ntransfered; - --len; - ++ntransfered; - ++offset; - ++fromOffset; - } - return ntransfered; - } - case pvUInt: { - PVUIntArray *pvdata = static_cast(pv); - uint32 data[1]; - while (len > 0) { - String fromString = from[fromOffset]; - unsigned int ival; - int result = sscanf(fromString.c_str(),"%u",&ival); - if (result != 1) - { - char soffset[64]; - sprintf(soffset, "%u", (unsigned int)offset); - throw runtime_error("failed to parse field " + pv->getFieldName() + " (uint array at index " + soffset + ") from string value '" + fromString + "'"); - } - data[0] = ival; - if (pvdata->put(offset, 1, data, 0) == 0) - return ntransfered; - --len; - ++ntransfered; - ++offset; - ++fromOffset; - } - return ntransfered; - } - case pvULong: { - PVULongArray *pvdata = static_cast(pv); - uint64 data[1]; - while (len > 0) { - String fromString = from[fromOffset]; - uint64 ival; - int result = sscanf(fromString.c_str(),"%lld",(unsigned long long int *)&ival); - if (result != 1) - { - char soffset[64]; - sprintf(soffset, "%u", (unsigned int)offset); - throw runtime_error("failed to parse field " + pv->getFieldName() + " (ulong array at index " + soffset + ") from string value '" + fromString + "'"); - } - data[0] = ival; - if (pvdata->put(offset, 1, data, 0) == 0) - return ntransfered; - --len; - ++ntransfered; - ++offset; - ++fromOffset; - } - return ntransfered; - } - case pvFloat: { - PVFloatArray *pvdata = static_cast(pv); - float data[1]; - while (len > 0) { - String fromString = from[fromOffset]; - float fval; - int result = sscanf(fromString.c_str(),"%f",&fval); - if (result != 1) - { - char soffset[64]; - sprintf(soffset, "%u", (unsigned int)offset); - throw runtime_error("failed to parse field " + pv->getFieldName() + " (float array at index " + soffset + ") from string value '" + fromString + "'"); - } - data[0] = fval; - if (pvdata->put(offset, 1, data, 0) == 0) - return ntransfered; - --len; - ++ntransfered; - ++offset; - ++fromOffset; - } - return ntransfered; - } - case pvDouble: { - PVDoubleArray *pvdata = static_cast(pv); - double data[1]; - while (len > 0) { - String fromString = from[fromOffset]; - double fval; - int result = sscanf(fromString.c_str(),"%lf",&fval); - if (result != 1) - { - char soffset[64]; - sprintf(soffset, "%u", (unsigned int)offset); - throw runtime_error("failed to parse field " + pv->getFieldName() + " (double array at index " + soffset + ") from string value '" + fromString + "'"); - } - data[0] = fval; - if (pvdata->put(offset, 1, data, 0) == 0) - return ntransfered; - --len; - ++ntransfered; - ++offset; - ++fromOffset; - } - return ntransfered; - } - case pvString: - PVStringArray *pvdata = static_cast(pv); - while (len > 0) { - String * xxx = const_cast(get(from)); - size_t n = pvdata->put(offset, len, xxx, fromOffset); - if (n == 0) - break; - len -= n; - offset += n; - fromOffset += n; - ntransfered += n; - } - return ntransfered; - } - String message("convertFromStringArray should never get here"); - throw std::logic_error(message); -} - size_t fromStringArray(PVScalarArrayPtr const &pv, size_t offset, size_t length, StringArray const & from, size_t fromOffset) { - return convertFromStringArray(pv.get(),offset,length,from,fromOffset); + return getConvert()->fromStringArray(pv,offset,length,from,fromOffset); } size_t fromString(PVScalarArrayPtr const &pv, StringArray const & from, size_t fromStartIndex = 0) @@ -443,7 +39,7 @@ size_t fromString(PVScalarArrayPtr const &pv, StringArray const & from, size_t f // first get count if (fromStartIndex >= fromValueCount) - throw std::runtime_error("not enough values, stopped at field " + pv->getFieldName()); + throw std::runtime_error("not enough of values"); size_t count; istringstream iss(from[fromStartIndex]); @@ -482,33 +78,44 @@ size_t fromString(PVStructurePtr const & pvStructure, StringArray const & from, for(size_t i = 0; i < length; i++) { PVFieldPtr fieldField = fieldsData[i]; - Type type = fieldField->getField()->getType(); - if(type==structure) { - PVStructurePtr pv = static_pointer_cast(fieldField); - size_t count = fromString(pv, from, fromStartIndex); - processed += count; - fromStartIndex += count; - } - else if(type==scalarArray) { - PVScalarArrayPtr pv = static_pointer_cast(fieldField); - size_t count = fromString(pv, from, fromStartIndex); - processed += count; - fromStartIndex += count; - } - else if(type==scalar) { + try + { + Type type = fieldField->getField()->getType(); + // TODO union/unionArray support + if(type==structure) { + PVStructurePtr pv = static_pointer_cast(fieldField); + size_t count = fromString(pv, from, fromStartIndex); + processed += count; + fromStartIndex += count; + } + else if(type==scalarArray) { + PVScalarArrayPtr pv = static_pointer_cast(fieldField); + size_t count = fromString(pv, from, fromStartIndex); + processed += count; + fromStartIndex += count; + } + else if(type==scalar) { + + if (fromStartIndex >= fromValueCount) + throw std::runtime_error("not enough of values"); - if (fromStartIndex >= fromValueCount) - throw std::runtime_error("not enough values, stopped at field " + fieldField->getFieldName()); - - PVScalarPtr pv = static_pointer_cast(fieldField); - fromString(pv, from[fromStartIndex++]); - processed++; + PVScalarPtr pv = static_pointer_cast(fieldField); + getConvert()->fromString(pv, from[fromStartIndex++]); + processed++; + } + else { + // structureArray not supported + String message("fromString unsupported fieldType "); + TypeFunc::toString(&message,type); + throw std::logic_error(message); + } } - else { - // structureArray not supported - String message("fromString unsupported fieldType "); - TypeFunc::toString(&message,type); - throw std::logic_error(message); + catch (std::exception &ex) + { + std::ostringstream os; + os << "failed to parse '" << fieldField->getField()->getID() << ' ' << fieldField->getFieldName() << "'"; + os << ": " << ex.what(); + throw std::runtime_error(os.str()); } } } diff --git a/testApp/remote/pvutils.cpp b/testApp/remote/pvutils.cpp index 939120d..7fff54b 100644 --- a/testApp/remote/pvutils.cpp +++ b/testApp/remote/pvutils.cpp @@ -150,8 +150,7 @@ std::ostream& terseStructureArray(std::ostream& o, PVStructureArray::shared_poin o << length << separator; } - StructureArrayData data = StructureArrayData(); - pvArray->get(0, length, data); + PVStructureArray::const_svector data = pvArray->view(); bool first = true; for (size_t i = 0; i < length; i++) { if (first) @@ -159,7 +158,7 @@ std::ostream& terseStructureArray(std::ostream& o, PVStructureArray::shared_poin else o << separator; - terseStructure(o, data.data[i]); + terseStructure(o, data[i]); } return o; }