2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2012 - Scilab Enterprises - Calixte DENIZET
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution. The terms
8 * are also available at
9 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
13 #include "H5DataFactory.hxx"
15 namespace org_modules_hdf5
18 H5Data & H5DataFactory::getData(H5Object & parent, const hid_t obj, H5Dataspace * space, hsize_t * selectdims, const bool isAttribute)
20 const hid_t type = isAttribute ? H5Aget_type(obj) : H5Dget_type(obj);
23 throw H5Exception(__LINE__, __FILE__, _("Cannot get the data type"));
26 hid_t spaceId = space ? space->getH5Id() : -1;
28 // TODO: le type est ferme dans getNativeData: bonne idee ou pas ??
30 switch (H5Tget_class(type))
33 data = &getIntegerData(parent, obj, spaceId, selectdims, type, isAttribute);
36 data = &getFloatingData(parent, obj, spaceId, selectdims, type, isAttribute);
39 data = &getTimeData(parent, obj, spaceId, selectdims, type, isAttribute);
42 data = &getStringData(parent, obj, spaceId, selectdims, type, isAttribute);
45 data = &getBitfieldData(parent, obj, spaceId, selectdims, type, isAttribute);
48 data = &getOpaqueData(parent, obj, spaceId, selectdims, type, isAttribute);
51 data = &getCompoundData(parent, obj, spaceId, selectdims, type, isAttribute);
54 data = &getReferenceData(parent, obj, spaceId, selectdims, type, isAttribute);
57 std::cout << "ENUM" << std::endl;
58 //data = &getEnumData(parent, obj, type, isAttribute);
61 std::cout << "VLEN" << std::endl;
63 //data = &getVlenData(parent, obj, type, isAttribute);
66 std::cout << "ARRAY" << std::endl;
67 //data = &getArrayData(parent, obj, type, isAttribute);
71 throw H5Exception(__LINE__, __FILE__, _("Cannot get data from an unknown data type."));
77 H5Data & H5DataFactory::getData(H5Object & parent, const hsize_t totalSize, const hid_t type, hsize_t ndims, hsize_t * dims, void * data, const hsize_t stride, const size_t offset, const bool dataOwner)
79 hsize_t dataSize = H5Tget_size(type);
80 if (H5Tget_class(type) == H5T_STRING && !H5Tis_variable_str(type))
82 // We have a C-string so it is null terminated
86 switch (H5Tget_class(type))
89 if (H5Tequal(type, H5T_NATIVE_SCHAR))
91 return *new H5CharData(parent, totalSize, dataSize, ndims, dims, (char *)data, stride, offset, false);
93 else if (H5Tequal(type, H5T_NATIVE_UCHAR))
95 return *new H5UnsignedCharData(parent, totalSize, dataSize, ndims, dims, (unsigned char *)data, stride, offset, false);
97 else if (H5Tequal(type, H5T_NATIVE_SHORT))
99 return *new H5BasicData<short>(parent, totalSize, dataSize, ndims, dims, (short *)data, stride, offset, false);
101 else if (H5Tequal(type, H5T_NATIVE_USHORT))
103 return *new H5BasicData<unsigned short>(parent, totalSize, dataSize, ndims, dims, (unsigned short *)data, stride, offset, false);
105 else if (H5Tequal(type, H5T_NATIVE_INT))
107 return *new H5BasicData<int>(parent, totalSize, dataSize, ndims, dims, (int *)data, stride, offset, false);
109 else if (H5Tequal(type, H5T_NATIVE_UINT))
111 return *new H5BasicData<unsigned int>(parent, totalSize, dataSize, ndims, dims, (unsigned int *)data, stride, offset, false);
114 #ifdef __SCILAB_INT64__
116 else if (H5Tequal(type, H5T_NATIVE_LONG))
118 return *new H5BasicData<long long>(parent, totalSize, dataSize, ndims, dims, (long long *)data, stride, offset, false);
120 else if (H5Tequal(type, H5T_NATIVE_ULONG))
122 return *new H5BasicData<unsigned long long>(parent, totalSize, dataSize, ndims, dims, (unsigned long long *)data, stride, offset, false);
126 else if (H5Tequal(type, H5T_NATIVE_LONG))
128 return *new H5TransformedData<long long, int>(parent, totalSize, dataSize, ndims, dims, (long long *)data, stride, offset, false);
130 else if (H5Tequal(type, H5T_NATIVE_ULONG))
132 return *new H5TransformedData<unsigned long long, unsigned int>(parent, totalSize, dataSize, ndims, dims, (unsigned long long *)data, stride, offset, false);
135 #endif // __SCILAB_INT64__
140 throw H5Exception(__LINE__, __FILE__, _("Unknown integer datatype."));
144 if (H5Tequal(type, H5T_NATIVE_FLOAT))
146 //return *new H5FloatData(parent, totalSize, dataSize, ndims, dims, (float *)data, stride, offset, false);
147 return *new H5TransformedData<float, double>(parent, totalSize, dataSize, ndims, dims, (float *)data, stride, offset, false);
149 else if (H5Tequal(type, H5T_NATIVE_DOUBLE))
151 return *new H5BasicData<double>(parent, totalSize, dataSize, ndims, dims, (double *)data, stride, offset, false);
155 throw H5Exception(__LINE__, __FILE__, _("Unknown floating-point datatype."));
159 return *new H5TimeData(parent, totalSize, dataSize, ndims, dims, (char *)data, stride, offset, false);
161 if (H5Tis_variable_str(type))
163 return *new H5StringData(parent, totalSize, dataSize, ndims, dims, (char **)data, stride, offset, false);
167 return *new H5StringData(parent, totalSize, dataSize, ndims, dims, (char *)data, stride, offset, false);
170 return *new H5BitfieldData(parent, totalSize, dataSize, ndims, dims, (char *)data, stride, offset, false);
172 return *new H5OpaqueData(parent, totalSize, dataSize, ndims, dims, (char *)data, stride, offset, false);
175 const unsigned int nmembers = (unsigned int)H5Tget_nmembers(type);
176 std::string * names = new std::string[nmembers];
178 H5Data ** fields = new H5Data *[nmembers];
180 for (unsigned int i = 0; i < nmembers; i++)
182 hid_t mtype = H5Tget_member_type(type, i);
183 char * mname = H5Tget_member_name(type, i);
184 size_t offs = H5Tget_member_offset(type, i);
185 names[i] = std::string(mname);
187 fields[i] = &getData(parent, totalSize, mtype, ndims, dims, data, stride, offset + offs, false);
190 return *new H5CompoundData(parent, totalSize, dataSize, ndims, dims, nmembers, names, fields, (char *)data, dataOwner);
193 // TODO: virer le false
194 return *new H5ReferenceData(parent, false, totalSize, dataSize, ndims, dims, (char *)data, offset);
197 /*int nmembers = H5Tget_nmembers(type);
198 std::string * names = new std::string[nmembers];
200 for (unsigned int i = 0; i < nmembers; i++)
202 char * mname = H5Tget_member_name(type, i);
203 names[i] = std::string(mname);
206 return *new H5EnumData(parent, totalSize, dataSize, ndims, dims, data, offset, names);*/
209 //return *new H5VlenData(parent, totalSize, dataSize, ndims, dims, data, offset);
211 //return *new H5ArrayData(parent, totalSize, dataSize, ndims, dims, data, offset);
213 throw H5Exception(__LINE__, __FILE__, _("Cannot get data from an unknown data type."));
216 throw H5Exception(__LINE__, __FILE__, _("Cannot get data from an unknown data type."));
219 H5Data & H5DataFactory::getIntegerData(H5Object & parent, const hid_t obj, const hid_t space, hsize_t * selectdims, const hid_t type, const bool isAttribute)
226 H5Data * dataObj = 0;
227 const hid_t nativeType = H5Tget_native_type(type, H5T_DIR_DEFAULT);
229 getNativeData(obj, space, selectdims, type, &totalSize, &dataSize, &ndims, &dims, &data, isAttribute);
231 if (H5Tequal(nativeType, H5T_NATIVE_SCHAR))
233 dataObj = new H5CharData(parent, totalSize, dataSize, ndims, dims, (char *)data);
235 else if (H5Tequal(nativeType, H5T_NATIVE_UCHAR))
237 dataObj = new H5UnsignedCharData(parent, totalSize, dataSize, ndims, dims, (unsigned char *)data);
239 else if (H5Tequal(nativeType, H5T_NATIVE_SHORT))
241 dataObj = new H5BasicData<short>(parent, totalSize, dataSize, ndims, dims, (short *)data);
243 else if (H5Tequal(nativeType, H5T_NATIVE_USHORT))
245 dataObj = new H5BasicData<unsigned short>(parent, totalSize, dataSize, ndims, dims, (unsigned short *)data);
247 else if (H5Tequal(nativeType, H5T_NATIVE_INT))
249 dataObj = new H5BasicData<int>(parent, totalSize, dataSize, ndims, dims, (int *)data);
251 else if (H5Tequal(nativeType, H5T_NATIVE_UINT))
253 dataObj = new H5BasicData<unsigned int>(parent, totalSize, dataSize, ndims, dims, (unsigned int *)data);
256 #ifdef __SCILAB_INT64__
258 else if (H5Tequal(nativeType, H5T_NATIVE_LONG))
260 dataObj = new H5BasicData<long long>(parent, totalSize, dataSize, ndims, dims, (long long *)data);
262 else if (H5Tequal(nativeType, H5T_NATIVE_ULONG))
264 dataObj = new H5BasicData<unsigned long long>(parent, totalSize, dataSize, ndims, dims, (unsigned long long *)data);
269 else if (H5Tequal(nativeType, H5T_NATIVE_LONG))
271 dataObj = new H5TransformedData<long long, int>(parent, totalSize, dataSize, ndims, dims, (long long *)data);
273 else if (H5Tequal(nativeType, H5T_NATIVE_ULONG))
275 dataObj = new H5TransformedData<unsigned long long, unsigned int>(parent, totalSize, dataSize, ndims, dims, (unsigned long long *)data);
278 #endif // __SCILAB_INT64__
282 H5Tclose(nativeType);
284 delete[] static_cast<char *>(data);
285 throw H5Exception(__LINE__, __FILE__, _("Unknown integer datatype."));
288 H5Tclose(nativeType);
293 H5Data & H5DataFactory::getFloatingData(H5Object & parent, const hid_t obj, const hid_t space, hsize_t * selectdims, const hid_t type, const bool isAttribute)
300 H5Data * dataObj = 0;
301 const hid_t nativeType = H5Tget_native_type(type, H5T_DIR_DEFAULT);
303 getNativeData(obj, space, selectdims, type, &totalSize, &dataSize, &ndims, &dims, &data, isAttribute);
305 if (H5Tequal(nativeType, H5T_NATIVE_FLOAT))
307 dataObj = new H5TransformedData<float, double>(parent, totalSize, dataSize, ndims, dims, (float *)data);
309 else if (H5Tequal(nativeType, H5T_NATIVE_DOUBLE))
311 dataObj = new H5BasicData<double>(parent, totalSize, dataSize, ndims, dims, (double *)data);
315 H5Tclose(nativeType);
317 delete[] static_cast<char *>(data);
318 throw H5Exception(__LINE__, __FILE__, _("Unknown floating-point datatype."));
321 H5Tclose(nativeType);
326 H5StringData & H5DataFactory::getStringData(H5Object & parent, const hid_t obj, const hid_t space, hsize_t * selectdims, const hid_t type, const bool isAttribute)
334 getNativeData(obj, space, selectdims, type, &totalSize, &dataSize, &ndims, &dims, &data, isAttribute);
335 if (H5Tis_variable_str(type))
337 return *new H5StringData(parent, totalSize, dataSize, ndims, dims, (char **)data);
341 return *new H5StringData(parent, totalSize, dataSize, ndims, dims, (char *)data);
345 H5TimeData & H5DataFactory::getTimeData(H5Object & parent, const hid_t obj, const hid_t space, hsize_t * selectdims, const hid_t type, const bool isAttribute)
353 getNativeData(obj, space, selectdims, type, &totalSize, &dataSize, &ndims, &dims, &data, isAttribute);
355 return *new H5TimeData(parent, totalSize, dataSize, ndims, dims, static_cast<char *>(data));
358 H5BitfieldData & H5DataFactory::getBitfieldData(H5Object & parent, const hid_t obj, const hid_t space, hsize_t * selectdims, const hid_t type, const bool isAttribute)
366 getNativeData(obj, space, selectdims, type, &totalSize, &dataSize, &ndims, &dims, &data, isAttribute);
368 return *new H5BitfieldData(parent, totalSize, dataSize, ndims, dims, static_cast<char *>(data));
371 H5OpaqueData & H5DataFactory::getOpaqueData(H5Object & parent, const hid_t obj, const hid_t space, hsize_t * selectdims, const hid_t type, const bool isAttribute)
379 getNativeData(obj, space, selectdims, type, &totalSize, &dataSize, &ndims, &dims, &data, isAttribute);
381 return *new H5OpaqueData(parent, totalSize, dataSize, ndims, dims, static_cast<char *>(data));
384 H5Data & H5DataFactory::getCompoundData(H5Object & parent, const hid_t obj, const hid_t space, hsize_t * selectdims, const hid_t type, const bool isAttribute)
391 const hid_t nativeType = H5Tget_native_type(type, H5T_DIR_DEFAULT);
393 getNativeData(obj, space, selectdims, type, &totalSize, &dataSize, &ndims, &dims, &data, isAttribute);
397 return getData(parent, totalSize, nativeType, ndims, dims, data, dataSize, 0, true);
399 catch (const H5Exception & e)
401 H5Tclose(nativeType);
406 H5ReferenceData & H5DataFactory::getReferenceData(H5Object & parent, const hid_t obj, const hid_t space, hsize_t * selectdims, const hid_t type, const bool isAttribute)
414 getNativeData(obj, space, selectdims, type, &totalSize, &dataSize, &ndims, &dims, &data, isAttribute);
415 return *new H5ReferenceData(parent, H5Tequal(type, H5T_STD_REF_DSETREG) > 0, totalSize, dataSize, ndims, dims, (char *)data);
418 /*H5EnumData & H5DataFactory::getEnumData(H5Object & parent, const hid_t obj, const hid_t type, const bool isAttribute)
425 int nmembers = H5Tget_nmembers(type);
426 std::string * names = new std::string[nmembers];
428 for (unsigned int i = 0; i < nmembers; i++)
430 char * mname = H5Tget_member_name(type, i);
431 names[i] = std::string(mname);
435 getNativeData(obj, type, &totalSize, &dataSize, &ndims, &dims, &data, isAttribute);
437 return *new H5EnumData(parent, totalSize, dataSize, ndims, dims, data, names);
440 H5VlenData & H5DataFactory::getVlenData(H5Object & parent, const hid_t obj, const hid_t type, const bool isAttribute)
448 getNativeData(obj, type, &totalSize, &dataSize, &ndims, &dims, &data, isAttribute);
450 return *new H5VlenData(parent, totalSize, dataSize, ndims, dims, data);
453 H5ArrayData & H5DataFactory::getArrayData(H5Object & parent, const hid_t obj, const hid_t type, const bool isAttribute)
461 getNativeData(obj, type, &totalSize, &dataSize, &ndims, &dims, &data, isAttribute);
463 return *new H5ArrayData(parent, totalSize, dataSize, ndims, dims, data);
466 // getNativeData(obj, type, &totalSize, &dataSize, &ndims, &dims, &data, isAttribute);
468 //return *new H5StringData(parent, totalSize, dataSize, ndims, dims, data);
470 /*void H5DataFactory::getNativeData(const hid_t obj, const hid_t type, hsize_t * totalSize, hsize_t * dataSize, hsize_t * ndims, hsize_t ** dims, void ** data, const bool isAttribute)
472 hid_t nativeType = H5Tget_native_type(type, H5T_DIR_DEFAULT);
473 const hid_t space = isAttribute ? H5Aget_space(obj) : H5Dget_space(obj);
474 hsize_t size = H5Tget_size(nativeType);
476 *dims = new hsize_t[__SCILAB_HDF5_MAX_DIMS__]();
477 *ndims = H5Sget_simple_extent_dims(space, *dims, 0);
479 if (H5Tget_class(nativeType) == H5T_STRING && !H5Tis_variable_str(nativeType))
481 // We have a C-string so it is null terminated
487 for (unsigned int i = 0; i < *ndims; i++)
489 *totalSize *= (*dims)[i];
494 if ((hsize_t)((size_t)size) != size)
497 H5Tclose(nativeType);
500 throw H5Exception(__LINE__, __FILE__, _("Memory to allocate is too big"));
503 *data = static_cast<void *>(new char[(size_t)size]());
507 H5Tclose(nativeType);
510 throw H5Exception(__LINE__, __FILE__, _("Cannot allocate memory to get the data"));
513 if ((isAttribute && H5Aread(obj, nativeType, *data) < 0)
514 || (!isAttribute && H5Dread(obj, nativeType, H5S_ALL, space, H5P_DEFAULT, *data) < 0))
517 H5Tclose(nativeType);
519 delete[] static_cast<char *>(*data);
521 throw H5Exception(__LINE__, __FILE__, _("Cannot retrieve the data from the attribute"));
524 H5Tclose(nativeType);
529 void H5DataFactory::getNativeData(const hid_t obj, const hid_t space, hsize_t * selectdims, const hid_t type, hsize_t * totalSize, hsize_t * dataSize, hsize_t * ndims, hsize_t ** dims, void ** data, const bool isAttribute)
531 hid_t nativeType = H5Tget_native_type(type, H5T_DIR_DEFAULT);
532 hid_t _space = space < 0 ? (isAttribute ? H5Aget_space(obj) : H5Dget_space(obj)) : space;
533 hsize_t size = H5Tget_size(nativeType);
537 hsize_t * blockbuf = 0;
538 bool hyperslab = false;
541 if (H5Tget_class(nativeType) == H5T_STRING && !H5Tis_variable_str(nativeType))
543 // We have a C-string so it is null terminated
548 *ndims = H5Sget_simple_extent_dims(_space, 0, 0);
549 *dims = new hsize_t[*ndims];
553 H5Sget_simple_extent_dims(_space, *dims, 0);
557 sel = H5Sget_select_type(_space);
562 H5Sget_simple_extent_dims(_space, *dims, 0);
563 for (unsigned int i = 0; i < *ndims; i++)
565 *totalSize *= (*dims)[i];
570 case H5S_SEL_HYPERSLABS:
571 for (unsigned int i = 0; i < *ndims; i++)
573 (*dims)[i] = selectdims[i];
574 *totalSize *= (*dims)[i];
582 if ((hsize_t)((size_t)size) != size)
585 H5Tclose(nativeType);
591 throw H5Exception(__LINE__, __FILE__, _("Memory to allocate is too big"));
594 *data = static_cast<void *>(new char[(size_t)size]());
598 H5Tclose(nativeType);
604 throw H5Exception(__LINE__, __FILE__, _("Cannot allocate memory to get the data"));
609 targetspace = H5Screate_simple(*ndims, *dims, 0);
610 err = H5Dread(obj, nativeType, targetspace, _space, H5P_DEFAULT, *data);
611 H5Sclose(targetspace);
617 err = H5Aread(obj, nativeType, *data);
621 err = H5Dread(obj, nativeType, H5S_ALL, H5S_ALL, H5P_DEFAULT, *data);
628 H5Tclose(nativeType);
633 delete[] static_cast<char *>(*data);
635 throw H5Exception(__LINE__, __FILE__, _("Cannot retrieve the data from the attribute"));
638 H5Tclose(nativeType);