libSplash
ColTypeString.hpp
1 
24 #ifndef COLTYPESTRING_H
25 #define COLTYPESTRING_H
26 
27 #include "splash/CollectionType.hpp"
28 
29 #include <string>
30 
31 namespace splash
32 {
44  {
45  public:
46 
48  {
49  this->type = H5Tcopy(H5T_C_S1);
50  H5Tset_size(this->type, H5T_VARIABLE);
51  }
52 
53  ColTypeString(size_t len)
54  {
55  this->type = H5Tcopy(H5T_C_S1);
56  /* HDF5 requires space for the \0-terminator character,
57  * otherwise it will not be stored or retrieved */
58  H5Tset_size(this->type, len + 1);
59  }
60 
61  ~ColTypeString()
62  {
63  H5Tclose(this->type);
64  }
65 
66  size_t getSize() const
67  {
68  size_t myElements = H5Tget_size(this->type);
69 
70  /* for variable length string the size is first known after reading
71  * the actual data or attribute, so we forward HDF5's behavior */
72  if(isVariableLength())
73  return myElements; /* == sizeof(char*) see H5Tget_size description */
74  else if( isNullTerminated() )
75  return sizeof(char) * (myElements - 1); /* just as strlen() */
76  else
77  return sizeof(char) * myElements;
78  }
79 
80  static CollectionType* genType(hid_t datatype_id)
81  {
82  H5T_class_t h5_class = H5Tget_class(datatype_id);
83 
84  if(h5_class == H5T_STRING)
85  {
86  return new ColTypeString(datatype_id, true);
87  } else
88  {
89  return NULL;
90  }
91  }
92 
93  std::string toString() const
94  {
95  if(isVariableLength())
96  return "VLString";
97  else
98  return "String";
99  }
100 
101  bool isVariableLength() const
102  {
103  return H5Tis_variable_str(this->type);
104  }
105 
106  bool isNullTerminated() const
107  {
108  return H5Tget_strpad(type) == H5T_STR_NULLTERM;
109  }
110 
111  private:
112  explicit ColTypeString(H5DataType inType, bool /*dummy*/):
113  CollectionType(H5Tcopy(inType))
114  {
115  }
116  };
117 
118 }
119 
120 #endif /* COLTYPESTRING_H */
std::string toString() const
size_t getSize() const