@@ -30,50 +30,82 @@ Hdf5Recorder::Hdf5Recorder()
3030 // Initialize the logger for file operations
3131 fileLogger_ = log4cplus::Logger::getInstance (LOG4CPLUS_TEXT (" file" ));
3232
33+ // Initialize the HDF5 file object to nullptr
34+ // This is the HDF5 file (H5File) object.
3335 resultOut_ = nullptr ;
34- // I just comment this for now because something wrong with my init function
35- // init();
36+ }
37+
38+ // destructor
39+ Hdf5Recorder::~Hdf5Recorder ()
40+ {
41+ term ();
3642}
3743
3844// Other member functions implementation...
3945void Hdf5Recorder::init ()
4046{
4147 // Check the output file extension is .h5
42- /* string suffix = ".h5";
43- if ((resultFileName_.size() <= suffix.size()) ||
44- (resultFileName_.compare(resultFileName_.size() - suffix.size(), suffix.size(), suffix) != 0)) {
45- std::cerr << "The file extension is not .h5" << std::endl;
46- exit(EXIT_FAILURE);
47- }
48+ string suffix = " .h5" ;
49+ if ((resultFileName_.size () <= suffix.size ())
50+ || (resultFileName_.compare (resultFileName_.size () - suffix.size (), suffix.size (), suffix)
51+ != 0 )) {
52+ // perror("the file extention is not .h5 ");
53+ string errorMsg
54+ = " Error: the file extension is not .h5. Provided file name: " + resultFileName_;
55+ perror (errorMsg.c_str ());
56+ exit (EXIT_FAILURE);
57+ }
4858
49- // Check if we can create and write to the file
50- std::ofstream testFileWrite(resultFileName_);
51- if (!testFileWrite.is_open()) {
52- std::cerr << "Error opening output file for writing" << std::endl;
53- exit(EXIT_FAILURE);
54- }
55- testFileWrite.close();
59+ // Check if we can create and write to the file
60+ ofstream testFileWrite (resultFileName_.c_str ());
61+ if (!testFileWrite.is_open ()) {
62+ perror (" Error opening output file for writing " );
63+ exit (EXIT_FAILURE);
64+ }
65+ testFileWrite.close ();
66+
67+ try {
68+ // Create a new file using the default property lists
69+ resultOut_ = new H5File (resultFileName_, H5F_ACC_TRUNC);
70+ initDataSet ();
71+ } catch (FileIException &error) {
72+ cerr << " HDF5 File I/O Exception: " << endl;
73+ error.printErrorStack ();
74+ return ;
75+ } catch (DataSetIException &error) {
76+ cerr << " HDF5 Dataset Exception: " << endl;
77+ error.printErrorStack ();
78+ return ;
79+ } catch (DataSpaceIException &error) {
80+ cerr << " HDF5 Dataspace Exception: " << endl;
81+ error.printErrorStack ();
82+ return ;
83+ } catch (DataTypeIException &error) {
84+ cerr << " HDF5 Datatype Exception: " << endl;
85+ error.printErrorStack ();
86+ return ;
87+ }
88+ }
5689
57- try {
58- // Create a new file using the default property lists
59- resultOut_ = H5File(resultFileName_, H5F_ACC_TRUNC);
60- initDataSet();
61- } catch (const FileIException& error) {
62- std::cerr << "HDF5 File I/O Exception: ";
63- error.printErrorStack();
64- return;
65- } catch (const DataSetIException& error) {
66- std::cerr << "HDF5 Dataset Exception: ";
67- error.printErrorStack();
68- return;
69- } catch (const DataSpaceIException& error) {
70- std::cerr << "HDF5 Dataspace Exception: ";
71- error.printErrorStack();
72- return;
73- } catch (const DataTypeIException& error) {
74- std::cerr << "HDF5 Datatype Exception: ";
75- error.printErrorStack();
76- return;
77- }*/
90+ // This method closes the HDF5 file and releases any associated resources
91+ void Hdf5Recorder::term ()
92+ {
93+ // checks if the file object `resultOut_` is not null, then attempts to close the file and delete the object
94+ if (resultOut_ != nullptr ) {
95+ try {
96+ resultOut_->close ();
97+ delete resultOut_;
98+ resultOut_ = nullptr ;
99+ } catch (FileIException &error) {
100+ // If an exception occurs during this process, it prints the error stack for debugging purposes
101+ cerr << " HDF5 File I/O Exception during termination: " ;
102+ error.printErrorStack ();
103+ }
104+ }
105+ }
106+
107+ // Create data spaces and data sets of the hdf5 for recording histories.
108+ void Hdf5Recorder::initDataSet ()
109+ {
78110}
79111#endif // HDF5
0 commit comments