-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFile.cpp
More file actions
159 lines (140 loc) · 3.27 KB
/
File.cpp
File metadata and controls
159 lines (140 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
* File.cpp
*
* Created on: Jul 16, 2013
* Author: marcel
*/
#include "File.h"
#include "Exception.h"
#include <hdf5.h>
#include <sstream>
#include <iostream>
using namespace std;
namespace hdf5
{
File::File(): fFile(-1)
{
fType = ObjectType::File;
}
File::~File()
{
closeFile();
}
OpenFile& OpenFile::operator =(const OpenFile& original)
{
if (this != &original) {
fFileName = original.fFileName;
fTruncate = original.fTruncate;
fCreate = original.fCreate;
fRead = original.fRead;
fWrite = original.fWrite;
}
return *this;
}
File& File::closeFile()
{
if (isOpen()) {
herr_t id = H5Fclose(fFile);
if (id > -1) {
fFile = -1;
}
else {
stringstream ss;
ss << "Could not close file \"" << fFileMode.fFileName << "\": err_id=" << id;
throw Exception(ss.str());
}
}
return *this;
}
File::File(const std::string& fileName): fFile(-1)
{
fType = ObjectType::File;
openFile(OpenFile(fileName));
}
hsize_t File::getFileSize() const
{
if (this->fFile > -1) {
hsize_t size;
herr_t err = H5Fget_filesize(fFile, &size);
if (err > -1) {
return size;
}
else {
stringstream ss;
ss << "Error while getting filesize. Id=" << err;
throw Exception(ss);
}
}
else {
throw Exception("File is not opened");
}
}
hsize_t File::getFreeSpace() const
{
if (this->fFile > -1) {
hssize_t ret = H5Fget_freespace(fFile);
if (ret > -1) {
return static_cast<hsize_t>(ret);
}
else {
stringstream ss;
ss << "Error while getting filesize. Id=" << ret;
throw Exception(ss);
}
}
else {
throw Exception("File is not opened");
}
}
inline bool File::isReadOnly() const
{
if (this->fFile > -1) {
unsigned int intent;
herr_t ret = H5Fget_intent(fFile, &intent);
if (ret > -1) {
return (intent == H5F_ACC_RDONLY);
}
else {
stringstream ss;
ss << "Error while getting filemode. Id=" << ret;
throw Exception(ss);
}
}
else {
throw Exception("File is not opened");
}
}
File& File::openFile(const OpenFile& fileMode)
{
closeFile();
fFileMode = fileMode;
if (fFileMode.fCreate && fFileMode.fWrite && fFileMode.fRead) {
unsigned int flags = fFileMode.fTruncate ? H5F_ACC_TRUNC : H5F_ACC_EXCL;
fFile = H5Fcreate(fFileMode.fFileName.c_str(), flags, H5P_DEFAULT, H5P_DEFAULT);
}
if ( (fFile < 0 || !fFileMode.fCreate) ) {
unsigned int flags = (fFileMode.fRead && !fFileMode.fWrite) ? H5F_ACC_RDONLY : H5F_ACC_RDWR;
fFile = H5Fopen(fFileMode.fFileName.c_str(), flags, H5P_DEFAULT);
}
if (fFile < 0) {
throw Exception("Could not open file \"" + fFileMode.fFileName + "\"");
}
// retrieve object list
updateGroup(fFile);
// cout << " --- Groups in root:" << endl;
// for (Group::ObjectConstIterator itObj = objectsBegin(); itObj != objectsEnd(); ++itObj) {
// cout << " * name=" << itObj->first << endl;
// if (itObj->second->getType() == Object::Group) {
// Object::Ptr o = itObj->second;
// try {
// Group::Ptr g = boost::dynamic_pointer_cast<Group>(o);
// cout << " --> n-Objects: " << g->getNumObjects() << endl;
// }
// catch (const std::bad_cast& e) {
// cout << "Could not cast to Group: " << e.what() << endl;
// }
// }
// }
return *this;
}
} /* namespace hdf5 */