std::string read_string_attr(H5::H5File &f, H5::Group &group, const char *name) { std::string strreadbuf (""); H5::Attribute attr = group.openAttribute(name); H5::StrType strdatatype(H5::PredType::C_S1, attr.getStorageSize()); attr.read(strdatatype, strreadbuf); return strreadbuf; }
/// Return the attribute name of obj, and "" if the attribute does not exist. inline std::string read_string_attribute (H5::H5Object const * obj, std::string name ) { std::string value =""; H5::Attribute attr; if (H5LTfind_attribute(obj -> getId(), name.c_str() )==0) return value;// not present // can not find how to get the size with hl. Using full interface //herr_t err2 = H5LTget_attribute_string(gr.getId(), x.c_str(), name.c_str() , &(buf.front()) ) ; //value.append( &(buf.front()) ); try { attr= obj->openAttribute(name.c_str());} catch (H5::AttributeIException) { return value;} try { H5::DataSpace dataspace = attr.getSpace(); int rank = dataspace.getSimpleExtentNdims(); if (rank != 0) TRIQS_RUNTIME_ERROR << "Reading a string attribute and got rank !=0"; size_t size = attr.getStorageSize(); H5::StrType strdatatype(H5::PredType::C_S1, size+1); std::vector<char> buf(size+1, 0x00); attr.read(strdatatype, (void *)(&buf[0])); value.append( &(buf.front()) ); } TRIQS_ARRAYS_H5_CATCH_EXCEPTION; return value; }