Warning, /include/Geant4/toolx/hdf5/tools is written in an unsupported language. File is not indexed.
0001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
0002 // See the file tools.license for terms.
0003
0004 #ifndef toolx_hdf5_tools
0005 #define toolx_hdf5_tools
0006
0007 #include "hdf5_h"
0008
0009 #include <cstdlib>
0010
0011 namespace toolx {
0012 namespace hdf5 {
0013
0014 inline bool check_sizes(){
0015 if(sizeof(bool)!=1) return false;
0016 if(sizeof(char)!=1) return false;
0017 if(sizeof(short)!=2) return false;
0018 if(sizeof(int)!=4) return false;
0019 if(sizeof(float)!=4) return false;
0020 if(sizeof(double)!=8) return false;
0021 return true;
0022 }
0023
0024 inline int failure() {return -1;}
0025
0026 inline hid_t string_datatype(size_t aSize){
0027 // aSize should include the trailing null char.
0028 hid_t datatype = ::H5Tcopy(H5T_C_S1);
0029 if(datatype<0) return failure();
0030
0031 if(::H5Tset_size(datatype,aSize)<0) {
0032 ::H5Tclose(datatype);
0033 return failure();
0034 }
0035
0036 if(::H5Tset_strpad(datatype,H5T_STR_NULLTERM)<0) {
0037 ::H5Tclose(datatype);
0038 return failure();
0039 }
0040
0041 return datatype;
0042 }
0043
0044 inline hid_t str_datatype() {
0045 hid_t datatype = ::H5Tcopy(H5T_C_S1);
0046 if(datatype<0) return failure();
0047 if(::H5Tset_size(datatype,H5T_VARIABLE)<0) {
0048 ::H5Tclose(datatype);
0049 return failure();
0050 }
0051 if(::H5Tset_strpad(datatype,H5T_STR_NULLTERM)<0) {
0052 ::H5Tclose(datatype);
0053 return failure();
0054 }
0055 return datatype;
0056 }
0057
0058 inline hid_t basic_mem_type(hid_t a_file_type){
0059 H5T_class_t mclass = H5Tget_class(a_file_type);
0060 size_t msize = H5Tget_size(a_file_type);
0061 if(mclass==H5T_INTEGER) {
0062 H5T_sign_t msign = H5Tget_sign(a_file_type);
0063 if(msize==1) {
0064 if(msign==H5T_SGN_NONE) {
0065 return H5Tcopy(H5T_NATIVE_UCHAR);
0066 } else {
0067 return H5Tcopy(H5T_NATIVE_CHAR);
0068 }
0069 } else if(msize==4) {
0070 if(msign==H5T_SGN_NONE) {
0071 return H5Tcopy(H5T_NATIVE_UINT);
0072 } else {
0073 return H5Tcopy(H5T_NATIVE_INT);
0074 }
0075 } else if(msize==8) { //for osc_file::header::fDate.
0076 if(msign==H5T_SGN_NONE) {
0077 return H5Tcopy(H5T_NATIVE_UINT64);
0078 } else {
0079 return H5Tcopy(H5T_NATIVE_INT64);
0080 }
0081 } else {
0082 return failure();
0083 }
0084 } else if(mclass==H5T_FLOAT) {
0085 if(msize==4) {
0086 return H5Tcopy(H5T_NATIVE_FLOAT);
0087 } else if(msize==8) {
0088 return H5Tcopy(H5T_NATIVE_DOUBLE);
0089 } else {
0090 return failure();
0091 }
0092 } else if(mclass==H5T_STRING) {
0093 return H5Tcopy(a_file_type);
0094 }
0095
0096 return failure();
0097 }
0098
0099 }}
0100
0101 #include <vector>
0102
0103 namespace toolx {
0104 namespace hdf5 {
0105
0106 inline hid_t compound_mem_type(hid_t a_file_type){
0107 // FIXME : In principle H5T_get_native_type should do the job but it crashes.
0108
0109 H5T_class_t t_class = H5Tget_class(a_file_type);
0110 if(t_class!=H5T_COMPOUND) return failure();
0111
0112 size_t sz = H5Tget_size(a_file_type);
0113 //printf("debug : compound_mem_type : sz %lu\n",sz);
0114
0115 hid_t mem_type = ::H5Tcreate(H5T_COMPOUND,sz);
0116 if(mem_type<0) return failure();
0117
0118 //FIXME : WARNING : is order the booked order ?
0119
0120 int mn = H5Tget_nmembers(a_file_type);
0121 std::vector<unsigned int> szs(mn);
0122 //printf("debug : members : %d\n",mn);
0123 for(int index=0;index<mn;index++) {
0124 char* mname = H5Tget_member_name(a_file_type,index);
0125 size_t moffset = H5Tget_member_offset(a_file_type,index);
0126 hid_t mtype = H5Tget_member_type(a_file_type,index);
0127 //printf("debug : members : %d (%d) : %s : begin\n",index,mn,mname);
0128
0129 {H5T_class_t mclass = H5Tget_class(mtype);
0130 if( (mclass==H5T_INTEGER) ||
0131 (mclass==H5T_STRING) ||
0132 (mclass==H5T_FLOAT) ) {
0133 hid_t mmem_type = basic_mem_type(mtype);
0134 if(mmem_type<0) {
0135 ::H5Tclose(mtype);
0136 if(mname) toolx_H5free_memory(mname);
0137 ::H5Tclose(mem_type);
0138 return failure();
0139 }
0140 if(H5Tinsert(mem_type,mname,moffset,mmem_type)<0) {
0141 ::H5Tclose(mmem_type);
0142 ::H5Tclose(mtype);
0143 if(mname) toolx_H5free_memory(mname);
0144 ::H5Tclose(mem_type);
0145 return failure();
0146 }
0147 ::H5Tclose(mmem_type);
0148
0149 } else if(mclass==H5T_ARRAY) {
0150 int dimn = ::H5Tget_array_ndims(mtype); //Should be 1;
0151 hsize_t* dims = new hsize_t[dimn];
0152 int* perms = new int[dimn];
0153 if(toolx_H5Tget_array_dims(mtype,dims,perms)<0) {
0154 delete [] dims;
0155 delete [] perms;
0156 ::H5Tclose(mtype);
0157 if(mname) toolx_H5free_memory(mname);
0158 ::H5Tclose(mem_type);
0159 return failure();
0160 }
0161 hid_t base_type = H5Tget_super(mtype);
0162 if(base_type<0) {
0163 delete [] dims;
0164 delete [] perms;
0165 ::H5Tclose(mtype);
0166 if(mname) toolx_H5free_memory(mname);
0167 ::H5Tclose(mem_type);
0168 return failure();
0169 }
0170 hid_t mmem_type = basic_mem_type(base_type);
0171 if(mmem_type<0) {
0172 delete [] dims;
0173 delete [] perms;
0174 ::H5Tclose(base_type);
0175 ::H5Tclose(mtype);
0176 if(mname) toolx_H5free_memory(mname);
0177 ::H5Tclose(mem_type);
0178 return failure();
0179 }
0180 ::H5Tclose(base_type);
0181 hid_t array_type = toolx_H5Tarray_create(mmem_type,dimn,dims,perms);
0182 delete [] dims;
0183 delete [] perms;
0184 if(array_type<0) {
0185 ::H5Tclose(mmem_type);
0186 ::H5Tclose(mtype);
0187 if(mname) toolx_H5free_memory(mname);
0188 ::H5Tclose(mem_type);
0189 return failure();
0190 }
0191 ::H5Tclose(mmem_type);
0192
0193 if(H5Tinsert(mem_type,mname,moffset,array_type)<0) {
0194 ::H5Tclose(array_type);
0195 ::H5Tclose(mtype);
0196 if(mname) toolx_H5free_memory(mname);
0197 ::H5Tclose(mem_type);
0198 return failure();
0199 }
0200 ::H5Tclose(array_type);
0201
0202 } else if(mclass==H5T_COMPOUND) {
0203 hid_t mmem_type = compound_mem_type(mtype);
0204 if(mem_type<0) {
0205 ::H5Tclose(mtype);
0206 if(mname) toolx_H5free_memory(mname);
0207 ::H5Tclose(mem_type);
0208 return failure();
0209 }
0210 if(H5Tinsert(mem_type,mname,moffset,mmem_type)<0) {
0211 ::H5Tclose(mmem_type);
0212 ::H5Tclose(mtype);
0213 if(mname) toolx_H5free_memory(mname);
0214 ::H5Tclose(mem_type);
0215 return failure();
0216 }
0217 ::H5Tclose(mmem_type);
0218 } else {
0219 ::H5Tclose(mtype);
0220 if(mname) toolx_H5free_memory(mname);
0221 ::H5Tclose(mem_type);
0222 return failure();
0223 }}
0224 ::H5Tclose(mtype);
0225 //printf("debug : compound_mem_type : %d (%d) : %s : end\n",index,mn,mname);
0226 if(mname) toolx_H5free_memory(mname);
0227 }
0228
0229 return mem_type;
0230 }
0231
0232 }}
0233
0234 #include "atb"
0235 #include <string>
0236
0237 namespace toolx {
0238 namespace hdf5 {
0239
0240 inline bool read_atb(hid_t a_id,const std::string& a_name,std::string& a_data,unsigned int aSize = 100){
0241 // From H5LT.c/H5LTget_attribute_string.
0242 if(!H5LT_find_attribute(a_id,a_name.c_str())) {a_data.clear();return false;}
0243 char* b = new char[aSize];
0244 if(H5LT_get_attribute_disk(a_id,a_name.c_str(),b)<0) {
0245 delete [] b;
0246 a_data.clear();
0247 return false;
0248 }
0249 a_data = std::string(b);
0250 delete [] b;
0251 return true;
0252 }
0253
0254 inline bool read_atb(hid_t a_id,const std::string& a_name,unsigned int& a_data){
0255 if(!H5LT_find_attribute(a_id,a_name.c_str())) {a_data=0;return false;}
0256 if(H5LT_get_attribute_mem(a_id,a_name.c_str(),H5T_NATIVE_UINT,&a_data)<0) {a_data=0;return false;}
0257 return true;
0258 }
0259
0260 inline bool read_atb(hid_t a_id,const std::string& a_name,int& a_data){
0261 if(!H5LT_find_attribute(a_id,a_name.c_str())) {a_data=0;return false;}
0262 if(H5LT_get_attribute_mem(a_id,a_name.c_str(),H5T_NATIVE_INT,&a_data)<0) {a_data=0;return false;}
0263 return true;
0264 }
0265
0266 inline hid_t H5T_STD_U8XX() {return H5T_STD_U8LE;}
0267 inline hid_t H5T_STD_U32XX() {return H5T_STD_U32LE;}
0268 inline hid_t H5T_STD_U64XX() {return H5T_STD_U64LE;}
0269 inline hid_t H5T_STD_I8XX() {return H5T_STD_I8LE;}
0270 inline hid_t H5T_STD_I16XX() {return H5T_STD_I16LE;}
0271 inline hid_t H5T_STD_I32XX() {return H5T_STD_I32LE;}
0272 inline hid_t H5T_STD_I64XX() {return H5T_STD_I64LE;}
0273 inline hid_t H5T_IEEE_F32XX() {return H5T_IEEE_F32LE;}
0274 inline hid_t H5T_IEEE_F64XX() {return H5T_IEEE_F64LE;}
0275
0276 inline bool dataset_vec_size(hid_t a_loc,const std::string& a_name,hsize_t& a_size) {
0277 hid_t dataset = toolx_H5Dopen(a_loc,a_name.c_str());
0278 if(dataset<0) {
0279 a_size = 0;
0280 return false; // data set not found.
0281 }
0282
0283 hid_t file_space = H5Dget_space(dataset);
0284 if(file_space<0) {
0285 ::H5Dclose(dataset);
0286 a_size = 0;
0287 return false;
0288 }
0289
0290 int dimn = H5Sget_simple_extent_ndims(file_space);
0291 if(dimn<0) {
0292 ::H5Sclose(file_space);
0293 ::H5Dclose(dataset);
0294 a_size = 0;
0295 return false;
0296 }
0297 if(dimn!=1) {
0298 ::H5Sclose(file_space);
0299 ::H5Dclose(dataset);
0300 a_size = 0;
0301 return false;
0302 }
0303 //printf("debug : read dimn %d\n",dimn);
0304
0305 hsize_t dims[1];
0306 {if(H5Sget_simple_extent_dims(file_space,dims,NULL)<0) {
0307 ::H5Sclose(file_space);
0308 ::H5Dclose(dataset);
0309 a_size = 0;
0310 return false;
0311 }}
0312
0313 ::H5Sclose(file_space);
0314 ::H5Dclose(dataset);
0315
0316 a_size = dims[0];
0317 return true;
0318 }
0319
0320 inline bool write_atb(hid_t a_id,const std::string& a_name,const std::string& a_data){
0321 // From H5LT.c/H5LTset_attribute_string.
0322 int has_attr = H5LT_find_attribute(a_id,a_name.c_str());
0323 if(has_attr==1) {
0324 if(H5Adelete(a_id,a_name.c_str())<0) return false;
0325 }
0326
0327 hid_t datatype = string_datatype(a_data.size()+1);
0328 if(datatype<0) return false;
0329
0330 hid_t scalar = ::H5Screate(H5S_SCALAR);
0331 if(scalar<0) {
0332 ::H5Tclose(datatype);
0333 return false;
0334 }
0335
0336 hid_t aid = toolx_H5Acreate(a_id,a_name.c_str(),datatype,scalar,H5P_DEFAULT);
0337 if(aid<0) {
0338 ::H5Sclose(scalar);
0339 ::H5Tclose(datatype);
0340 return false;
0341 }
0342
0343 if(H5Awrite(aid,datatype,a_data.c_str())<0) {
0344 ::H5Aclose(aid);
0345 ::H5Sclose(scalar);
0346 ::H5Tclose(datatype);
0347 return false;
0348 }
0349
0350 ::H5Aclose(aid);
0351 ::H5Sclose(scalar);
0352 ::H5Tclose(datatype);
0353
0354 return true;
0355 }
0356
0357 inline bool write_bool(hid_t a_loc,const std::string& a_name,bool a_data) {
0358 hid_t scalar = ::H5Screate(H5S_SCALAR);
0359 if(scalar<0) return false;
0360
0361 hid_t compact = ::H5Pcreate(H5P_DATASET_CREATE);
0362 if(compact<0) {
0363 ::H5Sclose(scalar);
0364 return false;
0365 }
0366 if(H5Pset_layout(compact,H5D_COMPACT)<0) {
0367 ::H5Pclose(compact);
0368 ::H5Sclose(scalar);
0369 return false;
0370 }
0371
0372 hid_t dataset = toolx_H5Dcreate(a_loc,a_name.c_str(),H5T_STD_U8XX(),scalar,compact);
0373 if(dataset<0) {
0374 ::H5Pclose(compact);
0375 ::H5Sclose(scalar);
0376 return false;
0377 }
0378
0379 unsigned char data = a_data?1:0;
0380 if(::H5Dwrite(dataset,H5T_NATIVE_UCHAR,H5S_ALL,H5S_ALL,H5P_DEFAULT,&data)<0) {
0381 ::H5Pclose(compact);
0382 ::H5Sclose(scalar);
0383 ::H5Dclose(dataset);
0384 return false;
0385 }
0386
0387 ::H5Pclose(compact);
0388 ::H5Sclose(scalar);
0389 ::H5Dclose(dataset);
0390 return true;
0391 }
0392
0393 inline bool write_string(hid_t a_loc,const std::string& a_name,const std::string& a_string) {
0394 hid_t scalar = ::H5Screate(H5S_SCALAR);
0395 if(scalar<0) return false;
0396
0397 hid_t compact = ::H5Pcreate(H5P_DATASET_CREATE);
0398 if(compact<0) {
0399 ::H5Sclose(scalar);
0400 return false;
0401 }
0402
0403 if(H5Pset_layout(compact,H5D_COMPACT)<0) {
0404 ::H5Pclose(compact);
0405 ::H5Sclose(scalar);
0406 return false;
0407 }
0408
0409 // From H5LTmakge_dataset_string.
0410 hid_t file_type = string_datatype(a_string.size()+1);
0411 if(file_type<0) {
0412 ::H5Pclose(compact);
0413 ::H5Sclose(scalar);
0414 return false;
0415 }
0416
0417 hid_t dataset = toolx_H5Dcreate(a_loc,a_name.c_str(),file_type,scalar,compact);
0418 if(dataset<0) {
0419 ::H5Pclose(compact);
0420 ::H5Sclose(scalar);
0421 ::H5Tclose(file_type);
0422 return false;
0423 }
0424
0425 hid_t mem_type = file_type;
0426 if(H5Dwrite(dataset,mem_type,H5S_ALL,H5S_ALL,H5P_DEFAULT,a_string.c_str())<0) {
0427 ::H5Pclose(compact);
0428 ::H5Sclose(scalar);
0429 ::H5Dclose(dataset);
0430 ::H5Tclose(file_type);
0431 return false;
0432 }
0433
0434 ::H5Pclose(compact);
0435 ::H5Sclose(scalar);
0436 ::H5Dclose(dataset);
0437 ::H5Tclose(file_type);
0438
0439 return true;
0440 }
0441
0442 inline bool write_string_dataset(hid_t a_loc,const std::string& a_name,
0443 unsigned int a_chunked,unsigned int a_compress,
0444 const std::string& a_string) {
0445 hid_t cpt = -1;
0446 if(a_compress || a_chunked) {
0447 cpt = ::H5Pcreate(H5P_DATASET_CREATE);
0448 if(cpt<0) return false;
0449 if(a_chunked) {
0450 if(H5Pset_layout(cpt,H5D_CHUNKED)<0) {
0451 ::H5Pclose(cpt);
0452 return false;
0453 }
0454 hsize_t cdims[1];
0455 cdims[0] = a_chunked;
0456 if(H5Pset_chunk(cpt,1,cdims)<0) {
0457 ::H5Pclose(cpt);
0458 return false;
0459 }
0460 } else {
0461 if(H5Pset_layout(cpt,H5D_COMPACT)<0) {
0462 ::H5Pclose(cpt);
0463 return false;
0464 }
0465 }
0466 if(a_compress) {
0467 if(H5Pset_deflate(cpt,a_compress>9?9:a_compress)<0) {
0468 ::H5Pclose(cpt);
0469 return false;
0470 }
0471 }
0472 } else {
0473 cpt = H5P_DEFAULT;
0474 }
0475
0476 hid_t file_type = str_datatype(); //first input => H5T_VARIABLE.
0477 if(file_type<0) {
0478 if(cpt>=0) ::H5Pclose(cpt);
0479 return false;
0480 }
0481
0482 hid_t file_space = -1;
0483 {hsize_t dims[1];
0484 dims[0] = 1;
0485 if(a_chunked) {
0486 hsize_t mx_dims[1];
0487 mx_dims[0] = H5S_UNLIMITED; //extendable.
0488 file_space = ::H5Screate_simple(1,dims,mx_dims);
0489 } else {
0490 file_space = ::H5Screate_simple(1,dims,NULL);
0491 }
0492 if(file_space<0) {if(cpt>=0) ::H5Pclose(cpt);::H5Tclose(file_type);return false;}}
0493
0494 hid_t dataset = toolx_H5Dcreate(a_loc,a_name.c_str(),file_type,file_space,cpt);
0495 if(cpt>=0) ::H5Pclose(cpt);
0496 ::H5Sclose(file_space);
0497 if(dataset<0) {
0498 ::H5Tclose(file_type);
0499 return false;
0500 }
0501
0502 hid_t mem_type = file_type;
0503
0504 const char* wdata[1];
0505 wdata[0] = a_string.c_str();
0506
0507 if(H5Dwrite(dataset,mem_type,H5S_ALL,H5S_ALL,H5P_DEFAULT,wdata)<0) {
0508 ::H5Dclose(dataset);
0509 ::H5Tclose(file_type);
0510 return false;
0511 }
0512
0513 ::H5Tclose(file_type);
0514 ::H5Dclose(dataset);
0515
0516 return true;
0517 }
0518
0519 inline bool write_string_dataset(hid_t a_loc,const std::string& a_name,
0520 const std::string& a_string,
0521 unsigned int a_chunked = 0,unsigned int a_compress = 0) {
0522 return hdf5::write_string_dataset(a_loc,a_name,a_chunked,a_compress,a_string);
0523 }
0524
0525 inline bool write_append_string_dataset(hid_t a_dataset,const std::string& a_string) {
0526 hsize_t old_size = 0;
0527
0528 {hid_t dataspace = H5Dget_space(a_dataset);
0529 if(dataspace<0) return false;
0530 hsize_t dims[1];
0531 if(H5Sget_simple_extent_dims(dataspace,dims,NULL)<0) {
0532 ::H5Sclose(dataspace);
0533 return false;
0534 }
0535 old_size = dims[0];
0536 ::H5Sclose(dataspace);}
0537
0538 {hsize_t exts[1];
0539 exts[0] = old_size+1;
0540 // if(H5Dextend(dataset,exts)<0) {
0541 if(H5Dset_extent(a_dataset,exts)<0) return false;}
0542
0543 hid_t file_space = H5Dget_space(a_dataset);
0544 if(file_space<0) return false;
0545
0546 {hsize_t offset[1];
0547 offset[0] = old_size;
0548 hsize_t count[1];
0549 count[0] = 1;
0550 if(H5Sselect_hyperslab(file_space,H5S_SELECT_SET,offset,NULL,count,NULL)<0) {
0551 ::H5Sclose(file_space);
0552 return false;
0553 }}
0554
0555 hsize_t dims[1];
0556 dims[0] = 1;
0557 hid_t mem_space = ::H5Screate_simple(1,dims,NULL);
0558 if(mem_space<0) {
0559 ::H5Sclose(file_space);
0560 return false;
0561 }
0562
0563 hid_t mem_type = str_datatype();
0564 if(mem_type<0) {
0565 ::H5Sclose(mem_space);
0566 ::H5Sclose(file_space);
0567 return false;
0568 }
0569
0570 const char* wdata[1];
0571 wdata[0] = a_string.c_str();
0572
0573 if(H5Dwrite(a_dataset,mem_type,mem_space,file_space,H5P_DEFAULT,wdata)<0) {
0574 ::H5Tclose(mem_type);
0575 ::H5Sclose(mem_space);
0576 ::H5Sclose(file_space);
0577 return false;
0578 }
0579
0580 ::H5Tclose(mem_type);
0581 ::H5Sclose(mem_space);
0582 ::H5Sclose(file_space);
0583
0584 return true;
0585 }
0586
0587 }}
0588
0589 #include <tools/buf2lines>
0590
0591 namespace toolx {
0592 namespace hdf5 {
0593
0594 inline bool write_array_string(hid_t a_loc,const std::string& a_name,const std::vector<std::string>& a_array) {
0595 hid_t scalar = ::H5Screate(H5S_SCALAR);
0596 if(scalar<0) return false;
0597
0598 // From H5LTmake_dataset_string.
0599 size_t sz;
0600 char* buffer;
0601 if(!tools::strings2buf(a_array,sz,buffer)) {
0602 ::H5Sclose(scalar);
0603 return false;
0604 }
0605
0606 hid_t file_type = string_datatype(sz);
0607 if(file_type<0) {
0608 delete [] buffer;
0609 ::H5Sclose(scalar);
0610 return false;
0611 }
0612
0613 hid_t dataset = toolx_H5Dcreate(a_loc,a_name.c_str(),file_type,scalar,H5P_DEFAULT);
0614 if(dataset<0) {
0615 delete [] buffer;
0616 ::H5Tclose(file_type);
0617 ::H5Sclose(scalar);
0618 return false;
0619 }
0620
0621 hid_t mem_type = file_type;
0622 if(H5Dwrite(dataset,mem_type,H5S_ALL,H5S_ALL,H5P_DEFAULT,buffer)<0) {
0623 delete [] buffer;
0624 ::H5Dclose(dataset);
0625 ::H5Tclose(file_type);
0626 ::H5Sclose(scalar);
0627 return false;
0628 }
0629
0630 delete [] buffer;
0631
0632 ::H5Dclose(dataset);
0633 ::H5Tclose(file_type);
0634 ::H5Sclose(scalar);
0635 return true;
0636 }
0637
0638 inline bool write_object(hid_t a_loc,const std::string& a_name,hid_t a_file_type,char* a_data) {
0639 unsigned int chunked = 0;
0640 unsigned int compress = 0;
0641
0642 hid_t cpt = -1;
0643 if(compress || chunked) {
0644 cpt = ::H5Pcreate(H5P_DATASET_CREATE);
0645 if(cpt<0) return false;
0646 if(chunked) {
0647 if(H5Pset_layout(cpt,H5D_CHUNKED)<0) {
0648 ::H5Pclose(cpt);
0649 return false;
0650 }
0651 hsize_t cdims[1];
0652 cdims[0] = chunked;
0653 if(H5Pset_chunk(cpt,1,cdims)<0) {
0654 ::H5Pclose(cpt);
0655 return false;
0656 }
0657 } else {
0658 if(H5Pset_layout(cpt,H5D_COMPACT)<0) {
0659 ::H5Pclose(cpt);
0660 return false;
0661 }
0662 }
0663 if(compress) {
0664 if(H5Pset_deflate(cpt,compress>9?9:compress)<0) {
0665 ::H5Pclose(cpt);
0666 return false;
0667 }
0668 }
0669 } else {
0670 cpt = H5P_DEFAULT;
0671 }
0672
0673 hsize_t dims[1];
0674 dims[0] = 1;
0675 hid_t simple = ::H5Screate_simple(1,dims,NULL);
0676 if(simple<0) {
0677 if(cpt>=0) ::H5Pclose(cpt);
0678 return false;
0679 }
0680
0681 hid_t mem_type = compound_mem_type(a_file_type);
0682 if(mem_type<0) {
0683 ::H5Sclose(simple);
0684 if(cpt>=0) ::H5Pclose(cpt);
0685 return false;
0686 }
0687
0688 hid_t dataset = toolx_H5Dcreate(a_loc,a_name.c_str(),a_file_type,simple,cpt);
0689 if(dataset<0) {
0690 ::H5Tclose(mem_type);
0691 ::H5Sclose(simple);
0692 if(cpt>=0) ::H5Pclose(cpt);
0693 return false;
0694 }
0695
0696 if(H5Dwrite(dataset,mem_type,H5S_ALL,H5S_ALL,H5P_DEFAULT,a_data)<0) {
0697 ::H5Dclose(dataset);
0698 ::H5Tclose(mem_type);
0699 ::H5Sclose(simple);
0700 if(cpt>=0) ::H5Pclose(cpt);
0701 return false;
0702 }
0703
0704 ::H5Dclose(dataset);
0705 ::H5Tclose(mem_type);
0706 ::H5Sclose(simple);
0707 if(cpt>=0) ::H5Pclose(cpt);
0708
0709 return true;
0710 }
0711
0712 inline bool read_string(hid_t a_loc,const std::string& a_name,std::string& a_string) {
0713 // From H5LTread_dataset_string.
0714 hid_t dataset = toolx_H5Dopen(a_loc,a_name.c_str());
0715 if(dataset<0) {
0716 a_string.clear();
0717 return false; // data set not found.
0718 }
0719
0720 hid_t file_type = H5Dget_type(dataset);
0721 if(file_type<0) {
0722 ::H5Dclose(dataset);
0723 a_string.clear();
0724 return false;
0725 }
0726
0727 H5T_class_t t_class = H5Tget_class(file_type);
0728 if(t_class!=H5T_STRING) {
0729 ::H5Tclose(file_type);
0730 ::H5Dclose(dataset);
0731 a_string.clear();
0732 return false;
0733 }
0734
0735 size_t sz = H5Tget_size(file_type);
0736 ::H5Tclose(file_type);
0737 if(!sz) {
0738 ::H5Dclose(dataset);
0739 a_string.clear();
0740 return false;
0741 }
0742
0743 // We could have use file_type since, for string,
0744 // file type is the same than memory type.
0745 hid_t mem_type = string_datatype(sz);
0746 if(mem_type<0) {
0747 ::H5Dclose(dataset);
0748 a_string.clear();
0749 return false;
0750 }
0751
0752 char* buff = new char[sz];
0753 herr_t stat = H5Dread(dataset,mem_type,H5S_ALL,H5S_ALL,H5P_DEFAULT,buff);
0754 ::H5Tclose(mem_type);
0755 ::H5Dclose(dataset);
0756 if(stat<0) {
0757 delete [] buff;
0758 a_string.clear();
0759 return false;
0760 }
0761
0762 size_t len = sz-1;
0763 a_string.resize(len,0);
0764 for(size_t index=0;index<len;index++) a_string[index] = buff[index];
0765
0766 delete [] buff;
0767
0768 return true;
0769 }
0770
0771 inline bool read_sub_string(hid_t a_loc,const std::string& a_name,unsigned int a_offset,std::string& a_string) {
0772 hid_t dataset = toolx_H5Dopen(a_loc,a_name.c_str());
0773 if(dataset<0) {
0774 a_string.clear();
0775 return false; // data set not found.
0776 }
0777
0778 hid_t file_space = H5Dget_space(dataset);
0779 if(file_space<0) {
0780 ::H5Dclose(dataset);
0781 a_string.clear();
0782 return false;
0783 }
0784
0785 {int dimn = H5Sget_simple_extent_ndims(file_space);
0786 if(dimn<0) {
0787 ::H5Sclose(file_space);
0788 ::H5Dclose(dataset);
0789 a_string.clear();
0790 return false;
0791 }
0792 if(dimn!=1) {
0793 ::H5Sclose(file_space);
0794 ::H5Dclose(dataset);
0795 a_string.clear();
0796 return false;
0797 }}
0798
0799 hsize_t dims[1];
0800 {if(H5Sget_simple_extent_dims(file_space,dims,NULL)<0) {
0801 ::H5Sclose(file_space);
0802 ::H5Dclose(dataset);
0803 a_string.clear();
0804 return false;
0805 }}
0806
0807 {unsigned int sz = (unsigned int)dims[0];
0808 if(!sz) {
0809 ::H5Sclose(file_space);
0810 ::H5Dclose(dataset);
0811 a_string.clear();
0812 return false; //Is it ok ?
0813 }
0814
0815 // abcdef
0816 // 012345
0817 int remain = sz-a_offset;
0818 if(remain<=0) {
0819 ::H5Sclose(file_space);
0820 ::H5Dclose(dataset);
0821 a_string.clear();
0822 return false;
0823 }}
0824
0825 {hsize_t offset[1];
0826 offset[0] = a_offset;
0827 hsize_t count[1];
0828 count[0] = 1;
0829 if(H5Sselect_hyperslab(file_space,H5S_SELECT_SET,offset,NULL,count,NULL)<0) {
0830 ::H5Sclose(file_space);
0831 ::H5Dclose(dataset);
0832 a_string.clear();
0833 return false;
0834 }}
0835
0836 dims[0] = 1;
0837 hid_t mem_space = ::H5Screate_simple(1,dims,NULL);
0838 if(mem_space<0) {
0839 ::H5Sclose(file_space);
0840 ::H5Dclose(dataset);
0841 a_string.clear();
0842 return false;
0843 }
0844
0845 hid_t file_type = H5Dget_type(dataset);
0846 if(file_type<0) {
0847 ::H5Sclose(file_space);
0848 ::H5Dclose(dataset);
0849 a_string.clear();
0850 return false;
0851 }
0852
0853 H5T_class_t t_class = H5Tget_class(file_type);
0854 if(t_class!=H5T_STRING) {
0855 ::H5Tclose(file_type);
0856 ::H5Sclose(file_space);
0857 ::H5Dclose(dataset);
0858 a_string.clear();
0859 return false;
0860 }
0861
0862 //size_t sz = H5Tget_size(file_type); //it gives the largest string size in the dataset.
0863 //if(!sz) {
0864 // ::H5Tclose(file_type);
0865 // ::H5Sclose(file_space);
0866 // ::H5Dclose(dataset);
0867 // a_string.clear();
0868 // return false;
0869 //}
0870
0871 ::H5Tclose(file_type);
0872
0873 hid_t mem_type = str_datatype();
0874 if(mem_type<0) {
0875 ::H5Sclose(file_space);
0876 ::H5Dclose(dataset);
0877 a_string.clear();
0878 return false;
0879 }
0880
0881 char* rdata[1];
0882 herr_t stat = H5Dread(dataset,mem_type,mem_space,file_space,H5P_DEFAULT,rdata);
0883 if(stat<0) {
0884 ::H5Dvlen_reclaim(mem_type,mem_space, H5P_DEFAULT,rdata);
0885 ::H5Tclose(mem_type);
0886 ::H5Sclose(mem_space);
0887 ::H5Sclose(file_space);
0888 ::H5Dclose(dataset);
0889 a_string.clear();
0890 return false;
0891 }
0892
0893 char* buff = rdata[0];
0894
0895 size_t len = ::strlen(buff);
0896 a_string.resize(len,0);
0897 for(size_t index=0;index<len;index++) a_string[index] = buff[index];
0898
0899 ::H5Dvlen_reclaim(mem_type,mem_space, H5P_DEFAULT,rdata);
0900
0901 ::H5Tclose(mem_type);
0902 ::H5Sclose(mem_space);
0903 ::H5Sclose(file_space);
0904 ::H5Dclose(dataset);
0905
0906 return true;
0907 }
0908
0909 inline bool read_object(hid_t a_loc,const std::string& a_name,size_t& a_size,char*& a_data) {
0910 hid_t dataset = toolx_H5Dopen(a_loc,a_name.c_str());
0911 if(dataset<0) {
0912 a_size = 0;
0913 a_data = 0;
0914 return false;
0915 }
0916
0917 hid_t file_type = H5Dget_type(dataset);
0918 if(file_type<0) {
0919 ::H5Dclose(dataset);
0920 a_size = 0;
0921 a_data = 0;
0922 return false;
0923 }
0924
0925 H5T_class_t t_class = H5Tget_class(file_type);
0926 if(t_class!=H5T_COMPOUND) {
0927 ::H5Tclose(file_type);
0928 ::H5Dclose(dataset);
0929 a_size = 0;
0930 a_data = 0;
0931 return false;
0932 }
0933
0934 size_t sz = H5Tget_size(file_type);
0935 if(!sz) {
0936 ::H5Tclose(file_type);
0937 ::H5Dclose(dataset);
0938 a_size = 0;
0939 a_data = 0;
0940 return false;
0941 }
0942
0943 hid_t mem_type = compound_mem_type(file_type);
0944 if(mem_type<0) {
0945 ::H5Tclose(file_type);
0946 ::H5Dclose(dataset);
0947 a_size = 0;
0948 a_data = 0;
0949 return false;
0950 }
0951
0952 ::H5Tclose(file_type);
0953
0954 hid_t dataspace = H5Dget_space(dataset);
0955 if(dataspace<0) {
0956 ::H5Tclose(mem_type);
0957 ::H5Dclose(dataset);
0958 a_size = 0;
0959 a_data = 0;
0960 return false;
0961 }
0962
0963 hid_t scalar = ::H5Screate(H5S_SCALAR);
0964 if(scalar<0) {
0965 ::H5Sclose(dataspace);
0966 ::H5Tclose(mem_type);
0967 ::H5Dclose(dataset);
0968 a_size = 0;
0969 a_data = 0;
0970 return false;
0971 }
0972
0973 char* buffer = new char[sz];
0974 if(H5Dread(dataset,mem_type,scalar,dataspace,H5P_DEFAULT,buffer)<0) {
0975 delete [] buffer;
0976 ::H5Sclose(scalar);
0977 ::H5Sclose(dataspace);
0978 ::H5Tclose(mem_type);
0979 ::H5Dclose(dataset);
0980 a_size = 0;
0981 a_data = 0;
0982 return false;
0983 }
0984
0985 ::H5Sclose(scalar);
0986 ::H5Sclose(dataspace);
0987 ::H5Tclose(mem_type);
0988 ::H5Dclose(dataset);
0989
0990 a_size = sz;
0991 a_data = buffer;
0992 return true;
0993 }
0994
0995 inline bool read_array_string(hid_t a_loc,const std::string& a_name,std::vector<std::string>& a_array) {
0996 a_array.clear();
0997 hid_t dataset = toolx_H5Dopen(a_loc,a_name.c_str());
0998 if(dataset<0) return false;
0999
1000 hid_t file_type = H5Dget_type(dataset);
1001 if(file_type<0) {
1002 ::H5Dclose(dataset);
1003 return false;
1004 }
1005
1006 H5T_class_t t_class = H5Tget_class(file_type);
1007 if(t_class!=H5T_STRING) {
1008 ::H5Tclose(file_type);
1009 ::H5Dclose(dataset);
1010 return false;
1011 }
1012
1013 size_t sz = H5Tget_size(file_type);
1014 ::H5Tclose(file_type);
1015 if(!sz) {
1016 ::H5Dclose(dataset);
1017 return false;
1018 }
1019
1020 // We could have use file_type since, for string,
1021 // file type is the same than memory type.
1022 hid_t mem_type = string_datatype(sz);
1023 if(mem_type<0) {
1024 ::H5Dclose(dataset);
1025 return false;
1026 }
1027
1028 char* buffer = new char[sz];
1029 herr_t stat = H5Dread(dataset,mem_type,H5S_ALL,H5S_ALL,H5P_DEFAULT,buffer);
1030 ::H5Tclose(mem_type);
1031 ::H5Dclose(dataset);
1032 if(stat<0) {
1033 delete [] buffer;
1034 return false;
1035 }
1036
1037 if(!tools::buf2strings(sz,buffer,a_array)) {
1038 delete [] buffer;
1039 return false;
1040 }
1041
1042 delete [] buffer;
1043 return true;
1044 }
1045
1046 }}
1047
1048 #endif