1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * Copyright by The HDF Group.                                               *
3  * Copyright by the Board of Trustees of the University of Illinois.         *
4  * All rights reserved.                                                      *
5  *                                                                           *
6  * This file is part of HDF5.  The full HDF5 copyright notice, including     *
7  * terms governing use, modification, and redistribution, is contained in    *
8  * the COPYING file, which can be found at the root of the source code       *
9  * distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases.  *
10  * If you do not have access to either file, you may request a copy from     *
11  * help@hdfgroup.org.                                                        *
12  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
13 
14 #ifdef OLD_HEADER_FILENAME
15 #include <iostream.h>
16 #else
17 #include <iostream>
18 #endif
19 #include <string>
20 
21 #include "H5Include.h"
22 #include "H5Exception.h"
23 #include "H5IdComponent.h"
24 #include "H5PropList.h"
25 #include "H5FaccProp.h"
26 #include "H5FcreatProp.h"
27 #include "H5OcreatProp.h"
28 #include "H5DcreatProp.h"
29 #include "H5LaccProp.h"
30 #include "H5StrcreatProp.h"
31 #include "H5LcreatProp.h"
32 #include "H5Location.h"
33 #include "H5Object.h"
34 #include "H5CommonFG.h"
35 #include "H5Group.h"
36 #include "H5File.h"
37 
38 namespace H5 {
39 #ifndef H5_NO_STD
40     using std::cerr;
41     using std::endl;
42 #endif  // H5_NO_STD
43 
44 //--------------------------------------------------------------------------
45 // Function     H5File default constructor
46 ///\brief       Default constructor: creates a stub H5File object.
47 // Programmer   Binh-Minh Ribler - 2000
48 //--------------------------------------------------------------------------
H5File()49 H5File::H5File() : Group(), id(H5I_INVALID_HID) {}
50 
51 //--------------------------------------------------------------------------
52 // Function:    H5File overloaded constructor
53 ///\brief       Creates or opens an HDF5 file depending on the parameter flags.
54 ///\param       name         - IN: Name of the file
55 ///\param       flags        - IN: File access flags
56 ///\param       create_plist - IN: File creation property list, used when
57 ///             modifying default file meta-data.  Default to
58 ///             FileCreatPropList::DEFAULT
59 ///\param       access_plist - IN: File access property list.  Default to
60 ///             FileAccPropList::DEFAULT
61 ///\par Description
62 ///             Valid values of \a flags include:
63 ///             \li \c H5F_ACC_TRUNC - Truncate file, if it already exists,
64 ///                                    erasing all data previously stored in
65 ///                                    the file.
66 ///             \li \c H5F_ACC_EXCL - Fail if file already exists.
67 ///                     \c H5F_ACC_TRUNC and \c H5F_ACC_EXCL are mutually exclusive
68 ///             \li \c H5F_ACC_RDONLY - Open file as read-only, if it already
69 ///                                     exists, and fail, otherwise
70 ///             \li \c H5F_ACC_RDWR - Open file for read/write, if it already
71 ///                                     exists, and fail, otherwise
72 ///\par
73 ///             For info on file creation in the case of an already-open file,
74 ///             please refer to the \b Special \b case section in the C layer
75 ///             Reference Manual at:
76 /// https://support.hdfgroup.org/HDF5/doc/RM/RM_H5F.html#File-Create
77 // Notes        With a PGI compiler (~2012-2013), the exception thrown by p_get_file
78 //              could not be caught in the applications.  Added try block here
79 //              to catch then re-throw it. -BMR 2013/03/21
80 // Programmer   Binh-Minh Ribler - 2000
81 //--------------------------------------------------------------------------
H5File(const char * name,unsigned int flags,const FileCreatPropList & create_plist,const FileAccPropList & access_plist)82 H5File::H5File(const char* name, unsigned int flags, const FileCreatPropList& create_plist, const FileAccPropList& access_plist) : Group(), id(H5I_INVALID_HID)
83 {
84     try {
85         p_get_file(name, flags, create_plist, access_plist);
86     } catch (FileIException& open_file) {
87         throw open_file;
88     }
89 }
90 
91 //--------------------------------------------------------------------------
92 // Function:    H5File overloaded constructor
93 ///\brief       This is another overloaded constructor.  It differs from the
94 ///             above constructor only in the type of the \a name argument.
95 ///\param       name - IN: Name of the file - \c H5std_string
96 ///\param       flags - IN: File access flags
97 ///\param       create_plist - IN: File creation property list, used when
98 ///             modifying default file meta-data.  Default to
99 ///             FileCreatPropList::DEFAULT
100 ///\param       access_plist - IN: File access property list.  Default to
101 ///             FileAccPropList::DEFAULT
102 // Notes        With a PGI compiler (~2012-2013), the exception thrown by p_get_file
103 //              could not be caught in the applications.  Added try block here
104 //              to catch then re-throw it. -BMR 2013/03/21
105 // Programmer   Binh-Minh Ribler - 2000
106 //--------------------------------------------------------------------------
H5File(const H5std_string & name,unsigned int flags,const FileCreatPropList & create_plist,const FileAccPropList & access_plist)107 H5File::H5File(const H5std_string& name, unsigned int flags, const FileCreatPropList& create_plist, const FileAccPropList& access_plist) : Group(), id(H5I_INVALID_HID)
108 {
109     try {
110         p_get_file(name.c_str(), flags, create_plist, access_plist);
111     } catch (FileIException& open_file) {
112         throw open_file;
113     }
114 }
115 
116 #ifndef DOXYGEN_SHOULD_SKIP_THIS
117 //--------------------------------------------------------------------------
118 // This function is private and contains common code between the
119 // constructors taking a string or a char*
120 // Programmer   Binh-Minh Ribler - 2000
121 // Modification
122 //              - removed H5F_ACC_CREAT because H5Fcreate will fail with
123 //              H5F_ACC_CREAT. - BMR, Sep 17, 2014
124 //--------------------------------------------------------------------------
p_get_file(const char * name,unsigned int flags,const FileCreatPropList & create_plist,const FileAccPropList & access_plist)125 void H5File::p_get_file(const char* name, unsigned int flags, const FileCreatPropList& create_plist, const FileAccPropList& access_plist)
126 {
127     // These bits only set for creation, so if any of them are set,
128     // create the file.
129     if(flags & (H5F_ACC_EXCL|H5F_ACC_TRUNC))
130     {
131         hid_t create_plist_id = create_plist.getId();
132         hid_t access_plist_id = access_plist.getId();
133         id = H5Fcreate(name, flags, create_plist_id, access_plist_id);
134         if(id < 0)  // throw an exception when open/create fail
135         {
136             throw FileIException("H5File constructor", "H5Fcreate failed");
137         }
138     }
139     // Open the file if none of the bits above are set.
140     else
141     {
142         hid_t access_plist_id = access_plist.getId();
143         id = H5Fopen(name, flags, access_plist_id);
144         if(id < 0)  // throw an exception when open/create fail
145         {
146             throw FileIException("H5File constructor", "H5Fopen failed");
147         }
148     }
149 }
150 
151 //--------------------------------------------------------------------------
152 // Function:    H5File overloaded constructor
153 ///\brief       Creates an H5File object using an existing file id.
154 ///\param       existing_id - IN: Id of an existing file
155 // Programmer   Binh-Minh Ribler - 2015
156 // Description
157 //      Mar 29, 2015
158 //              Added in responding to a request from user Jason Newton.
159 //              However, it is not recommended to use the private member "id"
160 //              in applications.  Unlike other situations, where similar
161 //              constructor is needed by the library in order to return
162 //              an object, H5File doesn't need it. -BMR (HDFFV-8766 partially)
163 //--------------------------------------------------------------------------
H5File(hid_t existing_id)164 H5File::H5File(hid_t existing_id) : Group()
165 {
166     id = existing_id;
167     incRefCount(); // increment number of references to this id
168 }
169 
170 #endif // DOXYGEN_SHOULD_SKIP_THIS
171 
172 //--------------------------------------------------------------------------
173 // Function:    H5File copy constructor
174 ///\brief       Copy constructor: makes a copy of the original
175 ///             H5File object.
176 ///\param       original - IN: H5File instance to copy
177 // Programmer   Binh-Minh Ribler - 2000
178 //--------------------------------------------------------------------------
H5File(const H5File & original)179 H5File::H5File(const H5File& original) : Group()
180 {
181     id = original.getId();
182     incRefCount(); // increment number of references to this id
183 }
184 
185 //--------------------------------------------------------------------------
186 // Function:    H5File::isHdf5 (static)
187 ///\brief       Determines whether a file in HDF5 format. (Static)
188 ///\param       name - IN: Name of the file
189 ///\return      true if the file is in HDF5 format, and false, otherwise
190 ///\exception   H5::FileIException
191 // Programmer   Binh-Minh Ribler - 2000
192 //--------------------------------------------------------------------------
isHdf5(const char * name)193 bool H5File::isHdf5(const char* name)
194 {
195     // Calls C routine H5Fis_hdf5 to determine whether the file is in
196     // HDF5 format.  It returns positive value, 0, or negative value
197     htri_t ret_value = H5Fis_hdf5(name);
198     if(ret_value > 0)
199         return true;
200     else if(ret_value == 0)
201         return false;
202     else // Raise exception when H5Fis_hdf5 returns a negative value
203     {
204         throw FileIException("H5File::isHdf5", "H5Fis_hdf5 returned negative value");
205     }
206 }
207 
208 //--------------------------------------------------------------------------
209 // Function:    H5File::isHdf5 (static)
210 ///\brief       This is an overloaded member function, provided for convenience.
211 ///             It takes an \c H5std_string for \a name. (Static)
212 ///\param       name - IN: Name of the file - \c H5std_string
213 // Programmer   Binh-Minh Ribler - 2000
214 //--------------------------------------------------------------------------
isHdf5(const H5std_string & name)215 bool H5File::isHdf5(const H5std_string& name)
216 {
217     return(isHdf5(name.c_str()));
218 }
219 
220 //--------------------------------------------------------------------------
221 // Function:    openFile
222 ///\brief       Opens an HDF5 file
223 ///\param       name         - IN: Name of the file
224 ///\param       flags        - IN: File access flags
225 ///\param       access_plist - IN: File access property list.  Default to
226 ///             FileAccPropList::DEFAULT
227 ///\par Description
228 ///             Valid values of \a flags include:
229 ///             H5F_ACC_RDWR:   Open with read/write access. If the file is
230 ///                             currently open for read-only access then it
231 ///                             will be reopened. Absence of this flag
232 ///                             implies read-only access.
233 ///
234 ///             H5F_ACC_RDONLY: Open with read only access.
235 ///
236 // Programmer   Binh-Minh Ribler - Oct, 2005
237 //--------------------------------------------------------------------------
openFile(const char * name,unsigned int flags,const FileAccPropList & access_plist)238 void H5File::openFile(const char* name, unsigned int flags, const FileAccPropList& access_plist)
239 {
240     try {
241         close();
242     }
243     catch (Exception& close_error) {
244         throw FileIException("H5File::openFile", close_error.getDetailMsg());
245     }
246 
247     hid_t access_plist_id = access_plist.getId();
248     id = H5Fopen (name, flags, access_plist_id);
249     if (id < 0)  // throw an exception when open fails
250     {
251         throw FileIException("H5File::openFile", "H5Fopen failed");
252     }
253 }
254 
255 //--------------------------------------------------------------------------
256 // Function:    H5File::openFile
257 ///\brief       This is an overloaded member function, provided for convenience.
258 ///             It takes an \c H5std_string for \a name.
259 ///\param       name         - IN: Name of the file - \c H5std_string
260 ///\param       flags        - IN: File access flags
261 ///\param       access_plist - IN: File access property list.  Default to
262 ///             FileAccPropList::DEFAULT
263 // Programmer   Binh-Minh Ribler - 2000
264 //--------------------------------------------------------------------------
openFile(const H5std_string & name,unsigned int flags,const FileAccPropList & access_plist)265 void H5File::openFile(const H5std_string& name, unsigned int flags, const FileAccPropList& access_plist)
266 {
267     openFile(name.c_str(), flags, access_plist);
268 }
269 
270 //--------------------------------------------------------------------------
271 // Function:    H5File::reOpen
272 ///\brief       Reopens this file.
273 ///
274 ///\exception   H5::FileIException
275 // Description
276 //              If this object has represented another HDF5 file, the previous
277 //              HDF5 file need to be closed first.
278 // Programmer   Binh-Minh Ribler - 2000
279 // Note:        This wrapper doesn't seem right regarding the 'id' and should
280 //              be investigated.  BMR - 2/20/2005
281 // Modification
282 //              - Replaced resetIdComponent() with decRefCount() to use C
283 //              library ID reference counting mechanism - BMR, Feb 20, 2005
284 //              - Replaced decRefCount with close() to let the C library
285 //              handle the reference counting - BMR, Jun 1, 2006
286 //--------------------------------------------------------------------------
reOpen()287 void H5File::reOpen()
288 {
289     try {
290         close();
291     }
292     catch (Exception& close_error) {
293         throw FileIException("H5File::reOpen", close_error.getDetailMsg());
294     }
295 
296     // call C routine to reopen the file - Note: not sure about this,
297     // which id to be the parameter when closing?
298     id = H5Freopen(id);
299     if(id < 0) // Raise exception when H5Freopen returns a neg value
300         throw FileIException("H5File::reOpen", "H5Freopen failed");
301 }
302 
303 //--------------------------------------------------------------------------
304 // Function:    H5File::getCreatePlist
305 ///\brief       Returns the creation property list of this file
306 ///\return      FileCreatPropList object
307 ///\exception   H5::FileIException
308 // Programmer   Binh-Minh Ribler - 2000
309 //--------------------------------------------------------------------------
getCreatePlist() const310 FileCreatPropList H5File::getCreatePlist() const
311 {
312     hid_t create_plist_id = H5Fget_create_plist(id);
313 
314     // if H5Fget_create_plist returns a valid id, create and return
315     // the FileCreatPropList object for this property list
316     if(create_plist_id > 0)
317     {
318       FileCreatPropList create_plist(create_plist_id);
319         return(create_plist);
320     }
321     else
322     {
323         throw FileIException("H5File::getCreatePlist", "H5Fget_create_plist failed");
324     }
325 }
326 
327 //--------------------------------------------------------------------------
328 // Function:    H5File::getAccessPlist
329 ///\brief       Returns the access property list of this file
330 ///\return      FileAccPropList object
331 ///\exception   H5::FileIException
332 // Programmer   Binh-Minh Ribler - 2000
333 //--------------------------------------------------------------------------
getAccessPlist() const334 FileAccPropList H5File::getAccessPlist() const
335 {
336     hid_t access_plist_id = H5Fget_access_plist(id);
337 
338     // if H5Fget_access_plist returns a valid id, create and return
339     // the FileAccPropList object for this property list
340     if(access_plist_id > 0)
341     {
342       FileAccPropList access_plist(access_plist_id);
343         return access_plist;
344     }
345     else // Raise an exception
346     {
347         throw FileIException("H5File::getAccessPlist", "H5Fget_access_plist failed");
348     }
349 }
350 
351 //--------------------------------------------------------------------------
352 // Function:    H5File::getFileInfo
353 ///\brief       Retrieves the general information of this file.
354 ///
355 ///\exception   H5::FileIException
356 ///\par Description
357 ///             The retrieved information may include information about
358 ///             superblock extension, free space management, and shared object
359 // Programmer   Binh-Minh Ribler - May 2017
360 //--------------------------------------------------------------------------
getFileInfo(H5F_info_t & file_info) const361 void H5File::getFileInfo(H5F_info_t& file_info) const
362 {
363     herr_t ret_value = H5Fget_info(id, &file_info);
364     if (ret_value < 0)
365     {
366         throw FileIException("H5File::getFileInfo", "H5Fget_info failed");
367     }
368 }
369 
370 //--------------------------------------------------------------------------
371 // Function:    H5File::getFreeSpace
372 ///\brief       Returns the amount of free space in the file.
373 ///\return      Amount of free space
374 ///\exception   H5::FileIException
375 // Programmer   Binh-Minh Ribler - May 2004
376 //--------------------------------------------------------------------------
getFreeSpace() const377 hssize_t H5File::getFreeSpace() const
378 {
379     hssize_t free_space = H5Fget_freespace(id);
380     if(free_space < 0)
381     {
382         throw FileIException("H5File::getFreeSpace", "H5Fget_freespace failed");
383     }
384     return (free_space);
385 }
386 
387 
388 //--------------------------------------------------------------------------
389 // Function:    H5File::getObjCount
390 ///\brief       Returns the number of opened object IDs (files, datasets,
391 ///             groups and datatypes) in the same file.
392 ///\param       types - Type of object to retrieve the count
393 ///\return      Number of opened object IDs
394 ///\exception   H5::FileIException
395 ///\par Description
396 ///             The valid values for \a types include:
397 ///             \li \c H5F_OBJ_FILE     - Files only
398 ///             \li \c H5F_OBJ_DATASET  - Datasets only
399 ///             \li \c H5F_OBJ_GROUP    - Groups only
400 ///             \li \c H5F_OBJ_DATATYPE - Named datatypes only
401 ///             \li \c H5F_OBJ_ATTR     - Attributes only
402 ///             \li \c H5F_OBJ_ALL    - All of the above, i.e., \c H5F_OBJ_FILE
403 ///                                     | \c H5F_OBJ_DATASET | \c H5F_OBJ_GROUP
404 ///                                     | \c H5F_OBJ_DATATYPE | \c H5F_OBJ_ATTR
405 ///\par
406 /// Multiple object types can be combined with the logical OR operator (|).
407 // Programmer   Binh-Minh Ribler - May 2004
408 //--------------------------------------------------------------------------
getObjCount(unsigned types) const409 ssize_t H5File::getObjCount(unsigned types) const
410 {
411     ssize_t num_objs = H5Fget_obj_count(id, types);
412     if(num_objs < 0)
413     {
414         throw FileIException("H5File::getObjCount", "H5Fget_obj_count failed");
415     }
416     return (num_objs);
417 }
418 
419 //--------------------------------------------------------------------------
420 // Function:    H5File::getObjIDs
421 ///\brief       Retrieves a list of opened object IDs (files, datasets,
422 ///             groups and datatypes) in the same file.
423 ///\param       types    - Type of object to retrieve the count
424 ///\param       max_objs - Maximum number of object identifiers to place
425 ///                        into obj_id_list.
426 ///\param       oid_list - List of open object identifiers
427 ///\exception   H5::FileIException
428 ///\par Description
429 ///             The valid values for \a types include:
430 ///             \li \c H5F_OBJ_FILE     - Files only
431 ///             \li \c H5F_OBJ_DATASET  - Datasets only
432 ///             \li \c H5F_OBJ_GROUP    - Groups only
433 ///             \li \c H5F_OBJ_DATATYPE - Named datatypes only
434 ///             \li \c H5F_OBJ_ATTR     - Attributes only
435 ///             \li \c H5F_OBJ_ALL    - All of the above, i.e., \c H5F_OBJ_FILE
436 ///                                     | \c H5F_OBJ_DATASET | \c H5F_OBJ_GROUP
437 ///                                     | \c H5F_OBJ_DATATYPE | \c H5F_OBJ_ATTR
438 ///\par
439 /// Multiple object types can be combined with the logical OR operator (|).
440 //
441 // Notes: will do the overload for this one after hearing from Quincey???
442 // Programmer   Binh-Minh Ribler - May 2004
443 //--------------------------------------------------------------------------
getObjIDs(unsigned types,size_t max_objs,hid_t * oid_list) const444 void H5File::getObjIDs(unsigned types, size_t max_objs, hid_t *oid_list) const
445 {
446     ssize_t ret_value = H5Fget_obj_ids(id, types, max_objs, oid_list);
447     if(ret_value < 0)
448     {
449         throw FileIException("H5File::getObjIDs", "H5Fget_obj_ids failed");
450     }
451 }
452 
453 //--------------------------------------------------------------------------
454 // Function:    H5File::getVFDHandle
455 ///\brief       Returns the pointer to the file handle of the low-level file
456 ///             driver.
457 ///\param       fapl        - File access property list
458 ///\param       file_handle - Pointer to the file handle being used by
459 ///                           the low-level virtual file driver
460 ///\exception   H5::FileIException
461 ///\par Description
462 ///             For the \c FAMILY or \c MULTI drivers, \a fapl should be
463 ///             defined through the property list functions:
464 ///             \c FileAccPropList::setFamilyOffset for the \c FAMILY driver
465 ///             and \c FileAccPropList::setMultiType for the \c MULTI driver.
466 ///
467 ///             The obtained file handle is dynamic and is valid only while
468 ///             the file remains open; it will be invalid if the file is
469 ///             closed and reopened or opened during a subsequent session.
470 // Programmer   Binh-Minh Ribler - May 2004
471 // Modification
472 //              Replaced the version without const parameter - Apr, 2014
473 //--------------------------------------------------------------------------
getVFDHandle(const FileAccPropList & fapl,void ** file_handle) const474 void H5File::getVFDHandle(const FileAccPropList& fapl, void **file_handle) const
475 {
476     hid_t fapl_id = fapl.getId();
477     herr_t ret_value = H5Fget_vfd_handle(id, fapl_id, file_handle);
478     if(ret_value < 0)
479     {
480         throw FileIException("H5File::getVFDHandle", "H5Fget_vfd_handle failed");
481     }
482 }
483 
484 //--------------------------------------------------------------------------
485 // Function:    H5File::getVFDHandle
486 // Purpose      This is an overloaded member function, kept for backward
487 //              compatibility.  It differs from the above function in that it
488 //              misses const's.  This wrapper will be removed in future release.
489 // Param        fapl        - File access property list
490 // Param        file_handle - Pointer to the file handle being used by
491 //                            the low-level virtual file driver
492 // Exception    H5::FileIException
493 // Programmer   Binh-Minh Ribler - May 2004
494 // Modification
495 //              Planned for removal. -BMR, 2014/04/16
496 //              Removed from documentation. -BMR, 2016/03/07 1.8.17 and 1.10.0
497 //              Removed from code. -BMR, 2016/08/11 1.8.18 and 1.10.1
498 //--------------------------------------------------------------------------
499 //void H5File::getVFDHandle(FileAccPropList& fapl, void **file_handle) const
500 //{
501 //    getVFDHandle((const FileAccPropList)fapl, file_handle);
502 //}
503 
504 //--------------------------------------------------------------------------
505 // Function:    H5File::getVFDHandle
506 ///\brief       This is an overloaded member function, provided for convenience.
507 ///             It differs from the above function only in what arguments it
508 ///             accepts.
509 ///\param       file_handle - Pointer to the file handle being used by
510 ///                           the low-level virtual file driver
511 ///\exception   H5::FileIException
512 // Programmer   Binh-Minh Ribler - May 2004
513 //--------------------------------------------------------------------------
getVFDHandle(void ** file_handle) const514 void H5File::getVFDHandle(void **file_handle) const
515 {
516     herr_t ret_value = H5Fget_vfd_handle(id, H5P_DEFAULT, file_handle);
517     if(ret_value < 0)
518     {
519         throw FileIException("H5File::getVFDHandle", "H5Fget_vfd_handle failed");
520     }
521 }
522 
523 //--------------------------------------------------------------------------
524 // Function:    H5File::getFileSize
525 ///\brief       Returns the file size of the HDF5 file.
526 ///\return      File size
527 ///\exception   H5::FileIException
528 ///\par Description
529 ///             This function is called after an existing file is opened in
530 ///             order to learn the true size of the underlying file.
531 // Programmer   Raymond Lu - June 24, 2004
532 //--------------------------------------------------------------------------
getFileSize() const533 hsize_t H5File::getFileSize() const
534 {
535     hsize_t file_size;
536     herr_t ret_value = H5Fget_filesize(id, &file_size);
537     if (ret_value < 0)
538     {
539         throw FileIException("H5File::getFileSize", "H5Fget_filesize failed");
540     }
541     return (file_size);
542 }
543 
544 //--------------------------------------------------------------------------
545 // Function:    H5File::getId
546 ///\brief       Get the id of this file
547 ///\return      File identifier
548 // Modification:
549 //      May 2008 - BMR
550 //              Class hierarchy is revised to address bugzilla 1068.  Class
551 //              AbstractDS and Attribute are moved out of H5Object.  In
552 //              addition, member IdComponent::id is moved into subclasses, and
553 //              IdComponent::getId now becomes pure virtual function.
554 // Programmer   Binh-Minh Ribler - May, 2008
555 //--------------------------------------------------------------------------
getId() const556 hid_t H5File::getId() const
557 {
558     return(id);
559 }
560 
561 #ifndef DOXYGEN_SHOULD_SKIP_THIS
562 //--------------------------------------------------------------------------
563 // Function:    H5File::reopen
564 // Purpose:     Reopens this file.
565 // Exception    H5::FileIException
566 // Description
567 //              This function is replaced by the above function reOpen.
568 // Programmer   Binh-Minh Ribler - 2000
569 //--------------------------------------------------------------------------
reopen()570 void H5File::reopen()
571 {
572     H5File::reOpen();
573 }
574 
575 //--------------------------------------------------------------------------
576 // Function:    H5File::getLocId
577 // Purpose:     Get the id of this file
578 // Description
579 //              This function is a redefinition of CommonFG::getLocId.  It
580 //              is used by CommonFG member functions to get the file id.
581 // Programmer   Binh-Minh Ribler - 2000
582 //--------------------------------------------------------------------------
getLocId() const583 hid_t H5File::getLocId() const
584 {
585     return(getId());
586 }
587 
588 //--------------------------------------------------------------------------
589 // Function:    H5File::p_setId (protected)
590 ///\brief       Sets the identifier of this object to a new value.
591 ///
592 ///\exception   H5::IdComponentException when the attempt to close the HDF5
593 ///             object fails
594 // Description:
595 //              The underlaying reference counting in the C library ensures
596 //              that the current valid id of this object is properly closed.
597 //              Then the object's id is reset to the new id.
598 // Programmer   Binh-Minh Ribler - 2000
599 //--------------------------------------------------------------------------
p_setId(const hid_t new_id)600 void H5File::p_setId(const hid_t new_id)
601 {
602     // handling references to this old id
603     try {
604         close();
605     }
606     catch (Exception& E) {
607         throw FileIException("H5File::p_setId", E.getDetailMsg());
608     }
609     // reset object's id to the given id
610     id = new_id;
611 }
612 #endif // DOXYGEN_SHOULD_SKIP_THIS
613 
614 //--------------------------------------------------------------------------
615 // Function:    H5File::close
616 ///\brief       Closes this HDF5 file.
617 ///
618 ///\exception   H5::FileIException
619 // Programmer   Binh-Minh Ribler - Mar 9, 2005
620 //--------------------------------------------------------------------------
close()621 void H5File::close()
622 {
623     if (p_valid_id(id))
624     {
625         herr_t ret_value = H5Fclose(id);
626         if(ret_value < 0)
627         {
628             throw FileIException("H5File::close", "H5Fclose failed");
629         }
630         // reset the id
631         id = H5I_INVALID_HID;
632     }
633 }
634 
635 //--------------------------------------------------------------------------
636 // Function:    H5File::throwException
637 ///\brief       Throws H5::FileIException.
638 ///\param       func_name - Name of the function where failure occurs
639 ///\param       msg       - Message describing the failure
640 ///\exception   H5::FileIException
641 // December 2000
642 //--------------------------------------------------------------------------
throwException(const H5std_string & func_name,const H5std_string & msg) const643 void H5File::throwException(const H5std_string& func_name, const H5std_string& msg) const
644 {
645     throw FileIException(inMemFunc(func_name.c_str()), msg);
646 }
647 
648 //--------------------------------------------------------------------------
649 // Function:    H5File destructor
650 ///\brief       Properly terminates access to this file.
651 // Programmer   Binh-Minh Ribler - 2000
652 // Modification
653 //              - Replaced resetIdComponent() with decRefCount() to use C
654 //              library ID reference counting mechanism - BMR, Feb 20, 2005
655 //              - Replaced decRefCount with close() to let the C library
656 //              handle the reference counting - BMR, Jun 1, 2006
657 //--------------------------------------------------------------------------
~H5File()658 H5File::~H5File()
659 {
660     try {
661         close();
662     } catch (Exception& close_error) {
663         cerr << "H5File::~H5File - " << close_error.getDetailMsg() << endl;
664     }
665 }
666 
667 } // end namespace
668