libSplash
DCAttribute.cpp
1 
23 #include "splash/AttributeInfo.hpp"
24 #include "splash/core/DCAttribute.hpp"
25 #include "splash/core/H5IdWrapper.hpp"
26 #include <cassert>
27 
28 namespace splash
29 {
30  std::string DCAttribute::getExceptionString(const char *name, std::string msg)
31  {
32  return (std::string("Exception for DCAttribute [") + std::string(name) +
33  std::string("] ") + msg);
34  }
35 
36  AttributeInfo DCAttribute::readAttributeInfo(const char* name, hid_t parent)
37  throw (DCException)
38  {
39  H5AttributeId attr(H5Aopen(parent, name, H5P_DEFAULT));
40  if (!attr)
41  throw DCException(getExceptionString(name, "Attribute could not be opened for reading"));
42  return AttributeInfo(attr);
43  }
44 
45  void DCAttribute::readAttribute(const char* name, hid_t parent, void* dst)
46  throw (DCException)
47  {
48  H5AttributeId attr(H5Aopen(parent, name, H5P_DEFAULT));
49  if (!attr)
50  throw DCException(getExceptionString(name, "Attribute could not be opened for reading"));
51 
52  H5TypeId attr_type(H5Aget_type(attr));
53  if (!attr_type)
54  throw DCException(getExceptionString(name, "Could not get type of attribute"));
55 
56  if (H5Aread(attr, attr_type, dst) < 0)
57  throw DCException(getExceptionString(name, "Attribute could not be read"));
58  }
59 
60  void DCAttribute::writeAttribute(const char* name, const hid_t type, hid_t parent,
61  uint32_t ndims, Dimensions dims, const void* src)
62  throw (DCException)
63  {
64  H5AttributeId attr;
65  if (H5Aexists(parent, name))
66  attr.reset(H5Aopen(parent, name, H5P_DEFAULT));
67  else
68  {
69  dims.swapDims(ndims);
70  H5DataspaceId dsp;
71  if( ndims == 1 && dims.getScalarSize() == 1 )
72  dsp.reset(H5Screate(H5S_SCALAR));
73  else
74  dsp.reset(H5Screate_simple( ndims, dims.getPointer(), dims.getPointer() ));
75 
76  attr.reset(H5Acreate(parent, name, type, dsp, H5P_DEFAULT, H5P_DEFAULT));
77  }
78 
79  if (!attr)
80  throw DCException(getExceptionString(name, "Attribute could not be opened or created"));
81 
82  if (H5Awrite(attr, type, src) < 0)
83  throw DCException(getExceptionString(name, "Attribute could not be written"));
84  }
85 
86  void DCAttribute::writeAttribute(const char* name, const hid_t type, hid_t parent, const void* src)
87  throw (DCException)
88  {
89  const Dimensions dims(1, 1, 1);
90  writeAttribute(name, type, parent, 1u, dims, src);
91  }
92 
93 }
void reset(hid_t id)
H5IdWrapper< H5Tclose, policies::NoCopy > H5TypeId
H5IdWrapper< H5Sclose, policies::NoCopy > H5DataspaceId
H5IdWrapper< H5Aclose, policies::NoCopy > H5AttributeId