/* Hdf_attr.cpp */ #undef DEBUG_HDF_ATTR_CPP #ifdef DEBUG_HDF_ATTR_CPP #define PDEBUG { std::fprintf(stderr, "call to %s (%s)\n", __PRETTY_FUNCTION__, __FILE__); } #else #define PDEBUG #endif #include #include #include #include // nothrow #include "Hdf_common.hpp" #include "Hdf_attr.hpp" #define DEFAULT_CHARS_TO_DISPLAY 80 #define DEFAULT_NUMBERS_TO_DISPLAY 10 int32 Hdf_attr::chars_to_display = DEFAULT_CHARS_TO_DISPLAY; int32 Hdf_attr::numbers_to_display = DEFAULT_NUMBERS_TO_DISPLAY; void Hdf_attr::allocate_values() { PDEBUG; if (nvalues) { assert(values == NULL); switch (type) { case DFNT_CHAR8 : { values = new(std::nothrow) char8[nvalues]; /* FIXME: nothrow to be replaced by try ... catch */ break; } case DFNT_UCHAR8 : { values = new(std::nothrow) uchar8[nvalues]; /* FIXME: nothrow to be replaced by try ... catch */ break; } case DFNT_INT8 : { values = new(std::nothrow) int8[nvalues]; /* FIXME: nothrow to be replaced by try ... catch */ break; } case DFNT_UINT8 : { values = new(std::nothrow) uint8[nvalues]; /* FIXME: nothrow to be replaced by try ... catch */ break; } case DFNT_INT16 : { values = new(std::nothrow) int16[nvalues]; /* FIXME: nothrow to be replaced by try ... catch */ break; } case DFNT_UINT16 : { values = new(std::nothrow) uint16[nvalues]; /* FIXME: nothrow to be replaced by try ... catch */ break; } case DFNT_INT32 : { values = new(std::nothrow) int32[nvalues]; /* FIXME: nothrow to be replaced by try ... catch */ break; } case DFNT_UINT32 : { values = new(std::nothrow) uint32[nvalues]; /* FIXME: nothrow to be replaced by try ... catch */ break; } case DFNT_FLOAT32 : { values = new(std::nothrow) float32[nvalues]; /* FIXME: nothrow to be replaced by try ... catch */ break; } case DFNT_FLOAT64 : { values = new(std::nothrow) float64[nvalues]; /* FIXME: nothrow to be replaced by try ... catch */ break; } default: { std::cerr << __FILE__ << ": " << __PRETTY_FUNCTION__ << ": unexpected value type for attribute " << name << std::endl; std::abort(); // hopefully we should never arrive there unless HDF types are updated } } // switch (attr_type) assert(values != NULL); } // if (nvalues) } void Hdf_attr::deallocate_values() { PDEBUG; if (nvalues) { assert(values != NULL); switch (type) { case DFNT_CHAR8 : { delete[] static_cast(values); break; } case DFNT_UCHAR8 : { delete[] static_cast(values); break; } case DFNT_INT8 : { delete[] static_cast(values); break; } case DFNT_UINT8 : { delete[] static_cast(values); break; } case DFNT_INT16 : { delete[] static_cast(values); break; } case DFNT_UINT16 : { delete[] static_cast(values); break; } case DFNT_INT32 : { delete[] static_cast(values); break; } case DFNT_UINT32 : { delete[] static_cast(values); break; } case DFNT_FLOAT32 : { delete[] static_cast(values); break; } case DFNT_FLOAT64 : { delete[] static_cast(values); break; } default: { std::cerr << __FILE__ << ": " << __PRETTY_FUNCTION__ << ": unexpected value type for attribute " << name << std::endl; std::abort(); // hopefully we should never arrive there unless HDF types are updated } } // switch (attr_type) values = NULL; } // if (nvalues) } void Hdf_attr::copy_values(const Hdf_attr &hdf_attr) { PDEBUG; if (nvalues) { assert(nvalues == hdf_attr.nvalues); assert(hdf_attr.values != NULL); assert(values != NULL); switch (type) { case DFNT_CHAR8 : { for (int32 ival = 0 ; ival < nvalues ; ival++) static_cast(values)[ival] = static_cast(hdf_attr.values)[ival]; break; } case DFNT_UCHAR8 : { for (int32 ival = 0 ; ival < nvalues ; ival++) static_cast(values)[ival] = static_cast(hdf_attr.values)[ival]; break; } case DFNT_INT8 : { for (int32 ival = 0 ; ival < nvalues ; ival++) static_cast(values)[ival] = static_cast(hdf_attr.values)[ival]; break; } case DFNT_UINT8 : { for (int32 ival = 0 ; ival < nvalues ; ival++) static_cast(values)[ival] = static_cast(hdf_attr.values)[ival]; break; } case DFNT_INT16 : { for (int32 ival = 0 ; ival < nvalues ; ival++) static_cast(values)[ival] = static_cast(hdf_attr.values)[ival]; break; } case DFNT_UINT16 : { for (int32 ival = 0 ; ival < nvalues ; ival++) static_cast(values)[ival] = static_cast(hdf_attr.values)[ival]; break; } case DFNT_INT32 : { for (int32 ival = 0 ; ival < nvalues ; ival++) static_cast(values)[ival] = static_cast(hdf_attr.values)[ival]; break; } case DFNT_UINT32 : { for (int32 ival = 0 ; ival < nvalues ; ival++) static_cast(values)[ival] = static_cast(hdf_attr.values)[ival]; break; } case DFNT_FLOAT32 : { for (int32 ival = 0 ; ival < nvalues ; ival++) static_cast(values)[ival] = static_cast(hdf_attr.values)[ival]; break; } case DFNT_FLOAT64 : { for (int32 ival = 0 ; ival < nvalues ; ival++) static_cast(values)[ival] = static_cast(hdf_attr.values)[ival]; break; } default: { std::cerr << __FILE__ << ": " << __PRETTY_FUNCTION__ << ": unexpected value type for attribute " << name << std::endl; std::abort(); // hopefully we should never arrive there unless HDF types are updated } } // switch (attr_type) } // if (nvalues) } void Hdf_attr::get_value(void *value, int32 ival) const { PDEBUG; assert(value); if (ival < 0 || ival >= nvalues) { std::cerr << __FILE__ << ": " << __PRETTY_FUNCTION__ << ": index " << ival << " not in the range [0," << nvalues - 1 << "]" << std::endl; return; } switch (type) { case DFNT_CHAR8 : { *static_cast(value) = static_cast(values)[ival]; break; } case DFNT_UCHAR8 : { *static_cast(value) = static_cast(values)[ival]; break; } case DFNT_INT8 : { *static_cast(value) = static_cast(values)[ival]; break; } case DFNT_UINT8 : { *static_cast(value) = static_cast(values)[ival]; break; } case DFNT_INT16 : { *static_cast(value) = static_cast(values)[ival]; break; } case DFNT_UINT16 : { *static_cast(value) = static_cast(values)[ival]; break; } case DFNT_INT32 : { *static_cast(value) = static_cast(values)[ival]; break; } case DFNT_UINT32 : { *static_cast(value) = static_cast(values)[ival]; break; } case DFNT_FLOAT32 : { *static_cast(value) = static_cast(values)[ival]; break; } case DFNT_FLOAT64 : { *static_cast(value) = static_cast(values)[ival]; break; } default: { std::cerr << __FILE__ << ": " << __PRETTY_FUNCTION__ << ": unexpected value type for attribute " << name << std::endl; std::abort(); // hopefully we should never arrive there unless HDF types are updated } } // switch (attr_type) } void Hdf_attr::get_values(void *values) const { PDEBUG; assert(values); switch (type) { case DFNT_CHAR8 : { for (int32 ival = 0 ; ival < nvalues ; ival++) static_cast(values)[ival] = static_cast(this->values)[ival]; break; } case DFNT_UCHAR8 : { for (int32 ival = 0 ; ival < nvalues ; ival++) static_cast(values)[ival] = static_cast(this->values)[ival]; break; } case DFNT_INT8 : { for (int32 ival = 0 ; ival < nvalues ; ival++) static_cast(values)[ival] = static_cast(this->values)[ival]; break; } case DFNT_UINT8 : { for (int32 ival = 0 ; ival < nvalues ; ival++) static_cast(values)[ival] = static_cast(this->values)[ival]; break; } case DFNT_INT16 : { for (int32 ival = 0 ; ival < nvalues ; ival++) static_cast(values)[ival] = static_cast(this->values)[ival]; break; } case DFNT_UINT16 : { for (int32 ival = 0 ; ival < nvalues ; ival++) static_cast(values)[ival] = static_cast(this->values)[ival]; break; } case DFNT_INT32 : { for (int32 ival = 0 ; ival < nvalues ; ival++) static_cast(values)[ival] = static_cast(this->values)[ival]; break; } case DFNT_UINT32 : { for (int32 ival = 0 ; ival < nvalues ; ival++) static_cast(values)[ival] = static_cast(this->values)[ival]; break; } case DFNT_FLOAT32 : { for (int32 ival = 0 ; ival < nvalues ; ival++) static_cast(values)[ival] = static_cast(this->values)[ival]; break; } case DFNT_FLOAT64 : { for (int32 ival = 0 ; ival < nvalues ; ival++) static_cast(values)[ival] = static_cast(this->values)[ival]; break; } default: { std::cerr << __FILE__ << ": " << __PRETTY_FUNCTION__ << ": unexpected value type for attribute " << name << std::endl; std::abort(); // hopefully we should never arrive there unless HDF types are updated } } // switch (attr_type) } std::string Hdf_attr::list_of_values() const { using namespace std; ostringstream s; int32 xchars_to_display = chars_to_display; int32 xnumbers_to_display = numbers_to_display; if (xchars_to_display <= 0) xchars_to_display = nvalues; if (xnumbers_to_display <= 0) xnumbers_to_display = nvalues; if (nvalues) { assert(values != NULL); switch (type) { case DFNT_CHAR8 : { if (nvalues <= xchars_to_display) for (int32 ival = 0 ; ival < nvalues ; ival++) s << static_cast(values)[ival]; else { for (int32 ival = 0 ; ival < xchars_to_display/2 ; ival++) s << static_cast(values)[ival]; s << " ... "; for (int32 ival = nvalues - xchars_to_display/2 ; ival < nvalues ; ival++) s << static_cast(values)[ival]; } break; } case DFNT_UCHAR8 : { if (nvalues <= xchars_to_display) for (int32 ival = 0 ; ival < nvalues ; ival++) s << static_cast(values)[ival]; else { for (int32 ival = 0 ; ival < xchars_to_display/2 ; ival++) s << static_cast(values)[ival]; s << " ... "; for (int32 ival = nvalues - xchars_to_display/2 ; ival < nvalues ; ival++) s << static_cast(values)[ival]; } break; } case DFNT_INT8 : { if (nvalues <= xnumbers_to_display) for (int32 ival = 0 ; ival < nvalues ; ival++) s << dec << static_cast(static_cast(values)[ival]) << " "; else { for (int32 ival = 0 ; ival < xnumbers_to_display/2 ; ival++) s << dec << static_cast(static_cast(values)[ival]) << " "; s << "... "; for (int32 ival = nvalues - xnumbers_to_display/2 ; ival < nvalues ; ival++) s << dec << static_cast(static_cast(values)[ival]) << " "; } break; } case DFNT_UINT8 : { if (nvalues <= xnumbers_to_display) for (int32 ival = 0 ; ival < nvalues ; ival++) s << dec << static_cast(static_cast(values)[ival]) << " "; else { for (int32 ival = 0 ; ival < xnumbers_to_display/2 ; ival++) s << dec << static_cast(static_cast(values)[ival]) << " "; s << "... "; for (int32 ival = nvalues - xnumbers_to_display/2 ; ival < nvalues ; ival++) s << dec << static_cast(static_cast(values)[ival]) << " "; } break; } case DFNT_INT16 : { if (nvalues <= xnumbers_to_display) for (int32 ival = 0 ; ival < nvalues ; ival++) s << dec << static_cast(static_cast(values)[ival]) << " "; else { for (int32 ival = 0 ; ival < xnumbers_to_display/2 ; ival++) s << dec << static_cast(static_cast(values)[ival]) << " "; s << "... "; for (int32 ival = nvalues - xnumbers_to_display/2 ; ival < nvalues ; ival++) s << dec << static_cast(static_cast(values)[ival]) << " "; } break; } case DFNT_UINT16 : { if (nvalues <= xnumbers_to_display) for (int32 ival = 0 ; ival < nvalues ; ival++) s << dec << static_cast(static_cast(values)[ival]) << " "; else { for (int32 ival = 0 ; ival < xnumbers_to_display/2 ; ival++) s << dec << static_cast(static_cast(values)[ival]) << " "; s << "... "; for (int32 ival = nvalues - xnumbers_to_display/2 ; ival < nvalues ; ival++) s << dec << static_cast(static_cast(values)[ival]) << " "; } break; } case DFNT_INT32 : { if (nvalues <= xnumbers_to_display) for (int32 ival = 0 ; ival < nvalues ; ival++) s << dec << static_cast(values)[ival] << " "; else { for (int32 ival = 0 ; ival < xnumbers_to_display/2 ; ival++) s << dec << static_cast(values)[ival] << " "; s << "... "; for (int32 ival = nvalues - xnumbers_to_display/2 ; ival < nvalues ; ival++) s << dec << static_cast(values)[ival] << " "; } break; } case DFNT_UINT32 : { if (nvalues <= xnumbers_to_display) for (int32 ival = 0 ; ival < nvalues ; ival++) s << dec << static_cast(values)[ival] << " "; else { for (int32 ival = 0 ; ival < xnumbers_to_display/2 ; ival++) s << dec << static_cast(values)[ival] << " "; s << "... "; for (int32 ival = nvalues - xnumbers_to_display/2 ; ival < nvalues ; ival++) s << dec << static_cast(values)[ival] << " "; } break; } case DFNT_FLOAT32 : { if (nvalues <= xnumbers_to_display) for (int32 ival = 0 ; ival < nvalues ; ival++) s << dec << static_cast(values)[ival] << " "; else { for (int32 ival = 0 ; ival < xnumbers_to_display/2 ; ival++) s << dec << static_cast(values)[ival] << " "; s << "... "; for (int32 ival = nvalues - xnumbers_to_display/2 ; ival < nvalues ; ival++) s << dec << static_cast(values)[ival] << " "; } break; } case DFNT_FLOAT64 : { if (nvalues <= xnumbers_to_display) for (int32 ival = 0 ; ival < nvalues ; ival++) s << dec << static_cast(values)[ival] << " "; else { for (int32 ival = 0 ; ival < xnumbers_to_display/2 ; ival++) s << dec << static_cast(values)[ival] << " "; s << "... "; for (int32 ival = nvalues - xnumbers_to_display/2 ; ival < nvalues ; ival++) s << dec << static_cast(values)[ival] << " "; } break; } default: { cerr << __FILE__ << ": " << __PRETTY_FUNCTION__ << ": unexpected value type for attribute " << name << endl; abort(); // hopefully we should never arrive there unless HDF types are updated } } // switch (attr_type) } // if (nvalues) return s.str(); } Hdf_attr::Hdf_attr( const int32 object_id, const int32 iattr, const char * attr_name, const int32 attr_type, const int32 attr_nvalues, attr_own owner, int32 vd_field_idx ) : obj_id(object_id), index(iattr), name(attr_name), type(attr_type), nvalues(attr_nvalues), values(NULL) { intn status= FAIL; PDEBUG; allocate_values(); if (values) { if (owner==SDS) { status = SDreadattr(obj_id, index, values); if (status==FAIL) { bad_sd_read_attr e(name); throw e; } } else if (owner==VDATA) { status = VSgetattr(object_id,vd_field_idx,iattr,values); if (status==FAIL) { bad_vs_get_attr e(name); throw e; } } else { cerr<<"In "<<__FILE__<<" at "<<__LINE__<<" : invalid (or untreated yet) attribute owner "<