h5cpp 0.8
A modern C++ wrapper for the HDF5 C library
Loading...
Searching...
No Matches
fixed_length_string.hpp
Go to the documentation of this file.
1//
2// (c) Copyright 2017 DESY,ESS
3// 2018 Eugen Wintersberger
4//
5// This file is part of h5cpp.
6//
7// This library is free software; you can redistribute it and/or modify it
8// under the terms of the GNU Lesser General Public License as published
9// by the Free Software Foundation; either version 2.1 of the License, or
10// (at your option) any later version.
11//
12// This library is distributed in the hope that it will be useful, but
13// WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY
14// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15// License for more details.
16//
17// You should have received a copy of the GNU Lesser General Public License
18// along with this library; if not, write to the
19// Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor
20// Boston, MA 02110-1301 USA
21// ===========================================================================
22//
23// Author: Eugen Wintersberger <eugen.wintersberger@gmail.com>
24// Created on: Apr 4, 2019
25//
26#pragma once
27
28#include <algorithm>
29#include <vector>
30#include <string>
34
35namespace hdf5 {
36
37template<typename CharT>
38class FixedLengthStringBuffer : public std::vector<CharT>
39{
40 public:
41
42 using std::vector<CharT>::vector;
43
45 const dataspace::Dataspace &dataspace)
46 {
49 buffer = FixedLengthStringBuffer<CharT>(datatype.size()*static_cast<size_t>(dataspace.size()));
50 else
51 buffer = FixedLengthStringBuffer<CharT>(datatype.size()*dataspace.selection.size());
52
53 return buffer;
54 }
55};
56
57template<typename T>
59{
60 using DataType = T;
62
64 const dataspace::Dataspace &dataspace)
65 {
66 return BufferType::create(datatype,dataspace);
67 }
68
71 {
72 return BufferType();
73 }
74
77 {
78 return DataType();
79 }
80};
81
82template<>
83struct FixedLengthStringTrait<std::string>
84{
85 using DataType = std::string;
87
98 static BufferType to_buffer(const DataType &data,
99 const datatype::String &memory_type,
100 const dataspace::Dataspace &memory_space)
101 {
102 BufferType buffer = BufferType::create(memory_type,memory_space);
103
104 auto buffer_iter = buffer.begin();
105 auto buffer_end = buffer.end();
106 auto data_iter = data.begin();
107 auto data_end = data.end();
108
109 for(;buffer_iter!=buffer_end && data_iter != data_end; ++buffer_iter,++data_iter)
110 *buffer_iter = *data_iter;
111
112 return buffer;
113 }
114
120 static DataType from_buffer(const BufferType &buffer,
121 const datatype::String &,
122 const dataspace::Dataspace &)
123 {
124 return DataType(buffer.begin(), buffer.end());
125 }
126
134 const datatype::String &memory_type,
135 const dataspace::Dataspace &)
136 {
137 const auto padding = memory_type.padding();
138 auto end_it = buffer.end();
139 switch (padding)
140 {
142 end_it = std::find(buffer.begin(), buffer.end(), '\0');
143 break;
145 // get iterator to last padding \0 character
146 end_it = std::find_if_not(buffer.rbegin(), buffer.rend(), [](const BufferType::value_type &c)
147 { return c == '\0'; })
148 .base();
149 break;
151 // get iterator to last padding space character
152 end_it = std::find_if_not(buffer.rbegin(), buffer.rend(), [](const BufferType::value_type &c)
153 { return c == ' '; })
154 .base();
155 break;
156#ifdef __clang__
157#pragma clang diagnostic push
158#pragma clang diagnostic ignored "-Wcovered-switch-default"
159#endif
160 default:
161 break;
162#ifdef __clang__
163#pragma clang diagnostic pop
164#endif
165 }
166 return DataType(buffer.begin(), end_it);
167 }
168};
169
170template<>
171struct FixedLengthStringTrait<std::vector<std::string>>
172{
173 using DataType = std::vector<std::string>;
175
176 static BufferType to_buffer(const DataType &data,
177 const datatype::String &memory_type,
178 const dataspace::Dataspace &memory_space)
179 {
180 BufferType buffer = BufferType::create(memory_type,memory_space);
181
182 auto iter = buffer.begin();
183 for(const auto &str: data)
184 {
185 std::copy(str.begin(),str.end(),iter);
186 std::advance(iter,unsigned2signed<ssize_t>(memory_type.size())); //move iterator to the next position
187 }
188
189 return buffer;
190 }
191
192 static DataType from_buffer(const BufferType &buffer,
193 const datatype::String &memory_type,
194 const dataspace::Dataspace &)
195 {
196 DataType data;
197
198 auto start=buffer.begin();
199 while(start!=buffer.end())
200 {
201 auto end = start+unsigned2signed<std::string::difference_type>(memory_type.size());
202 data.push_back(std::string(start,end));
203 start=end;
204 }
205 return data;
206 }
207};
208
209
210}
Definition fixed_length_string.hpp:39
static FixedLengthStringBuffer< CharT > create(const datatype::String &datatype, const dataspace::Dataspace &dataspace)
Definition fixed_length_string.hpp:44
dataspace base class
Definition dataspace.hpp:41
SelectionManager selection
access to selection manager
Definition dataspace.hpp:144
virtual hssize_t size() const
number of elements in the dataspace
size_t size() const
return the number of elements in the current selection
SelectionType type() const
get the type of the current selection
string datatype
Definition string.hpp:40
StringPad padding() const
get the current string padding
size_t size() const override
get current size of the string type
@ NullTerm
indicates a null terminated string type
@ NullPad
indicates a null padded string type
@ SpacePad
indicates a space padded string type
top-level namespace of the entire library
Definition attribute.hpp:45
static DataType from_buffer_trimmed(const BufferType &buffer, const datatype::String &memory_type, const dataspace::Dataspace &)
store data from buffer in target memory and trim any tailing null-terminating characters
Definition fixed_length_string.hpp:133
std::string DataType
Definition fixed_length_string.hpp:85
static DataType from_buffer(const BufferType &buffer, const datatype::String &, const dataspace::Dataspace &)
store data from buffer in target memory
Definition fixed_length_string.hpp:120
static BufferType to_buffer(const DataType &data, const datatype::String &memory_type, const dataspace::Dataspace &memory_space)
create fixed length string buffer from data
Definition fixed_length_string.hpp:98
std::vector< std::string > DataType
Definition fixed_length_string.hpp:173
static DataType from_buffer(const BufferType &buffer, const datatype::String &memory_type, const dataspace::Dataspace &)
Definition fixed_length_string.hpp:192
static BufferType to_buffer(const DataType &data, const datatype::String &memory_type, const dataspace::Dataspace &memory_space)
Definition fixed_length_string.hpp:176
Definition fixed_length_string.hpp:59
FixedLengthStringBuffer< char > BufferType
Definition fixed_length_string.hpp:61
static BufferType create_buffer(const datatype::String &datatype, const dataspace::Dataspace &dataspace)
Definition fixed_length_string.hpp:63
static DataType from_buffer(const BufferType &, const datatype::String &, const dataspace::Dataspace &)
Definition fixed_length_string.hpp:75
T DataType
Definition fixed_length_string.hpp:60
static BufferType to_buffer(const DataType &, const datatype::String &, const dataspace::Dataspace &)
Definition fixed_length_string.hpp:69