void read_1darray_vector_of_string_impl (group_or_file f, std::string const & name, ArrayVectorOfStringType & V) { if (!h5::exists(f, name)) TRIQS_RUNTIME_ERROR << "no such dataset : "<<name <<" in file "; try { DataSet ds = f->openDataSet( name.c_str() ); DataSpace dataspace = ds.getSpace(); mini_vector<hsize_t,1> dims_out; int ndims = dataspace.getSimpleExtentDims( &dims_out[0], NULL); if (ndims !=1) TRIQS_RUNTIME_ERROR << "triqs::h5 : Trying to read 1d array/vector . Rank mismatch : the array stored in the hdf5 file has rank = "<<ndims; size_t Len = dims_out[0]; V.resize(Len); size_t size = ds.getStorageSize(); StrType strdatatype(PredType::C_S1, size); std::vector<char> buf(Len*(size+1), 0x00); mini_vector<hsize_t,1> L; L[0]=V.size(); mini_vector<hsize_t,1> S; S[0]=1; auto d_space = dataspace_from_LS<1,false > (L,L,S); ds.read( (void *)(&buf[0]),strdatatype, d_space ); size_t i=0; for (auto & x : V) { x = ""; x.append(&buf[i*(size)]); ++i;} } TRIQS_ARRAYS_H5_CATCH_EXCEPTION; }
/** * \brief Write a string into an hdf5 file * \param f The h5 file or group of type H5::H5File or H5::Group * \param name The name of the hdf5 array in the file/group where the stack will be stored * \param value The string * \exception The HDF5 exceptions will be caught and rethrown as TRIQS_RUNTIME_ERROR (with a full stackstrace, cf triqs doc). */ inline void h5_write (h5::group_or_file f, std::string const & name, std::string const & value) { try { DataSet ds; StrType strdatatype(PredType::C_S1, value.size()); ds = f->createDataSet( name.c_str(), strdatatype, DataSpace() ); ds.write((void*)(value.c_str()), strdatatype ); } TRIQS_ARRAYS_H5_CATCH_EXCEPTION; }
inline void write_attribute ( DataSet & dataset, std::string name, std::string value ) { DataSpace attr_dataspace = DataSpace(H5S_SCALAR); // Create new string datatype for attribute StrType strdatatype(PredType::C_S1, value.size()); // Set up write buffer for attribute const H5std_string strwritebuf (value); // Create attribute and write to it Attribute myatt_in = dataset.createAttribute(name, strdatatype, attr_dataspace); myatt_in.write(strdatatype, strwritebuf); }
inline void write_string_attribute ( H5::H5Object const * obj, std::string name, std::string value ) { H5::DataSpace attr_dataspace = H5::DataSpace(H5S_SCALAR); // Create new string datatype for attribute H5::StrType strdatatype(H5::PredType::C_S1, value.size()); // Set up write buffer for attribute //const H5std_string strwritebuf (value); // Create attribute and write to it H5::Attribute myatt_in = obj->createAttribute(name.c_str(), strdatatype, attr_dataspace); //myatt_in.write(strdatatype, strwritebuf); myatt_in.write(strdatatype, (void *)(value.c_str())); }
/** * \brief Read a string from an hdf5 file * \param f The h5 file or group of type H5::H5File or H5::Group * \param name The name of the hdf5 array in the file/group where the stack will be stored * \param value The string to fill * \exception The HDF5 exceptions will be caught and rethrown as TRIQS_RUNTIME_ERROR (with a full stackstrace, cf triqs doc). */ inline void h5_read (h5::group_or_file f, std::string const & name, std::string & value) { if (!f.exists(name)) TRIQS_RUNTIME_ERROR << "no such dataset : "<<name <<" in file "; try { DataSet ds = f->openDataSet( name.c_str() ); DataSpace dataspace = ds.getSpace(); int rank = dataspace.getSimpleExtentNdims(); if (rank != 0) TRIQS_RUNTIME_ERROR << "Reading a string and got rank !=0"; size_t size = ds.getStorageSize(); StrType strdatatype(PredType::C_S1, size); std::vector<char> buf(size+1, 0x00); ds.read( (void *)(&buf[0]), strdatatype, DataSpace(), DataSpace() ); value = ""; value.append( &(buf.front()) ); } TRIQS_ARRAYS_H5_CATCH_EXCEPTION; }
void write_1darray_vector_of_string_impl (group_or_file f, std::string const & name, ArrayVectorOfStringType const & V) { size_t s=0; for (auto & x : V) s = std::max(s,x.size()); try { if (h5::exists(f, name)) f->unlink( name.c_str()); // put some option here ? DataSet ds; StrType strdatatype(PredType::C_S1, s); const size_t n = V.size(); std::vector<char> buf(n*s, 0x00); size_t i=0; for (auto & x : V) {strcpy( &buf[i*s], x.c_str()); ++i;} mini_vector<hsize_t,1> L; L[0]=V.size(); mini_vector<hsize_t,1> S; S[0]=1; auto d_space = dataspace_from_LS<1,false > (L,L,S); ds = f->createDataSet( name.c_str(), strdatatype, d_space ); ds.write( (void *)(&buf[0]),strdatatype, d_space ); } TRIQS_ARRAYS_H5_CATCH_EXCEPTION; }
/// 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; }