v2.0.0
Loading...
Searching...
No Matches
fiff_dir_node.cpp
Go to the documentation of this file.
1//=============================================================================================================
21
22//=============================================================================================================
23// INCLUDES
24//=============================================================================================================
25
26#include "fiff_dir_node.h"
27#include "fiff_stream.h"
28#include "fiff_tag.h"
29#include "fiff_explain.h"
30#include <QDebug>
31//#include "fiff_ctf_comp.h"
32//#include "fiff_proj.h"
33//#include "fiff_info.h"
34
35//=============================================================================================================
36// USED NAMESPACES
37//=============================================================================================================
38
39using namespace FIFFLIB;
40
41//=============================================================================================================
42// DEFINE MEMBER METHODS
43//=============================================================================================================
44
46: type(-1)
47, nent_tree(-1)
48, parent(nullptr)
49{
50}
51
52//=============================================================================================================
53
55: type(p_FiffDirTree->type)
56, id(p_FiffDirTree->id)
57, dir(p_FiffDirTree->dir)
58, nent_tree(p_FiffDirTree->nent_tree)
59, parent(p_FiffDirTree->parent)
60, parent_id(p_FiffDirTree->parent_id)
61, children(p_FiffDirTree->children)
62{
63}
64
65//=============================================================================================================
66
68{
69 // QList<FiffDirNode*>::iterator i;
70 // for (i = this->children.begin(); i != this->children.end(); ++i)
71 // if (*i)
72 // delete *i;
73}
74
75//=============================================================================================================
76
77bool FiffDirNode::copy_tree(FiffStream::SPtr& p_pStreamIn, const FiffId& in_id, const QList<FiffDirNode::SPtr>& p_Nodes, FiffStream::SPtr& p_pStreamOut)
78{
79 if(p_Nodes.size() <= 0)
80 return false;
81
82 qint32 k, p;
83
84 for(k = 0; k < p_Nodes.size(); ++k)
85 {
86 p_pStreamOut->start_block(p_Nodes[k]->type);//8
87 if (p_Nodes[k]->id.version != -1)
88 {
89 if (in_id.version != -1)
90 p_pStreamOut->write_id(FIFF_PARENT_FILE_ID, in_id);//9
91
92 p_pStreamOut->write_id(FIFF_BLOCK_ID);//10
93 p_pStreamOut->write_id(FIFF_PARENT_BLOCK_ID, p_Nodes[k]->id);//11
94 }
95 for (p = 0; p < p_Nodes[k]->nent(); ++p)
96 {
97 //
98 // Do not copy these tags
99 //
100 if(p_Nodes[k]->dir[p]->kind == FIFF_BLOCK_ID || p_Nodes[k]->dir[p]->kind == FIFF_PARENT_BLOCK_ID || p_Nodes[k]->dir[p]->kind == FIFF_PARENT_FILE_ID)
101 continue;
102
103 //
104 // Read and write tags, pass data through transparently
105 //
106 if (!p_pStreamIn->device()->seek(p_Nodes[k]->dir[p]->pos)) //fseek(fidin, nodes(k).dir(p).pos, 'bof') == -1
107 {
108 qWarning("Could not seek to the tag\n");
109 return false;
110 }
111
112 //ToDo this is the same like read_tag
113 auto tag = std::make_unique<FiffTag>();
114 //QDataStream in(fidin);
115 FiffStream::SPtr in = p_pStreamIn;
116 in->setByteOrder(QDataStream::BigEndian);
117
118 //
119 // Read fiff tag header from stream
120 //
121 *in >> tag->kind;
122 *in >> tag->type;
123 qint32 size;
124 *in >> size;
125 tag->resize(size);
126 *in >> tag->next;
127
128 //
129 // Read data when available
130 //
131 if (tag->size() > 0)
132 {
133 in->readRawData(tag->data(), tag->size());
134 // Don't do this conversion because it breaks the writing on
135 // little endian systems (i.e., OSX, Linux, Windows...)
136 // FiffTag::convert_tag_data(tag.get(),FIFFV_BIG_ENDIAN,FIFFV_NATIVE_ENDIAN);
137 }
138
139 //QDataStream out(p_pStreamOut);
140 FiffStream::SPtr out = p_pStreamOut;
141 out->setByteOrder(QDataStream::BigEndian);
142
143 *out << (qint32)tag->kind;
144 *out << (qint32)tag->type;
145 *out << (qint32)tag->size();
146 *out << (qint32)FIFFV_NEXT_SEQ;
147
148 out->writeRawData(tag->data(),tag->size());
149 }
150 for(p = 0; p < p_Nodes[k]->nchild(); ++p)
151 {
152 QList<FiffDirNode::SPtr> childList;
153 childList << p_Nodes[k]->children[p];
154 FiffDirNode::copy_tree(p_pStreamIn, in_id, childList, p_pStreamOut);
155 }
156 p_pStreamOut->end_block(p_Nodes[k]->type);
157 }
158 return true;
159}
160
161//=============================================================================================================
162
163QList<FiffDirNode::SPtr> FiffDirNode::dir_tree_find(fiff_int_t p_kind) const
164{
165 QList<FiffDirNode::SPtr> nodes;
166 if(this->type == p_kind)
167 nodes.append(FiffDirNode::SPtr(new FiffDirNode(this)));
168
169 QList<FiffDirNode::SPtr>::const_iterator i;
170 for (i = this->children.begin(); i != this->children.end(); ++i)
171 nodes.append((*i)->dir_tree_find(p_kind));
172
173 return nodes;
174}
175
176//=============================================================================================================
177
178bool FiffDirNode::find_tag(FiffStream* p_pStream, fiff_int_t findkind, FiffTag::UPtr& p_pTag) const
179{
180 for (qint32 p = 0; p < this->nent(); ++p)
181 {
182 if (this->dir[p]->kind == findkind)
183 {
184 p_pStream->read_tag(p_pTag,this->dir[p]->pos);
185 return true;
186 }
187 }
188 if (p_pTag)
189 p_pTag.reset();
190
191 return false;
192}
193
194//=============================================================================================================
195
196bool FiffDirNode::has_tag(fiff_int_t findkind)
197{
198 for(qint32 p = 0; p < this->nent(); ++p)
199 if(this->dir.at(p)->kind == findkind)
200 return true;
201 return false;
202}
203
204//=============================================================================================================
205
206bool FiffDirNode::has_kind(fiff_int_t p_kind) const
207{
208 if(this->type == p_kind)
209 return true;
210
211 QList<FiffDirNode::SPtr>::const_iterator i;
212 for(i = this->children.begin(); i != this->children.end(); ++i)
213 if((*i)->has_kind(p_kind))
214 return true;
215
216 return false;
217}
218
219//=============================================================================================================
220
221void FiffDirNode::print(int indent) const
222{
223 int j, prev_kind,count;
224 QList<FiffDirEntry::SPtr> dentry = this->dir;
225
226 for (int k = 0; k < indent; k++)
227 putchar(' ');
228 explain_block (this->type);
229 qDebug(" { ");
230 if (!this->id.isEmpty())
231 this->id.print();
232 qDebug("\n");
233
234 for (j = 0, prev_kind = -1, count = 0; j < this->nent(); j++) {
235 if (dentry[j]->kind != prev_kind) {
236 if (count > 1)
237 qDebug(" [%d]\n",count);
238 else if (j > 0)
239 putchar('\n');
240 for (int k = 0; k < indent+2; k++)
241 putchar(' ');
242 explain (dentry[j]->kind);
243 prev_kind = dentry[j]->kind;
244 count = 1;
245 }
246 else
247 count++;
248 prev_kind = dentry[j]->kind;
249 }
250 if (count > 1)
251 qDebug(" [%d]\n",count);
252 else if (j > 0)
253 putchar ('\n');
254 for (j = 0; j < this->nchild(); j++)
255 this->children[j]->print(indent+5);
256 for (int k = 0; k < indent; k++)
257 putchar(' ');
258 qDebug("}\n");
259}
260
261//=============================================================================================================
262
264{
265 for (int k = 0; _fiff_block_explanations[k].kind >= 0; k++) {
266 if (_fiff_block_explanations[k].kind == kind) {
267 qDebug("%d = %s",kind,_fiff_block_explanations[k].text);
268 return;
269 }
270 }
271 qWarning("Cannot explain: %d",kind);
272}
273
274//=============================================================================================================
275
277{
278 int k;
279 for (k = 0; _fiff_explanations[k].kind >= 0; k++) {
280 if (_fiff_explanations[k].kind == kind) {
281 qDebug("%d = %s",kind,_fiff_explanations[k].text);
282 return;
283 }
284 }
285 qWarning("Cannot explain: %d",kind);
286}
287
288//=============================================================================================================
289
291{
292 int k;
293 for (k = 0; _fiff_explanations[k].kind >= 0; k++) {
294 if (_fiff_explanations[k].kind == kind)
295 return _fiff_explanations[k].text;
296 }
297 return "unknown";
298}
299
300//=============================================================================================================
301
302fiff_int_t FiffDirNode::nent() const
303{
304 return dir.size();
305}
306
307//=============================================================================================================
308
309fiff_int_t FiffDirNode::nchild() const
310{
311 return children.size();
312}
Static lookup table mapping every documented FIFF tag kind to a fixed-width human-readable label,...
#define FIFFV_NEXT_SEQ
Recursive node of the parsed FIFF block tree (FIFFB_* hierarchy with directory entries and children).
#define FIFF_PARENT_BLOCK_ID
Definition fiff_file.h:326
#define FIFF_BLOCK_ID
Definition fiff_file.h:319
#define FIFF_PARENT_FILE_ID
Definition fiff_file.h:325
FIFF tag: the 16-byte tag header (kind, type, size, next) plus its decoded payload.
FIFF binary tag-stream layer: wraps a QIODevice to read and write FIFF tags, directories,...
FIFF file I/O, in-memory data structures and high-level readers/writers.
fiff_int_t nchild() const
QList< FiffDirNode::SPtr > children
static void explain(int kind)
FiffDirNode::SPtr parent
QSharedPointer< FiffDirNode > SPtr
static void explain_block(int kind)
fiff_int_t nent() const
static const char * get_tag_explanation(int kind)
QList< FiffDirNode::SPtr > dir_tree_find(fiff_int_t p_kind) const
void print(int indent) const
bool has_kind(fiff_int_t p_kind) const
bool has_tag(fiff_int_t findkind)
QList< FiffDirEntry::SPtr > dir
bool find_tag(QSharedPointer< FiffStream > &p_pStream, fiff_int_t findkind, std::unique_ptr< FiffTag > &p_pTag) const
static bool copy_tree(QSharedPointer< FiffStream > &p_pStreamIn, const FiffId &in_id, const QList< QSharedPointer< FiffDirNode > > &p_Nodes, QSharedPointer< FiffStream > &p_pStreamOut)
128-bit FIFF identifier: hardware machine ID plus creation time, stamped on every file and block.
Definition fiff_id.h:66
fiff_int_t version
Definition fiff_id.h:177
FIFF tag-stream reader/writer: wraps a QIODevice and exposes typed read_* / write_* methods for every...
QSharedPointer< FiffStream > SPtr
bool read_tag(std::unique_ptr< FiffTag > &p_pTag, fiff_long_t pos=-1)
std::unique_ptr< FiffTag > UPtr
Definition fiff_tag.h:164