Import Geant4 10.6.0 source tree

This commit is contained in:
Gabriele Cosmo
2019-12-06 15:12:28 +01:00
parent b2a62ae692
commit 5baee230e9
2997 changed files with 141580 additions and 98673 deletions
@@ -106,6 +106,81 @@ inline bool write_array(hid_t a_loc,const std::string& a_name,
return true;
}
template <class T>
inline bool write_vlen(hid_t a_loc,const std::string& a_name,
hid_t a_file_type,hid_t a_mem_type,
unsigned int a_chunked,unsigned int a_compress,
unsigned int a_size,const T a_array[]) {
hid_t cpt = -1;
if(a_compress || a_chunked) {
cpt = ::H5Pcreate(H5P_DATASET_CREATE);
if(cpt<0) return false;
if(a_chunked) {
if(H5Pset_layout(cpt,H5D_CHUNKED)<0) {
::H5Pclose(cpt);
return false;
}
hsize_t cdims[1];
cdims[0] = a_chunked;
if(H5Pset_chunk(cpt,1,cdims)<0) {
::H5Pclose(cpt);
return false;
}
} else {
if(H5Pset_layout(cpt,H5D_COMPACT)<0) {
::H5Pclose(cpt);
return false;
}
}
if(a_compress) {
if(H5Pset_deflate(cpt,a_compress>9?9:a_compress)<0) {
::H5Pclose(cpt);
return false;
}
}
} else {
cpt = H5P_DEFAULT;
}
hid_t dataset = -1;
{hsize_t dims[1];
dims[0] = 1;
hid_t file_space = -1;
if(a_chunked) {
hsize_t mx_dims[1];
mx_dims[0] = H5S_UNLIMITED; //extendable.
file_space = ::H5Screate_simple(1,dims,mx_dims);
} else {
file_space = ::H5Screate_simple(1,dims,NULL);
}
if(file_space<0) {if(cpt>=0) ::H5Pclose(cpt);return false;}
hid_t file_type = ::H5Tvlen_create(a_file_type);
dataset = tools_H5Dcreate(a_loc,a_name.c_str(),file_type,file_space,cpt);
if(cpt>=0) ::H5Pclose(cpt);
::H5Sclose(file_space);
::H5Tclose(file_type);
if(dataset<0) return false;}
hid_t mem_type = ::H5Tvlen_create(a_mem_type);
hvl_t wdata[1];
wdata[0].len = a_size;
wdata[0].p = (void*)a_array;
if(H5Dwrite(dataset,mem_type,H5S_ALL,H5S_ALL,H5P_DEFAULT,wdata)<0) {
::H5Tclose(mem_type);
::H5Dclose(dataset);
return false;
}
::H5Tclose(mem_type);
::H5Dclose(dataset);
return true;
}
template <class T>
inline bool write_sub_array(hid_t a_loc,const std::string& a_name,
hid_t a_file_type,hid_t a_mem_type,
@@ -286,6 +361,66 @@ inline bool write_append_array_dataset(hid_t a_dataset,hid_t /*a_file_type*/,hid
return true;
}
template <class T>
inline bool write_append_vlen_dataset(hid_t a_dataset,hid_t /*a_file_type*/,hid_t a_mem_type,
unsigned int a_number,const T a_array[]) {
hsize_t old_size = 0;
{hid_t dataspace = H5Dget_space(a_dataset);
if(dataspace<0) return false;
hsize_t dims[1];
if(H5Sget_simple_extent_dims(dataspace,dims,NULL)<0) {
::H5Sclose(dataspace);
return false;
}
old_size = dims[0];
::H5Sclose(dataspace);}
{hsize_t exts[1];
exts[0] = old_size+1;
// if(H5Dextend(dataset,exts)<0) {
if(H5Dset_extent(a_dataset,exts)<0) return false;}
hid_t file_space = H5Dget_space(a_dataset);
if(file_space<0) return false;
{hsize_t offset[1];
offset[0] = old_size;
hsize_t count[1];
count[0] = 1;
if(H5Sselect_hyperslab(file_space,H5S_SELECT_SET,offset,NULL,count,NULL)<0) {
::H5Sclose(file_space);
return false;
}}
hsize_t dims[1];
dims[0] = 1;
hid_t mem_space = ::H5Screate_simple(1,dims,NULL);
if(mem_space<0) {
::H5Sclose(file_space);
return false;
}
hid_t mem_type = ::H5Tvlen_create(a_mem_type);
hvl_t wdata[1];
wdata[0].len = a_number;
wdata[0].p = (void*)a_array;
if(H5Dwrite(a_dataset,mem_type,mem_space,file_space,H5P_DEFAULT,wdata)<0) {
::H5Tclose(mem_type);
::H5Sclose(mem_space);
::H5Sclose(file_space);
return false;
}
::H5Tclose(mem_type);
::H5Sclose(mem_space);
::H5Sclose(file_space);
return true;
}
template <class T>
inline bool write_append_array(hid_t a_loc,const std::string& a_name,hid_t a_file_type,hid_t a_mem_type,
unsigned int a_number,const T a_array[]) {
@@ -443,6 +578,106 @@ inline bool read_array(hid_t a_loc,const std::string& a_name,hid_t a_mem_type,un
return true;
}
/*
template <class T>
inline bool read_vlen(hid_t a_loc,const std::string& a_name,hid_t a_mem_type,unsigned int& a_size,T*& a_array) {
hid_t dataset = tools_H5Dopen(a_loc,a_name.c_str());
if(dataset<0) {
a_size = 0;
a_array = 0;
return false; // data set not found.
}
hid_t file_space = H5Dget_space(dataset);
if(file_space<0) {
::H5Dclose(dataset);
a_size = 0;
a_array = 0;
return false;
}
{int dimn = H5Sget_simple_extent_ndims(file_space);
if(dimn<0) {
::H5Sclose(file_space);
::H5Dclose(dataset);
a_size = 0;
a_array = 0;
return false;
}
if(dimn!=1) {
::H5Sclose(file_space);
::H5Dclose(dataset);
a_size = 0;
a_array = 0;
return false;
}
//printf("debug : read dimn %d\n",dimn);
}
hsize_t dims[1];
{if(H5Sget_simple_extent_dims(file_space,dims,NULL)<0) {
::H5Sclose(file_space);
::H5Dclose(dataset);
a_size = 0;
a_array = 0;
return false;
}}
unsigned int _size = (unsigned int)dims[0];
if(_size!=1) {
::H5Sclose(file_space);
::H5Dclose(dataset);
a_size = 0;
a_array = 0;
return false;
}
hid_t mem_space = ::H5Screate_simple(1,dims,NULL);
if(mem_space<0) {
::H5Sclose(file_space);
::H5Dclose(dataset);
a_size = 0;
a_array = 0;
return false;
}
hid_t mem_type = ::H5Tvlen_create(a_mem_type);
hvl_t rdata[1];
if(H5Dread(dataset,mem_type,mem_space,file_space,H5P_DEFAULT,rdata)<0) {
//::H5Dvlen_reclaim(mem_type,mem_space,H5P_DEFAULT,rdata); ???
::H5Tclose(mem_type);
::H5Sclose(mem_space);
::H5Sclose(file_space);
::H5Dclose(dataset);
a_size = 0;
a_array = 0;
return false;
}
hsize_t len = rdata[0].len;
if(!len) {
//a_array = new T[1];
a_array = 0; //it is ok.
} else {
a_array = new T[len];
T* _data = (T*)rdata[0].p;
T* pos = a_array;
for(hsize_t index=0;index<len;index++,pos++,_data++) *pos = *_data;
}
a_size = len;
::H5Dvlen_reclaim(mem_type,mem_space,H5P_DEFAULT,rdata);
::H5Tclose(mem_type);
::H5Sclose(mem_space);
::H5Sclose(file_space);
::H5Dclose(dataset);
return true;
}
*/
template <class T>
inline bool read_sub_array(hid_t a_loc,const std::string& a_name,hid_t a_mem_type,
unsigned int a_offset,unsigned int a_number,
@@ -501,6 +736,14 @@ inline bool read_sub_array(hid_t a_loc,const std::string& a_name,hid_t a_mem_typ
// abcdef
// 012345
int remain = sz-a_offset;
if(remain<=0) {
::H5Sclose(file_space);
::H5Dclose(dataset);
a_size = 0;
a_array = 0;
return a_number?false:true;
}
int number = (int(a_number)<=remain) ? int(a_number) : remain;
if(number<=0) {
::H5Sclose(file_space);
@@ -551,6 +794,130 @@ inline bool read_sub_array(hid_t a_loc,const std::string& a_name,hid_t a_mem_typ
return true;
}
template <class T>
inline bool read_sub_vlen(hid_t a_loc,const std::string& a_name,hid_t a_mem_type,
unsigned int a_offset,
unsigned int& a_size,T*& a_array) {
hid_t dataset = tools_H5Dopen(a_loc,a_name.c_str());
if(dataset<0) {
a_size = 0;
a_array = 0;
return false; // data set not found.
}
hid_t file_space = H5Dget_space(dataset);
if(file_space<0) {
::H5Dclose(dataset);
a_size = 0;
a_array = 0;
return false;
}
{int dimn = H5Sget_simple_extent_ndims(file_space);
if(dimn<0) {
::H5Sclose(file_space);
::H5Dclose(dataset);
a_size = 0;
a_array = 0;
return false;
}
if(dimn!=1) {
::H5Sclose(file_space);
::H5Dclose(dataset);
a_size = 0;
a_array = 0;
return false;
}
//printf("debug : read dimn %d\n",dimn);
}
hsize_t dims[1];
{if(H5Sget_simple_extent_dims(file_space,dims,NULL)<0) {
::H5Sclose(file_space);
::H5Dclose(dataset);
a_size = 0;
a_array = 0;
return false;
}}
unsigned int sz = (unsigned int)dims[0];
if(!sz) {
::H5Sclose(file_space);
::H5Dclose(dataset);
a_size = 0;
a_array = 0;
return true; //It is ok.
}
// abcdef
// 012345
int remain = sz-a_offset;
if(remain<=0) {
::H5Sclose(file_space);
::H5Dclose(dataset);
a_size = 0;
a_array = 0;
return false;
}
{hsize_t offset[1];
offset[0] = a_offset;
hsize_t count[1];
count[0] = 1;
if(H5Sselect_hyperslab(file_space,H5S_SELECT_SET,offset,NULL,count,NULL)<0) {
::H5Sclose(file_space);
::H5Dclose(dataset);
a_size = 0;
a_array = 0;
return false;
}}
dims[0] = 1;
hid_t mem_space = ::H5Screate_simple(1,dims,NULL);
if(mem_space<0) {
::H5Sclose(file_space);
::H5Dclose(dataset);
a_size = 0;
a_array = 0;
return false;
}
hid_t mem_type = ::H5Tvlen_create(a_mem_type);
hvl_t rdata[1];
if(H5Dread(dataset,mem_type,mem_space,file_space,H5P_DEFAULT,rdata)<0) {
//::H5Dvlen_reclaim(mem_type,mem_space,H5P_DEFAULT,rdata); ???
::H5Tclose(mem_type);
::H5Sclose(mem_space);
::H5Sclose(file_space);
::H5Dclose(dataset);
a_size = 0;
a_array = 0;
return false;
}
hsize_t len = rdata[0].len;
if(!len) {
//a_array = new T[1];
a_array = 0; //it is ok.
} else {
a_array = new T[len];
T* _data = (T*)rdata[0].p;
T* pos = a_array;
for(hsize_t index=0;index<len;index++,pos++,_data++) *pos = *_data;
}
a_size = len;
::H5Dvlen_reclaim(mem_type,mem_space,H5P_DEFAULT,rdata);
::H5Tclose(mem_type);
::H5Sclose(mem_space);
::H5Sclose(file_space);
::H5Dclose(dataset);
return true;
}
template <class TYPE>
inline bool read_scalar(hid_t a_loc,const std::string& a_name,TYPE& aValue) {
return read_scalar<TYPE>(a_loc,a_name,to_T_mem_type(TYPE()),aValue);
@@ -576,7 +943,7 @@ inline bool read_std_vec_vec(hid_t a_loc,const std::string& a_name,hid_t a_mem_t
a_vec_vec.resize((size_t)sz);
std::string scount;
for(size_t count=0;count<(size_t)sz;count++) {
tools::num2s(count,scount);
tools::num2s(tools::uint64(count),scount);
if(!read_std_vec<TYPE>(a_loc,a_name+"_elem_"+scount,a_mem_type,a_vec_vec[count])) {a_vec_vec.clear();return false;}
}
return true;
@@ -784,6 +1151,14 @@ inline bool write_array(hid_t a_loc,const std::string& a_name,
a_chunked,a_compress,a_size,a_array);
}
template <class TYPE>
inline bool write_vlen(hid_t a_loc,const std::string& a_name,
unsigned int a_size,const TYPE a_array[],
unsigned int a_chunked = 0,unsigned int a_compress = 0) {
return hdf5::write_vlen<TYPE>(a_loc,a_name,to_T_file_type(TYPE()),to_T_mem_type(TYPE()),
a_chunked,a_compress,a_size,a_array);
}
template <class T>
inline bool write_std_vec(hid_t a_loc,const std::string& a_name,
hid_t a_file_type,hid_t a_mem_type,
@@ -805,7 +1180,7 @@ template <class TYPE>
inline bool write_std_vec_vec(hid_t a_loc,const std::string& a_name,const std::vector< std::vector<TYPE> >& a_vec_vec,
unsigned int /*a_chunked*/ = 0,unsigned int /*a_compress*/ = 0) {
if(!write_scalar<tools::uint64>(a_loc,a_name+"_size",a_vec_vec.size())) return false;
size_t count = 0;
unsigned int count = 0; //uint for num2s.
std::string scount;
tools_typename_vforcit(std::vector<TYPE>,a_vec_vec,it) {
tools::num2s(count,scount);
@@ -819,7 +1194,7 @@ inline bool write_std_vec_vec(hid_t a_loc,const std::string& a_name,const std::v
//inline bool write_std_map(hid_t a_loc,const std::string& a_name,const std::map<TKEY,TVALUE>& a_map,
// unsigned int a_chunked = 0,unsigned int a_compress = 0) {
// if(!write_scalar<tools::uint64>(a_loc,a_name+"_size",a_map.size())) return false;
// size_t count = 0;
// unsigned int count = 0; //uint for num2s.
// std::string scount;
// inlib_typename_mforcit(TKEY,TVALUE,a_map,it) {
// tools::num2s(count,scount);
@@ -844,6 +1219,11 @@ inline bool write_append_array_dataset(hid_t a_dataset,unsigned int a_number,con
return hdf5::write_append_array_dataset<TYPE>(a_dataset,to_T_file_type(TYPE()),to_T_mem_type(TYPE()),a_number,a_array);
}
template <class TYPE>
inline bool write_append_vlen_dataset(hid_t a_dataset,unsigned int a_number,const TYPE a_array[]) {
return hdf5::write_append_vlen_dataset<TYPE>(a_dataset,to_T_file_type(TYPE()),to_T_mem_type(TYPE()),a_number,a_array);
}
template <class TYPE>
inline bool write_append_array(hid_t a_loc,const std::string& a_name,unsigned int a_number,const TYPE a_array[]) {
return hdf5::write_append_array<TYPE>(a_loc,a_name,to_T_file_type(TYPE()),to_T_mem_type(TYPE()),a_number,a_array);
@@ -854,6 +1234,11 @@ inline bool read_array(hid_t a_loc,const std::string& a_name,unsigned int& a_siz
return read_array<TYPE>(a_loc,a_name,to_T_mem_type(TYPE()),a_size,a_array);
}
//template <class TYPE>
//inline bool read_vlen(hid_t a_loc,const std::string& a_name,unsigned int& a_size,TYPE*& a_array) {
// return read_vlen<TYPE>(a_loc,a_name,to_T_mem_type(TYPE()),a_size,a_array);
//}
template <class TYPE>
inline bool read_std_vec(hid_t a_loc,const std::string& a_name,std::vector<TYPE>& a_vec) {
return read_std_vec<TYPE>(a_loc,a_name,to_T_mem_type(TYPE()),a_vec);
@@ -870,6 +1255,12 @@ inline bool read_sub_array(hid_t a_loc,const std::string& a_name,unsigned int a_
return read_sub_array<TYPE>(a_loc,a_name,to_T_mem_type(TYPE()),a_offset,a_number,a_size,a_array);
}
template <class TYPE>
inline bool read_sub_vlen(hid_t a_loc,const std::string& a_name,unsigned int a_offset,
unsigned int& a_size,TYPE*& a_array) {
return read_sub_vlen<TYPE>(a_loc,a_name,to_T_mem_type(TYPE()),a_offset,a_size,a_array);
}
inline bool read_bool(hid_t a_loc,const std::string& a_name,bool& aValue) {
unsigned char value = 0;
if(!read_scalar<unsigned char>(a_loc,a_name,H5T_NATIVE_UCHAR,value)) {
@@ -901,13 +1292,13 @@ inline bool write_scalar_atb(hid_t aDS,const std::string& a_name,const TYPE& aDa
hid_t scalar = ::H5Screate(H5S_SCALAR);
if(scalar<0) return false;
hid_t aid = tools_H5Acreate(aDS,a_name.c_str(),H5T_STD_I32LE,scalar,H5P_DEFAULT);
hid_t aid = tools_H5Acreate(aDS,a_name.c_str(),to_T_file_type(TYPE()),scalar,H5P_DEFAULT);
if(aid<0) {
::H5Sclose(scalar);
return false;
}
if(H5Awrite(aid,H5T_NATIVE_INT,&aData)<0) {
if(H5Awrite(aid,to_T_mem_type(TYPE()),&aData)<0) {
::H5Sclose(scalar);
::H5Aclose(aid);
return false;
@@ -0,0 +1,49 @@
// Copyright (C) 2010, Guy Barrand. All rights reserved.
// See the file tools.license for terms.
#ifndef tools_hdf5_group_exists
#define tools_hdf5_group_exists
#include "hdf5_h"
#include <string>
namespace tools {
namespace hdf5 {
class group_exists_struct {
public:
group_exists_struct(const std::string& a_what):m_what(a_what),m_found(false){}
virtual ~group_exists_struct() {}
protected:
group_exists_struct(const group_exists_struct& a_from):m_what(a_from.m_what),m_found(a_from.m_found){}
group_exists_struct& operator=(const group_exists_struct&){return *this;}
public:
std::string m_what;
bool m_found;
};
inline herr_t group_exists_visit(hid_t a_id,const char* a_name,void* a_tag){ //a_id = parent of a_name object.
group_exists_struct* visitor = (group_exists_struct*)a_tag;
H5G_stat_t statbuf;
::H5Gget_objinfo(a_id,a_name,0,&statbuf);
switch(statbuf.type) {
case H5G_GROUP:{
if(a_name==visitor->m_what) {visitor->m_found=true;return 0;} //found
//::H5Giterate(a_id,a_name,NULL,group_exists_visit,visitor);
}break;
default:
break;
}
return 0;
}
inline bool group_exists(hid_t a_file_id,const std::string& a_name) {
group_exists_struct visitor(a_name);
::H5Giterate(a_file_id,".",NULL,group_exists_visit,&visitor);
return visitor.m_found;
}
}}
#endif
@@ -24,7 +24,7 @@ namespace hdf5 {
inline bool write_std_map_ss(hid_t a_loc,const std::string& a_name,const std::map<std::string,std::string>& a_map,
unsigned int /*a_chunked*/ = 0,unsigned int /*a_compress*/ = 0) {
if(!write_scalar<tools::uint64>(a_loc,a_name+"_size",a_map.size())) return false;
size_t count = 0;
unsigned int count = 0; //uint for num2s.
std::string scount;
tools_mforcit(std::string,std::string,a_map,it) {
tools::num2s(count,scount);
@@ -205,9 +205,11 @@ inline bool read_hdata(hid_t a_loc,histo_data_t& a_hdata) {
}
template <class HISTO>
inline bool read_histo(std::ostream& a_out,hid_t a_loc,const std::string& a_name,HISTO*& a_histo) {
inline bool read_histo(std::ostream& a_out,hid_t a_loc,const std::string& a_name,HISTO*& a_histo,bool a_verb_class = true) {
a_histo = 0;
//if(::H5Gget_objinfo(a_loc,a_name.c_str(),0,NULL)<0) {return false;}
hid_t histo = tools_H5Gopen(a_loc,a_name.c_str());
if(histo<0) {
a_out << "tools::hdf5::read_histo : can't open group." << std::endl;
@@ -222,7 +224,10 @@ inline bool read_histo(std::ostream& a_out,hid_t a_loc,const std::string& a_name
}
if(sclass!=HISTO::s_class()) {
a_out << "tools::hdf5::read_histo : unknown class : " << sclass << std::endl;
if(a_verb_class) {
a_out << "tools::hdf5::read_histo :"
<< " read class " << tools::sout(sclass) << " not " << tools::sout(HISTO::s_class()) << std::endl;
}
::H5Gclose(histo);
return false;
}
@@ -249,7 +254,7 @@ inline bool read_histo(std::ostream& a_out,hid_t a_loc,const std::string& a_name
}
template <class PROFILE>
inline bool read_profile(std::ostream& a_out,hid_t a_loc,const std::string& a_name,PROFILE*& a_histo) {
inline bool read_profile(std::ostream& a_out,hid_t a_loc,const std::string& a_name,PROFILE*& a_histo,bool a_verb_class = true) {
a_histo = 0;
hid_t histo = tools_H5Gopen(a_loc,a_name.c_str());
@@ -266,7 +271,10 @@ inline bool read_profile(std::ostream& a_out,hid_t a_loc,const std::string& a_na
}
if(sclass!=PROFILE::s_class()) {
a_out << "tools::hdf5::read_profile : unknown class : " << sclass << std::endl;
if(a_verb_class) {
a_out << "tools::hdf5::read_profile :"
<< " read class " << tools::sout(sclass) << " not " << tools::sout(PROFILE::s_class()) << std::endl;
}
::H5Gclose(histo);
return false;
}
@@ -298,6 +306,36 @@ inline bool read_profile(std::ostream& a_out,hid_t a_loc,const std::string& a_na
return true;
}
inline bool read_class_version(std::ostream& a_out,hid_t a_loc,const std::string& a_name,
std::string& a_class,int& a_version,bool a_verbose = true) {
hid_t id = tools_H5Gopen(a_loc,a_name.c_str());
if(id<0) {
if(a_verbose) a_out << "tools::hdf5::read_class_version : can't open group." << std::endl;
a_class.clear();
a_version = 0;
return false;
}
if(!read_atb(id,"class",a_class)) {
if(a_verbose) a_out << "tools::hdf5::read_class_version : can't read_atb() class." << std::endl;
::H5Gclose(id);
a_class.clear();
a_version = 0;
return false;
}
if(!read_atb(id,"version",a_version)) {
if(a_verbose) a_out << "tools::hdf5::read_class_version : read_atb version failed." << std::endl;
::H5Gclose(id);
a_class.clear();
a_version = 0;
return false;
}
::H5Gclose(id);
return true;
}
}}
+179 -14
View File
@@ -139,12 +139,15 @@ public:
m_basket = new T[m_basket_size];
if(m_write) {
} else { //read.
size_t n = tools::mn<size_t>((size_t)m_branch.entries(),m_basket_size);
if(!m_branch.read_page<T>(n,m_basket)) {
m_store.out() << "tools::hdf5::ntuple::column_ref:column_ref : read_page() failed." << std::endl;
m_basket_pos = 0;
m_basket_end = 0;
return;
tools::uint64 _entries = m_branch.entries();
size_t n = tools::mn<size_t>((size_t)_entries,m_basket_size);
if(_entries) {
if(!m_branch.read_page<T>(n,m_basket)) {
m_store.out() << "tools::hdf5::ntuple::column_ref:column_ref : read_page() failed." << std::endl;
m_basket_pos = 0;
m_basket_end = 0;
return;
}
}
m_basket_pos = 0;
m_basket_end = n;
@@ -262,7 +265,93 @@ public:
TOOLS_CLASS_STRING_VALUE(vector_ushort,vector<tools::ushort>)
TOOLS_CLASS_STRING_VALUE(vector_uint32,vector<tools::uint32>)
TOOLS_CLASS_STRING_VALUE(vector_uint64,vector<tools::uint64>)
//
#define TOOLS_HDF5_NTUPLE_VLEN
//
#define TOOLS_HDF5_NTUPLE_STRING
#ifdef TOOLS_HDF5_NTUPLE_VLEN
template <class T>
class std_vector_column_ref : public virtual icol {
#ifdef TOOLS_MEM
static const std::string& s_class() {
static const std::string s_v("tools::hdf5::ntuple::std_vector_column_ref<"+tools::stype(T())+">");
return s_v;
}
#endif
public:
static tools::cid id_class() {return tools::_cid_std_vector<T>()+10000;}
virtual void* cast(tools::cid a_class) const {
if(void* p = tools::cmp_cast<std_vector_column_ref>(this,a_class)) {return p;}
else return 0;
}
virtual tools::cid id_cls() const {return id_class();}
public: //icol
virtual bool add() { //write.
if(!m_write) return false;
const T* _data = tools::vec_data(m_ref);
return m_branch.write_vlen<T>(m_ref.size(),_data);
}
virtual bool fetch_entry() { //read.
if(m_write) return false;
size_t n;
T* _data;
if(!m_branch.read_vlen<T>(n,_data)) {
m_store.out() << "tools::hdf5::ntuple::std_vector_column_ref:fetch_entry : read_page() failed." << std::endl;
return false;
}
m_ref.resize(n);
T* dpos = _data;
T* pos = tools::vec_data(m_ref);
for(size_t index=0;index<n;index++,pos++,dpos++) *pos = *dpos;
delete [] _data;
return true;
}
virtual void reset() {m_branch.reset_pos();}
virtual const std::string& name() const {return m_name;}
virtual bool set_basket_size(size_t) {return true;}
public:
std_vector_column_ref(store& a_store,pages& a_pages,bool a_write,const std::string& a_name,
size_t /*a_basket_size*/,
std::vector<T>& a_ref)
:m_store(a_store)
,m_branch(a_pages)
,m_write(a_write)
,m_name(a_name)
,m_ref(a_ref)
{
#ifdef TOOLS_MEM
tools::mem::increment(s_class().c_str());
#endif
}
virtual ~std_vector_column_ref(){
#ifdef TOOLS_MEM
tools::mem::decrement(s_class().c_str());
#endif
}
protected:
std_vector_column_ref(const std_vector_column_ref& a_from)
:icol(a_from)
,m_store(a_from.m_store)
,m_branch(a_from.m_branch)
,m_write(a_from.m_write)
,m_name(a_from.m_name)
,m_ref(a_from.m_ref)
{}
std_vector_column_ref& operator=(const std_vector_column_ref& a_from){
if(&a_from==this) return *this;
m_name = a_from.m_name;
return *this;
}
protected:
store& m_store;
pages& m_branch;
bool m_write;
std::string m_name;
std::vector<T>& m_ref;
};
#else //!TOOLS_HDF5_NTUPLE_VLEN
template <class T>
class std_vector_column_ref : public column<unsigned int> {
typedef column<unsigned int> parent;
@@ -288,7 +377,7 @@ public:
if(!m_write) return false;
if(m_ref.size()) {
const T* _data = tools::vec_data(m_ref);
if(!m_data_pages.write_page<T>(m_ref.size(),(void*)_data)) return false;
if(!m_data_pages.write_page<T>(m_ref.size(),_data)) return false;
}
if(!parent::fill((unsigned int)m_ref.size())) return false;
if(!parent::add()) return false;
@@ -351,7 +440,8 @@ public:
std::vector<T>& m_ref;
pages m_data_pages;
};
#endif //TOOLS_HDF5_NTUPLE_VLEN
template <class T>
class std_vector_column : public std_vector_column_ref<T> {
typedef std_vector_column_ref<T> parent;
@@ -392,6 +482,78 @@ public:
std::vector<T> m_tmp;
};
#ifdef TOOLS_HDF5_NTUPLE_STRING
class column_string_ref : public virtual icol {
#ifdef TOOLS_MEM
TOOLS_SCLASS(tools::hdf5::ntuple::column_string_ref)
#endif
public:
static tools::cid id_class() {
static const std::string s_v;
return tools::_cid(s_v)+10000;
}
virtual void* cast(tools::cid a_class) const {
if(void* p = tools::cmp_cast<column_string_ref>(this,a_class)) {return p;}
else return 0;
}
virtual tools::cid id_cls() const {return id_class();}
public: //icol
virtual bool add() { //write.
if(!m_write) return false;
return m_branch.write_string(m_ref);
}
virtual bool fetch_entry() { //read.
if(m_write) return false;
if(!m_branch.read_string(m_ref)) {
m_store.out() << "tools::hdf5::ntuple::column_string_ref:fetch_entry : read_page() failed." << std::endl;
return false;
}
return true;
}
virtual void reset() {m_branch.reset_pos();}
virtual const std::string& name() const {return m_name;}
virtual bool set_basket_size(size_t) {return true;}
public:
column_string_ref(store& a_store,pages& a_pages,bool a_write,const std::string& a_name,
size_t /*a_basket_size*/,
std::string& a_ref)
:m_store(a_store)
,m_branch(a_pages)
,m_write(a_write)
,m_name(a_name)
,m_ref(a_ref)
{
#ifdef TOOLS_MEM
tools::mem::increment(s_class().c_str());
#endif
}
virtual ~column_string_ref(){
#ifdef TOOLS_MEM
tools::mem::decrement(s_class().c_str());
#endif
}
protected:
column_string_ref(const column_string_ref& a_from)
:icol(a_from)
,m_store(a_from.m_store)
,m_branch(a_from.m_branch)
,m_write(a_from.m_write)
,m_name(a_from.m_name)
,m_ref(a_from.m_ref)
{}
column_string_ref& operator=(const column_string_ref& a_from){
if(&a_from==this) return *this;
m_name = a_from.m_name;
return *this;
}
protected:
store& m_store;
pages& m_branch;
bool m_write;
std::string m_name;
std::string& m_ref;
};
#else //!TOOLS_HDF5_NTUPLE_STRING
class column_string_ref : public column<unsigned int> {
typedef column<unsigned int> parent;
#ifdef TOOLS_MEM
@@ -416,7 +578,7 @@ public:
if(!m_write) return false;
if(m_ref.size()) {
const char* _data = m_ref.c_str();
if(!m_data_pages.write_page<char>(m_ref.size(),(void*)_data)) return false;
if(!m_data_pages.write_page<char>(m_ref.size(),_data)) return false;
}
if(!parent::fill((unsigned int)m_ref.size())) return false;
if(!parent::add()) return false;
@@ -479,6 +641,7 @@ public:
std::string& m_ref;
pages m_data_pages;
};
#endif //TOOLS_HDF5_NTUPLE_STRING
class column_string : public column_string_ref {
typedef column_string_ref parent;
@@ -813,15 +976,17 @@ public:
return true;
}
const std::string& title() const {return m_title;}
const std::vector<icol*>& columns() const {return m_cols;}
std::vector<icol*>& columns() {return m_cols;}
template <class T>
column_ref<T>* create_column_ref(const std::string& a_name,size_t a_basket_size,T& a_user_var) {
column_ref<T>* create_column_ref(const std::string& a_name,size_t a_basket_size,T& a_ref) {
if(tools::find_named<icol>(m_cols,a_name)) return 0;
pages* _pages = create_pages(a_name,tools::stype(T()));
if(!_pages) return 0;
column_ref<T>* col = new column_ref<T>(*this,*_pages,true,a_name,a_basket_size,a_user_var);
column_ref<T>* col = new column_ref<T>(*this,*_pages,true,a_name,a_basket_size,a_ref);
if(!col) return 0;
m_cols.push_back(col);
return col;
@@ -849,12 +1014,12 @@ public:
}
template <class T>
std_vector_column_ref<T>* create_std_column_ref(const std::string& a_name,size_t a_basket_size,std::vector<T>& a_user_vec) {
std_vector_column_ref<T>* create_std_column_ref(const std::string& a_name,size_t a_basket_size,std::vector<T>& a_ref) {
//NOTE : to optimize, we do not handle a default std::vector value logic.
if(tools::find_named<icol>(m_cols,a_name)) return 0;
pages* _pages = create_pages(a_name,"vector<"+tools::stype(T())+">");
if(!_pages) return 0;
std_vector_column_ref<T>* col = new std_vector_column_ref<T>(*this,*_pages,true,a_name,a_basket_size,a_user_vec);
std_vector_column_ref<T>* col = new std_vector_column_ref<T>(*this,*_pages,true,a_name,a_basket_size,a_ref);
if(!col) return 0;
m_cols.push_back(col);
return col;
@@ -102,9 +102,9 @@ public:
void reset_pos() {m_pos = 0;}
template <class TYPE>
bool write_page(size_t a_size,void* a_array) {
bool write_page(size_t a_size,const TYPE* a_array) {
if(!m_pos) {
if(!write_array<TYPE>(m_group,s_pages(),a_size,(TYPE*)a_array,a_size,m_compress)) {
if(!write_array<TYPE>(m_group,s_pages(),a_size,a_array,a_size?a_size:32,m_compress)) {
m_out << "pages::write_page : write_array<TYPE>() failed. Pos " << m_pos << std::endl;
return false;
}
@@ -114,8 +114,8 @@ public:
return false;
}
} else {
if(!write_append_array_dataset<TYPE>(m_dataset,a_size,(TYPE*)a_array)) {
m_out << "pages::write_page : write_append_array<TYPE>() failed. Pos " << m_pos << std::endl;
if(!write_append_array_dataset<TYPE>(m_dataset,a_size,a_array)) {
m_out << "pages::write_page : write_append_array_dataset<TYPE>() failed. Pos " << m_pos << std::endl;
return false;
}
}
@@ -125,7 +125,7 @@ public:
}
template <class TYPE>
bool read_page(size_t a_size,void* a_array) {
bool read_page(size_t a_size,TYPE* a_array) {
//it is assumed that a_array can contain a_size*sizeof(TYPE) bytes.
unsigned int _size = a_size;
unsigned int n = 0;
@@ -151,6 +151,91 @@ public:
return true;
}
template <class TYPE>
bool write_vlen(size_t a_size,const TYPE* a_array) {
if(!m_pos) {
if(!hdf5::write_vlen<TYPE>(m_group,s_pages(),a_size,a_array,a_size?a_size:32,m_compress)) {
m_out << "pages::write_vlen : write_vlen<TYPE>() failed. Pos " << m_pos << std::endl;
return false;
}
m_dataset = tools_H5Dopen(m_group,s_pages().c_str());
if(m_dataset<0) {
m_out << "pages::write_vlen : H5Dopen failed. Pos " << m_pos << std::endl;
return false;
}
} else {
if(!write_append_vlen_dataset<TYPE>(m_dataset,a_size,a_array)) {
m_out << "pages::write_vlen : write_append_vlen_dataset<TYPE>() failed. Pos " << m_pos << std::endl;
return false;
}
}
m_pos++;
m_entries++;
return true;
}
template <class TYPE>
bool read_vlen(size_t& a_size,TYPE*& a_array) {
//it is assumed that a_array can contain a_size*sizeof(TYPE) bytes.
unsigned int sz;
if(!read_sub_vlen<TYPE>(m_group,s_pages(),(unsigned int)m_pos,sz,a_array)) {
m_out << "pages::read_vlen : read_sub_vlen<TYPE>() failed." << std::endl;
a_size = 0;
a_array = 0;
return false;
}
a_size = sz;
m_pos++;
m_entries++;
return true;
}
bool write_string(const std::string& a_string) {
if(!m_pos) {
if(!hdf5::write_string_dataset(m_group,s_pages(),a_string,128,m_compress)) { //32=>enforce extendable.
m_out << "pages::write_string : hdf5::write_string() failed. Pos " << m_pos << std::endl;
return false;
}
m_dataset = tools_H5Dopen(m_group,s_pages().c_str());
if(m_dataset<0) {
m_out << "pages::write_string : H5Dopen failed. Pos " << m_pos << std::endl;
return false;
}
} else {
if(!write_append_string_dataset(m_dataset,a_string)) {
m_out << "pages::write_string : write_append_string_dataset() failed. Pos " << m_pos << std::endl;
return false;
}
}
m_pos++;
m_entries++;
return true;
}
bool read_string(std::string& a_string) {
if(!read_sub_string(m_group,s_pages(),(unsigned int)m_pos,a_string)) {
m_out << "pages::read_string : read_sub_string() failed." << std::endl;
a_string.clear();
return false;
}
m_pos++;
m_entries++;
return true;
}
/*
bool write_string(const std::string& a_string) {return write_vlen<char>(a_string.size(),a_string.c_str());}
bool read_string(std::string& a_string) {
size_t sz;char* _data;
if(!read_vlen<char>(sz,_data)) {a_string.clear();return false;}
a_string.resize(sz);
for(size_t index=0;index<sz;index++) a_string[index] = _data[index];
delete [] _data;
return true;
}
*/
/*
bool write_strings(size_t a_number,void* a_array) {
size_t sz = m_size*a_number;
@@ -191,7 +276,7 @@ public:
return true;
}
bool read_strings(size_t a_size,void* a_array) {
bool read_strings(size_t a_size,char* a_array) {
unsigned int _size = a_size*m_size;
unsigned int n = 0;
char* array = 0;
@@ -37,13 +37,19 @@ public:
m_group = -1;
return;
}
if(!write_atb(m_group,"type","object")) {
m_out << "tools::hdf5::store::store : write_atb(type) failed." << std::endl;
::H5Gclose(m_group);
m_group = -1;
return;
}
if(!write_atb(m_group,"class",s_class())) {
m_out << "tools::hdf5::store::store : write_atb(class) failed." << std::endl;
::H5Gclose(m_group);
m_group = -1;
return;
}
int v = 1;
int v = 2; //1->2 add "type" atb.
if(!write_scalar_atb<int>(m_group,"version",v)) {
m_out << "tools::hdf5::store::store : write_scalar_atb(version) failed." << std::endl;
::H5Gclose(m_group);
@@ -99,29 +105,35 @@ public:
if(!entries(_entries)) {
m_out << "tools::hdf5::store::~store : not same entries on all columns. Write 0." << std::endl;
}
if(!write_scalar<tools::uint64>(m_group,s_entries(),_entries)) {
m_out << "tools::hdf5::store::~store : write_scalar(entries) failed." << std::endl;
if(m_group<0) { //constructor may have failed.
} else {
if(!write_scalar<tools::uint64>(m_group,s_entries(),_entries)) {
m_out << "tools::hdf5::store::~store : write_scalar(entries) failed." << std::endl;
}
if(!write_scalar<unsigned int>(m_group,s_columns(),m_pagess.size())) {
m_out << "tools::hdf5::store::~store : write_scalar(columns) failed." << std::endl;
}
{std::vector<std::string> names;
tools_vforcit(pages*,m_pagess,it) names.push_back((*it)->name());
//{m_out << "debug : write : names :" << std::endl;
// tools_vforcit(std::string,names,it) m_out << *it << std::endl;}
if(!write_array_string(m_group,s_names(),names)) {
m_out << "tools::hdf5::store::~store : write_array_string(names) failed." << std::endl;
}}
{std::vector<std::string> TFORMs;
tools_vforcit(pages*,m_pagess,it) TFORMs.push_back((*it)->form());
//{m_out << "debug : write : TFORMs :" << std::endl;
// tools_vforcit(std::string,TFORMs,it) m_out << *it << std::endl;}
if(!write_array_string(m_group,s_forms(),TFORMs)) {
m_out << "tools::hdf5::store::~store : write_array_string(tforms) failed." << std::endl;
}}
}
if(!write_scalar<unsigned int>(m_group,s_columns(),m_pagess.size())) {
m_out << "tools::hdf5::store::~store : write_scalar(columns) failed." << std::endl;
}
{std::vector<std::string> names;
tools_vforcit(pages*,m_pagess,it) names.push_back((*it)->name());
//{m_out << "debug : write : names :" << std::endl;
// tools_vforcit(std::string,names,it) m_out << *it << std::endl;}
if(!write_array_string(m_group,s_names(),names)) {
m_out << "tools::hdf5::store::~store : write_array_string(names) failed." << std::endl;
}}
{std::vector<std::string> TFORMs;
tools_vforcit(pages*,m_pagess,it) TFORMs.push_back((*it)->form());
//{m_out << "debug : write : TFORMs :" << std::endl;
// tools_vforcit(std::string,TFORMs,it) m_out << *it << std::endl;}
if(!write_array_string(m_group,s_forms(),TFORMs)) {
m_out << "tools::hdf5::store::~store : write_array_string(tforms) failed." << std::endl;
}}
}
tools::safe_clear(m_pagess);
::H5Gclose(m_group);
if(m_group<0) { //constructor may have failed.
} else {
::H5Gclose(m_group);
}
#ifdef TOOLS_MEM
tools::mem::decrement(s_class().c_str());
#endif
+421 -122
View File
@@ -41,6 +41,20 @@ inline hid_t string_datatype(size_t aSize){
return datatype;
}
inline hid_t str_datatype() {
hid_t datatype = ::H5Tcopy(H5T_C_S1);
if(datatype<0) return failure();
if(::H5Tset_size(datatype,H5T_VARIABLE)<0) {
::H5Tclose(datatype);
return failure();
}
if(::H5Tset_strpad(datatype,H5T_STR_NULLTERM)<0) {
::H5Tclose(datatype);
return failure();
}
return datatype;
}
inline hid_t basic_mem_type(hid_t a_file_type){
H5T_class_t mclass = H5Tget_class(a_file_type);
size_t msize = H5Tget_size(a_file_type);
@@ -98,8 +112,8 @@ inline hid_t compound_mem_type(hid_t a_file_type){
size_t sz = H5Tget_size(a_file_type);
//printf("debug : compound_mem_type : sz %lu\n",sz);
hid_t memtype = ::H5Tcreate(H5T_COMPOUND,sz);
if(memtype<0) return failure();
hid_t mem_type = ::H5Tcreate(H5T_COMPOUND,sz);
if(mem_type<0) return failure();
//FIXME : WARNING : is order the booked order ?
@@ -116,21 +130,21 @@ inline hid_t compound_mem_type(hid_t a_file_type){
if( (mclass==H5T_INTEGER) ||
(mclass==H5T_STRING) ||
(mclass==H5T_FLOAT) ) {
hid_t mmemtype = basic_mem_type(mtype);
if(mmemtype<0) {
hid_t mmem_type = basic_mem_type(mtype);
if(mmem_type<0) {
::H5Tclose(mtype);
if(mname) ::free(mname);
::H5Tclose(memtype);
::H5Tclose(mem_type);
return failure();
}
if(H5Tinsert(memtype,mname,moffset,mmemtype)<0) {
::H5Tclose(mmemtype);
if(H5Tinsert(mem_type,mname,moffset,mmem_type)<0) {
::H5Tclose(mmem_type);
::H5Tclose(mtype);
if(mname) ::free(mname);
::H5Tclose(memtype);
::H5Tclose(mem_type);
return failure();
}
::H5Tclose(mmemtype);
::H5Tclose(mmem_type);
} else if(mclass==H5T_ARRAY) {
int dimn = ::H5Tget_array_ndims(mtype); //Should be 1;
@@ -141,7 +155,7 @@ inline hid_t compound_mem_type(hid_t a_file_type){
delete [] perms;
::H5Tclose(mtype);
if(mname) ::free(mname);
::H5Tclose(memtype);
::H5Tclose(mem_type);
return failure();
}
hid_t base_type = H5Tget_super(mtype);
@@ -150,61 +164,61 @@ inline hid_t compound_mem_type(hid_t a_file_type){
delete [] perms;
::H5Tclose(mtype);
if(mname) ::free(mname);
::H5Tclose(memtype);
::H5Tclose(mem_type);
return failure();
}
hid_t mmemtype = basic_mem_type(base_type);
if(mmemtype<0) {
hid_t mmem_type = basic_mem_type(base_type);
if(mmem_type<0) {
delete [] dims;
delete [] perms;
::H5Tclose(base_type);
::H5Tclose(mtype);
if(mname) ::free(mname);
::H5Tclose(memtype);
::H5Tclose(mem_type);
return failure();
}
::H5Tclose(base_type);
hid_t array_type = tools_H5Tarray_create(mmemtype,dimn,dims,perms);
hid_t array_type = tools_H5Tarray_create(mmem_type,dimn,dims,perms);
delete [] dims;
delete [] perms;
if(array_type<0) {
::H5Tclose(mmemtype);
::H5Tclose(mmem_type);
::H5Tclose(mtype);
if(mname) ::free(mname);
::H5Tclose(memtype);
::H5Tclose(mem_type);
return failure();
}
::H5Tclose(mmemtype);
::H5Tclose(mmem_type);
if(H5Tinsert(memtype,mname,moffset,array_type)<0) {
if(H5Tinsert(mem_type,mname,moffset,array_type)<0) {
::H5Tclose(array_type);
::H5Tclose(mtype);
if(mname) ::free(mname);
::H5Tclose(memtype);
::H5Tclose(mem_type);
return failure();
}
::H5Tclose(array_type);
} else if(mclass==H5T_COMPOUND) {
hid_t mmemtype = compound_mem_type(mtype);
if(memtype<0) {
hid_t mmem_type = compound_mem_type(mtype);
if(mem_type<0) {
::H5Tclose(mtype);
if(mname) ::free(mname);
::H5Tclose(memtype);
::H5Tclose(mem_type);
return failure();
}
if(H5Tinsert(memtype,mname,moffset,mmemtype)<0) {
::H5Tclose(mmemtype);
if(H5Tinsert(mem_type,mname,moffset,mmem_type)<0) {
::H5Tclose(mmem_type);
::H5Tclose(mtype);
if(mname) ::free(mname);
::H5Tclose(memtype);
::H5Tclose(mem_type);
return failure();
}
::H5Tclose(mmemtype);
::H5Tclose(mmem_type);
} else {
::H5Tclose(mtype);
if(mname) ::free(mname);
::H5Tclose(memtype);
::H5Tclose(mem_type);
return failure();
}}
::H5Tclose(mtype);
@@ -212,7 +226,7 @@ inline hid_t compound_mem_type(hid_t a_file_type){
if(mname) ::free(mname);
}
return memtype;
return mem_type;
}
}}
@@ -223,28 +237,29 @@ inline hid_t compound_mem_type(hid_t a_file_type){
namespace tools {
namespace hdf5 {
inline bool read_atb(hid_t aID,const std::string& a_name,std::string& aData,unsigned int aSize = 100){
inline bool read_atb(hid_t a_id,const std::string& a_name,std::string& a_data,unsigned int aSize = 100){
// From H5LT.c/H5LTget_attribute_string.
if(!H5LT_find_attribute(aID,a_name.c_str())) {aData.clear();return false;}
if(!H5LT_find_attribute(a_id,a_name.c_str())) {a_data.clear();return false;}
char* b = new char[aSize];
if(H5LT_get_attribute_disk(aID,a_name.c_str(),b)<0) {
if(H5LT_get_attribute_disk(a_id,a_name.c_str(),b)<0) {
delete [] b;
a_data.clear();
return false;
}
aData = std::string(b);
a_data = std::string(b);
delete [] b;
return true;
}
inline bool read_atb(hid_t aID,const std::string& a_name,unsigned int& aData){
if(!H5LT_find_attribute(aID,a_name.c_str())) {aData=0;return false;}
if(H5LT_get_attribute_mem(aID,a_name.c_str(),H5T_NATIVE_UINT,&aData)<0) return false;
inline bool read_atb(hid_t a_id,const std::string& a_name,unsigned int& a_data){
if(!H5LT_find_attribute(a_id,a_name.c_str())) {a_data=0;return false;}
if(H5LT_get_attribute_mem(a_id,a_name.c_str(),H5T_NATIVE_UINT,&a_data)<0) {a_data=0;return false;}
return true;
}
inline bool read_atb(hid_t aID,const std::string& a_name,int& aData){
if(!H5LT_find_attribute(aID,a_name.c_str())) {aData=0;return false;}
if(H5LT_get_attribute_mem(aID,a_name.c_str(),H5T_NATIVE_INT,&aData)<0) return false;
inline bool read_atb(hid_t a_id,const std::string& a_name,int& a_data){
if(!H5LT_find_attribute(a_id,a_name.c_str())) {a_data=0;return false;}
if(H5LT_get_attribute_mem(a_id,a_name.c_str(),H5T_NATIVE_INT,&a_data)<0) {a_data=0;return false;}
return true;
}
@@ -302,14 +317,14 @@ inline bool dataset_vec_size(hid_t a_loc,const std::string& a_name,hsize_t& a_si
return true;
}
inline bool write_atb(hid_t aDS,const std::string& a_name,const std::string& aData){
inline bool write_atb(hid_t a_id,const std::string& a_name,const std::string& a_data){
// From H5LT.c/H5LTset_attribute_string.
int has_attr = H5LT_find_attribute(aDS,a_name.c_str());
int has_attr = H5LT_find_attribute(a_id,a_name.c_str());
if(has_attr==1) {
if(H5Adelete(aDS,a_name.c_str())<0) return false;
if(H5Adelete(a_id,a_name.c_str())<0) return false;
}
hid_t datatype = string_datatype(aData.size()+1);
hid_t datatype = string_datatype(a_data.size()+1);
if(datatype<0) return false;
hid_t scalar = ::H5Screate(H5S_SCALAR);
@@ -318,14 +333,14 @@ inline bool write_atb(hid_t aDS,const std::string& a_name,const std::string& aDa
return false;
}
hid_t aid = tools_H5Acreate(aDS,a_name.c_str(),datatype,scalar,H5P_DEFAULT);
hid_t aid = tools_H5Acreate(a_id,a_name.c_str(),datatype,scalar,H5P_DEFAULT);
if(aid<0) {
::H5Sclose(scalar);
::H5Tclose(datatype);
return false;
}
if(H5Awrite(aid,datatype,aData.c_str())<0) {
if(H5Awrite(aid,datatype,a_data.c_str())<0) {
::H5Aclose(aid);
::H5Sclose(scalar);
::H5Tclose(datatype);
@@ -339,7 +354,7 @@ inline bool write_atb(hid_t aDS,const std::string& a_name,const std::string& aDa
return true;
}
inline bool write_bool(hid_t a_loc,const std::string& a_name,bool aData) {
inline bool write_bool(hid_t a_loc,const std::string& a_name,bool a_data) {
hid_t scalar = ::H5Screate(H5S_SCALAR);
if(scalar<0) return false;
@@ -361,7 +376,7 @@ inline bool write_bool(hid_t a_loc,const std::string& a_name,bool aData) {
return false;
}
unsigned char data = aData?1:0;
unsigned char data = a_data?1:0;
if(::H5Dwrite(dataset,H5T_NATIVE_UCHAR,H5S_ALL,H5S_ALL,H5P_DEFAULT,&data)<0) {
::H5Pclose(compact);
::H5Sclose(scalar);
@@ -375,7 +390,7 @@ inline bool write_bool(hid_t a_loc,const std::string& a_name,bool aData) {
return true;
}
inline bool write_string(hid_t a_loc,const std::string& a_name,const std::string& aString) {
inline bool write_string(hid_t a_loc,const std::string& a_name,const std::string& a_string) {
hid_t scalar = ::H5Screate(H5S_SCALAR);
if(scalar<0) return false;
@@ -392,34 +407,180 @@ inline bool write_string(hid_t a_loc,const std::string& a_name,const std::string
}
// From H5LTmakge_dataset_string.
hid_t filetype = string_datatype(aString.size()+1);
if(filetype<0) {
hid_t file_type = string_datatype(a_string.size()+1);
if(file_type<0) {
::H5Pclose(compact);
::H5Sclose(scalar);
return false;
}
hid_t dataset = tools_H5Dcreate(a_loc,a_name.c_str(),filetype,scalar,compact);
hid_t dataset = tools_H5Dcreate(a_loc,a_name.c_str(),file_type,scalar,compact);
if(dataset<0) {
::H5Pclose(compact);
::H5Sclose(scalar);
::H5Tclose(filetype);
::H5Tclose(file_type);
return false;
}
hid_t memtype = filetype;
if(H5Dwrite(dataset,memtype,H5S_ALL,H5S_ALL,H5P_DEFAULT,aString.c_str())<0) {
hid_t mem_type = file_type;
if(H5Dwrite(dataset,mem_type,H5S_ALL,H5S_ALL,H5P_DEFAULT,a_string.c_str())<0) {
::H5Pclose(compact);
::H5Sclose(scalar);
::H5Dclose(dataset);
::H5Tclose(filetype);
::H5Tclose(file_type);
return false;
}
::H5Pclose(compact);
::H5Sclose(scalar);
::H5Dclose(dataset);
::H5Tclose(filetype);
::H5Tclose(file_type);
return true;
}
inline bool write_string_dataset(hid_t a_loc,const std::string& a_name,
unsigned int a_chunked,unsigned int a_compress,
const std::string& a_string) {
hid_t cpt = -1;
if(a_compress || a_chunked) {
cpt = ::H5Pcreate(H5P_DATASET_CREATE);
if(cpt<0) return false;
if(a_chunked) {
if(H5Pset_layout(cpt,H5D_CHUNKED)<0) {
::H5Pclose(cpt);
return false;
}
hsize_t cdims[1];
cdims[0] = a_chunked;
if(H5Pset_chunk(cpt,1,cdims)<0) {
::H5Pclose(cpt);
return false;
}
} else {
if(H5Pset_layout(cpt,H5D_COMPACT)<0) {
::H5Pclose(cpt);
return false;
}
}
if(a_compress) {
if(H5Pset_deflate(cpt,a_compress>9?9:a_compress)<0) {
::H5Pclose(cpt);
return false;
}
}
} else {
cpt = H5P_DEFAULT;
}
hid_t file_type = str_datatype(); //first input => H5T_VARIABLE.
if(file_type<0) {
if(cpt>=0) ::H5Pclose(cpt);
return false;
}
hid_t file_space = -1;
{hsize_t dims[1];
dims[0] = 1;
if(a_chunked) {
hsize_t mx_dims[1];
mx_dims[0] = H5S_UNLIMITED; //extendable.
file_space = ::H5Screate_simple(1,dims,mx_dims);
} else {
file_space = ::H5Screate_simple(1,dims,NULL);
}
if(file_space<0) {if(cpt>=0) ::H5Pclose(cpt);::H5Tclose(file_type);return false;}}
hid_t dataset = tools_H5Dcreate(a_loc,a_name.c_str(),file_type,file_space,cpt);
if(cpt>=0) ::H5Pclose(cpt);
::H5Sclose(file_space);
if(dataset<0) {
::H5Tclose(file_type);
return false;
}
hid_t mem_type = file_type;
const char* wdata[1];
wdata[0] = a_string.c_str();
if(H5Dwrite(dataset,mem_type,H5S_ALL,H5S_ALL,H5P_DEFAULT,wdata)<0) {
::H5Dclose(dataset);
::H5Tclose(file_type);
return false;
}
::H5Tclose(file_type);
::H5Dclose(dataset);
return true;
}
inline bool write_string_dataset(hid_t a_loc,const std::string& a_name,
const std::string& a_string,
unsigned int a_chunked = 0,unsigned int a_compress = 0) {
return hdf5::write_string_dataset(a_loc,a_name,a_chunked,a_compress,a_string);
}
inline bool write_append_string_dataset(hid_t a_dataset,const std::string& a_string) {
hsize_t old_size = 0;
{hid_t dataspace = H5Dget_space(a_dataset);
if(dataspace<0) return false;
hsize_t dims[1];
if(H5Sget_simple_extent_dims(dataspace,dims,NULL)<0) {
::H5Sclose(dataspace);
return false;
}
old_size = dims[0];
::H5Sclose(dataspace);}
{hsize_t exts[1];
exts[0] = old_size+1;
// if(H5Dextend(dataset,exts)<0) {
if(H5Dset_extent(a_dataset,exts)<0) return false;}
hid_t file_space = H5Dget_space(a_dataset);
if(file_space<0) return false;
{hsize_t offset[1];
offset[0] = old_size;
hsize_t count[1];
count[0] = 1;
if(H5Sselect_hyperslab(file_space,H5S_SELECT_SET,offset,NULL,count,NULL)<0) {
::H5Sclose(file_space);
return false;
}}
hsize_t dims[1];
dims[0] = 1;
hid_t mem_space = ::H5Screate_simple(1,dims,NULL);
if(mem_space<0) {
::H5Sclose(file_space);
return false;
}
hid_t mem_type = str_datatype();
if(mem_type<0) {
::H5Sclose(mem_space);
::H5Sclose(file_space);
return false;
}
const char* wdata[1];
wdata[0] = a_string.c_str();
if(H5Dwrite(a_dataset,mem_type,mem_space,file_space,H5P_DEFAULT,wdata)<0) {
::H5Tclose(mem_type);
::H5Sclose(mem_space);
::H5Sclose(file_space);
return false;
}
::H5Tclose(mem_type);
::H5Sclose(mem_space);
::H5Sclose(file_space);
return true;
}
@@ -442,26 +603,26 @@ inline bool write_array_string(hid_t a_loc,const std::string& a_name,const std::
return false;
}
hid_t filetype = string_datatype(sz);
if(filetype<0) {
hid_t file_type = string_datatype(sz);
if(file_type<0) {
delete [] buffer;
::H5Sclose(scalar);
return false;
}
hid_t dataset = tools_H5Dcreate(a_loc,a_name.c_str(),filetype,scalar,H5P_DEFAULT);
hid_t dataset = tools_H5Dcreate(a_loc,a_name.c_str(),file_type,scalar,H5P_DEFAULT);
if(dataset<0) {
delete [] buffer;
::H5Tclose(filetype);
::H5Tclose(file_type);
::H5Sclose(scalar);
return false;
}
hid_t memtype = filetype;
if(H5Dwrite(dataset,memtype,H5S_ALL,H5S_ALL,H5P_DEFAULT,buffer)<0) {
hid_t mem_type = file_type;
if(H5Dwrite(dataset,mem_type,H5S_ALL,H5S_ALL,H5P_DEFAULT,buffer)<0) {
delete [] buffer;
::H5Dclose(dataset);
::H5Tclose(filetype);
::H5Tclose(file_type);
::H5Sclose(scalar);
return false;
}
@@ -469,12 +630,12 @@ inline bool write_array_string(hid_t a_loc,const std::string& a_name,const std::
delete [] buffer;
::H5Dclose(dataset);
::H5Tclose(filetype);
::H5Tclose(file_type);
::H5Sclose(scalar);
return true;
}
inline bool write_object(hid_t a_loc,const std::string& a_name,hid_t a_file_type,char* aData) {
inline bool write_object(hid_t a_loc,const std::string& a_name,hid_t a_file_type,char* a_data) {
unsigned int chunked = 0;
unsigned int compress = 0;
@@ -532,7 +693,7 @@ inline bool write_object(hid_t a_loc,const std::string& a_name,hid_t a_file_type
return false;
}
if(H5Dwrite(dataset,mem_type,H5S_ALL,H5S_ALL,H5P_DEFAULT,aData)<0) {
if(H5Dwrite(dataset,mem_type,H5S_ALL,H5S_ALL,H5P_DEFAULT,a_data)<0) {
::H5Dclose(dataset);
::H5Tclose(mem_type);
::H5Sclose(simple);
@@ -548,148 +709,286 @@ inline bool write_object(hid_t a_loc,const std::string& a_name,hid_t a_file_type
return true;
}
inline bool read_string(hid_t a_loc,const std::string& a_name,std::string& aString) {
inline bool read_string(hid_t a_loc,const std::string& a_name,std::string& a_string) {
// From H5LTread_dataset_string.
hid_t dataset = tools_H5Dopen(a_loc,a_name.c_str());
if(dataset<0) {
aString.clear();
a_string.clear();
return false; // data set not found.
}
hid_t filetype = H5Dget_type(dataset);
if(filetype<0) {
hid_t file_type = H5Dget_type(dataset);
if(file_type<0) {
::H5Dclose(dataset);
aString.clear();
a_string.clear();
return false;
}
H5T_class_t t_class = H5Tget_class(filetype);
H5T_class_t t_class = H5Tget_class(file_type);
if(t_class!=H5T_STRING) {
::H5Tclose(filetype);
::H5Tclose(file_type);
::H5Dclose(dataset);
aString.clear();
a_string.clear();
return false;
}
size_t sz = H5Tget_size(filetype);
::H5Tclose(filetype);
size_t sz = H5Tget_size(file_type);
::H5Tclose(file_type);
if(!sz) {
::H5Dclose(dataset);
aString.clear();
a_string.clear();
return false;
}
// We could have use filetype since, for string,
// We could have use file_type since, for string,
// file type is the same than memory type.
hid_t memtype = string_datatype(sz);
if(memtype<0) {
hid_t mem_type = string_datatype(sz);
if(mem_type<0) {
::H5Dclose(dataset);
aString.clear();
a_string.clear();
return false;
}
char* buff = new char[sz];
herr_t stat = H5Dread(dataset,memtype,H5S_ALL,H5S_ALL,H5P_DEFAULT,buff);
::H5Tclose(memtype);
herr_t stat = H5Dread(dataset,mem_type,H5S_ALL,H5S_ALL,H5P_DEFAULT,buff);
::H5Tclose(mem_type);
::H5Dclose(dataset);
if(stat<0) {
delete [] buff;
aString.clear();
a_string.clear();
return false;
}
size_t len = sz-1;
aString.resize(len,0);
for(size_t index=0;index<len;index++) aString[index] = buff[index];
a_string.resize(len,0);
for(size_t index=0;index<len;index++) a_string[index] = buff[index];
delete [] buff;
return true;
}
inline bool read_object(hid_t a_loc,const std::string& a_name,size_t& a_size,char*& aData) {
inline bool read_sub_string(hid_t a_loc,const std::string& a_name,unsigned int a_offset,std::string& a_string) {
hid_t dataset = tools_H5Dopen(a_loc,a_name.c_str());
if(dataset<0) {
a_string.clear();
return false; // data set not found.
}
hid_t file_space = H5Dget_space(dataset);
if(file_space<0) {
::H5Dclose(dataset);
a_string.clear();
return false;
}
{int dimn = H5Sget_simple_extent_ndims(file_space);
if(dimn<0) {
::H5Sclose(file_space);
::H5Dclose(dataset);
a_string.clear();
return false;
}
if(dimn!=1) {
::H5Sclose(file_space);
::H5Dclose(dataset);
a_string.clear();
return false;
}}
hsize_t dims[1];
{if(H5Sget_simple_extent_dims(file_space,dims,NULL)<0) {
::H5Sclose(file_space);
::H5Dclose(dataset);
a_string.clear();
return false;
}}
{unsigned int sz = (unsigned int)dims[0];
if(!sz) {
::H5Sclose(file_space);
::H5Dclose(dataset);
a_string.clear();
return false; //Is it ok ?
}
// abcdef
// 012345
int remain = sz-a_offset;
if(remain<=0) {
::H5Sclose(file_space);
::H5Dclose(dataset);
a_string.clear();
return false;
}}
{hsize_t offset[1];
offset[0] = a_offset;
hsize_t count[1];
count[0] = 1;
if(H5Sselect_hyperslab(file_space,H5S_SELECT_SET,offset,NULL,count,NULL)<0) {
::H5Sclose(file_space);
::H5Dclose(dataset);
a_string.clear();
return false;
}}
dims[0] = 1;
hid_t mem_space = ::H5Screate_simple(1,dims,NULL);
if(mem_space<0) {
::H5Sclose(file_space);
::H5Dclose(dataset);
a_string.clear();
return false;
}
hid_t file_type = H5Dget_type(dataset);
if(file_type<0) {
::H5Sclose(file_space);
::H5Dclose(dataset);
a_string.clear();
return false;
}
H5T_class_t t_class = H5Tget_class(file_type);
if(t_class!=H5T_STRING) {
::H5Tclose(file_type);
::H5Sclose(file_space);
::H5Dclose(dataset);
a_string.clear();
return false;
}
//size_t sz = H5Tget_size(file_type); //it gives the largest string size in the dataset.
//if(!sz) {
// ::H5Tclose(file_type);
// ::H5Sclose(file_space);
// ::H5Dclose(dataset);
// a_string.clear();
// return false;
//}
::H5Tclose(file_type);
hid_t mem_type = str_datatype();
if(mem_type<0) {
::H5Sclose(file_space);
::H5Dclose(dataset);
a_string.clear();
return false;
}
char* rdata[1];
herr_t stat = H5Dread(dataset,mem_type,mem_space,file_space,H5P_DEFAULT,rdata);
if(stat<0) {
::H5Dvlen_reclaim(mem_type,mem_space, H5P_DEFAULT,rdata);
::H5Tclose(mem_type);
::H5Sclose(mem_space);
::H5Sclose(file_space);
::H5Dclose(dataset);
a_string.clear();
return false;
}
char* buff = rdata[0];
size_t len = ::strlen(buff);
a_string.resize(len,0);
for(size_t index=0;index<len;index++) a_string[index] = buff[index];
::H5Dvlen_reclaim(mem_type,mem_space, H5P_DEFAULT,rdata);
::H5Tclose(mem_type);
::H5Sclose(mem_space);
::H5Sclose(file_space);
::H5Dclose(dataset);
return true;
}
inline bool read_object(hid_t a_loc,const std::string& a_name,size_t& a_size,char*& a_data) {
hid_t dataset = tools_H5Dopen(a_loc,a_name.c_str());
if(dataset<0) {
a_size = 0;
aData = 0;
a_data = 0;
return false;
}
hid_t filetype = H5Dget_type(dataset);
if(filetype<0) {
hid_t file_type = H5Dget_type(dataset);
if(file_type<0) {
::H5Dclose(dataset);
a_size = 0;
aData = 0;
a_data = 0;
return false;
}
H5T_class_t t_class = H5Tget_class(filetype);
H5T_class_t t_class = H5Tget_class(file_type);
if(t_class!=H5T_COMPOUND) {
::H5Tclose(filetype);
::H5Tclose(file_type);
::H5Dclose(dataset);
a_size = 0;
aData = 0;
a_data = 0;
return false;
}
size_t sz = H5Tget_size(filetype);
size_t sz = H5Tget_size(file_type);
if(!sz) {
::H5Tclose(filetype);
::H5Tclose(file_type);
::H5Dclose(dataset);
a_size = 0;
aData = 0;
a_data = 0;
return false;
}
hid_t memtype = compound_mem_type(filetype);
if(memtype<0) {
::H5Tclose(filetype);
hid_t mem_type = compound_mem_type(file_type);
if(mem_type<0) {
::H5Tclose(file_type);
::H5Dclose(dataset);
a_size = 0;
aData = 0;
a_data = 0;
return false;
}
::H5Tclose(filetype);
::H5Tclose(file_type);
hid_t dataspace = H5Dget_space(dataset);
if(dataspace<0) {
::H5Tclose(memtype);
::H5Tclose(mem_type);
::H5Dclose(dataset);
a_size = 0;
aData = 0;
a_data = 0;
return false;
}
hid_t scalar = ::H5Screate(H5S_SCALAR);
if(scalar<0) {
::H5Sclose(dataspace);
::H5Tclose(memtype);
::H5Tclose(mem_type);
::H5Dclose(dataset);
a_size = 0;
aData = 0;
a_data = 0;
return false;
}
char* buffer = new char[sz];
if(H5Dread(dataset,memtype,scalar,dataspace,H5P_DEFAULT,buffer)<0) {
if(H5Dread(dataset,mem_type,scalar,dataspace,H5P_DEFAULT,buffer)<0) {
delete [] buffer;
::H5Sclose(scalar);
::H5Sclose(dataspace);
::H5Tclose(memtype);
::H5Tclose(mem_type);
::H5Dclose(dataset);
a_size = 0;
aData = 0;
a_data = 0;
return false;
}
::H5Sclose(scalar);
::H5Sclose(dataspace);
::H5Tclose(memtype);
::H5Tclose(mem_type);
::H5Dclose(dataset);
a_size = sz;
aData = buffer;
a_data = buffer;
return true;
}
@@ -698,37 +997,37 @@ inline bool read_array_string(hid_t a_loc,const std::string& a_name,std::vector<
hid_t dataset = tools_H5Dopen(a_loc,a_name.c_str());
if(dataset<0) return false;
hid_t filetype = H5Dget_type(dataset);
if(filetype<0) {
hid_t file_type = H5Dget_type(dataset);
if(file_type<0) {
::H5Dclose(dataset);
return false;
}
H5T_class_t t_class = H5Tget_class(filetype);
H5T_class_t t_class = H5Tget_class(file_type);
if(t_class!=H5T_STRING) {
::H5Tclose(filetype);
::H5Tclose(file_type);
::H5Dclose(dataset);
return false;
}
size_t sz = H5Tget_size(filetype);
::H5Tclose(filetype);
size_t sz = H5Tget_size(file_type);
::H5Tclose(file_type);
if(!sz) {
::H5Dclose(dataset);
return false;
}
// We could have use filetype since, for string,
// We could have use file_type since, for string,
// file type is the same than memory type.
hid_t memtype = string_datatype(sz);
if(memtype<0) {
hid_t mem_type = string_datatype(sz);
if(mem_type<0) {
::H5Dclose(dataset);
return false;
}
char* buffer = new char[sz];
herr_t stat = H5Dread(dataset,memtype,H5S_ALL,H5S_ALL,H5P_DEFAULT,buffer);
::H5Tclose(memtype);
herr_t stat = H5Dread(dataset,mem_type,H5S_ALL,H5S_ALL,H5P_DEFAULT,buffer);
::H5Tclose(mem_type);
::H5Dclose(dataset);
if(stat<0) {
delete [] buffer;