Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights 1 // Copyright (C) 2010, Guy Barrand. All rights reserved. 2 // See the file tools.license for terms. 2 // See the file tools.license for terms. 3 3 4 #ifndef toolx_hdf5_tools 4 #ifndef toolx_hdf5_tools 5 #define toolx_hdf5_tools 5 #define toolx_hdf5_tools 6 6 7 #include "hdf5_h" 7 #include "hdf5_h" 8 8 9 #include <cstdlib> 9 #include <cstdlib> 10 10 11 namespace toolx { 11 namespace toolx { 12 namespace hdf5 { 12 namespace hdf5 { 13 13 14 inline bool check_sizes(){ 14 inline bool check_sizes(){ 15 if(sizeof(bool)!=1) return false; 15 if(sizeof(bool)!=1) return false; 16 if(sizeof(char)!=1) return false; 16 if(sizeof(char)!=1) return false; 17 if(sizeof(short)!=2) return false; 17 if(sizeof(short)!=2) return false; 18 if(sizeof(int)!=4) return false; 18 if(sizeof(int)!=4) return false; 19 if(sizeof(float)!=4) return false; 19 if(sizeof(float)!=4) return false; 20 if(sizeof(double)!=8) return false; 20 if(sizeof(double)!=8) return false; 21 return true; 21 return true; 22 } 22 } 23 23 24 inline int failure() {return -1;} 24 inline int failure() {return -1;} 25 25 26 inline hid_t string_datatype(size_t aSize){ 26 inline hid_t string_datatype(size_t aSize){ 27 // aSize should include the trailing null ch 27 // aSize should include the trailing null char. 28 hid_t datatype = ::H5Tcopy(H5T_C_S1); 28 hid_t datatype = ::H5Tcopy(H5T_C_S1); 29 if(datatype<0) return failure(); 29 if(datatype<0) return failure(); 30 30 31 if(::H5Tset_size(datatype,aSize)<0) { 31 if(::H5Tset_size(datatype,aSize)<0) { 32 ::H5Tclose(datatype); 32 ::H5Tclose(datatype); 33 return failure(); 33 return failure(); 34 } 34 } 35 35 36 if(::H5Tset_strpad(datatype,H5T_STR_NULLTERM 36 if(::H5Tset_strpad(datatype,H5T_STR_NULLTERM)<0) { 37 ::H5Tclose(datatype); 37 ::H5Tclose(datatype); 38 return failure(); 38 return failure(); 39 } 39 } 40 40 41 return datatype; 41 return datatype; 42 } 42 } 43 43 44 inline hid_t str_datatype() { 44 inline hid_t str_datatype() { 45 hid_t datatype = ::H5Tcopy(H5T_C_S1); 45 hid_t datatype = ::H5Tcopy(H5T_C_S1); 46 if(datatype<0) return failure(); 46 if(datatype<0) return failure(); 47 if(::H5Tset_size(datatype,H5T_VARIABLE)<0) { 47 if(::H5Tset_size(datatype,H5T_VARIABLE)<0) { 48 ::H5Tclose(datatype); 48 ::H5Tclose(datatype); 49 return failure(); 49 return failure(); 50 } 50 } 51 if(::H5Tset_strpad(datatype,H5T_STR_NULLTERM 51 if(::H5Tset_strpad(datatype,H5T_STR_NULLTERM)<0) { 52 ::H5Tclose(datatype); 52 ::H5Tclose(datatype); 53 return failure(); 53 return failure(); 54 } 54 } 55 return datatype; 55 return datatype; 56 } 56 } 57 57 58 inline hid_t basic_mem_type(hid_t a_file_type) 58 inline hid_t basic_mem_type(hid_t a_file_type){ 59 H5T_class_t mclass = H5Tget_class(a_file_typ 59 H5T_class_t mclass = H5Tget_class(a_file_type); 60 size_t msize = H5Tget_size(a_file_type); 60 size_t msize = H5Tget_size(a_file_type); 61 if(mclass==H5T_INTEGER) { 61 if(mclass==H5T_INTEGER) { 62 H5T_sign_t msign = H5Tget_sign(a_file_type 62 H5T_sign_t msign = H5Tget_sign(a_file_type); 63 if(msize==1) { 63 if(msize==1) { 64 if(msign==H5T_SGN_NONE) { 64 if(msign==H5T_SGN_NONE) { 65 return H5Tcopy(H5T_NATIVE_UCHAR); 65 return H5Tcopy(H5T_NATIVE_UCHAR); 66 } else { 66 } else { 67 return H5Tcopy(H5T_NATIVE_CHAR); 67 return H5Tcopy(H5T_NATIVE_CHAR); 68 } 68 } 69 } else if(msize==4) { 69 } else if(msize==4) { 70 if(msign==H5T_SGN_NONE) { 70 if(msign==H5T_SGN_NONE) { 71 return H5Tcopy(H5T_NATIVE_UINT); 71 return H5Tcopy(H5T_NATIVE_UINT); 72 } else { 72 } else { 73 return H5Tcopy(H5T_NATIVE_INT); 73 return H5Tcopy(H5T_NATIVE_INT); 74 } 74 } 75 } else if(msize==8) { //for osc_file::head 75 } else if(msize==8) { //for osc_file::header::fDate. 76 if(msign==H5T_SGN_NONE) { 76 if(msign==H5T_SGN_NONE) { 77 return H5Tcopy(H5T_NATIVE_UINT64); 77 return H5Tcopy(H5T_NATIVE_UINT64); 78 } else { 78 } else { 79 return H5Tcopy(H5T_NATIVE_INT64); 79 return H5Tcopy(H5T_NATIVE_INT64); 80 } 80 } 81 } else { 81 } else { 82 return failure(); 82 return failure(); 83 } 83 } 84 } else if(mclass==H5T_FLOAT) { 84 } else if(mclass==H5T_FLOAT) { 85 if(msize==4) { 85 if(msize==4) { 86 return H5Tcopy(H5T_NATIVE_FLOAT); 86 return H5Tcopy(H5T_NATIVE_FLOAT); 87 } else if(msize==8) { 87 } else if(msize==8) { 88 return H5Tcopy(H5T_NATIVE_DOUBLE); 88 return H5Tcopy(H5T_NATIVE_DOUBLE); 89 } else { 89 } else { 90 return failure(); 90 return failure(); 91 } 91 } 92 } else if(mclass==H5T_STRING) { 92 } else if(mclass==H5T_STRING) { 93 return H5Tcopy(a_file_type); 93 return H5Tcopy(a_file_type); 94 } 94 } 95 95 96 return failure(); 96 return failure(); 97 } 97 } 98 98 99 }} 99 }} 100 100 101 #include <vector> 101 #include <vector> 102 102 103 namespace toolx { 103 namespace toolx { 104 namespace hdf5 { 104 namespace hdf5 { 105 105 106 inline hid_t compound_mem_type(hid_t a_file_ty 106 inline hid_t compound_mem_type(hid_t a_file_type){ 107 // FIXME : In principle H5T_get_native_type 107 // FIXME : In principle H5T_get_native_type should do the job but it crashes. 108 108 109 H5T_class_t t_class = H5Tget_class(a_file_ty 109 H5T_class_t t_class = H5Tget_class(a_file_type); 110 if(t_class!=H5T_COMPOUND) return failure(); 110 if(t_class!=H5T_COMPOUND) return failure(); 111 111 112 size_t sz = H5Tget_size(a_file_type); 112 size_t sz = H5Tget_size(a_file_type); 113 //printf("debug : compound_mem_type : sz %lu 113 //printf("debug : compound_mem_type : sz %lu\n",sz); 114 114 115 hid_t mem_type = ::H5Tcreate(H5T_COMPOUND,sz 115 hid_t mem_type = ::H5Tcreate(H5T_COMPOUND,sz); 116 if(mem_type<0) return failure(); 116 if(mem_type<0) return failure(); 117 117 118 //FIXME : WARNING : is order the booked orde 118 //FIXME : WARNING : is order the booked order ? 119 119 120 int mn = H5Tget_nmembers(a_file_type); 120 int mn = H5Tget_nmembers(a_file_type); 121 std::vector<unsigned int> szs(mn); 121 std::vector<unsigned int> szs(mn); 122 //printf("debug : members : %d\n",mn); 122 //printf("debug : members : %d\n",mn); 123 for(int index=0;index<mn;index++) { 123 for(int index=0;index<mn;index++) { 124 char* mname = H5Tget_member_name(a_file_ty 124 char* mname = H5Tget_member_name(a_file_type,index); 125 size_t moffset = H5Tget_member_offset(a_fi 125 size_t moffset = H5Tget_member_offset(a_file_type,index); 126 hid_t mtype = H5Tget_member_type(a_file_ty 126 hid_t mtype = H5Tget_member_type(a_file_type,index); 127 //printf("debug : members : %d (%d) : %s 127 //printf("debug : members : %d (%d) : %s : begin\n",index,mn,mname); 128 128 129 {H5T_class_t mclass = H5Tget_class(mtype); 129 {H5T_class_t mclass = H5Tget_class(mtype); 130 if( (mclass==H5T_INTEGER) || 130 if( (mclass==H5T_INTEGER) || 131 (mclass==H5T_STRING) || 131 (mclass==H5T_STRING) || 132 (mclass==H5T_FLOAT) ) { 132 (mclass==H5T_FLOAT) ) { 133 hid_t mmem_type = basic_mem_type(mtype); 133 hid_t mmem_type = basic_mem_type(mtype); 134 if(mmem_type<0) { 134 if(mmem_type<0) { 135 ::H5Tclose(mtype); 135 ::H5Tclose(mtype); 136 if(mname) toolx_H5free_memory(mname); 136 if(mname) toolx_H5free_memory(mname); 137 ::H5Tclose(mem_type); 137 ::H5Tclose(mem_type); 138 return failure(); 138 return failure(); 139 } 139 } 140 if(H5Tinsert(mem_type,mname,moffset,mmem 140 if(H5Tinsert(mem_type,mname,moffset,mmem_type)<0) { 141 ::H5Tclose(mmem_type); 141 ::H5Tclose(mmem_type); 142 ::H5Tclose(mtype); 142 ::H5Tclose(mtype); 143 if(mname) toolx_H5free_memory(mname); 143 if(mname) toolx_H5free_memory(mname); 144 ::H5Tclose(mem_type); 144 ::H5Tclose(mem_type); 145 return failure(); 145 return failure(); 146 } 146 } 147 ::H5Tclose(mmem_type); 147 ::H5Tclose(mmem_type); 148 148 149 } else if(mclass==H5T_ARRAY) { 149 } else if(mclass==H5T_ARRAY) { 150 int dimn = ::H5Tget_array_ndims(mtype); 150 int dimn = ::H5Tget_array_ndims(mtype); //Should be 1; 151 hsize_t* dims = new hsize_t[dimn]; 151 hsize_t* dims = new hsize_t[dimn]; 152 int* perms = new int[dimn]; 152 int* perms = new int[dimn]; 153 if(toolx_H5Tget_array_dims(mtype,dims,pe 153 if(toolx_H5Tget_array_dims(mtype,dims,perms)<0) { 154 delete [] dims; 154 delete [] dims; 155 delete [] perms; 155 delete [] perms; 156 ::H5Tclose(mtype); 156 ::H5Tclose(mtype); 157 if(mname) toolx_H5free_memory(mname); 157 if(mname) toolx_H5free_memory(mname); 158 ::H5Tclose(mem_type); 158 ::H5Tclose(mem_type); 159 return failure(); 159 return failure(); 160 } 160 } 161 hid_t base_type = H5Tget_super(mtype); 161 hid_t base_type = H5Tget_super(mtype); 162 if(base_type<0) { 162 if(base_type<0) { 163 delete [] dims; 163 delete [] dims; 164 delete [] perms; 164 delete [] perms; 165 ::H5Tclose(mtype); 165 ::H5Tclose(mtype); 166 if(mname) toolx_H5free_memory(mname); 166 if(mname) toolx_H5free_memory(mname); 167 ::H5Tclose(mem_type); 167 ::H5Tclose(mem_type); 168 return failure(); 168 return failure(); 169 } 169 } 170 hid_t mmem_type = basic_mem_type(base_ty 170 hid_t mmem_type = basic_mem_type(base_type); 171 if(mmem_type<0) { 171 if(mmem_type<0) { 172 delete [] dims; 172 delete [] dims; 173 delete [] perms; 173 delete [] perms; 174 ::H5Tclose(base_type); 174 ::H5Tclose(base_type); 175 ::H5Tclose(mtype); 175 ::H5Tclose(mtype); 176 if(mname) toolx_H5free_memory(mname); 176 if(mname) toolx_H5free_memory(mname); 177 ::H5Tclose(mem_type); 177 ::H5Tclose(mem_type); 178 return failure(); 178 return failure(); 179 } 179 } 180 ::H5Tclose(base_type); 180 ::H5Tclose(base_type); 181 hid_t array_type = toolx_H5Tarray_create 181 hid_t array_type = toolx_H5Tarray_create(mmem_type,dimn,dims,perms); 182 delete [] dims; 182 delete [] dims; 183 delete [] perms; 183 delete [] perms; 184 if(array_type<0) { 184 if(array_type<0) { 185 ::H5Tclose(mmem_type); 185 ::H5Tclose(mmem_type); 186 ::H5Tclose(mtype); 186 ::H5Tclose(mtype); 187 if(mname) toolx_H5free_memory(mname); 187 if(mname) toolx_H5free_memory(mname); 188 ::H5Tclose(mem_type); 188 ::H5Tclose(mem_type); 189 return failure(); 189 return failure(); 190 } 190 } 191 ::H5Tclose(mmem_type); 191 ::H5Tclose(mmem_type); 192 192 193 if(H5Tinsert(mem_type,mname,moffset,arra 193 if(H5Tinsert(mem_type,mname,moffset,array_type)<0) { 194 ::H5Tclose(array_type); 194 ::H5Tclose(array_type); 195 ::H5Tclose(mtype); 195 ::H5Tclose(mtype); 196 if(mname) toolx_H5free_memory(mname); 196 if(mname) toolx_H5free_memory(mname); 197 ::H5Tclose(mem_type); 197 ::H5Tclose(mem_type); 198 return failure(); 198 return failure(); 199 } 199 } 200 ::H5Tclose(array_type); 200 ::H5Tclose(array_type); 201 201 202 } else if(mclass==H5T_COMPOUND) { 202 } else if(mclass==H5T_COMPOUND) { 203 hid_t mmem_type = compound_mem_type(mtyp 203 hid_t mmem_type = compound_mem_type(mtype); 204 if(mem_type<0) { 204 if(mem_type<0) { 205 ::H5Tclose(mtype); 205 ::H5Tclose(mtype); 206 if(mname) toolx_H5free_memory(mname); 206 if(mname) toolx_H5free_memory(mname); 207 ::H5Tclose(mem_type); 207 ::H5Tclose(mem_type); 208 return failure(); 208 return failure(); 209 } 209 } 210 if(H5Tinsert(mem_type,mname,moffset,mmem 210 if(H5Tinsert(mem_type,mname,moffset,mmem_type)<0) { 211 ::H5Tclose(mmem_type); 211 ::H5Tclose(mmem_type); 212 ::H5Tclose(mtype); 212 ::H5Tclose(mtype); 213 if(mname) toolx_H5free_memory(mname); 213 if(mname) toolx_H5free_memory(mname); 214 ::H5Tclose(mem_type); 214 ::H5Tclose(mem_type); 215 return failure(); 215 return failure(); 216 } 216 } 217 ::H5Tclose(mmem_type); 217 ::H5Tclose(mmem_type); 218 } else { 218 } else { 219 ::H5Tclose(mtype); 219 ::H5Tclose(mtype); 220 if(mname) toolx_H5free_memory(mname); 220 if(mname) toolx_H5free_memory(mname); 221 ::H5Tclose(mem_type); 221 ::H5Tclose(mem_type); 222 return failure(); 222 return failure(); 223 }} 223 }} 224 ::H5Tclose(mtype); 224 ::H5Tclose(mtype); 225 //printf("debug : compound_mem_type : %d 225 //printf("debug : compound_mem_type : %d (%d) : %s : end\n",index,mn,mname); 226 if(mname) toolx_H5free_memory(mname); 226 if(mname) toolx_H5free_memory(mname); 227 } 227 } 228 228 229 return mem_type; 229 return mem_type; 230 } 230 } 231 231 232 }} 232 }} 233 233 234 #include "atb" 234 #include "atb" 235 #include <string> 235 #include <string> 236 236 237 namespace toolx { 237 namespace toolx { 238 namespace hdf5 { 238 namespace hdf5 { 239 239 240 inline bool read_atb(hid_t a_id,const std::str 240 inline bool read_atb(hid_t a_id,const std::string& a_name,std::string& a_data,unsigned int aSize = 100){ 241 // From H5LT.c/H5LTget_attribute_string. 241 // From H5LT.c/H5LTget_attribute_string. 242 if(!H5LT_find_attribute(a_id,a_name.c_str()) 242 if(!H5LT_find_attribute(a_id,a_name.c_str())) {a_data.clear();return false;} 243 char* b = new char[aSize]; 243 char* b = new char[aSize]; 244 if(H5LT_get_attribute_disk(a_id,a_name.c_str 244 if(H5LT_get_attribute_disk(a_id,a_name.c_str(),b)<0) { 245 delete [] b; 245 delete [] b; 246 a_data.clear(); 246 a_data.clear(); 247 return false; 247 return false; 248 } 248 } 249 a_data = std::string(b); 249 a_data = std::string(b); 250 delete [] b; 250 delete [] b; 251 return true; 251 return true; 252 } 252 } 253 253 254 inline bool read_atb(hid_t a_id,const std::str 254 inline bool read_atb(hid_t a_id,const std::string& a_name,unsigned int& a_data){ 255 if(!H5LT_find_attribute(a_id,a_name.c_str()) 255 if(!H5LT_find_attribute(a_id,a_name.c_str())) {a_data=0;return false;} 256 if(H5LT_get_attribute_mem(a_id,a_name.c_str( 256 if(H5LT_get_attribute_mem(a_id,a_name.c_str(),H5T_NATIVE_UINT,&a_data)<0) {a_data=0;return false;} 257 return true; 257 return true; 258 } 258 } 259 259 260 inline bool read_atb(hid_t a_id,const std::str 260 inline bool read_atb(hid_t a_id,const std::string& a_name,int& a_data){ 261 if(!H5LT_find_attribute(a_id,a_name.c_str()) 261 if(!H5LT_find_attribute(a_id,a_name.c_str())) {a_data=0;return false;} 262 if(H5LT_get_attribute_mem(a_id,a_name.c_str( 262 if(H5LT_get_attribute_mem(a_id,a_name.c_str(),H5T_NATIVE_INT,&a_data)<0) {a_data=0;return false;} 263 return true; 263 return true; 264 } 264 } 265 265 266 inline hid_t H5T_STD_U8XX() {return H5T_STD_U8 266 inline hid_t H5T_STD_U8XX() {return H5T_STD_U8LE;} 267 inline hid_t H5T_STD_U32XX() {return H5T_STD_U 267 inline hid_t H5T_STD_U32XX() {return H5T_STD_U32LE;} 268 inline hid_t H5T_STD_U64XX() {return H5T_STD_U 268 inline hid_t H5T_STD_U64XX() {return H5T_STD_U64LE;} 269 inline hid_t H5T_STD_I8XX() {return H5T_STD_I8 269 inline hid_t H5T_STD_I8XX() {return H5T_STD_I8LE;} 270 inline hid_t H5T_STD_I16XX() {return H5T_STD_I 270 inline hid_t H5T_STD_I16XX() {return H5T_STD_I16LE;} 271 inline hid_t H5T_STD_I32XX() {return H5T_STD_I 271 inline hid_t H5T_STD_I32XX() {return H5T_STD_I32LE;} 272 inline hid_t H5T_STD_I64XX() {return H5T_STD_I 272 inline hid_t H5T_STD_I64XX() {return H5T_STD_I64LE;} 273 inline hid_t H5T_IEEE_F32XX() {return H5T_IEEE 273 inline hid_t H5T_IEEE_F32XX() {return H5T_IEEE_F32LE;} 274 inline hid_t H5T_IEEE_F64XX() {return H5T_IEEE 274 inline hid_t H5T_IEEE_F64XX() {return H5T_IEEE_F64LE;} 275 275 276 inline bool dataset_vec_size(hid_t a_loc,const 276 inline bool dataset_vec_size(hid_t a_loc,const std::string& a_name,hsize_t& a_size) { 277 hid_t dataset = toolx_H5Dopen(a_loc,a_name.c 277 hid_t dataset = toolx_H5Dopen(a_loc,a_name.c_str()); 278 if(dataset<0) { 278 if(dataset<0) { 279 a_size = 0; 279 a_size = 0; 280 return false; // data set not found. 280 return false; // data set not found. 281 } 281 } 282 282 283 hid_t file_space = H5Dget_space(dataset); 283 hid_t file_space = H5Dget_space(dataset); 284 if(file_space<0) { 284 if(file_space<0) { 285 ::H5Dclose(dataset); 285 ::H5Dclose(dataset); 286 a_size = 0; 286 a_size = 0; 287 return false; 287 return false; 288 } 288 } 289 289 290 int dimn = H5Sget_simple_extent_ndims(file_s 290 int dimn = H5Sget_simple_extent_ndims(file_space); 291 if(dimn<0) { 291 if(dimn<0) { 292 ::H5Sclose(file_space); 292 ::H5Sclose(file_space); 293 ::H5Dclose(dataset); 293 ::H5Dclose(dataset); 294 a_size = 0; 294 a_size = 0; 295 return false; 295 return false; 296 } 296 } 297 if(dimn!=1) { 297 if(dimn!=1) { 298 ::H5Sclose(file_space); 298 ::H5Sclose(file_space); 299 ::H5Dclose(dataset); 299 ::H5Dclose(dataset); 300 a_size = 0; 300 a_size = 0; 301 return false; 301 return false; 302 } 302 } 303 //printf("debug : read dimn %d\n",dimn); 303 //printf("debug : read dimn %d\n",dimn); 304 304 305 hsize_t dims[1]; 305 hsize_t dims[1]; 306 {if(H5Sget_simple_extent_dims(file_space,dims 306 {if(H5Sget_simple_extent_dims(file_space,dims,NULL)<0) { 307 ::H5Sclose(file_space); 307 ::H5Sclose(file_space); 308 ::H5Dclose(dataset); 308 ::H5Dclose(dataset); 309 a_size = 0; 309 a_size = 0; 310 return false; 310 return false; 311 }} 311 }} 312 312 313 ::H5Sclose(file_space); 313 ::H5Sclose(file_space); 314 ::H5Dclose(dataset); 314 ::H5Dclose(dataset); 315 315 316 a_size = dims[0]; 316 a_size = dims[0]; 317 return true; 317 return true; 318 } 318 } 319 319 320 inline bool write_atb(hid_t a_id,const std::st 320 inline bool write_atb(hid_t a_id,const std::string& a_name,const std::string& a_data){ 321 // From H5LT.c/H5LTset_attribute_string. 321 // From H5LT.c/H5LTset_attribute_string. 322 int has_attr = H5LT_find_attribute(a_id,a_na 322 int has_attr = H5LT_find_attribute(a_id,a_name.c_str()); 323 if(has_attr==1) { 323 if(has_attr==1) { 324 if(H5Adelete(a_id,a_name.c_str())<0) retur 324 if(H5Adelete(a_id,a_name.c_str())<0) return false; 325 } 325 } 326 326 327 hid_t datatype = string_datatype(a_data.size 327 hid_t datatype = string_datatype(a_data.size()+1); 328 if(datatype<0) return false; 328 if(datatype<0) return false; 329 329 330 hid_t scalar = ::H5Screate(H5S_SCALAR); 330 hid_t scalar = ::H5Screate(H5S_SCALAR); 331 if(scalar<0) { 331 if(scalar<0) { 332 ::H5Tclose(datatype); 332 ::H5Tclose(datatype); 333 return false; 333 return false; 334 } 334 } 335 335 336 hid_t aid = toolx_H5Acreate(a_id,a_name.c_st 336 hid_t aid = toolx_H5Acreate(a_id,a_name.c_str(),datatype,scalar,H5P_DEFAULT); 337 if(aid<0) { 337 if(aid<0) { 338 ::H5Sclose(scalar); 338 ::H5Sclose(scalar); 339 ::H5Tclose(datatype); 339 ::H5Tclose(datatype); 340 return false; 340 return false; 341 } 341 } 342 342 343 if(H5Awrite(aid,datatype,a_data.c_str())<0) 343 if(H5Awrite(aid,datatype,a_data.c_str())<0) { 344 ::H5Aclose(aid); 344 ::H5Aclose(aid); 345 ::H5Sclose(scalar); 345 ::H5Sclose(scalar); 346 ::H5Tclose(datatype); 346 ::H5Tclose(datatype); 347 return false; 347 return false; 348 } 348 } 349 349 350 ::H5Aclose(aid); 350 ::H5Aclose(aid); 351 ::H5Sclose(scalar); 351 ::H5Sclose(scalar); 352 ::H5Tclose(datatype); 352 ::H5Tclose(datatype); 353 353 354 return true; 354 return true; 355 } 355 } 356 356 357 inline bool write_bool(hid_t a_loc,const std:: 357 inline bool write_bool(hid_t a_loc,const std::string& a_name,bool a_data) { 358 hid_t scalar = ::H5Screate(H5S_SCALAR); 358 hid_t scalar = ::H5Screate(H5S_SCALAR); 359 if(scalar<0) return false; 359 if(scalar<0) return false; 360 360 361 hid_t compact = ::H5Pcreate(H5P_DATASET_CREA 361 hid_t compact = ::H5Pcreate(H5P_DATASET_CREATE); 362 if(compact<0) { 362 if(compact<0) { 363 ::H5Sclose(scalar); 363 ::H5Sclose(scalar); 364 return false; 364 return false; 365 } 365 } 366 if(H5Pset_layout(compact,H5D_COMPACT)<0) { 366 if(H5Pset_layout(compact,H5D_COMPACT)<0) { 367 ::H5Pclose(compact); 367 ::H5Pclose(compact); 368 ::H5Sclose(scalar); 368 ::H5Sclose(scalar); 369 return false; 369 return false; 370 } 370 } 371 371 372 hid_t dataset = toolx_H5Dcreate(a_loc,a_name 372 hid_t dataset = toolx_H5Dcreate(a_loc,a_name.c_str(),H5T_STD_U8XX(),scalar,compact); 373 if(dataset<0) { 373 if(dataset<0) { 374 ::H5Pclose(compact); 374 ::H5Pclose(compact); 375 ::H5Sclose(scalar); 375 ::H5Sclose(scalar); 376 return false; 376 return false; 377 } 377 } 378 378 379 unsigned char data = a_data?1:0; 379 unsigned char data = a_data?1:0; 380 if(::H5Dwrite(dataset,H5T_NATIVE_UCHAR,H5S_A 380 if(::H5Dwrite(dataset,H5T_NATIVE_UCHAR,H5S_ALL,H5S_ALL,H5P_DEFAULT,&data)<0) { 381 ::H5Pclose(compact); 381 ::H5Pclose(compact); 382 ::H5Sclose(scalar); 382 ::H5Sclose(scalar); 383 ::H5Dclose(dataset); 383 ::H5Dclose(dataset); 384 return false; 384 return false; 385 } 385 } 386 386 387 ::H5Pclose(compact); 387 ::H5Pclose(compact); 388 ::H5Sclose(scalar); 388 ::H5Sclose(scalar); 389 ::H5Dclose(dataset); 389 ::H5Dclose(dataset); 390 return true; 390 return true; 391 } 391 } 392 392 393 inline bool write_string(hid_t a_loc,const std 393 inline bool write_string(hid_t a_loc,const std::string& a_name,const std::string& a_string) { 394 hid_t scalar = ::H5Screate(H5S_SCALAR); 394 hid_t scalar = ::H5Screate(H5S_SCALAR); 395 if(scalar<0) return false; 395 if(scalar<0) return false; 396 396 397 hid_t compact = ::H5Pcreate(H5P_DATASET_CREA 397 hid_t compact = ::H5Pcreate(H5P_DATASET_CREATE); 398 if(compact<0) { 398 if(compact<0) { 399 ::H5Sclose(scalar); 399 ::H5Sclose(scalar); 400 return false; 400 return false; 401 } 401 } 402 402 403 if(H5Pset_layout(compact,H5D_COMPACT)<0) { 403 if(H5Pset_layout(compact,H5D_COMPACT)<0) { 404 ::H5Pclose(compact); 404 ::H5Pclose(compact); 405 ::H5Sclose(scalar); 405 ::H5Sclose(scalar); 406 return false; 406 return false; 407 } 407 } 408 408 409 // From H5LTmakge_dataset_string. 409 // From H5LTmakge_dataset_string. 410 hid_t file_type = string_datatype(a_string.s 410 hid_t file_type = string_datatype(a_string.size()+1); 411 if(file_type<0) { 411 if(file_type<0) { 412 ::H5Pclose(compact); 412 ::H5Pclose(compact); 413 ::H5Sclose(scalar); 413 ::H5Sclose(scalar); 414 return false; 414 return false; 415 } 415 } 416 416 417 hid_t dataset = toolx_H5Dcreate(a_loc,a_name 417 hid_t dataset = toolx_H5Dcreate(a_loc,a_name.c_str(),file_type,scalar,compact); 418 if(dataset<0) { 418 if(dataset<0) { 419 ::H5Pclose(compact); 419 ::H5Pclose(compact); 420 ::H5Sclose(scalar); 420 ::H5Sclose(scalar); 421 ::H5Tclose(file_type); 421 ::H5Tclose(file_type); 422 return false; 422 return false; 423 } 423 } 424 424 425 hid_t mem_type = file_type; 425 hid_t mem_type = file_type; 426 if(H5Dwrite(dataset,mem_type,H5S_ALL,H5S_ALL 426 if(H5Dwrite(dataset,mem_type,H5S_ALL,H5S_ALL,H5P_DEFAULT,a_string.c_str())<0) { 427 ::H5Pclose(compact); 427 ::H5Pclose(compact); 428 ::H5Sclose(scalar); 428 ::H5Sclose(scalar); 429 ::H5Dclose(dataset); 429 ::H5Dclose(dataset); 430 ::H5Tclose(file_type); 430 ::H5Tclose(file_type); 431 return false; 431 return false; 432 } 432 } 433 433 434 ::H5Pclose(compact); 434 ::H5Pclose(compact); 435 ::H5Sclose(scalar); 435 ::H5Sclose(scalar); 436 ::H5Dclose(dataset); 436 ::H5Dclose(dataset); 437 ::H5Tclose(file_type); 437 ::H5Tclose(file_type); 438 438 439 return true; 439 return true; 440 } 440 } 441 441 442 inline bool write_string_dataset(hid_t a_loc,c 442 inline bool write_string_dataset(hid_t a_loc,const std::string& a_name, 443 unsigned int 443 unsigned int a_chunked,unsigned int a_compress, 444 const std::st 444 const std::string& a_string) { 445 hid_t cpt = -1; 445 hid_t cpt = -1; 446 if(a_compress || a_chunked) { 446 if(a_compress || a_chunked) { 447 cpt = ::H5Pcreate(H5P_DATASET_CREATE); 447 cpt = ::H5Pcreate(H5P_DATASET_CREATE); 448 if(cpt<0) return false; 448 if(cpt<0) return false; 449 if(a_chunked) { 449 if(a_chunked) { 450 if(H5Pset_layout(cpt,H5D_CHUNKED)<0) { 450 if(H5Pset_layout(cpt,H5D_CHUNKED)<0) { 451 ::H5Pclose(cpt); 451 ::H5Pclose(cpt); 452 return false; 452 return false; 453 } 453 } 454 hsize_t cdims[1]; 454 hsize_t cdims[1]; 455 cdims[0] = a_chunked; 455 cdims[0] = a_chunked; 456 if(H5Pset_chunk(cpt,1,cdims)<0) { 456 if(H5Pset_chunk(cpt,1,cdims)<0) { 457 ::H5Pclose(cpt); 457 ::H5Pclose(cpt); 458 return false; 458 return false; 459 } 459 } 460 } else { 460 } else { 461 if(H5Pset_layout(cpt,H5D_COMPACT)<0) { 461 if(H5Pset_layout(cpt,H5D_COMPACT)<0) { 462 ::H5Pclose(cpt); 462 ::H5Pclose(cpt); 463 return false; 463 return false; 464 } 464 } 465 } 465 } 466 if(a_compress) { 466 if(a_compress) { 467 if(H5Pset_deflate(cpt,a_compress>9?9:a_c 467 if(H5Pset_deflate(cpt,a_compress>9?9:a_compress)<0) { 468 ::H5Pclose(cpt); 468 ::H5Pclose(cpt); 469 return false; 469 return false; 470 } 470 } 471 } 471 } 472 } else { 472 } else { 473 cpt = H5P_DEFAULT; 473 cpt = H5P_DEFAULT; 474 } 474 } 475 475 476 hid_t file_type = str_datatype(); //first in 476 hid_t file_type = str_datatype(); //first input => H5T_VARIABLE. 477 if(file_type<0) { 477 if(file_type<0) { 478 if(cpt>=0) ::H5Pclose(cpt); 478 if(cpt>=0) ::H5Pclose(cpt); 479 return false; 479 return false; 480 } 480 } 481 481 482 hid_t file_space = -1; 482 hid_t file_space = -1; 483 {hsize_t dims[1]; 483 {hsize_t dims[1]; 484 dims[0] = 1; 484 dims[0] = 1; 485 if(a_chunked) { 485 if(a_chunked) { 486 hsize_t mx_dims[1]; 486 hsize_t mx_dims[1]; 487 mx_dims[0] = H5S_UNLIMITED; //extendable. 487 mx_dims[0] = H5S_UNLIMITED; //extendable. 488 file_space = ::H5Screate_simple(1,dims,mx_ 488 file_space = ::H5Screate_simple(1,dims,mx_dims); 489 } else { 489 } else { 490 file_space = ::H5Screate_simple(1,dims,NUL 490 file_space = ::H5Screate_simple(1,dims,NULL); 491 } 491 } 492 if(file_space<0) {if(cpt>=0) ::H5Pclose(cpt) 492 if(file_space<0) {if(cpt>=0) ::H5Pclose(cpt);::H5Tclose(file_type);return false;}} 493 493 494 hid_t dataset = toolx_H5Dcreate(a_loc,a_name 494 hid_t dataset = toolx_H5Dcreate(a_loc,a_name.c_str(),file_type,file_space,cpt); 495 if(cpt>=0) ::H5Pclose(cpt); 495 if(cpt>=0) ::H5Pclose(cpt); 496 ::H5Sclose(file_space); 496 ::H5Sclose(file_space); 497 if(dataset<0) { 497 if(dataset<0) { 498 ::H5Tclose(file_type); 498 ::H5Tclose(file_type); 499 return false; 499 return false; 500 } 500 } 501 501 502 hid_t mem_type = file_type; 502 hid_t mem_type = file_type; 503 503 504 const char* wdata[1]; 504 const char* wdata[1]; 505 wdata[0] = a_string.c_str(); 505 wdata[0] = a_string.c_str(); 506 506 507 if(H5Dwrite(dataset,mem_type,H5S_ALL,H5S_ALL 507 if(H5Dwrite(dataset,mem_type,H5S_ALL,H5S_ALL,H5P_DEFAULT,wdata)<0) { 508 ::H5Dclose(dataset); 508 ::H5Dclose(dataset); 509 ::H5Tclose(file_type); 509 ::H5Tclose(file_type); 510 return false; 510 return false; 511 } 511 } 512 512 513 ::H5Tclose(file_type); 513 ::H5Tclose(file_type); 514 ::H5Dclose(dataset); 514 ::H5Dclose(dataset); 515 515 516 return true; 516 return true; 517 } 517 } 518 518 519 inline bool write_string_dataset(hid_t a_loc,c 519 inline bool write_string_dataset(hid_t a_loc,const std::string& a_name, 520 const std::st 520 const std::string& a_string, 521 unsigned int 521 unsigned int a_chunked = 0,unsigned int a_compress = 0) { 522 return hdf5::write_string_dataset(a_loc,a_na 522 return hdf5::write_string_dataset(a_loc,a_name,a_chunked,a_compress,a_string); 523 } 523 } 524 524 525 inline bool write_append_string_dataset(hid_t 525 inline bool write_append_string_dataset(hid_t a_dataset,const std::string& a_string) { 526 hsize_t old_size = 0; 526 hsize_t old_size = 0; 527 527 528 {hid_t dataspace = H5Dget_space(a_dataset); 528 {hid_t dataspace = H5Dget_space(a_dataset); 529 if(dataspace<0) return false; 529 if(dataspace<0) return false; 530 hsize_t dims[1]; 530 hsize_t dims[1]; 531 if(H5Sget_simple_extent_dims(dataspace,dims, 531 if(H5Sget_simple_extent_dims(dataspace,dims,NULL)<0) { 532 ::H5Sclose(dataspace); 532 ::H5Sclose(dataspace); 533 return false; 533 return false; 534 } 534 } 535 old_size = dims[0]; 535 old_size = dims[0]; 536 ::H5Sclose(dataspace);} 536 ::H5Sclose(dataspace);} 537 537 538 {hsize_t exts[1]; 538 {hsize_t exts[1]; 539 exts[0] = old_size+1; 539 exts[0] = old_size+1; 540 // if(H5Dextend(dataset,exts)<0) { 540 // if(H5Dextend(dataset,exts)<0) { 541 if(H5Dset_extent(a_dataset,exts)<0) return f 541 if(H5Dset_extent(a_dataset,exts)<0) return false;} 542 542 543 hid_t file_space = H5Dget_space(a_dataset); 543 hid_t file_space = H5Dget_space(a_dataset); 544 if(file_space<0) return false; 544 if(file_space<0) return false; 545 545 546 {hsize_t offset[1]; 546 {hsize_t offset[1]; 547 offset[0] = old_size; 547 offset[0] = old_size; 548 hsize_t count[1]; 548 hsize_t count[1]; 549 count[0] = 1; 549 count[0] = 1; 550 if(H5Sselect_hyperslab(file_space,H5S_SELECT 550 if(H5Sselect_hyperslab(file_space,H5S_SELECT_SET,offset,NULL,count,NULL)<0) { 551 ::H5Sclose(file_space); 551 ::H5Sclose(file_space); 552 return false; 552 return false; 553 }} 553 }} 554 554 555 hsize_t dims[1]; 555 hsize_t dims[1]; 556 dims[0] = 1; 556 dims[0] = 1; 557 hid_t mem_space = ::H5Screate_simple(1,dims, 557 hid_t mem_space = ::H5Screate_simple(1,dims,NULL); 558 if(mem_space<0) { 558 if(mem_space<0) { 559 ::H5Sclose(file_space); 559 ::H5Sclose(file_space); 560 return false; 560 return false; 561 } 561 } 562 562 563 hid_t mem_type = str_datatype(); 563 hid_t mem_type = str_datatype(); 564 if(mem_type<0) { 564 if(mem_type<0) { 565 ::H5Sclose(mem_space); 565 ::H5Sclose(mem_space); 566 ::H5Sclose(file_space); 566 ::H5Sclose(file_space); 567 return false; 567 return false; 568 } 568 } 569 569 570 const char* wdata[1]; 570 const char* wdata[1]; 571 wdata[0] = a_string.c_str(); 571 wdata[0] = a_string.c_str(); 572 572 573 if(H5Dwrite(a_dataset,mem_type,mem_space,fil 573 if(H5Dwrite(a_dataset,mem_type,mem_space,file_space,H5P_DEFAULT,wdata)<0) { 574 ::H5Tclose(mem_type); 574 ::H5Tclose(mem_type); 575 ::H5Sclose(mem_space); 575 ::H5Sclose(mem_space); 576 ::H5Sclose(file_space); 576 ::H5Sclose(file_space); 577 return false; 577 return false; 578 } 578 } 579 579 580 ::H5Tclose(mem_type); 580 ::H5Tclose(mem_type); 581 ::H5Sclose(mem_space); 581 ::H5Sclose(mem_space); 582 ::H5Sclose(file_space); 582 ::H5Sclose(file_space); 583 583 584 return true; 584 return true; 585 } 585 } 586 586 587 }} 587 }} 588 588 589 #include <tools/buf2lines> 589 #include <tools/buf2lines> 590 590 591 namespace toolx { 591 namespace toolx { 592 namespace hdf5 { 592 namespace hdf5 { 593 593 594 inline bool write_array_string(hid_t a_loc,con 594 inline bool write_array_string(hid_t a_loc,const std::string& a_name,const std::vector<std::string>& a_array) { 595 hid_t scalar = ::H5Screate(H5S_SCALAR); 595 hid_t scalar = ::H5Screate(H5S_SCALAR); 596 if(scalar<0) return false; 596 if(scalar<0) return false; 597 597 598 // From H5LTmake_dataset_string. 598 // From H5LTmake_dataset_string. 599 size_t sz; 599 size_t sz; 600 char* buffer; 600 char* buffer; 601 if(!tools::strings2buf(a_array,sz,buffer)) { 601 if(!tools::strings2buf(a_array,sz,buffer)) { 602 ::H5Sclose(scalar); 602 ::H5Sclose(scalar); 603 return false; 603 return false; 604 } 604 } 605 605 606 hid_t file_type = string_datatype(sz); 606 hid_t file_type = string_datatype(sz); 607 if(file_type<0) { 607 if(file_type<0) { 608 delete [] buffer; 608 delete [] buffer; 609 ::H5Sclose(scalar); 609 ::H5Sclose(scalar); 610 return false; 610 return false; 611 } 611 } 612 612 613 hid_t dataset = toolx_H5Dcreate(a_loc,a_name 613 hid_t dataset = toolx_H5Dcreate(a_loc,a_name.c_str(),file_type,scalar,H5P_DEFAULT); 614 if(dataset<0) { 614 if(dataset<0) { 615 delete [] buffer; 615 delete [] buffer; 616 ::H5Tclose(file_type); 616 ::H5Tclose(file_type); 617 ::H5Sclose(scalar); 617 ::H5Sclose(scalar); 618 return false; 618 return false; 619 } 619 } 620 620 621 hid_t mem_type = file_type; 621 hid_t mem_type = file_type; 622 if(H5Dwrite(dataset,mem_type,H5S_ALL,H5S_ALL 622 if(H5Dwrite(dataset,mem_type,H5S_ALL,H5S_ALL,H5P_DEFAULT,buffer)<0) { 623 delete [] buffer; 623 delete [] buffer; 624 ::H5Dclose(dataset); 624 ::H5Dclose(dataset); 625 ::H5Tclose(file_type); 625 ::H5Tclose(file_type); 626 ::H5Sclose(scalar); 626 ::H5Sclose(scalar); 627 return false; 627 return false; 628 } 628 } 629 629 630 delete [] buffer; 630 delete [] buffer; 631 631 632 ::H5Dclose(dataset); 632 ::H5Dclose(dataset); 633 ::H5Tclose(file_type); 633 ::H5Tclose(file_type); 634 ::H5Sclose(scalar); 634 ::H5Sclose(scalar); 635 return true; 635 return true; 636 } 636 } 637 637 638 inline bool write_object(hid_t a_loc,const std 638 inline bool write_object(hid_t a_loc,const std::string& a_name,hid_t a_file_type,char* a_data) { 639 unsigned int chunked = 0; 639 unsigned int chunked = 0; 640 unsigned int compress = 0; 640 unsigned int compress = 0; 641 641 642 hid_t cpt = -1; 642 hid_t cpt = -1; 643 if(compress || chunked) { 643 if(compress || chunked) { 644 cpt = ::H5Pcreate(H5P_DATASET_CREATE); 644 cpt = ::H5Pcreate(H5P_DATASET_CREATE); 645 if(cpt<0) return false; 645 if(cpt<0) return false; 646 if(chunked) { 646 if(chunked) { 647 if(H5Pset_layout(cpt,H5D_CHUNKED)<0) { 647 if(H5Pset_layout(cpt,H5D_CHUNKED)<0) { 648 ::H5Pclose(cpt); 648 ::H5Pclose(cpt); 649 return false; 649 return false; 650 } 650 } 651 hsize_t cdims[1]; 651 hsize_t cdims[1]; 652 cdims[0] = chunked; 652 cdims[0] = chunked; 653 if(H5Pset_chunk(cpt,1,cdims)<0) { 653 if(H5Pset_chunk(cpt,1,cdims)<0) { 654 ::H5Pclose(cpt); 654 ::H5Pclose(cpt); 655 return false; 655 return false; 656 } 656 } 657 } else { 657 } else { 658 if(H5Pset_layout(cpt,H5D_COMPACT)<0) { 658 if(H5Pset_layout(cpt,H5D_COMPACT)<0) { 659 ::H5Pclose(cpt); 659 ::H5Pclose(cpt); 660 return false; 660 return false; 661 } 661 } 662 } 662 } 663 if(compress) { 663 if(compress) { 664 if(H5Pset_deflate(cpt,compress>9?9:compr 664 if(H5Pset_deflate(cpt,compress>9?9:compress)<0) { 665 ::H5Pclose(cpt); 665 ::H5Pclose(cpt); 666 return false; 666 return false; 667 } 667 } 668 } 668 } 669 } else { 669 } else { 670 cpt = H5P_DEFAULT; 670 cpt = H5P_DEFAULT; 671 } 671 } 672 672 673 hsize_t dims[1]; 673 hsize_t dims[1]; 674 dims[0] = 1; 674 dims[0] = 1; 675 hid_t simple = ::H5Screate_simple(1,dims,NUL 675 hid_t simple = ::H5Screate_simple(1,dims,NULL); 676 if(simple<0) { 676 if(simple<0) { 677 if(cpt>=0) ::H5Pclose(cpt); 677 if(cpt>=0) ::H5Pclose(cpt); 678 return false; 678 return false; 679 } 679 } 680 680 681 hid_t mem_type = compound_mem_type(a_file_ty 681 hid_t mem_type = compound_mem_type(a_file_type); 682 if(mem_type<0) { 682 if(mem_type<0) { 683 ::H5Sclose(simple); 683 ::H5Sclose(simple); 684 if(cpt>=0) ::H5Pclose(cpt); 684 if(cpt>=0) ::H5Pclose(cpt); 685 return false; 685 return false; 686 } 686 } 687 687 688 hid_t dataset = toolx_H5Dcreate(a_loc,a_name 688 hid_t dataset = toolx_H5Dcreate(a_loc,a_name.c_str(),a_file_type,simple,cpt); 689 if(dataset<0) { 689 if(dataset<0) { 690 ::H5Tclose(mem_type); 690 ::H5Tclose(mem_type); 691 ::H5Sclose(simple); 691 ::H5Sclose(simple); 692 if(cpt>=0) ::H5Pclose(cpt); 692 if(cpt>=0) ::H5Pclose(cpt); 693 return false; 693 return false; 694 } 694 } 695 695 696 if(H5Dwrite(dataset,mem_type,H5S_ALL,H5S_ALL 696 if(H5Dwrite(dataset,mem_type,H5S_ALL,H5S_ALL,H5P_DEFAULT,a_data)<0) { 697 ::H5Dclose(dataset); 697 ::H5Dclose(dataset); 698 ::H5Tclose(mem_type); 698 ::H5Tclose(mem_type); 699 ::H5Sclose(simple); 699 ::H5Sclose(simple); 700 if(cpt>=0) ::H5Pclose(cpt); 700 if(cpt>=0) ::H5Pclose(cpt); 701 return false; 701 return false; 702 } 702 } 703 703 704 ::H5Dclose(dataset); 704 ::H5Dclose(dataset); 705 ::H5Tclose(mem_type); 705 ::H5Tclose(mem_type); 706 ::H5Sclose(simple); 706 ::H5Sclose(simple); 707 if(cpt>=0) ::H5Pclose(cpt); 707 if(cpt>=0) ::H5Pclose(cpt); 708 708 709 return true; 709 return true; 710 } 710 } 711 711 712 inline bool read_string(hid_t a_loc,const std: 712 inline bool read_string(hid_t a_loc,const std::string& a_name,std::string& a_string) { 713 // From H5LTread_dataset_string. 713 // From H5LTread_dataset_string. 714 hid_t dataset = toolx_H5Dopen(a_loc,a_name.c 714 hid_t dataset = toolx_H5Dopen(a_loc,a_name.c_str()); 715 if(dataset<0) { 715 if(dataset<0) { 716 a_string.clear(); 716 a_string.clear(); 717 return false; // data set not found. 717 return false; // data set not found. 718 } 718 } 719 719 720 hid_t file_type = H5Dget_type(dataset); 720 hid_t file_type = H5Dget_type(dataset); 721 if(file_type<0) { 721 if(file_type<0) { 722 ::H5Dclose(dataset); 722 ::H5Dclose(dataset); 723 a_string.clear(); 723 a_string.clear(); 724 return false; 724 return false; 725 } 725 } 726 726 727 H5T_class_t t_class = H5Tget_class(file_type 727 H5T_class_t t_class = H5Tget_class(file_type); 728 if(t_class!=H5T_STRING) { 728 if(t_class!=H5T_STRING) { 729 ::H5Tclose(file_type); 729 ::H5Tclose(file_type); 730 ::H5Dclose(dataset); 730 ::H5Dclose(dataset); 731 a_string.clear(); 731 a_string.clear(); 732 return false; 732 return false; 733 } 733 } 734 734 735 size_t sz = H5Tget_size(file_type); 735 size_t sz = H5Tget_size(file_type); 736 ::H5Tclose(file_type); 736 ::H5Tclose(file_type); 737 if(!sz) { 737 if(!sz) { 738 ::H5Dclose(dataset); 738 ::H5Dclose(dataset); 739 a_string.clear(); 739 a_string.clear(); 740 return false; 740 return false; 741 } 741 } 742 742 743 // We could have use file_type since, for st 743 // We could have use file_type since, for string, 744 // file type is the same than memory type. 744 // file type is the same than memory type. 745 hid_t mem_type = string_datatype(sz); 745 hid_t mem_type = string_datatype(sz); 746 if(mem_type<0) { 746 if(mem_type<0) { 747 ::H5Dclose(dataset); 747 ::H5Dclose(dataset); 748 a_string.clear(); 748 a_string.clear(); 749 return false; 749 return false; 750 } 750 } 751 751 752 char* buff = new char[sz]; 752 char* buff = new char[sz]; 753 herr_t stat = H5Dread(dataset,mem_type,H5S_A 753 herr_t stat = H5Dread(dataset,mem_type,H5S_ALL,H5S_ALL,H5P_DEFAULT,buff); 754 ::H5Tclose(mem_type); 754 ::H5Tclose(mem_type); 755 ::H5Dclose(dataset); 755 ::H5Dclose(dataset); 756 if(stat<0) { 756 if(stat<0) { 757 delete [] buff; 757 delete [] buff; 758 a_string.clear(); 758 a_string.clear(); 759 return false; 759 return false; 760 } 760 } 761 761 762 size_t len = sz-1; 762 size_t len = sz-1; 763 a_string.resize(len,0); 763 a_string.resize(len,0); 764 for(size_t index=0;index<len;index++) a_stri 764 for(size_t index=0;index<len;index++) a_string[index] = buff[index]; 765 765 766 delete [] buff; 766 delete [] buff; 767 767 768 return true; 768 return true; 769 } 769 } 770 770 771 inline bool read_sub_string(hid_t a_loc,const 771 inline bool read_sub_string(hid_t a_loc,const std::string& a_name,unsigned int a_offset,std::string& a_string) { 772 hid_t dataset = toolx_H5Dopen(a_loc,a_name.c 772 hid_t dataset = toolx_H5Dopen(a_loc,a_name.c_str()); 773 if(dataset<0) { 773 if(dataset<0) { 774 a_string.clear(); 774 a_string.clear(); 775 return false; // data set not found. 775 return false; // data set not found. 776 } 776 } 777 777 778 hid_t file_space = H5Dget_space(dataset); 778 hid_t file_space = H5Dget_space(dataset); 779 if(file_space<0) { 779 if(file_space<0) { 780 ::H5Dclose(dataset); 780 ::H5Dclose(dataset); 781 a_string.clear(); 781 a_string.clear(); 782 return false; 782 return false; 783 } 783 } 784 784 785 {int dimn = H5Sget_simple_extent_ndims(file_s 785 {int dimn = H5Sget_simple_extent_ndims(file_space); 786 if(dimn<0) { 786 if(dimn<0) { 787 ::H5Sclose(file_space); 787 ::H5Sclose(file_space); 788 ::H5Dclose(dataset); 788 ::H5Dclose(dataset); 789 a_string.clear(); 789 a_string.clear(); 790 return false; 790 return false; 791 } 791 } 792 if(dimn!=1) { 792 if(dimn!=1) { 793 ::H5Sclose(file_space); 793 ::H5Sclose(file_space); 794 ::H5Dclose(dataset); 794 ::H5Dclose(dataset); 795 a_string.clear(); 795 a_string.clear(); 796 return false; 796 return false; 797 }} 797 }} 798 798 799 hsize_t dims[1]; 799 hsize_t dims[1]; 800 {if(H5Sget_simple_extent_dims(file_space,dims 800 {if(H5Sget_simple_extent_dims(file_space,dims,NULL)<0) { 801 ::H5Sclose(file_space); 801 ::H5Sclose(file_space); 802 ::H5Dclose(dataset); 802 ::H5Dclose(dataset); 803 a_string.clear(); 803 a_string.clear(); 804 return false; 804 return false; 805 }} 805 }} 806 806 807 {unsigned int sz = (unsigned int)dims[0]; 807 {unsigned int sz = (unsigned int)dims[0]; 808 if(!sz) { 808 if(!sz) { 809 ::H5Sclose(file_space); 809 ::H5Sclose(file_space); 810 ::H5Dclose(dataset); 810 ::H5Dclose(dataset); 811 a_string.clear(); 811 a_string.clear(); 812 return false; //Is it ok ? 812 return false; //Is it ok ? 813 } 813 } 814 814 815 // abcdef 815 // abcdef 816 // 012345 816 // 012345 817 int remain = sz-a_offset; 817 int remain = sz-a_offset; 818 if(remain<=0) { 818 if(remain<=0) { 819 ::H5Sclose(file_space); 819 ::H5Sclose(file_space); 820 ::H5Dclose(dataset); 820 ::H5Dclose(dataset); 821 a_string.clear(); 821 a_string.clear(); 822 return false; 822 return false; 823 }} 823 }} 824 824 825 {hsize_t offset[1]; 825 {hsize_t offset[1]; 826 offset[0] = a_offset; 826 offset[0] = a_offset; 827 hsize_t count[1]; 827 hsize_t count[1]; 828 count[0] = 1; 828 count[0] = 1; 829 if(H5Sselect_hyperslab(file_space,H5S_SELECT 829 if(H5Sselect_hyperslab(file_space,H5S_SELECT_SET,offset,NULL,count,NULL)<0) { 830 ::H5Sclose(file_space); 830 ::H5Sclose(file_space); 831 ::H5Dclose(dataset); 831 ::H5Dclose(dataset); 832 a_string.clear(); 832 a_string.clear(); 833 return false; 833 return false; 834 }} 834 }} 835 835 836 dims[0] = 1; 836 dims[0] = 1; 837 hid_t mem_space = ::H5Screate_simple(1,dims, 837 hid_t mem_space = ::H5Screate_simple(1,dims,NULL); 838 if(mem_space<0) { 838 if(mem_space<0) { 839 ::H5Sclose(file_space); 839 ::H5Sclose(file_space); 840 ::H5Dclose(dataset); 840 ::H5Dclose(dataset); 841 a_string.clear(); 841 a_string.clear(); 842 return false; 842 return false; 843 } 843 } 844 844 845 hid_t file_type = H5Dget_type(dataset); 845 hid_t file_type = H5Dget_type(dataset); 846 if(file_type<0) { 846 if(file_type<0) { 847 ::H5Sclose(file_space); 847 ::H5Sclose(file_space); 848 ::H5Dclose(dataset); 848 ::H5Dclose(dataset); 849 a_string.clear(); 849 a_string.clear(); 850 return false; 850 return false; 851 } 851 } 852 852 853 H5T_class_t t_class = H5Tget_class(file_type 853 H5T_class_t t_class = H5Tget_class(file_type); 854 if(t_class!=H5T_STRING) { 854 if(t_class!=H5T_STRING) { 855 ::H5Tclose(file_type); 855 ::H5Tclose(file_type); 856 ::H5Sclose(file_space); 856 ::H5Sclose(file_space); 857 ::H5Dclose(dataset); 857 ::H5Dclose(dataset); 858 a_string.clear(); 858 a_string.clear(); 859 return false; 859 return false; 860 } 860 } 861 861 862 //size_t sz = H5Tget_size(file_type); //it giv 862 //size_t sz = H5Tget_size(file_type); //it gives the largest string size in the dataset. 863 //if(!sz) { 863 //if(!sz) { 864 // ::H5Tclose(file_type); 864 // ::H5Tclose(file_type); 865 // ::H5Sclose(file_space); 865 // ::H5Sclose(file_space); 866 // ::H5Dclose(dataset); 866 // ::H5Dclose(dataset); 867 // a_string.clear(); 867 // a_string.clear(); 868 // return false; 868 // return false; 869 //} 869 //} 870 870 871 ::H5Tclose(file_type); 871 ::H5Tclose(file_type); 872 872 873 hid_t mem_type = str_datatype(); 873 hid_t mem_type = str_datatype(); 874 if(mem_type<0) { 874 if(mem_type<0) { 875 ::H5Sclose(file_space); 875 ::H5Sclose(file_space); 876 ::H5Dclose(dataset); 876 ::H5Dclose(dataset); 877 a_string.clear(); 877 a_string.clear(); 878 return false; 878 return false; 879 } 879 } 880 880 881 char* rdata[1]; 881 char* rdata[1]; 882 herr_t stat = H5Dread(dataset,mem_type,mem_s 882 herr_t stat = H5Dread(dataset,mem_type,mem_space,file_space,H5P_DEFAULT,rdata); 883 if(stat<0) { 883 if(stat<0) { 884 ::H5Dvlen_reclaim(mem_type,mem_space, H5P_ 884 ::H5Dvlen_reclaim(mem_type,mem_space, H5P_DEFAULT,rdata); 885 ::H5Tclose(mem_type); 885 ::H5Tclose(mem_type); 886 ::H5Sclose(mem_space); 886 ::H5Sclose(mem_space); 887 ::H5Sclose(file_space); 887 ::H5Sclose(file_space); 888 ::H5Dclose(dataset); 888 ::H5Dclose(dataset); 889 a_string.clear(); 889 a_string.clear(); 890 return false; 890 return false; 891 } 891 } 892 892 893 char* buff = rdata[0]; 893 char* buff = rdata[0]; 894 894 895 size_t len = ::strlen(buff); 895 size_t len = ::strlen(buff); 896 a_string.resize(len,0); 896 a_string.resize(len,0); 897 for(size_t index=0;index<len;index++) a_stri 897 for(size_t index=0;index<len;index++) a_string[index] = buff[index]; 898 898 899 ::H5Dvlen_reclaim(mem_type,mem_space, H5P_DE 899 ::H5Dvlen_reclaim(mem_type,mem_space, H5P_DEFAULT,rdata); 900 900 901 ::H5Tclose(mem_type); 901 ::H5Tclose(mem_type); 902 ::H5Sclose(mem_space); 902 ::H5Sclose(mem_space); 903 ::H5Sclose(file_space); 903 ::H5Sclose(file_space); 904 ::H5Dclose(dataset); 904 ::H5Dclose(dataset); 905 905 906 return true; 906 return true; 907 } 907 } 908 908 909 inline bool read_object(hid_t a_loc,const std: 909 inline bool read_object(hid_t a_loc,const std::string& a_name,size_t& a_size,char*& a_data) { 910 hid_t dataset = toolx_H5Dopen(a_loc,a_name.c 910 hid_t dataset = toolx_H5Dopen(a_loc,a_name.c_str()); 911 if(dataset<0) { 911 if(dataset<0) { 912 a_size = 0; 912 a_size = 0; 913 a_data = 0; 913 a_data = 0; 914 return false; 914 return false; 915 } 915 } 916 916 917 hid_t file_type = H5Dget_type(dataset); 917 hid_t file_type = H5Dget_type(dataset); 918 if(file_type<0) { 918 if(file_type<0) { 919 ::H5Dclose(dataset); 919 ::H5Dclose(dataset); 920 a_size = 0; 920 a_size = 0; 921 a_data = 0; 921 a_data = 0; 922 return false; 922 return false; 923 } 923 } 924 924 925 H5T_class_t t_class = H5Tget_class(file_type 925 H5T_class_t t_class = H5Tget_class(file_type); 926 if(t_class!=H5T_COMPOUND) { 926 if(t_class!=H5T_COMPOUND) { 927 ::H5Tclose(file_type); 927 ::H5Tclose(file_type); 928 ::H5Dclose(dataset); 928 ::H5Dclose(dataset); 929 a_size = 0; 929 a_size = 0; 930 a_data = 0; 930 a_data = 0; 931 return false; 931 return false; 932 } 932 } 933 933 934 size_t sz = H5Tget_size(file_type); 934 size_t sz = H5Tget_size(file_type); 935 if(!sz) { 935 if(!sz) { 936 ::H5Tclose(file_type); 936 ::H5Tclose(file_type); 937 ::H5Dclose(dataset); 937 ::H5Dclose(dataset); 938 a_size = 0; 938 a_size = 0; 939 a_data = 0; 939 a_data = 0; 940 return false; 940 return false; 941 } 941 } 942 942 943 hid_t mem_type = compound_mem_type(file_type 943 hid_t mem_type = compound_mem_type(file_type); 944 if(mem_type<0) { 944 if(mem_type<0) { 945 ::H5Tclose(file_type); 945 ::H5Tclose(file_type); 946 ::H5Dclose(dataset); 946 ::H5Dclose(dataset); 947 a_size = 0; 947 a_size = 0; 948 a_data = 0; 948 a_data = 0; 949 return false; 949 return false; 950 } 950 } 951 951 952 ::H5Tclose(file_type); 952 ::H5Tclose(file_type); 953 953 954 hid_t dataspace = H5Dget_space(dataset); 954 hid_t dataspace = H5Dget_space(dataset); 955 if(dataspace<0) { 955 if(dataspace<0) { 956 ::H5Tclose(mem_type); 956 ::H5Tclose(mem_type); 957 ::H5Dclose(dataset); 957 ::H5Dclose(dataset); 958 a_size = 0; 958 a_size = 0; 959 a_data = 0; 959 a_data = 0; 960 return false; 960 return false; 961 } 961 } 962 962 963 hid_t scalar = ::H5Screate(H5S_SCALAR); 963 hid_t scalar = ::H5Screate(H5S_SCALAR); 964 if(scalar<0) { 964 if(scalar<0) { 965 ::H5Sclose(dataspace); 965 ::H5Sclose(dataspace); 966 ::H5Tclose(mem_type); 966 ::H5Tclose(mem_type); 967 ::H5Dclose(dataset); 967 ::H5Dclose(dataset); 968 a_size = 0; 968 a_size = 0; 969 a_data = 0; 969 a_data = 0; 970 return false; 970 return false; 971 } 971 } 972 972 973 char* buffer = new char[sz]; 973 char* buffer = new char[sz]; 974 if(H5Dread(dataset,mem_type,scalar,dataspace 974 if(H5Dread(dataset,mem_type,scalar,dataspace,H5P_DEFAULT,buffer)<0) { 975 delete [] buffer; 975 delete [] buffer; 976 ::H5Sclose(scalar); 976 ::H5Sclose(scalar); 977 ::H5Sclose(dataspace); 977 ::H5Sclose(dataspace); 978 ::H5Tclose(mem_type); 978 ::H5Tclose(mem_type); 979 ::H5Dclose(dataset); 979 ::H5Dclose(dataset); 980 a_size = 0; 980 a_size = 0; 981 a_data = 0; 981 a_data = 0; 982 return false; 982 return false; 983 } 983 } 984 984 985 ::H5Sclose(scalar); 985 ::H5Sclose(scalar); 986 ::H5Sclose(dataspace); 986 ::H5Sclose(dataspace); 987 ::H5Tclose(mem_type); 987 ::H5Tclose(mem_type); 988 ::H5Dclose(dataset); 988 ::H5Dclose(dataset); 989 989 990 a_size = sz; 990 a_size = sz; 991 a_data = buffer; 991 a_data = buffer; 992 return true; 992 return true; 993 } 993 } 994 994 995 inline bool read_array_string(hid_t a_loc,cons 995 inline bool read_array_string(hid_t a_loc,const std::string& a_name,std::vector<std::string>& a_array) { 996 a_array.clear(); 996 a_array.clear(); 997 hid_t dataset = toolx_H5Dopen(a_loc,a_name.c 997 hid_t dataset = toolx_H5Dopen(a_loc,a_name.c_str()); 998 if(dataset<0) return false; 998 if(dataset<0) return false; 999 999 1000 hid_t file_type = H5Dget_type(dataset); 1000 hid_t file_type = H5Dget_type(dataset); 1001 if(file_type<0) { 1001 if(file_type<0) { 1002 ::H5Dclose(dataset); 1002 ::H5Dclose(dataset); 1003 return false; 1003 return false; 1004 } 1004 } 1005 1005 1006 H5T_class_t t_class = H5Tget_class(file_typ 1006 H5T_class_t t_class = H5Tget_class(file_type); 1007 if(t_class!=H5T_STRING) { 1007 if(t_class!=H5T_STRING) { 1008 ::H5Tclose(file_type); 1008 ::H5Tclose(file_type); 1009 ::H5Dclose(dataset); 1009 ::H5Dclose(dataset); 1010 return false; 1010 return false; 1011 } 1011 } 1012 1012 1013 size_t sz = H5Tget_size(file_type); 1013 size_t sz = H5Tget_size(file_type); 1014 ::H5Tclose(file_type); 1014 ::H5Tclose(file_type); 1015 if(!sz) { 1015 if(!sz) { 1016 ::H5Dclose(dataset); 1016 ::H5Dclose(dataset); 1017 return false; 1017 return false; 1018 } 1018 } 1019 1019 1020 // We could have use file_type since, for s 1020 // We could have use file_type since, for string, 1021 // file type is the same than memory type. 1021 // file type is the same than memory type. 1022 hid_t mem_type = string_datatype(sz); 1022 hid_t mem_type = string_datatype(sz); 1023 if(mem_type<0) { 1023 if(mem_type<0) { 1024 ::H5Dclose(dataset); 1024 ::H5Dclose(dataset); 1025 return false; 1025 return false; 1026 } 1026 } 1027 1027 1028 char* buffer = new char[sz]; 1028 char* buffer = new char[sz]; 1029 herr_t stat = H5Dread(dataset,mem_type,H5S_ 1029 herr_t stat = H5Dread(dataset,mem_type,H5S_ALL,H5S_ALL,H5P_DEFAULT,buffer); 1030 ::H5Tclose(mem_type); 1030 ::H5Tclose(mem_type); 1031 ::H5Dclose(dataset); 1031 ::H5Dclose(dataset); 1032 if(stat<0) { 1032 if(stat<0) { 1033 delete [] buffer; 1033 delete [] buffer; 1034 return false; 1034 return false; 1035 } 1035 } 1036 1036 1037 if(!tools::buf2strings(sz,buffer,a_array)) 1037 if(!tools::buf2strings(sz,buffer,a_array)) { 1038 delete [] buffer; 1038 delete [] buffer; 1039 return false; 1039 return false; 1040 } 1040 } 1041 1041 1042 delete [] buffer; 1042 delete [] buffer; 1043 return true; 1043 return true; 1044 } 1044 } 1045 1045 1046 }} 1046 }} 1047 1047 1048 #endif 1048 #endif