CNORXZ
Container with Native Operation Routines and Expressions
Loading...
Searching...
No Matches
h5_file.cc
Go to the documentation of this file.
1// -*- C++ -*-
12#include "h5_file.h"
13#include <fstream>
14
15namespace CNORXZ
16{
17 namespace hdf5
18 {
19 File::File(const String& fname, bool _ro) :
21 mRo(_ro)
22 {}
23
25 {
26 this->close();
27 }
28
30 {
31 return ContentType::FILE;
32 }
33
34 bool File::ro() const
35 {
36 return mRo;
37 }
38
40 {
41 if(isOpen()){
42 return *this;
43 }
44 Int ex = this->exists();
45 const String fn = this->filename();
46 CXZ_ASSERT( ishdf5(), "tried to open non-h5 file '" << fn << "'" );
47 if(mRo){
48 CXZ_ASSERT( ex == 1, "could not open file as read-only: '"
49 << fn << "' does not exist'");
51 }
52 else {
53 if(ex == 1){
54 mId = H5Fopen( fn.c_str(), H5F_ACC_RDWR, H5P_DEFAULT );
55 }
56 else {
58 }
59 }
60 CXZ_ASSERT( mId > 0, "error while opening file '" << fn << "'" );
61 this->mkCont();
62 return *this;
63 }
64
66 {
67 if(mCont.range() != nullptr){
68 for(auto& x: mCont){
69 if(x != nullptr){
70 x->close();
71 }
72 }
73 }
74 if(mId != 0){
76 }
77 mId = 0;
78 return *this;
79 }
80
82 {
83 return "";
84 }
85
87 {
88 return name();
89 }
90
91 bool File::exists() const
92 {
93 bool ex = false;
94 std::ifstream fs(this->filename().c_str(), std::ios_base::binary);
95 if(fs.good()){
96 ex = true; // file exists
97 }
98 fs.close();
99 return ex;
100 }
101
102 bool File::ishdf5() const
103 {
104 if(exists()){
105 if(H5Fis_hdf5(this->filename().c_str()) <= 0){
106 return false;
107 }
108 }
109 return true; // a non-existing file can be created in hdf5 format
110 }
111 }
112}
#define CXZ_ASSERT(statement, errmsg)
Definition assert.h:40
virtual RangePtr range() const
const String & name() const
virtual File & close() override final
Definition h5_file.cc:65
virtual ContentType type() const override final
Definition h5_file.cc:29
bool ishdf5() const
Definition h5_file.cc:102
virtual File & open() override final
Definition h5_file.cc:39
virtual bool exists() const override final
Definition h5_file.cc:91
virtual String filename() const override final
Definition h5_file.cc:86
virtual bool ro() const override final
Definition h5_file.cc:34
File(const String &fname, bool _ro=true)
Definition h5_file.cc:19
virtual String path() const override final
Definition h5_file.cc:81
MArray< ContentPtr > mCont
Definition h5_group.h:148
Group declaration.
std::string String
Definition types.h:42
int32_t Int
Definition types.h:36
Sptr< Range > rangeCast(const RangePtr r)