diff --git a/src/h5_file_format.cpp b/src/h5_file_format.cpp new file mode 100644 index 0000000..edb992a --- /dev/null +++ b/src/h5_file_format.cpp @@ -0,0 +1,40 @@ +#include +#include +#include +#include + +#include "h5_file_format.hpp" + +using namespace std; + +void h5_utils::write_attribute(H5::H5Object& target, string name, string value){ + H5::StrType str_type(0, H5T_VARIABLE); + H5::DataSpace att_space(H5S_SCALAR); + + H5::Attribute h5_attribute = target.createAttribute(name, str_type, att_space); + h5_attribute.write(str_type, value); +} + +void h5_utils::write_attribute(H5::H5Object& target, string name, int32_t value){ + H5::IntType int_type(H5::PredType::NATIVE_INT32); + H5::DataSpace att_space(H5S_SCALAR); + + H5::Attribute h5_attribute = target.createAttribute(name, int_type, att_space); + h5_attribute.write(int_type, &value); +} + +void h5_utils::set_attributes(H5::H5Object& target, std::list& attributes) { + for (auto& attribute : attributes) { + + string name = attribute.name; + + if (attribute.data_type == NX_CHAR) { + auto attribute_ptr = reinterpret_cast*>(&attribute); + h5_utils::write_attribute(target, name, attribute_ptr->value); + + } else if (attribute.data_type == NX_INT) { + auto attribute_ptr = reinterpret_cast*>(&attribute); + h5_utils::write_attribute(target, name, attribute_ptr->value); + } + } +} \ No newline at end of file diff --git a/src/h5_file_format.hpp b/src/h5_file_format.hpp index 2f4c183..2b728c0 100644 --- a/src/h5_file_format.hpp +++ b/src/h5_file_format.hpp @@ -4,6 +4,8 @@ #include #include +#include + enum DATA_TYPE { NX_FLOAT, NX_CHAR, @@ -13,11 +15,21 @@ enum DATA_TYPE { NXnote }; +enum DATA_LOCATION { + IMMEDIATE, + REFERENCE +}; + + struct h5_base { - h5_base(std::string name) : name(name), value_alias(name){}; - h5_base(std::string name, std::string value_alias) : name(name), value_alias(value_alias){}; + h5_base(std::string name) : name(name){}; std::string name; - std::string value_alias; +}; + +struct h5_data_base{ + h5_data_base(DATA_TYPE data_type, DATA_LOCATION data_location) : data_type(data_type), data_location(data_location) {}; + DATA_TYPE data_type; + DATA_LOCATION data_location; }; struct h5_parent{ @@ -29,20 +41,32 @@ struct h5_group : public h5_base, public h5_parent { h5_group(std::string name, std::list items) : h5_base(name), h5_parent(items) {}; }; -struct h5_dataset : public h5_base, public h5_parent{ - h5_dataset(std::string name, DATA_TYPE type, std::list items={}) : h5_base(name), h5_parent(items), type(type) {}; - h5_dataset(std::string name, std::string value_alias, DATA_TYPE type, std::list items={}) : h5_base(name, value_alias), h5_parent(items), type(type) {}; - DATA_TYPE type; +struct h5_dataset : public h5_base, public h5_parent, public h5_data_base{ + h5_dataset(std::string name, std::string value_alias, DATA_TYPE data_type, std::list items={}) + : h5_base(name), h5_parent(items), h5_data_base(data_type, REFERENCE) {}; }; -struct h5_attribute : public h5_base { - h5_attribute(std::string name): h5_base(name){}; - h5_attribute(std::string name, std::string value_alias): h5_base(name, value_alias){}; +struct h5_attribute : public h5_base, public h5_data_base { + h5_attribute(std::string name, DATA_TYPE data_type): h5_base(name), h5_data_base(data_type, IMMEDIATE){}; + h5_attribute(std::string name, std::string value_alias, DATA_TYPE data_types): h5_base(name), h5_data_base(data_types, REFERENCE){}; }; -struct h5_attribute_val : public h5_attribute { - h5_attribute_val(std::string name, std::string value): h5_attribute(name){}; +template struct h5_attribute_val: public h5_base, public h5_data_base {}; + +template<> struct h5_attribute_val: public h5_attribute { + h5_attribute_val(std::string name, std::string value) : h5_attribute(name, NX_CHAR), value(value) {}; std::string value; }; +template<> struct h5_attribute_val: public h5_attribute { + h5_attribute_val(std::string name, int value) : h5_attribute(name, NX_CHAR), value(value) {}; + int value; +}; + +namespace h5_utils{ + void set_attributes(H5::H5Object& target, std::list& attributes); + void write_attribute(H5::H5Object& target, std::string name, std::string value); + void write_attribute(H5::H5Object& target, std::string name, int value); +} + #endif \ No newline at end of file