Skip to content

Commit 8248533

Browse files
committed
Change how strings are stored, use UTF-8 encoding
1 parent aeefdcd commit 8248533

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

examples/text2hdf5.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <binsparse/binsparse.hpp>
2+
#include <complex>
3+
#include <concepts>
4+
#include <iostream>
5+
#include <sstream>
6+
7+
int main(int argc, char** argv) {
8+
9+
if (argc < 4) {
10+
std::cout << "usage: ./write_text_file [input_file.txt] "
11+
"[output_file.hdf5] [HDF5 Dataset Name]\n";
12+
return 1;
13+
}
14+
15+
std::string input_file(argv[1]);
16+
std::string output_file(argv[2]);
17+
std::string dataset_name(argv[3]);
18+
19+
std::cout << "Read in text file " << input_file << " write to dataset "
20+
<< dataset_name << " in file " << output_file << std::endl;
21+
22+
std::ifstream f(input_file);
23+
std::stringstream ss;
24+
ss << f.rdbuf();
25+
26+
std::string file_contents = ss.str();
27+
28+
{
29+
H5::H5File f(output_file.c_str(), H5F_ACC_RDWR);
30+
31+
hdf5_tools::write_dataset(f, dataset_name, file_contents);
32+
}
33+
34+
return 0;
35+
}

include/binsparse/hdf5_tools.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <H5Cpp.h>
44
#include <cassert>
55
#include <ranges>
6+
#include <type_traits>
67
#include <vector>
78

89
#include <iostream>
@@ -120,6 +121,7 @@ inline H5::PredType get_type(H5::DataSet& dataset) {
120121
}
121122

122123
template <typename H5GroupOrFile, std::ranges::contiguous_range R>
124+
requires(!std::is_same_v<std::remove_cvref_t<R>, std::string>)
123125
void write_dataset(H5GroupOrFile& f, const std::string& label, R&& r) {
124126
using T = std::ranges::range_value_t<R>;
125127
hsize_t size = std::ranges::size(r);
@@ -132,6 +134,21 @@ void write_dataset(H5GroupOrFile& f, const std::string& label, R&& r) {
132134
dataspace.close();
133135
}
134136

137+
template <typename H5GroupOrFile, std::ranges::contiguous_range R>
138+
requires(std::is_same_v<std::remove_cvref_t<R>, std::string>)
139+
void write_dataset(H5GroupOrFile& f, const std::string& label, R&& r) {
140+
H5::StrType string_type(H5::PredType::C_S1, r.size());
141+
H5Tset_cset(string_type, H5T_CSET_UTF8);
142+
hsize_t size = r.size();
143+
H5::DataSpace dataspace(1, &size);
144+
145+
auto dataset = f.createDataSet(label.c_str(), string_type, H5S_SCALAR);
146+
147+
dataset.write(r.c_str(), string_type);
148+
149+
dataset.close();
150+
}
151+
135152
inline std::string get_attribute(H5::H5Object& f, const std::string& key) {
136153
auto attribute = f.openAttribute(key.c_str());
137154

@@ -151,6 +168,7 @@ inline std::string get_attribute(H5::H5Object& f, const std::string& key) {
151168
inline void set_attribute(H5::H5Object& f, const std::string& key,
152169
const std::string& value) {
153170
H5::StrType string_type(H5::PredType::C_S1, value.size());
171+
H5Tset_cset(string_type, H5T_CSET_UTF8);
154172
hsize_t size = value.size();
155173
H5::DataSpace dataspace(1, &size);
156174

include/binsparse/type_info.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include <cassert>
4+
#include <functional>
45
#include <type_traits>
56

67
namespace binsparse {

0 commit comments

Comments
 (0)