Warning, /include/Geant4/toolx/hdf5/T_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_T_tools
0005 #define toolx_hdf5_T_tools
0006
0007 #include "hdf5_h"
0008
0009 #include <tools/typedefs>
0010 #include <tools/forit>
0011 #include <tools/num2s>
0012 #include <tools/vdata>
0013 //#include <map>
0014
0015 namespace toolx {
0016 namespace hdf5 {
0017
0018 inline hid_t to_T_file_type(char) {return H5T_STD_I8LE;} //H5T_STD_I8XX()
0019 inline hid_t to_T_file_type(short) {return H5T_STD_I16LE;} //HST_STD_I16XX()
0020 inline hid_t to_T_file_type(int) {return H5T_STD_I32LE;} //H5T_STD_I32XX()
0021 inline hid_t to_T_file_type(tools::int64) {return H5T_STD_I64LE;} //H5T_STD_I64XX()
0022
0023 inline hid_t to_T_file_type(float) {return H5T_IEEE_F32LE;} //H5T_IEEE_F32XX()
0024 inline hid_t to_T_file_type(double) {return H5T_IEEE_F64LE;} //H5T_IEEE_F64XX()
0025
0026 inline hid_t to_T_file_type(unsigned char) {return H5T_STD_U8LE;}
0027 inline hid_t to_T_file_type(unsigned short) {return H5T_STD_U16LE;}
0028 inline hid_t to_T_file_type(unsigned int) {return H5T_STD_U32LE;}
0029 inline hid_t to_T_file_type(tools::uint64) {return H5T_STD_U64LE;}
0030
0031 inline hid_t to_T_mem_type(char) {return H5T_NATIVE_CHAR;}
0032 inline hid_t to_T_mem_type(short) {return H5T_NATIVE_SHORT;}
0033 inline hid_t to_T_mem_type(int) {return H5T_NATIVE_INT;}
0034 inline hid_t to_T_mem_type(tools::int64) {return H5T_NATIVE_INT64;}
0035
0036 inline hid_t to_T_mem_type(float) {return H5T_NATIVE_FLOAT;}
0037 inline hid_t to_T_mem_type(double) {return H5T_NATIVE_DOUBLE;}
0038
0039 inline hid_t to_T_mem_type(unsigned char) {return H5T_NATIVE_UCHAR;}
0040 inline hid_t to_T_mem_type(unsigned short) {return H5T_NATIVE_USHORT;}
0041 inline hid_t to_T_mem_type(unsigned int) {return H5T_NATIVE_UINT;}
0042 inline hid_t to_T_mem_type(tools::uint64) {return H5T_NATIVE_UINT64;}
0043
0044 template <class T>
0045 inline bool write_array(hid_t a_loc,const std::string& a_name,
0046 hid_t a_file_type,hid_t a_mem_type,
0047 unsigned int a_chunked,unsigned int a_compress,
0048 unsigned int a_size,const T a_array[]) {
0049 if(!a_size) return false;
0050
0051 hid_t cpt = -1;
0052 if(a_compress || a_chunked) {
0053 cpt = ::H5Pcreate(H5P_DATASET_CREATE);
0054 if(cpt<0) return false;
0055 if(a_chunked) {
0056 if(H5Pset_layout(cpt,H5D_CHUNKED)<0) {
0057 ::H5Pclose(cpt);
0058 return false;
0059 }
0060 hsize_t cdims[1];
0061 cdims[0] = a_chunked;
0062 if(H5Pset_chunk(cpt,1,cdims)<0) {
0063 ::H5Pclose(cpt);
0064 return false;
0065 }
0066 } else {
0067 if(H5Pset_layout(cpt,H5D_COMPACT)<0) {
0068 ::H5Pclose(cpt);
0069 return false;
0070 }
0071 }
0072 if(a_compress) {
0073 if(H5Pset_deflate(cpt,a_compress>9?9:a_compress)<0) {
0074 ::H5Pclose(cpt);
0075 return false;
0076 }
0077 }
0078 } else {
0079 cpt = H5P_DEFAULT;
0080 }
0081
0082 hid_t dataset = -1;
0083
0084 {hsize_t dims[1];
0085 dims[0] = a_size;
0086 hid_t file_space = -1;
0087 if(a_chunked) {
0088 hsize_t mx_dims[1];
0089 mx_dims[0] = H5S_UNLIMITED; //extendable.
0090 file_space = ::H5Screate_simple(1,dims,mx_dims);
0091 } else {
0092 file_space = ::H5Screate_simple(1,dims,NULL);
0093 }
0094 if(file_space<0) {if(cpt>=0) ::H5Pclose(cpt);return false;}
0095 dataset = toolx_H5Dcreate(a_loc,a_name.c_str(),a_file_type,file_space,cpt);
0096 if(cpt>=0) ::H5Pclose(cpt);
0097 ::H5Sclose(file_space);
0098 if(dataset<0) return false;}
0099
0100 if(H5Dwrite(dataset,a_mem_type,H5S_ALL,H5S_ALL,H5P_DEFAULT,a_array)<0) {
0101 ::H5Dclose(dataset);
0102 return false;
0103 }
0104 ::H5Dclose(dataset);
0105
0106 return true;
0107 }
0108
0109 template <class T>
0110 inline bool write_vlen(hid_t a_loc,const std::string& a_name,
0111 hid_t a_file_type,hid_t a_mem_type,
0112 unsigned int a_chunked,unsigned int a_compress,
0113 unsigned int a_size,const T a_array[]) {
0114 hid_t cpt = -1;
0115 if(a_compress || a_chunked) {
0116 cpt = ::H5Pcreate(H5P_DATASET_CREATE);
0117 if(cpt<0) return false;
0118 if(a_chunked) {
0119 if(H5Pset_layout(cpt,H5D_CHUNKED)<0) {
0120 ::H5Pclose(cpt);
0121 return false;
0122 }
0123 hsize_t cdims[1];
0124 cdims[0] = a_chunked;
0125 if(H5Pset_chunk(cpt,1,cdims)<0) {
0126 ::H5Pclose(cpt);
0127 return false;
0128 }
0129 } else {
0130 if(H5Pset_layout(cpt,H5D_COMPACT)<0) {
0131 ::H5Pclose(cpt);
0132 return false;
0133 }
0134 }
0135 if(a_compress) {
0136 if(H5Pset_deflate(cpt,a_compress>9?9:a_compress)<0) {
0137 ::H5Pclose(cpt);
0138 return false;
0139 }
0140 }
0141 } else {
0142 cpt = H5P_DEFAULT;
0143 }
0144
0145 hid_t dataset = -1;
0146
0147 {hsize_t dims[1];
0148 dims[0] = 1;
0149 hid_t file_space = -1;
0150 if(a_chunked) {
0151 hsize_t mx_dims[1];
0152 mx_dims[0] = H5S_UNLIMITED; //extendable.
0153 file_space = ::H5Screate_simple(1,dims,mx_dims);
0154 } else {
0155 file_space = ::H5Screate_simple(1,dims,NULL);
0156 }
0157 if(file_space<0) {if(cpt>=0) ::H5Pclose(cpt);return false;}
0158
0159 hid_t file_type = ::H5Tvlen_create(a_file_type);
0160
0161 dataset = toolx_H5Dcreate(a_loc,a_name.c_str(),file_type,file_space,cpt);
0162 if(cpt>=0) ::H5Pclose(cpt);
0163 ::H5Sclose(file_space);
0164 ::H5Tclose(file_type);
0165 if(dataset<0) return false;}
0166
0167 hid_t mem_type = ::H5Tvlen_create(a_mem_type);
0168
0169 hvl_t wdata[1];
0170 wdata[0].len = a_size;
0171 wdata[0].p = (void*)a_array;
0172
0173 if(H5Dwrite(dataset,mem_type,H5S_ALL,H5S_ALL,H5P_DEFAULT,wdata)<0) {
0174 ::H5Tclose(mem_type);
0175 ::H5Dclose(dataset);
0176 return false;
0177 }
0178 ::H5Tclose(mem_type);
0179 ::H5Dclose(dataset);
0180
0181 return true;
0182 }
0183
0184 template <class T>
0185 inline bool write_sub_array(hid_t a_loc,const std::string& a_name,
0186 hid_t a_file_type,hid_t a_mem_type,
0187 bool a_create,
0188 unsigned int a_chunked,unsigned int a_compress, // used if a_creat = true;
0189 unsigned int a_size,
0190 unsigned int a_offset,unsigned int a_number,const T a_array[]) {
0191
0192 int remain = a_size-a_offset;
0193 int number = (int(a_number)<=remain) ? int(a_number) : remain;
0194 if(number<=0) return false;
0195
0196 hid_t cpt = -1;
0197
0198 if(a_create) {
0199 if(a_compress || a_chunked) {
0200 cpt = ::H5Pcreate(H5P_DATASET_CREATE);
0201 if(cpt<0) return false;
0202 if(a_chunked) {
0203 if(H5Pset_layout(cpt,H5D_CHUNKED)<0) {
0204 ::H5Pclose(cpt);
0205 return false;
0206 }
0207 hsize_t cdims[1];
0208 //cdims[0] = (a_size<=32?a_size:32);
0209 cdims[0] = a_chunked;
0210 if(H5Pset_chunk(cpt,1,cdims)<0) {
0211 ::H5Pclose(cpt);
0212 return false;
0213 }
0214 } else {
0215 if(H5Pset_layout(cpt,H5D_COMPACT)<0) {
0216 ::H5Pclose(cpt);
0217 return false;
0218 }
0219 }
0220 if(a_compress) {
0221 if(H5Pset_deflate(cpt,a_compress>9?9:a_compress)<0) {
0222 ::H5Pclose(cpt);
0223 return false;
0224 }
0225 }
0226 } else {
0227 cpt = H5P_DEFAULT;
0228 }
0229 }
0230
0231 hid_t dataset = -1;
0232
0233 hid_t file_space = -1;
0234
0235 if(a_create) {
0236 hsize_t dims[1];
0237 dims[0] = a_size;
0238 file_space = ::H5Screate_simple(1,dims,NULL);
0239 if(file_space<0) {
0240 if(cpt>=0) ::H5Pclose(cpt);
0241 return false;
0242 }
0243
0244 {hsize_t offset[1];
0245 offset[0] = a_offset;
0246 hsize_t count[1];
0247 count[0] = number;
0248 if(H5Sselect_hyperslab(file_space,H5S_SELECT_SET,offset,NULL,count,NULL)<0) {
0249 ::H5Sclose(file_space);
0250 if(cpt>=0) ::H5Pclose(cpt);
0251 return false;
0252 }}
0253
0254 dataset = toolx_H5Dcreate(a_loc,a_name.c_str(),a_file_type,file_space,cpt);
0255 if(dataset<0) {
0256 ::H5Sclose(file_space);
0257 if(cpt>=0) ::H5Pclose(cpt);
0258 return false;
0259 }
0260
0261 } else { //open an existing dataset :
0262 dataset = toolx_H5Dopen(a_loc,a_name.c_str());
0263 if(dataset<0) {
0264 if(cpt>=0) ::H5Pclose(cpt);
0265 return false;
0266 }
0267
0268 file_space = H5Dget_space(dataset);
0269 if(file_space<0) {
0270 ::H5Dclose(dataset);
0271 if(cpt>=0) ::H5Pclose(cpt);
0272 return false;
0273 }
0274
0275 {hsize_t offset[1];
0276 offset[0] = a_offset;
0277 hsize_t count[1];
0278 count[0] = number;
0279 if(H5Sselect_hyperslab(file_space,H5S_SELECT_SET,offset,NULL,count,NULL)<0) {
0280 ::H5Sclose(file_space);
0281 ::H5Dclose(dataset);
0282 if(cpt>=0) ::H5Pclose(cpt);
0283 return false;
0284 }}
0285
0286 }
0287
0288 hsize_t dims[1];
0289 dims[0] = number;
0290 hid_t mem_space = ::H5Screate_simple(1,dims,NULL);
0291 if(mem_space<0) {
0292 ::H5Sclose(file_space);
0293 ::H5Dclose(dataset);
0294 if(cpt>=0) ::H5Pclose(cpt);
0295 return false;
0296 }
0297
0298 if(H5Dwrite(dataset,a_mem_type,mem_space,file_space,H5P_DEFAULT,a_array)<0) {
0299 ::H5Sclose(file_space);
0300 ::H5Dclose(dataset);
0301 if(cpt>=0) ::H5Pclose(cpt);
0302 return false;
0303 }
0304
0305 ::H5Sclose(file_space);
0306 ::H5Dclose(dataset);
0307 if(cpt>=0) ::H5Pclose(cpt);
0308
0309 return true;
0310 }
0311
0312 template <class T>
0313 inline bool write_append_array_dataset(hid_t a_dataset,hid_t /*a_file_type*/,hid_t a_mem_type,
0314 unsigned int a_number,const T a_array[]) {
0315 hsize_t old_size = 0;
0316
0317 {hid_t dataspace = H5Dget_space(a_dataset);
0318 if(dataspace<0) return false;
0319 hsize_t dims[1];
0320 if(H5Sget_simple_extent_dims(dataspace,dims,NULL)<0) {
0321 ::H5Sclose(dataspace);
0322 return false;
0323 }
0324 old_size = dims[0];
0325 ::H5Sclose(dataspace);}
0326
0327 {hsize_t exts[1];
0328 exts[0] = old_size+a_number;
0329 // if(H5Dextend(dataset,exts)<0) {
0330 if(H5Dset_extent(a_dataset,exts)<0) return false;}
0331
0332 hid_t file_space = H5Dget_space(a_dataset);
0333 if(file_space<0) return false;
0334
0335 {hsize_t offset[1];
0336 offset[0] = old_size;
0337 hsize_t count[1];
0338 count[0] = a_number;
0339 if(H5Sselect_hyperslab(file_space,H5S_SELECT_SET,offset,NULL,count,NULL)<0) {
0340 ::H5Sclose(file_space);
0341 return false;
0342 }}
0343
0344 hsize_t dims[1];
0345 dims[0] = a_number;
0346 hid_t mem_space = ::H5Screate_simple(1,dims,NULL);
0347 if(mem_space<0) {
0348 ::H5Sclose(file_space);
0349 return false;
0350 }
0351
0352 if(H5Dwrite(a_dataset,a_mem_type,mem_space,file_space,H5P_DEFAULT,a_array)<0) {
0353 ::H5Sclose(mem_space);
0354 ::H5Sclose(file_space);
0355 return false;
0356 }
0357
0358 ::H5Sclose(mem_space);
0359 ::H5Sclose(file_space);
0360
0361 return true;
0362 }
0363
0364 template <class T>
0365 inline bool write_append_vlen_dataset(hid_t a_dataset,hid_t /*a_file_type*/,hid_t a_mem_type,
0366 unsigned int a_number,const T a_array[]) {
0367 hsize_t old_size = 0;
0368
0369 {hid_t dataspace = H5Dget_space(a_dataset);
0370 if(dataspace<0) return false;
0371 hsize_t dims[1];
0372 if(H5Sget_simple_extent_dims(dataspace,dims,NULL)<0) {
0373 ::H5Sclose(dataspace);
0374 return false;
0375 }
0376 old_size = dims[0];
0377 ::H5Sclose(dataspace);}
0378
0379 {hsize_t exts[1];
0380 exts[0] = old_size+1;
0381 // if(H5Dextend(dataset,exts)<0) {
0382 if(H5Dset_extent(a_dataset,exts)<0) return false;}
0383
0384 hid_t file_space = H5Dget_space(a_dataset);
0385 if(file_space<0) return false;
0386
0387 {hsize_t offset[1];
0388 offset[0] = old_size;
0389 hsize_t count[1];
0390 count[0] = 1;
0391 if(H5Sselect_hyperslab(file_space,H5S_SELECT_SET,offset,NULL,count,NULL)<0) {
0392 ::H5Sclose(file_space);
0393 return false;
0394 }}
0395
0396 hsize_t dims[1];
0397 dims[0] = 1;
0398 hid_t mem_space = ::H5Screate_simple(1,dims,NULL);
0399 if(mem_space<0) {
0400 ::H5Sclose(file_space);
0401 return false;
0402 }
0403
0404 hid_t mem_type = ::H5Tvlen_create(a_mem_type);
0405
0406 hvl_t wdata[1];
0407 wdata[0].len = a_number;
0408 wdata[0].p = (void*)a_array;
0409
0410 if(H5Dwrite(a_dataset,mem_type,mem_space,file_space,H5P_DEFAULT,wdata)<0) {
0411 ::H5Tclose(mem_type);
0412 ::H5Sclose(mem_space);
0413 ::H5Sclose(file_space);
0414 return false;
0415 }
0416
0417 ::H5Tclose(mem_type);
0418 ::H5Sclose(mem_space);
0419 ::H5Sclose(file_space);
0420
0421 return true;
0422 }
0423
0424 template <class T>
0425 inline bool write_append_array(hid_t a_loc,const std::string& a_name,hid_t a_file_type,hid_t a_mem_type,
0426 unsigned int a_number,const T a_array[]) {
0427 hid_t dataset = toolx_H5Dopen(a_loc,a_name.c_str());
0428 if(dataset<0) return false;
0429 bool status = write_append_array_dataset(dataset,a_file_type,a_mem_type,a_number,a_array);
0430 ::H5Dclose(dataset);
0431 return status;
0432 }
0433
0434 /*
0435 template <class T>
0436 inline bool write_array_struct(
0437 hid_t a_loc
0438 ,const std::string& a_name
0439 ,hid_t a_create_type
0440 ,hid_t aWriteType
0441 ,unsigned int a_size
0442 ,const T a_array[]
0443 ){
0444 hsize_t dims[1];
0445 dims[0] = a_size;
0446 hid_t dataspace = ::H5Screate_simple(1,dims,NULL);
0447 if(dataspace<0) return false;
0448
0449 hid_t dataset = toolx_H5Dcreate(a_loc,a_name.c_str(),a_create_type,dataspace,H5P_DEFAULT);
0450 if(dataset<0) {
0451 ::H5Sclose(dataspace);
0452 return false;
0453 }
0454
0455 if(H5Dwrite(dataset,aWriteType,H5S_ALL,H5S_ALL,H5P_DEFAULT,a_array)<0) {
0456 ::H5Dclose(dataset);
0457 ::H5Sclose(dataspace);
0458 return false;
0459 }
0460
0461 ::H5Dclose(dataset);
0462 ::H5Sclose(dataspace);
0463
0464 return true;
0465 }
0466 */
0467
0468 template <class T>
0469 inline bool read_scalar(hid_t a_loc,const std::string& a_name,hid_t a_mem_type,T& a_data) {
0470 hid_t dataset = toolx_H5Dopen(a_loc,a_name.c_str());
0471 if(dataset<0) return false;
0472
0473 hid_t file_space = H5Dget_space(dataset);
0474 if(file_space<0) {
0475 ::H5Dclose(dataset);
0476 return false;
0477 }
0478
0479 hid_t mem_space = ::H5Screate(H5S_SCALAR);
0480 if(mem_space<0) {
0481 ::H5Sclose(file_space);
0482 ::H5Dclose(dataset);
0483 return false;
0484 }
0485
0486 if(H5Dread(dataset,a_mem_type,mem_space,file_space,H5P_DEFAULT,&a_data)<0) {
0487 ::H5Sclose(mem_space);
0488 ::H5Sclose(file_space);
0489 ::H5Dclose(dataset);
0490 return false;
0491 }
0492
0493 ::H5Sclose(mem_space);
0494 ::H5Sclose(file_space);
0495 ::H5Dclose(dataset);
0496
0497 return true;
0498 }
0499
0500 template <class T>
0501 inline bool read_array(hid_t a_loc,const std::string& a_name,hid_t a_mem_type,unsigned int& a_size,T*& a_array,bool a_alloc = true) {
0502 hid_t dataset = toolx_H5Dopen(a_loc,a_name.c_str());
0503 if(dataset<0) {
0504 a_size = 0;
0505 if(a_alloc) a_array = 0;
0506 return false; // data set not found.
0507 }
0508
0509 hid_t file_space = H5Dget_space(dataset);
0510 if(file_space<0) {
0511 ::H5Dclose(dataset);
0512 a_size = 0;
0513 if(a_alloc) a_array = 0;
0514 return false;
0515 }
0516
0517 {int dimn = H5Sget_simple_extent_ndims(file_space);
0518 if(dimn<0) {
0519 ::H5Sclose(file_space);
0520 ::H5Dclose(dataset);
0521 a_size = 0;
0522 if(a_alloc) a_array = 0;
0523 return false;
0524 }
0525 if(dimn!=1) {
0526 ::H5Sclose(file_space);
0527 ::H5Dclose(dataset);
0528 a_size = 0;
0529 if(a_alloc) a_array = 0;
0530 return false;
0531 }
0532 //printf("debug : read dimn %d\n",dimn);
0533 }
0534
0535 hsize_t dims[1];
0536 {if(H5Sget_simple_extent_dims(file_space,dims,NULL)<0) {
0537 ::H5Sclose(file_space);
0538 ::H5Dclose(dataset);
0539 a_size = 0;
0540 if(a_alloc) a_array = 0;
0541 return false;
0542 }}
0543
0544 a_size = (unsigned int)dims[0];
0545 if(!a_size) {
0546 ::H5Sclose(file_space);
0547 ::H5Dclose(dataset);
0548 a_size = 0;
0549 if(a_alloc) a_array = 0;
0550 return true; //It is ok.
0551 }
0552
0553 hid_t mem_space = ::H5Screate_simple(1,dims,NULL);
0554 if(mem_space<0) {
0555 ::H5Sclose(file_space);
0556 ::H5Dclose(dataset);
0557 a_size = 0;
0558 if(a_alloc) a_array = 0;
0559 return false;
0560 }
0561
0562 if(a_alloc) a_array = new T[a_size];
0563 if(H5Dread(dataset,a_mem_type,mem_space,file_space,H5P_DEFAULT,a_array)<0) {
0564 if(a_alloc) delete [] a_array;
0565 ::H5Sclose(mem_space);
0566 ::H5Sclose(file_space);
0567 ::H5Dclose(dataset);
0568 a_size = 0;
0569 if(a_alloc) a_array = 0;
0570 return false;
0571 }
0572
0573
0574 ::H5Sclose(mem_space);
0575 ::H5Sclose(file_space);
0576 ::H5Dclose(dataset);
0577
0578 return true;
0579 }
0580
0581 /*
0582 template <class T>
0583 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) {
0584 hid_t dataset = toolx_H5Dopen(a_loc,a_name.c_str());
0585 if(dataset<0) {
0586 a_size = 0;
0587 a_array = 0;
0588 return false; // data set not found.
0589 }
0590
0591 hid_t file_space = H5Dget_space(dataset);
0592 if(file_space<0) {
0593 ::H5Dclose(dataset);
0594 a_size = 0;
0595 a_array = 0;
0596 return false;
0597 }
0598
0599 {int dimn = H5Sget_simple_extent_ndims(file_space);
0600 if(dimn<0) {
0601 ::H5Sclose(file_space);
0602 ::H5Dclose(dataset);
0603 a_size = 0;
0604 a_array = 0;
0605 return false;
0606 }
0607 if(dimn!=1) {
0608 ::H5Sclose(file_space);
0609 ::H5Dclose(dataset);
0610 a_size = 0;
0611 a_array = 0;
0612 return false;
0613 }
0614 //printf("debug : read dimn %d\n",dimn);
0615 }
0616
0617 hsize_t dims[1];
0618 {if(H5Sget_simple_extent_dims(file_space,dims,NULL)<0) {
0619 ::H5Sclose(file_space);
0620 ::H5Dclose(dataset);
0621 a_size = 0;
0622 a_array = 0;
0623 return false;
0624 }}
0625
0626 unsigned int _size = (unsigned int)dims[0];
0627 if(_size!=1) {
0628 ::H5Sclose(file_space);
0629 ::H5Dclose(dataset);
0630 a_size = 0;
0631 a_array = 0;
0632 return false;
0633 }
0634
0635 hid_t mem_space = ::H5Screate_simple(1,dims,NULL);
0636 if(mem_space<0) {
0637 ::H5Sclose(file_space);
0638 ::H5Dclose(dataset);
0639 a_size = 0;
0640 a_array = 0;
0641 return false;
0642 }
0643
0644 hid_t mem_type = ::H5Tvlen_create(a_mem_type);
0645
0646 hvl_t rdata[1];
0647 if(H5Dread(dataset,mem_type,mem_space,file_space,H5P_DEFAULT,rdata)<0) {
0648 //::H5Dvlen_reclaim(mem_type,mem_space,H5P_DEFAULT,rdata); ???
0649 ::H5Tclose(mem_type);
0650 ::H5Sclose(mem_space);
0651 ::H5Sclose(file_space);
0652 ::H5Dclose(dataset);
0653 a_size = 0;
0654 a_array = 0;
0655 return false;
0656 }
0657
0658 hsize_t len = rdata[0].len;
0659 if(!len) {
0660 //a_array = new T[1];
0661 a_array = 0; //it is ok.
0662 } else {
0663 a_array = new T[len];
0664 T* _data = (T*)rdata[0].p;
0665 T* pos = a_array;
0666 for(hsize_t index=0;index<len;index++,pos++,_data++) *pos = *_data;
0667 }
0668 a_size = len;
0669
0670 ::H5Dvlen_reclaim(mem_type,mem_space,H5P_DEFAULT,rdata);
0671
0672 ::H5Tclose(mem_type);
0673 ::H5Sclose(mem_space);
0674 ::H5Sclose(file_space);
0675 ::H5Dclose(dataset);
0676
0677 return true;
0678 }
0679 */
0680
0681 template <class T>
0682 inline bool read_sub_array(hid_t a_loc,const std::string& a_name,hid_t a_mem_type,
0683 unsigned int a_offset,unsigned int a_number,
0684 unsigned int& a_size,T*& a_array) {
0685 hid_t dataset = toolx_H5Dopen(a_loc,a_name.c_str());
0686 if(dataset<0) {
0687 a_size = 0;
0688 a_array = 0;
0689 return false; // data set not found.
0690 }
0691
0692 hid_t file_space = H5Dget_space(dataset);
0693 if(file_space<0) {
0694 ::H5Dclose(dataset);
0695 a_size = 0;
0696 a_array = 0;
0697 return false;
0698 }
0699
0700 {int dimn = H5Sget_simple_extent_ndims(file_space);
0701 if(dimn<0) {
0702 ::H5Sclose(file_space);
0703 ::H5Dclose(dataset);
0704 a_size = 0;
0705 a_array = 0;
0706 return false;
0707 }
0708 if(dimn!=1) {
0709 ::H5Sclose(file_space);
0710 ::H5Dclose(dataset);
0711 a_size = 0;
0712 a_array = 0;
0713 return false;
0714 }
0715 //printf("debug : read dimn %d\n",dimn);
0716 }
0717
0718 hsize_t dims[1];
0719 {if(H5Sget_simple_extent_dims(file_space,dims,NULL)<0) {
0720 ::H5Sclose(file_space);
0721 ::H5Dclose(dataset);
0722 a_size = 0;
0723 a_array = 0;
0724 return false;
0725 }}
0726
0727 unsigned int sz = (unsigned int)dims[0];
0728 if(!sz) {
0729 ::H5Sclose(file_space);
0730 ::H5Dclose(dataset);
0731 a_size = 0;
0732 a_array = 0;
0733 return true; //It is ok.
0734 }
0735
0736 // abcdef
0737 // 012345
0738 int remain = sz-a_offset;
0739 if(remain<=0) {
0740 ::H5Sclose(file_space);
0741 ::H5Dclose(dataset);
0742 a_size = 0;
0743 a_array = 0;
0744 return a_number?false:true;
0745 }
0746
0747 int number = (int(a_number)<=remain) ? int(a_number) : remain;
0748 if(number<=0) {
0749 ::H5Sclose(file_space);
0750 ::H5Dclose(dataset);
0751 a_size = 0;
0752 a_array = 0;
0753 return true; //It is ok.
0754 }
0755
0756 {hsize_t offset[1];
0757 offset[0] = a_offset;
0758 hsize_t count[1];
0759 count[0] = number;
0760 if(H5Sselect_hyperslab(file_space,H5S_SELECT_SET,offset,NULL,count,NULL)<0) {
0761 ::H5Sclose(file_space);
0762 ::H5Dclose(dataset);
0763 a_size = 0;
0764 a_array = 0;
0765 return false;
0766 }}
0767
0768 dims[0] = number;
0769 hid_t mem_space = ::H5Screate_simple(1,dims,NULL);
0770 if(mem_space<0) {
0771 ::H5Sclose(file_space);
0772 ::H5Dclose(dataset);
0773 a_size = 0;
0774 a_array = 0;
0775 return false;
0776 }
0777
0778 a_array = new T[number];
0779 if(H5Dread(dataset,a_mem_type,mem_space,file_space,H5P_DEFAULT,a_array)<0) {
0780 delete [] a_array;
0781 ::H5Sclose(mem_space);
0782 ::H5Sclose(file_space);
0783 ::H5Dclose(dataset);
0784 a_array = 0;
0785 return false;
0786 }
0787
0788 ::H5Sclose(mem_space);
0789 ::H5Sclose(file_space);
0790 ::H5Dclose(dataset);
0791
0792 a_size = number;
0793
0794 return true;
0795 }
0796
0797 template <class T>
0798 inline bool read_sub_vlen(hid_t a_loc,const std::string& a_name,hid_t a_mem_type,
0799 unsigned int a_offset,
0800 unsigned int& a_size,T*& a_array) {
0801 hid_t dataset = toolx_H5Dopen(a_loc,a_name.c_str());
0802 if(dataset<0) {
0803 a_size = 0;
0804 a_array = 0;
0805 return false; // data set not found.
0806 }
0807
0808 hid_t file_space = H5Dget_space(dataset);
0809 if(file_space<0) {
0810 ::H5Dclose(dataset);
0811 a_size = 0;
0812 a_array = 0;
0813 return false;
0814 }
0815
0816 {int dimn = H5Sget_simple_extent_ndims(file_space);
0817 if(dimn<0) {
0818 ::H5Sclose(file_space);
0819 ::H5Dclose(dataset);
0820 a_size = 0;
0821 a_array = 0;
0822 return false;
0823 }
0824 if(dimn!=1) {
0825 ::H5Sclose(file_space);
0826 ::H5Dclose(dataset);
0827 a_size = 0;
0828 a_array = 0;
0829 return false;
0830 }
0831 //printf("debug : read dimn %d\n",dimn);
0832 }
0833
0834 hsize_t dims[1];
0835 {if(H5Sget_simple_extent_dims(file_space,dims,NULL)<0) {
0836 ::H5Sclose(file_space);
0837 ::H5Dclose(dataset);
0838 a_size = 0;
0839 a_array = 0;
0840 return false;
0841 }}
0842
0843 unsigned int sz = (unsigned int)dims[0];
0844 if(!sz) {
0845 ::H5Sclose(file_space);
0846 ::H5Dclose(dataset);
0847 a_size = 0;
0848 a_array = 0;
0849 return true; //It is ok.
0850 }
0851
0852 // abcdef
0853 // 012345
0854 int remain = sz-a_offset;
0855 if(remain<=0) {
0856 ::H5Sclose(file_space);
0857 ::H5Dclose(dataset);
0858 a_size = 0;
0859 a_array = 0;
0860 return false;
0861 }
0862
0863 {hsize_t offset[1];
0864 offset[0] = a_offset;
0865 hsize_t count[1];
0866 count[0] = 1;
0867 if(H5Sselect_hyperslab(file_space,H5S_SELECT_SET,offset,NULL,count,NULL)<0) {
0868 ::H5Sclose(file_space);
0869 ::H5Dclose(dataset);
0870 a_size = 0;
0871 a_array = 0;
0872 return false;
0873 }}
0874
0875 dims[0] = 1;
0876 hid_t mem_space = ::H5Screate_simple(1,dims,NULL);
0877 if(mem_space<0) {
0878 ::H5Sclose(file_space);
0879 ::H5Dclose(dataset);
0880 a_size = 0;
0881 a_array = 0;
0882 return false;
0883 }
0884
0885 hid_t mem_type = ::H5Tvlen_create(a_mem_type);
0886
0887 hvl_t rdata[1];
0888 if(H5Dread(dataset,mem_type,mem_space,file_space,H5P_DEFAULT,rdata)<0) {
0889 //::H5Dvlen_reclaim(mem_type,mem_space,H5P_DEFAULT,rdata); ???
0890 ::H5Tclose(mem_type);
0891 ::H5Sclose(mem_space);
0892 ::H5Sclose(file_space);
0893 ::H5Dclose(dataset);
0894 a_size = 0;
0895 a_array = 0;
0896 return false;
0897 }
0898
0899 hsize_t len = rdata[0].len;
0900 if(!len) {
0901 //a_array = new T[1];
0902 a_array = 0; //it is ok.
0903 } else {
0904 a_array = new T[len];
0905 T* _data = (T*)rdata[0].p;
0906 T* pos = a_array;
0907 for(hsize_t index=0;index<len;index++,pos++,_data++) *pos = *_data;
0908 }
0909 a_size = len;
0910
0911 ::H5Dvlen_reclaim(mem_type,mem_space,H5P_DEFAULT,rdata);
0912
0913 ::H5Tclose(mem_type);
0914 ::H5Sclose(mem_space);
0915 ::H5Sclose(file_space);
0916 ::H5Dclose(dataset);
0917
0918 return true;
0919 }
0920
0921 template <class TYPE>
0922 inline bool read_scalar(hid_t a_loc,const std::string& a_name,TYPE& aValue) {
0923 return read_scalar<TYPE>(a_loc,a_name,to_T_mem_type(TYPE()),aValue);
0924 }
0925
0926 template <class T>
0927 inline bool read_std_vec(hid_t a_loc,const std::string& a_name,hid_t a_mem_type,std::vector<T>& a_vec) {
0928 tools::uint64 sz;
0929 if(!read_scalar<tools::uint64>(a_loc,a_name+"_size",sz)) return false;
0930 if(!sz) {a_vec.clear();return true;} //it is ok.
0931 a_vec.resize((size_t)sz);
0932 T* data = tools::vec_data(a_vec);
0933 unsigned int _sz;
0934 if(!read_array(a_loc,a_name,a_mem_type,_sz,data,false)) return false; //false = do not alloc.
0935 if(tools::uint64(_sz)!=sz) {a_vec.clear();return false;}
0936 return true;
0937 }
0938
0939 template <class TYPE>
0940 inline bool read_std_vec_vec(hid_t a_loc,const std::string& a_name,hid_t a_mem_type,std::vector< std::vector<TYPE> >& a_vec_vec) {
0941 tools::uint64 sz;
0942 if(!read_scalar<tools::uint64>(a_loc,a_name+"_size",sz)) {a_vec_vec.clear();return false;}
0943 a_vec_vec.resize((size_t)sz);
0944 std::string scount;
0945 for(size_t count=0;count<(size_t)sz;count++) {
0946 tools::num2s(tools::uint64(count),scount);
0947 if(!read_std_vec<TYPE>(a_loc,a_name+"_elem_"+scount,a_mem_type,a_vec_vec[count])) {a_vec_vec.clear();return false;}
0948 }
0949 return true;
0950 }
0951
0952 /*
0953 template <class T>
0954 inline bool read_array_struct(
0955 hid_t a_loc
0956 ,const std::string& a_name
0957 ,hid_t aReadType
0958 ,unsigned int& a_size
0959 ,T*& a_array
0960 ){
0961 hid_t dataset = toolx_H5Dopen(a_loc,a_name.c_str());
0962 if(dataset<0) {
0963 a_size = 0;
0964 a_array = 0;
0965 return false; // data set not found.
0966 }
0967
0968 hid_t dataspace = H5Dget_space(dataset);
0969 if(dataspace<0) {
0970 ::H5Dclose(dataset);
0971 a_size = 0;
0972 a_array = 0;
0973 return false;
0974 }
0975
0976 int dimn = H5Sget_simple_extent_ndims(dataspace);
0977 if(dimn<0) {
0978 ::H5Sclose(dataspace);
0979 ::H5Dclose(dataset);
0980 a_size = 0;
0981 a_array = 0;
0982 return false;
0983 }
0984 //printf("debug : read dimn %d\n",dimn);
0985
0986 hsize_t* dims = new hsize_t[dimn];
0987 {int rdimn = H5Sget_simple_extent_dims(dataspace,dims,NULL);
0988 if(rdimn<0) {
0989 delete [] dims;
0990 ::H5Sclose(dataspace);
0991 ::H5Dclose(dataset);
0992 a_size = 0;
0993 a_array = 0;
0994 return false;
0995 }}
0996
0997 hid_t memspace = ::H5Screate_simple(dimn,dims,NULL);
0998 if(memspace<0) {
0999 delete [] dims;
1000 ::H5Sclose(dataspace);
1001 ::H5Dclose(dataset);
1002 a_size = 0;
1003 a_array = 0;
1004 return false;
1005 }
1006
1007 a_size = (unsigned int)dims[0];
1008 delete [] dims;
1009 if(!a_size) {
1010 ::H5Sclose(memspace);
1011 ::H5Sclose(dataspace);
1012 ::H5Dclose(dataset);
1013 a_size = 0;
1014 a_array = 0;
1015 return false;
1016 }
1017
1018 a_array = new T[a_size];
1019 if(H5Dread(dataset,aReadType,memspace,dataspace,H5P_DEFAULT,a_array)<0) {
1020 delete [] a_array;
1021 ::H5Sclose(memspace);
1022 ::H5Sclose(dataspace);
1023 ::H5Dclose(dataset);
1024 a_size = 0;
1025 a_array = 0;
1026 return false;
1027 }
1028
1029 ::H5Sclose(memspace);
1030 ::H5Sclose(dataspace);
1031 ::H5Dclose(dataset);
1032
1033 return true;
1034 }
1035 */
1036
1037 template <class T>
1038 inline bool read_struct(hid_t a_loc,const std::string& a_name,hid_t aReadType,T& a_data) {
1039 hid_t dataset = toolx_H5Dopen(a_loc,a_name.c_str());
1040 if(dataset<0) return false;
1041
1042 hid_t file_space = H5Dget_space(dataset);
1043 if(file_space<0) {
1044 ::H5Dclose(dataset);
1045 return false;
1046 }
1047
1048 hid_t mem_space = ::H5Screate(H5S_SCALAR);
1049 if(mem_space<0) {
1050 ::H5Sclose(file_space);
1051 ::H5Dclose(dataset);
1052 return false;
1053 }
1054
1055 if(H5Dread(dataset,aReadType,mem_space,file_space,H5P_DEFAULT,&a_data)<0) {
1056 ::H5Sclose(mem_space);
1057 ::H5Sclose(file_space);
1058 ::H5Dclose(dataset);
1059 return false;
1060 }
1061
1062 ::H5Sclose(mem_space);
1063 ::H5Sclose(file_space);
1064 ::H5Dclose(dataset);
1065 return true;
1066 }
1067
1068 template <class TYPE>
1069 inline bool write_scalar(hid_t a_loc,const std::string& a_name,const TYPE& aData) {
1070 hid_t scalar = ::H5Screate(H5S_SCALAR);
1071 if(scalar<0) return false;
1072
1073 hid_t compact = ::H5Pcreate(H5P_DATASET_CREATE);
1074 if(compact<0) {
1075 ::H5Sclose(scalar);
1076 return false;
1077 }
1078
1079 if(H5Pset_layout(compact,H5D_COMPACT)<0) {
1080 ::H5Pclose(compact);
1081 ::H5Sclose(scalar);
1082 return false;
1083 }
1084
1085 hid_t dataset = toolx_H5Dcreate(a_loc,a_name.c_str(),to_T_file_type(TYPE()),scalar,compact);
1086 if(dataset<0) {
1087 ::H5Pclose(compact);
1088 ::H5Sclose(scalar);
1089 return false;
1090 }
1091
1092 if(::H5Dwrite(dataset,to_T_mem_type(TYPE()),H5S_ALL,H5S_ALL,H5P_DEFAULT,&aData)<0) {
1093 ::H5Pclose(compact);
1094 ::H5Sclose(scalar);
1095 ::H5Dclose(dataset);
1096 return false;
1097 }
1098
1099 ::H5Pclose(compact);
1100 ::H5Sclose(scalar);
1101 ::H5Dclose(dataset);
1102 return true;
1103 }
1104
1105 template <class T>
1106 inline bool write_struct(hid_t a_loc,const std::string& a_name,
1107 hid_t a_create_type,hid_t aWriteType,const T& aData) {
1108 hid_t scalar = ::H5Screate(H5S_SCALAR);
1109 if(scalar<0) return false;
1110
1111 hid_t compact = ::H5Pcreate(H5P_DATASET_CREATE);
1112 if(compact<0) {
1113 ::H5Sclose(scalar);
1114 return false;
1115 }
1116
1117 if(H5Pset_layout(compact,H5D_COMPACT)<0) {
1118 ::H5Pclose(compact);
1119 ::H5Sclose(scalar);
1120 return false;
1121 }
1122
1123 hid_t dataset = toolx_H5Dcreate(a_loc,a_name.c_str(),a_create_type,scalar,compact);
1124 if(dataset<0) {
1125 ::H5Pclose(compact);
1126 ::H5Sclose(scalar);
1127 return false;
1128 }
1129
1130 if(H5Dwrite(dataset,aWriteType,H5S_ALL,H5S_ALL,H5P_DEFAULT,&aData)<0) {
1131 ::H5Pclose(compact);
1132 ::H5Sclose(scalar);
1133 ::H5Dclose(dataset);
1134 return false;
1135 }
1136
1137 ::H5Pclose(compact);
1138 ::H5Sclose(scalar);
1139 ::H5Dclose(dataset);
1140 return true;
1141 }
1142
1143 //////////////////////////////////////////////////////////////////////////////
1144 //////////////////////////////////////////////////////////////////////////////
1145 //////////////////////////////////////////////////////////////////////////////
1146 template <class TYPE>
1147 inline bool write_array(hid_t a_loc,const std::string& a_name,
1148 unsigned int a_size,const TYPE a_array[],
1149 unsigned int a_chunked = 0,unsigned int a_compress = 0) {
1150 return hdf5::write_array<TYPE>(a_loc,a_name,to_T_file_type(TYPE()),to_T_mem_type(TYPE()),
1151 a_chunked,a_compress,a_size,a_array);
1152 }
1153
1154 template <class TYPE>
1155 inline bool write_vlen(hid_t a_loc,const std::string& a_name,
1156 unsigned int a_size,const TYPE a_array[],
1157 unsigned int a_chunked = 0,unsigned int a_compress = 0) {
1158 return hdf5::write_vlen<TYPE>(a_loc,a_name,to_T_file_type(TYPE()),to_T_mem_type(TYPE()),
1159 a_chunked,a_compress,a_size,a_array);
1160 }
1161
1162 template <class T>
1163 inline bool write_std_vec(hid_t a_loc,const std::string& a_name,
1164 hid_t a_file_type,hid_t a_mem_type,
1165 unsigned int a_chunked,unsigned int a_compress,
1166 const std::vector<T>& a_vec) {
1167 if(!write_scalar<tools::uint64>(a_loc,a_name+"_size",a_vec.size())) return false;
1168 if(a_vec.empty()) return true; //it is ok.
1169 const T* data = tools::vec_data(a_vec);
1170 return write_array(a_loc,a_name,a_file_type,a_mem_type,a_chunked,a_compress,a_vec.size(),data);
1171 }
1172
1173 template <class TYPE>
1174 inline bool write_std_vec(hid_t a_loc,const std::string& a_name,const std::vector<TYPE>& a_array,
1175 unsigned int a_chunked = 0,unsigned int a_compress = 0) {
1176 return hdf5::write_std_vec<TYPE>(a_loc,a_name,to_T_file_type(TYPE()),to_T_mem_type(TYPE()),a_chunked,a_compress,a_array);
1177 }
1178
1179 template <class TYPE>
1180 inline bool write_std_vec_vec(hid_t a_loc,const std::string& a_name,const std::vector< std::vector<TYPE> >& a_vec_vec,
1181 unsigned int /*a_chunked*/ = 0,unsigned int /*a_compress*/ = 0) {
1182 if(!write_scalar<tools::uint64>(a_loc,a_name+"_size",a_vec_vec.size())) return false;
1183 unsigned int count = 0; //uint for num2s.
1184 std::string scount;
1185 tools_typename_vforcit(std::vector<TYPE>,a_vec_vec,it) {
1186 tools::num2s(count,scount);
1187 if(!write_std_vec<TYPE>(a_loc,a_name+"_elem_"+scount,*it)) return false;
1188 count++;
1189 }
1190 return true;
1191 }
1192
1193 //template <class TKEY,class TVALUE>
1194 //inline bool write_std_map(hid_t a_loc,const std::string& a_name,const std::map<TKEY,TVALUE>& a_map,
1195 // unsigned int a_chunked = 0,unsigned int a_compress = 0) {
1196 // if(!write_scalar<tools::uint64>(a_loc,a_name+"_size",a_map.size())) return false;
1197 // unsigned int count = 0; //uint for num2s.
1198 // std::string scount;
1199 // tools_typename_mforcit(TKEY,TVALUE,a_map,it) {
1200 // tools::num2s(count,scount);
1201 // if(!write_scalar<TKEY>(a_loc,a_name+"_elem_"+scount+"_first",(*it).first)) return false;
1202 // if(!write_scalar<TVALUE>(a_loc,a_name+"_elem_"+scount+"_secon",(*it).second)) return false;
1203 // count++;
1204 // }
1205 // return true;
1206 //}
1207
1208 template <class TYPE>
1209 inline bool write_sub_array(hid_t a_loc,const std::string& a_name,
1210 unsigned int a_size,unsigned int a_offset,unsigned int a_number,const TYPE a_array[],
1211 bool a_create = true,unsigned int a_chunked = 0,unsigned int a_compress = 0) {
1212 return hdf5::write_sub_array<TYPE>(a_loc,a_name,to_T_file_type(TYPE()),to_T_mem_type(TYPE()),
1213 a_create,a_chunked,a_compress,
1214 a_size,a_offset,a_number,a_array);
1215 }
1216
1217 template <class TYPE>
1218 inline bool write_append_array_dataset(hid_t a_dataset,unsigned int a_number,const TYPE a_array[]) {
1219 return hdf5::write_append_array_dataset<TYPE>(a_dataset,to_T_file_type(TYPE()),to_T_mem_type(TYPE()),a_number,a_array);
1220 }
1221
1222 template <class TYPE>
1223 inline bool write_append_vlen_dataset(hid_t a_dataset,unsigned int a_number,const TYPE a_array[]) {
1224 return hdf5::write_append_vlen_dataset<TYPE>(a_dataset,to_T_file_type(TYPE()),to_T_mem_type(TYPE()),a_number,a_array);
1225 }
1226
1227 template <class TYPE>
1228 inline bool write_append_array(hid_t a_loc,const std::string& a_name,unsigned int a_number,const TYPE a_array[]) {
1229 return hdf5::write_append_array<TYPE>(a_loc,a_name,to_T_file_type(TYPE()),to_T_mem_type(TYPE()),a_number,a_array);
1230 }
1231
1232 template <class TYPE>
1233 inline bool read_array(hid_t a_loc,const std::string& a_name,unsigned int& a_size,TYPE*& a_array) {
1234 return read_array<TYPE>(a_loc,a_name,to_T_mem_type(TYPE()),a_size,a_array);
1235 }
1236
1237 //template <class TYPE>
1238 //inline bool read_vlen(hid_t a_loc,const std::string& a_name,unsigned int& a_size,TYPE*& a_array) {
1239 // return read_vlen<TYPE>(a_loc,a_name,to_T_mem_type(TYPE()),a_size,a_array);
1240 //}
1241
1242 template <class TYPE>
1243 inline bool read_std_vec(hid_t a_loc,const std::string& a_name,std::vector<TYPE>& a_vec) {
1244 return read_std_vec<TYPE>(a_loc,a_name,to_T_mem_type(TYPE()),a_vec);
1245 }
1246
1247 template <class TYPE>
1248 inline bool read_std_vec_vec(hid_t a_loc,const std::string& a_name,std::vector< std::vector<TYPE> >& a_vec_vec) {
1249 return read_std_vec_vec<TYPE>(a_loc,a_name,to_T_mem_type(TYPE()),a_vec_vec);
1250 }
1251
1252 template <class TYPE>
1253 inline bool read_sub_array(hid_t a_loc,const std::string& a_name,unsigned int a_offset,unsigned int a_number,
1254 unsigned int& a_size,TYPE*& a_array) {
1255 return read_sub_array<TYPE>(a_loc,a_name,to_T_mem_type(TYPE()),a_offset,a_number,a_size,a_array);
1256 }
1257
1258 template <class TYPE>
1259 inline bool read_sub_vlen(hid_t a_loc,const std::string& a_name,unsigned int a_offset,
1260 unsigned int& a_size,TYPE*& a_array) {
1261 return read_sub_vlen<TYPE>(a_loc,a_name,to_T_mem_type(TYPE()),a_offset,a_size,a_array);
1262 }
1263
1264 inline bool read_bool(hid_t a_loc,const std::string& a_name,bool& aValue) {
1265 unsigned char value = 0;
1266 if(!read_scalar<unsigned char>(a_loc,a_name,H5T_NATIVE_UCHAR,value)) {
1267 aValue = false;
1268 return false;
1269 }
1270 if((value!=0) && (value!=1)) {
1271 aValue = false;
1272 return false;
1273 }
1274 aValue = (value==1?true:false);
1275 return true;
1276 }
1277
1278 }}
1279
1280 #include "atb"
1281
1282 namespace toolx {
1283 namespace hdf5 {
1284
1285 template <class TYPE>
1286 inline bool write_scalar_atb(hid_t aDS,const std::string& a_name,const TYPE& aData) {
1287 int has_attr = H5LT_find_attribute(aDS,a_name.c_str());
1288 if(has_attr==1) {
1289 if(H5Adelete(aDS,a_name.c_str())<0) return false;
1290 }
1291
1292 hid_t scalar = ::H5Screate(H5S_SCALAR);
1293 if(scalar<0) return false;
1294
1295 hid_t aid = toolx_H5Acreate(aDS,a_name.c_str(),to_T_file_type(TYPE()),scalar,H5P_DEFAULT);
1296 if(aid<0) {
1297 ::H5Sclose(scalar);
1298 return false;
1299 }
1300
1301 if(H5Awrite(aid,to_T_mem_type(TYPE()),&aData)<0) {
1302 ::H5Sclose(scalar);
1303 ::H5Aclose(aid);
1304 return false;
1305 }
1306
1307 ::H5Sclose(scalar);
1308 ::H5Aclose(aid);
1309
1310 return true;
1311 }
1312
1313 }}
1314
1315 /*
1316 #include <tools/buf2lines>
1317
1318 namespace toolx {
1319 namespace hdf5 {
1320
1321 inline bool write_strings(hid_t a_loc,const std::string& a_name,
1322 size_t a_number,const char** a_strings,
1323 unsigned int a_chunked = 0,unsigned int a_compress = 0) {
1324 size_t sz;char* buffer;
1325 if(!tools::strings2buf(a_number,a_strings,sz,buffer)) return false;
1326 sz--;
1327 bool status = hdf5::write_array<char>(a_loc,a_name,to_T_file_type(char()),to_T_mem_type(char()),
1328 a_chunked,a_compress,(unsigned int)sz,buffer);
1329 delete [] buffer;
1330 return status;
1331 }
1332
1333 inline bool write_strings(hid_t a_loc,const std::string& a_name,
1334 const std::vector<std::string>& a_strings,
1335 unsigned int a_chunked = 0,unsigned int a_compress = 0) {
1336 size_t sz;char* buffer;
1337 if(!tools::strings2buf(a_strings,sz,buffer)) return false;
1338 sz--;
1339 bool status = hdf5::write_array<char>(a_loc,a_name,to_T_file_type(char()),to_T_mem_type(char()),
1340 a_chunked,a_compress,(unsigned int)sz,buffer);
1341 delete [] buffer;
1342 return status;
1343 }
1344
1345 inline bool write_append_strings(hid_t a_loc,const std::string& a_name,size_t a_number,const char** a_strings) {
1346 size_t sz;char* buffer;
1347 if(!tools::strings2buf(a_number,a_strings,sz,buffer)) return false;
1348 sz--;
1349 bool status = hdf5::write_append_array<char>(a_loc,a_name,to_T_file_type(char()),to_T_mem_type(char()),(unsigned int)sz,buffer);
1350 delete [] buffer;
1351 return status;
1352 }
1353
1354 inline bool write_append_strings(hid_t a_loc,const std::string& a_name,const std::vector<std::string>& a_strings) {
1355 size_t sz;char* buffer;
1356 if(!tools::strings2buf(a_strings,sz,buffer)) return false;
1357 sz--;
1358 bool status = hdf5::write_append_array<char>(a_loc,a_name,to_T_file_type(char()),to_T_mem_type(char()),(unsigned int)sz,buffer);
1359 delete [] buffer;
1360 return status;
1361 }
1362
1363 }}
1364 */
1365
1366 #endif