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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
|
/*
* Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
* Copyright (C) 2012 - Scilab Enterprises - Calixte DENIZET
*
* This file must be used under the terms of the CeCILL.
* This source file is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at
* http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
*
*/
#include "H5Group.hxx"
#include "H5File.hxx"
#include "H5SoftLinksList.hxx"
#include "H5GroupsList.hxx"
#include "H5DatasetsList.hxx"
#include "H5TypesList.hxx"
#include "H5Link.hxx"
#include "H5BasicData.hxx"
namespace org_modules_hdf5
{
void H5Group::init()
{
group = H5Gopen(getParent().getH5Id(), name.c_str(), H5P_DEFAULT);
if (group < 0)
{
throw H5Exception(__LINE__, __FILE__, _("Cannot open the group %s."), name.c_str());
}
}
H5Group::H5Group(H5Object & _parent, const std::string & _name) : H5Object(_parent, _name)
{
init();
}
H5Group::H5Group(H5Object & _parent, hid_t _group, const std::string & _name) : H5Object(_parent, _name), group(_group)
{
}
H5Group::~H5Group()
{
if (group >= 0)
{
H5Gclose(group);
}
}
H5NamedObjectsList<H5SoftLink> & H5Group::getSoftLinks()
{
return *new H5NamedObjectsList<H5SoftLink>(*this, -1, H5L_TYPE_SOFT, "Soft Link");
}
H5NamedObjectsList<H5ExternalLink> & H5Group::getExternalLinks()
{
return *new H5NamedObjectsList<H5ExternalLink>(*this, -1, H5L_TYPE_EXTERNAL, "External Link");
}
H5GroupsList & H5Group::getGroups()
{
return *new H5GroupsList(*this);
}
H5NamedObjectsList<H5Group> & H5Group::getHardGroups()
{
return *new H5NamedObjectsList<H5Group>(*this, H5O_TYPE_GROUP, H5L_TYPE_HARD, "Group");
}
H5NamedObjectsList<H5Type> & H5Group::getHardTypes()
{
return *new H5NamedObjectsList<H5Type>(*this, H5O_TYPE_NAMED_DATATYPE, H5L_TYPE_HARD, "Type");
}
H5NamedObjectsList<H5Dataset> & H5Group::getHardDatasets()
{
return *new H5NamedObjectsList<H5Dataset>(*this, H5O_TYPE_DATASET, H5L_TYPE_HARD, "Dataset");
}
H5DatasetsList & H5Group::getDatasets()
{
return *new H5DatasetsList(*this);
}
H5TypesList & H5Group::getTypes()
{
return *new H5TypesList(*this);
}
const unsigned int H5Group::getLinksSize() const
{
herr_t err;
H5G_info_t info;
err = H5Gget_info(group, &info);
if (err < 0)
{
throw H5Exception(__LINE__, __FILE__, _("Cannot get the links number"));
}
return (unsigned int)info.nlinks;
}
std::string H5Group::getCompletePath() const
{
std::string name = getName();
if (name == "/")
{
return "/";
}
return H5Object::getCompletePath();
}
void H5Group::getAccessibleAttribute(const std::string & _name, const int pos, void * pvApiCtx) const
{
SciErr err;
std::string lower(_name);
std::transform(_name.begin(), _name.end(), lower.begin(), tolower);
if (lower == "attributes")
{
std::vector<std::string> names;
getNames(*this, names, ATTRIBUTE);
H5BasicData<char>::putStringVectorOnStack(names, (int)names.size(), 1, pos, pvApiCtx);
return;
}
else if (lower == "groups")
{
std::vector<std::string> names;
getNames(*this, names, GROUP);
H5BasicData<char>::putStringVectorOnStack(names, (int)names.size(), 1, pos, pvApiCtx);
return;
}
else if (lower == "datasets")
{
std::vector<std::string> names;
getNames(*this, names, DATASET);
H5BasicData<char>::putStringVectorOnStack(names, (int)names.size(), 1, pos, pvApiCtx);
return;
}
else if (lower == "types")
{
std::vector<std::string> names;
getNames(*this, names, TYPE);
H5BasicData<char>::putStringVectorOnStack(names, (int)names.size(), 1, pos, pvApiCtx);
return;
}
else if (lower == "externals")
{
std::vector<std::string> names;
getNames(*this, names, EXTERNAL);
H5BasicData<char>::putStringVectorOnStack(names, (int)names.size(), 1, pos, pvApiCtx);
return;
}
else if (lower == "softs")
{
std::vector<std::string> names;
getNames(*this, names, SOFT);
H5BasicData<char>::putStringVectorOnStack(names, (int)names.size(), 1, pos, pvApiCtx);
return;
}
else if (lower == "danglings")
{
std::vector<std::string> names;
getNames(*this, names, DANGLING);
H5BasicData<char>::putStringVectorOnStack(names, (int)names.size(), 1, pos, pvApiCtx);
return;
}
else if (lower == "hards")
{
std::vector<std::string> names;
getNames(*this, names, HARD);
H5BasicData<char>::putStringVectorOnStack(names, (int)names.size(), 1, pos, pvApiCtx);
return;
}
else if (lower == "links")
{
std::vector<std::string> names;
std::vector<std::string> types;
std::vector<std::string> linkstype;
std::vector<const char *> _str;
H5Object::getLinksInfo(*this, names, types, linkstype);
_str.reserve(names.size() * 3);
for (unsigned int i = 0; i < names.size(); i++)
{
_str.push_back(names[i].c_str());
}
for (unsigned int i = 0; i < names.size(); i++)
{
_str.push_back(linkstype[i].c_str());
}
for (unsigned int i = 0; i < names.size(); i++)
{
_str.push_back(types[i].c_str());
}
err = createMatrixOfString(pvApiCtx, pos, (int)names.size(), 3, &(_str[0]));
if (err.iErr)
{
throw H5Exception(__LINE__, __FILE__, _("Cannot create a column of strings on the stack."));
}
return;
}
else
{
try
{
H5Object & obj = H5Object::getObject(*const_cast<H5Group *>(this), _name);
obj.createOnScilabStack(pos, pvApiCtx);
return;
}
catch (const H5Exception & /*e*/) { }
}
H5Object::getAccessibleAttribute(_name, pos, pvApiCtx);
}
void H5Group::ls(std::vector<std::string> & name, std::vector<std::string> & type) const
{
herr_t err;
OpDataGetLs opdata(const_cast<H5Group *>(this), &name, &type);
hsize_t idx = 0;
err = H5Literate(group, H5_INDEX_NAME, H5_ITER_INC, &idx, getLsInfo, &opdata);
if (err < 0)
{
throw H5Exception(__LINE__, __FILE__, _("Cannot list group links."));
}
idx = 0;
err = H5Aiterate(group, H5_INDEX_NAME, H5_ITER_INC, &idx, H5Object::getLsAttributes, &opdata);
if (err < 0)
{
throw H5Exception(__LINE__, __FILE__, _("Cannot list group attributes."));
}
}
herr_t H5Group::getLsInfo(hid_t g_id, const char * name, const H5L_info_t * info, void * op_data)
{
H5O_info_t oinfo;
herr_t err;
hid_t obj;
OpDataGetLs & opdata = *(OpDataGetLs *)op_data;
switch (info->type)
{
case H5L_TYPE_SOFT:
opdata.name->push_back(name);
opdata.type->push_back("soft");
break;
case H5L_TYPE_EXTERNAL:
opdata.name->push_back(name);
opdata.type->push_back("external");
break;
case H5L_TYPE_HARD:
obj = H5Oopen_by_addr(g_id, info->u.address);
if (obj < 0)
{
return (herr_t) - 1;
}
err = H5Oget_info(obj, &oinfo);
H5Oclose(obj);
if (err < 0)
{
return (herr_t) - 1;
}
switch (oinfo.type)
{
case H5O_TYPE_GROUP:
opdata.name->push_back(name);
opdata.type->push_back("group");
break;
case H5O_TYPE_DATASET:
opdata.name->push_back(name);
opdata.type->push_back("dataset");
break;
case H5O_TYPE_NAMED_DATATYPE:
opdata.name->push_back(name);
opdata.type->push_back("type");
break;
default:
return (herr_t) - 1;
}
break;
default:
return (herr_t) - 1;
}
return (herr_t)0;
}
std::string H5Group::ls() const
{
std::ostringstream os;
herr_t err;
OpDataPrintLs opdata;
opdata.parent = const_cast<H5Group *>(this);
opdata.os = &os;
hsize_t idx = 0;
err = H5Literate(group, H5_INDEX_NAME, H5_ITER_INC, &idx, printLsInfo, &opdata);
if (err < 0)
{
throw H5Exception(__LINE__, __FILE__, _("Cannot list group contents"));
}
return os.str();
}
herr_t H5Group::printLsInfo(hid_t g_id, const char * name, const H5L_info_t * info, void * op_data)
{
H5O_info_t oinfo;
herr_t err;
H5Object * hobj = 0;
hid_t obj = 0;
OpDataPrintLs & opdata = *(OpDataPrintLs *)op_data;
switch (info->type)
{
case H5L_TYPE_SOFT:
hobj = new H5SoftLink(*opdata.parent, name);
break;
case H5L_TYPE_EXTERNAL:
hobj = new H5ExternalLink(*opdata.parent, name);
break;
case H5L_TYPE_HARD:
obj = H5Oopen(g_id, name, H5P_DEFAULT);
err = H5Oget_info(obj, &oinfo);
H5Oclose(obj);
if (err < 0)
{
return (herr_t) - 1;
}
switch (oinfo.type)
{
case H5O_TYPE_GROUP:
hobj = new H5Group(*opdata.parent, name);
break;
case H5O_TYPE_DATASET:
hobj = new H5Dataset(*opdata.parent, name);
break;
case H5O_TYPE_NAMED_DATATYPE:
hobj = new H5Type(*opdata.parent, name);
break;
default:
return (herr_t) - 1;
}
break;
default:
return (herr_t) - 1;
}
hobj->printLsInfo(*opdata.os);
delete hobj;
return (herr_t)0;
}
void H5Group::printLsInfo(std::ostringstream & os) const
{
std::string str(getName());
H5Object::getResizedString(str);
os << str << "Group" << std::endl;
}
std::string H5Group::dump(std::map<haddr_t, std::string> & alreadyVisited, const unsigned int indentLevel) const
{
std::ostringstream os;
haddr_t addr = this->getAddr();
std::map<haddr_t, std::string>::iterator it = alreadyVisited.find(addr);
if (it != alreadyVisited.end())
{
os << H5Object::getIndentString(indentLevel) << "GROUP \"" << getName() << "\" {" << std::endl
<< H5Object::getIndentString(indentLevel + 1) << "HARDLINK \"" << it->second << "\"" << std::endl
<< H5Object::getIndentString(indentLevel) << "}" << std::endl;
return os.str();
}
else
{
alreadyVisited.insert(std::pair<haddr_t, std::string>(addr, getCompletePath()));
}
H5AttributesList & attrs = const_cast<H5Group *>(this)->getAttributes();
H5NamedObjectsList<H5SoftLink> & softlinks = const_cast<H5Group *>(this)->getSoftLinks();
H5NamedObjectsList<H5ExternalLink> & externallinks = const_cast<H5Group *>(this)->getExternalLinks();
H5NamedObjectsList<H5Group> & hardgroups = const_cast<H5Group *>(this)->getHardGroups();
H5NamedObjectsList<H5Type> & hardtypes = const_cast<H5Group *>(this)->getHardTypes();
H5NamedObjectsList<H5Dataset> & harddatasets = const_cast<H5Group *>(this)->getHardDatasets();
os << H5Object::getIndentString(indentLevel) << "GROUP \"" << name << "\" {" << std::endl;
os << attrs.dump(alreadyVisited, indentLevel + 1);
os << hardgroups.dump(alreadyVisited, indentLevel + 1);
os << hardtypes.dump(alreadyVisited, indentLevel + 1);
os << harddatasets.dump(alreadyVisited, indentLevel + 1);
os << softlinks.dump(alreadyVisited, indentLevel + 1);
os << externallinks.dump(alreadyVisited, indentLevel + 1);
os << H5Object::getIndentString(indentLevel) << "}" << std::endl;
delete &attrs;
delete &softlinks;
delete &externallinks;
delete &hardgroups;
delete &hardtypes;
delete &harddatasets;
return os.str();
}
std::string H5Group::toString(const unsigned int indentLevel) const
{
std::ostringstream os;
std::string indentString = H5Object::getIndentString(indentLevel + 1);
OpDataCount opdata(false);
H5Object::count(*this, opdata);
os << H5Object::getIndentString(indentLevel) << "HDF5 Group" << std::endl
<< indentString << "Filename" << ": " << getFile().getFileName() << std::endl
<< indentString << "Name" << ": " << getBaseName() << std::endl
<< indentString << "Path" << ": " << getCompletePath() << std::endl
<< indentString << "Attributes" << ": [1 x " << getAttributesNumber() << "]" << std::endl
<< indentString << "Groups" << ": [1 x " << opdata.group << "]" << std::endl
<< indentString << "Datasets" << ": [1 x " << opdata.dataset << "]" << std::endl
<< indentString << "Types" << ": [1 x " << opdata.type << "]" << std::endl
<< indentString << "Externals" << ": [1 x " << opdata.external << "]" << std::endl
<< indentString << "Softs" << ": [1 x " << opdata.soft << "]";
return os.str();
}
void H5Group::createGroup(H5Object & parent, const std::string & name)
{
const char * _name = name.c_str();
createGroup(parent, 1, &_name);
}
void H5Group::createGroup(H5Object & parent, const int size, const char ** names)
{
hid_t obj;
hid_t loc = parent.getH5Id();
for (unsigned int i = 0; i < (unsigned int)size; i++)
{
if (H5Lexists(loc, names[i], H5P_DEFAULT) > 0)
{
throw H5Exception(__LINE__, __FILE__, _("The group already exists: %s."), names[i]);
}
obj = H5Gcreate(loc, names[i], H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
if (obj < 0)
{
throw H5Exception(__LINE__, __FILE__, _("Cannot create the group: %s."), names[i]);
}
H5Gclose(obj);
}
}
}
|