begin hdf5

This commit is contained in:
Christian Zimmermann 2023-01-12 19:29:45 +01:00
parent e8c8e519dc
commit 631d187c91
4 changed files with 229 additions and 0 deletions

View file

@ -0,0 +1,51 @@
#ifndef __cxz_h5_content_base_h__
#define __cxz_h5_content_base_h__
#include "cnorxz.h"
namespace CNORXZ
{
namespace hdf5
{
enum class ContentType {
ATTR = 1,
FILE = 2,
GROUP = 3,
DSET = 4,
TABLE = 5,
VALUE = 6,
};
class ContentBase
{
public:
DEFAULT_MEMBERS(ContentBase);
ContentBase(const String& _name, const ContentBase* _parent = nullptr);
virtual ~ContentBase() = default;
virtual ContentType type() const = 0;
virtual bool ro() const = 0;
virtual ContentBase& load() = 0;
virtual ContentBase& write() = 0;
virtual ContentBase& close() = 0;
virtual MArray<ContentBase>* get() = 0;
virtual const MArray<ContentBase>* get() const = 0;
virtual String path() const = 0;
virtual String file() const = 0;
const String& name() const;
const ContentBase* parent() const;
RangePtr range() const;
protected:
String mName;
const ContentBase* mParent = nullptr;
RangePtr mRange;
};
}
typedef Sptr<ContentType> ContentPtr;
}
#endif

View file

@ -0,0 +1,41 @@
#ifndef __cxz_h5_file_h__
#define __cxz_h5_file_h__
#include "h5_content_base.h"
#include <hdf5.h>
namespace CNORXZ
{
namespace hdf5
{
// maybe introduce abstraction layer between as base for File and Group
class File : public ContentBase
{
public:
DEFAULT_MEMBERS(File);
File(const String& fname, bool _ro = true);
~File();
virtual ContentType type() const override final;
virtual bool ro() const override final;
virtual File& load() override final;
virtual File& write() override final;
virtual File& close() override final;
virtual MArray<ContentBase>* get() override final;
virtual const MArray<ContentBase>* get() const override final;
virtual String path() const override final;
virtual String filename() const override final;
Int exists() const;
File& append(const ContentPtr& c);
private:
bool mRo = true;
hid_t mId = 0;
MArray<ContentPtr> mCont;
};
}
}
#endif

View file

@ -0,0 +1,27 @@
#include "h5_content_base.h"
namespace CNORXZ
{
namespace hdf5
{
ContentBase::ContentBase(const String& _name, const ContentBase* _parent) :
mName(_name), mParent(parent)
{}
const String& ContentBase::name() const
{
return mName;
}
const ContentBase* ContentBase::parent() const
{
return mParent;
}
RangePtr ContentBase::range() const
{
return mRange;
}
}
}

110
src/opt/hdf5/lib/h5_file.cc Normal file
View file

@ -0,0 +1,110 @@
#include "h5_file.h"
namespace CNORXZ
{
namespace hdf5
{
File::File(const String& fname, bool _ro) :
ContentBase(fname),
mRo(_ro)
{
Int ex = this->exists();
const String fn = this->filename();
CXZ_ASSERT( ex != 2, "tried to open non-h5 file '" << fn << "'" );
if(this->ro()){
CXZ_ASSERT( ex == 1, "could not open file as read-only: '"
<< fn << "' does not exist'");
mId = H5Fopen( fn.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT );
}
else {
if(ex == 1){
mId = H5Fopen( fn.c_str(), H5F_ACC_RDWR, H5P_DEFAULT );
}
else {
mId = H5Fcreate( fn.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT );
}
}
CXZ_ASSERT( mId > 0, "error while opening file '" << fn << "'" );
}
File::~File()
{
this->close();
}
ContentType File::type() const
{
return ContentType::FILE;
}
bool File::ro() const
{
return mRo;
}
File& File::load()
{
// !!!
return *this;
}
File& File::write()
{
CXZ_ASSERT( not mRo, "could not write to file: opened as read-only" );
for(auto& x: mCont){
x.write();
}
return *this;
}
File& File::close()
{
for(auto& x: mCont){
x.close();
}
if(mId != 0){
H5Fclose(mId);
}
return *this;
}
MArray<ContentBase>* File::get()
{
return &mCont;
}
const MArray<ContentBase>* File::get() const
{
return &mCont;
}
String File::path() const
{
return String("/");
}
String File::filename() const
{
return name();
}
Int File::exists() const
{
Int ex = 0;
std::ifstream fs(this->filename().c_str(), std::ios_base::binary);
if(fs.good()){
ex = 1; // file exists
}
fs.close();
if(ex != 0){
if(H5Fis_hdf5(this->filename().c_str()) <= 0){
ex = 2; // file not in h5 format
}
}
return ex;
}
}
}