summaryrefslogtreecommitdiffstats
path: root/c++/src/H5File.C
blob: 1ccea96405e996d348a07536ae673eaacc1bb672 (plain)
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
#include <string>

#include "H5Include.h"
#include "H5RefCounter.h"
#include "H5Exception.h"
#include "H5IdComponent.h"
#include "H5Idtemplates.h"
#include "H5PropList.h"
#include "H5Object.h"
#include "H5FaccProp.h"
#include "H5FcreatProp.h"
#include "H5DxferProp.h"
#include "H5DcreatProp.h"
#include "H5Group.h"
#include "H5AbstractDs.h"
#include "H5DataSpace.h"
#include "H5DataSet.h"
#include "H5File.h"
#include "H5Alltypes.h"
#include "H5CommonFG.h"

#ifndef H5_NO_NAMESPACE
namespace H5 {
#endif

// Creates or opens an HDF5 file depending on the parameter flags.
H5File::H5File( const string& name, unsigned int flags, const FileCreatPropList& create_plist, const FileAccPropList& access_plist ) : IdComponent()
{
   getFile( name.c_str(), flags, create_plist, access_plist );
}

H5File::H5File( const char* name, unsigned int flags, const FileCreatPropList& create_plist, const FileAccPropList& access_plist ) : IdComponent()
{
   getFile( name, flags, create_plist, access_plist );
}

// This function is private and contains common code between the 
// constructors taking a string or a char*
void H5File::getFile( const char* name, unsigned int flags, const FileCreatPropList& create_plist, const FileAccPropList& access_plist )
{
   // These bits only set for creation, so if any of them are set,
   // create the file.
   if( flags & (H5F_ACC_EXCL|H5F_ACC_TRUNC|H5F_ACC_DEBUG ))
   {
      hid_t create_plist_id = create_plist.getId();
      hid_t access_plist_id = access_plist.getId();
      id = H5Fcreate( name, flags, create_plist_id, access_plist_id );
   }
   // Open the file if none of the bits above are set.
   else
   {
      // use create_plist for access plist because of the default argument
      hid_t access_plist_id = create_plist.getId();
      id = H5Fopen( name, flags, access_plist_id );
   }

   if( id <= 0 )  // throw an exception when open/create fail
   {
      throw FileIException();
   }
}

// Copy constructor: makes a copy of the original H5File object.
H5File::H5File( const H5File& original ) : IdComponent( original ) {}

// Determines whether a file specified by its name in HDF5 format
bool H5File::isHdf5(const string& name ) 
{
   return( isHdf5( name.c_str()) );
}
bool H5File::isHdf5(const char* name ) 
{
   // Calls C routine H5Fis_hdf5 to determine whether the file is in 
   // HDF5 format.  It returns positive value, 0, or negative value
   htri_t ret_value = H5Fis_hdf5( name );
   if( ret_value > 0 )
      return true;
   else if( ret_value == 0 )
      return false;
   else // Raise exception when H5Fis_hdf5 returns a negative value 
   {
      throw FileIException();
   }
}

// Reopens this file
void H5File::reopen()
{
   // reset the identifier of this H5File - send 'this' in so that
   // H5Fclose can be called appropriately
   resetIdComponent( this );

   // call C routine to reopen the file - Note: not sure about this
   // does id need to be closed later?  which id to be the parameter?
   id = H5Freopen( id );
   if( id <= 0 ) // Raise exception when H5Freopen returns a neg value
   {
      throw FileIException();
   }
}

// Creates a new group in this file using the template function provided
// in FGtemplates.h  
Group H5File::createGroup( const string& name, size_t size_hint ) const
{
   return( createGroup( name.c_str(), size_hint ));
}

Group H5File::createGroup( const char* name, size_t size_hint ) const
{
   try {
      Group group = createGroupT( id, name, size_hint );
      return( group );
   }
   catch( File_GroupException error )
   {
      throw FileIException();
   }
}

// Opens an existing group in this file using the template function provided
// in FGtemplates.h  
Group H5File::openGroup( const string& name ) const
{
   return( openGroup( name.c_str() ));
}

Group H5File::openGroup( const char* name ) const
{
   try {
      Group group = openGroupT( id, name );
      return( group );
   }
   catch( File_GroupException error )
   {
      throw FileIException();
   }
}

// Creates a dataset in this file using the template function
// provided in FGtemplates.h  
DataSet H5File::createDataSet( const string& name, const DataType& data_type, const DataSpace& data_space, const DSetCreatPropList& create_plist ) const
{
   return( createDataSet( name.c_str(), data_type, data_space, create_plist ));
}

DataSet H5File::createDataSet( const char* name, const DataType& data_type, const DataSpace& data_space, const DSetCreatPropList& create_plist ) const
{
   try {
      DataSet dataset = createDataSetT( id, name, data_type, data_space, create_plist );
      return( dataset );
   }
   catch( File_GroupException error )
   {
      throw FileIException();
   }
}

// Opens an existing dataset in this file using the template function
// provided in FGtemplates.h  
DataSet H5File::openDataSet( const string& name ) const
{
   return( openDataSet( name.c_str() ));
}

DataSet H5File::openDataSet( const char* name ) const
{
   try {
      DataSet dataset = openDataSetT( id, name );
      return( dataset );
   }
   catch( File_GroupException error )
   {
      throw FileIException();
   }
}

// This private member function calls the C API H5Topen to open the
// named datatype in this file, and returns the datatype's identifier.  
// The function is used by the functions openXxxType's below for 
// opening the sub-types
hid_t H5File::p_openDataType( const char* name ) const
{
   // Call C function H5Topen to open the named datatype in this group,
   // giving the group id
   hid_t datatype_id = H5Topen( id, name );

   // If the datatype id is valid, return it, otherwise, throw an exception.
   if( datatype_id > 0 )
      return( datatype_id );
   else
   {
      throw FileIException();
   }
}

//
// The following member functions use the private function
// p_openDataType to open a named datatype in this group
//

// Opens the named generic datatype in this group.
DataType H5File::openDataType( const string& name ) const
{
   return( openDataType( name.c_str()) );
}
DataType H5File::openDataType( const char* name ) const
{
   DataType data_type( p_openDataType( name ));
   return( data_type );
}

// Opens the named enumeration datatype in this group.
EnumType H5File::openEnumType( const string& name ) const
{
   return( openEnumType( name.c_str()) );
}
EnumType H5File::openEnumType( const char* name ) const
{
   EnumType enum_type( p_openDataType( name ));
   return( enum_type );
}

// Opens the named compound datatype in this group.
CompType H5File::openCompType( const string& name ) const
{
   return( openCompType( name.c_str()) );
}
CompType H5File::openCompType( const char* name ) const
{
   CompType comp_type( p_openDataType( name ));
   return( comp_type );
}

// Opens the named integer datatype in this group.
IntType H5File::openIntType( const string& name ) const
{
   return( openIntType( name.c_str()) );
}
IntType H5File::openIntType( const char* name ) const
{ 
   IntType int_type( p_openDataType( name ));
   return( int_type );
}

// Opens the named floating-point datatype in this group.
FloatType H5File::openFloatType( const string& name ) const
{
   return( openFloatType( name.c_str()) );
}
FloatType H5File::openFloatType( const char* name ) const
{
   FloatType float_type( p_openDataType( name ));
   return( float_type );
}

// Opens the named string datatype of this group
StrType H5File::openStrType( const string& name ) const
{
   return( openStrType( name.c_str()) );
}
StrType H5File::openStrType( const char* name ) const
{
   StrType str_type( p_openDataType( name ));
   return( str_type );
}

// Returns the creation property list of this file
FileCreatPropList H5File::getCreatePlist() const
{
   hid_t create_plist_id = H5Fget_create_plist( id );

   // if H5Fget_create_plist returns a valid id, create and return
   // the FileCreatPropList object for this property list
   if( create_plist_id > 0 )
   {
      FileCreatPropList create_plist( create_plist_id );
      return( create_plist );
   }
   else
   {
      throw FileIException();
   }
}

// Returns the access property list of this file
FileAccPropList H5File::getAccessPlist() const
{
   hid_t access_plist_id = H5Fget_access_plist( id );

   // if H5Fget_access_plist returns a valid id, create and return
   // the FileAccPropList object for this property list
   if( access_plist_id > 0 )
   {
      FileAccPropList access_plist( access_plist_id );
      return access_plist;
   }
   else // Raise an exception
   {
      throw FileIException();
   }
}

// Creates a link of the specified type from new_name to current_name;
// both names are interpreted relative to this file
void H5File::link( H5G_link_t link_type, const string& curr_name, const string& new_name ) const
{
   link( link_type, curr_name.c_str(), new_name.c_str() );
}

void H5File::link( H5G_link_t link_type, const char* curr_name, const char* new_name ) const
{
   try {
      linkT( id, link_type, curr_name, new_name );
   }
   catch( File_GroupException error ) {
      throw FileIException();
   }
}

// Removes the specified name from this file.
void H5File::unlink( const string& name ) const
{
   unlink( name.c_str());
}

void H5File::unlink( const char* name ) const
{
   try {
      unlinkT( id, name );
   }
   catch( File_GroupException error ) {
      throw FileIException();
   }
}

// Renames an object from this file.
void H5File::move( const string& src, const string& dst ) const
{
   move( src.c_str(), dst.c_str());
}

void H5File::move( const char* src, const char* dst ) const
{
   try {
      moveT( id, src, dst );
   }
   catch( File_GroupException error ) {
      throw FileIException();
   }
}

// Returns information about an object
void H5File::getObjinfo( const string& name, hbool_t follow_link, H5G_stat_t& statbuf ) const
{
   getObjinfo( name, follow_link, statbuf );
}

void H5File::getObjinfo( const char* name, hbool_t follow_link, H5G_stat_t& statbuf ) const
{
   try {
      getObjinfoT( id, name, follow_link, statbuf );
   }
   catch( File_GroupException error ) {
      throw FileIException();
   }
}

// Returns the name of the object that the symbolic link points to.
string H5File::getLinkval( const string& name, size_t size ) const
{
   return( getLinkval( name.c_str(), size ));
}

string H5File::getLinkval( const char* name, size_t size ) const
{
   try {
      string value = getLinkvalT( id, name, size );
      return( value );
   }
   catch( File_GroupException error ) {
      throw FileIException();
   }
}

// Sets comment for specified object.
void H5File::setComment( const string& name, const string& comment ) const
{
   setComment( name.c_str(), comment.c_str());
}

void H5File::setComment( const char* name, const char* comment ) const
{
   try {
      setCommentT( id, name, comment );
   }
   catch( File_GroupException error ) {
      throw FileIException();
   }
}

// Retrieves comment for specified object
string H5File::getComment( const string& name, size_t bufsize ) const
{
   return( getComment( name.c_str(), bufsize ));
}

string H5File::getComment( const char* name, size_t bufsize ) const
{
   try {
      string comment = getCommentT( id, name, bufsize );
      return( comment );
   }
   catch( File_GroupException error ) {
      throw FileIException();
   }
}

// Mounts the file 'child' onto this file
void H5File::mount( const string& name, H5File& child, PropList& mount_plist ) const
{
   mount( name.c_str(), child, mount_plist );
}

void H5File::mount( const char* name, H5File& child, PropList& mount_plist ) const
{
   try {
      mountT( id, name, child.getId(), mount_plist );
   }
   catch( File_GroupException error ) {
      throw FileIException();
   }
}

// Unmounts the file named 'name' from this parent file
void H5File::unmount( const string& name ) const
{
   unmount( name.c_str() );
}

void H5File::unmount( const char* name ) const
{
   try {
      unmountT( id, name );
   }
   catch( File_GroupException error ) {
      throw FileIException();
   }
}

// Calls the C API H5Fclose to close this file.  Used by IdComponent::reset
void H5File::p_close() const
{
   herr_t ret_value = H5Fclose( id );
   if( ret_value < 0 )
   {
      throw FileIException();
   }
}

// The destructor of this instance calls IdComponent::reset to
// reset its identifier - no longer true
// Older compilers (baldric) don't support template member functions
// and IdComponent::reset is one; so at this time, the resetId is not
// a member function so it can be template to work around that problem.
H5File::~H5File() 
{  
   // The HDF5 file id will be closed properly
   resetIdComponent( this );
}  

#ifndef H5_NO_NAMESPACE
} // end namespace
#endif