fix: make sure to close opened hdf5 properties

Signed-off-by: Thorsten Liebig <Thorsten.Liebig@gmx.de>
pull/1/head
Thorsten Liebig 2012-02-06 14:09:51 +01:00
parent f507d20eb1
commit 645ef9a38b
2 changed files with 93 additions and 1 deletions

View File

@ -70,17 +70,22 @@ bool HDF5_File_Reader::ReadDataSet(string ds_name, hsize_t &nDim, hsize_t* &dims
if (dataset<0)
{
cerr << "HDF5_File_Reader::ReadDataSet: dataset not found" << endl;
H5Fclose(hdf5_file);
return false;
}
hid_t type = H5Dget_type(dataset);
if (type<0)
{
cerr << "HDF5_File_Reader::ReadDataSet: dataset type error" << endl;
H5Dclose(dataset);
H5Fclose(hdf5_file);
return false;
}
if (H5Tget_class(type)!=H5T_FLOAT)
{
cerr << "HDF5_File_Reader::ReadDataSet: dataset type not a float" << endl;
H5Dclose(dataset);
H5Fclose(hdf5_file);
return false;
}
@ -95,8 +100,12 @@ bool HDF5_File_Reader::ReadDataSet(string ds_name, hsize_t &nDim, hsize_t* &dims
if (H5Dread(dataset,type,H5S_ALL,H5S_ALL,H5P_DEFAULT,data)<0)
{
cerr << "HDF5_File_Reader::ReadDataSet: error reading data" << endl;
H5Dclose(dataset);
H5Fclose(hdf5_file);
return false;
}
H5Dclose(dataset);
H5Fclose(hdf5_file);
return true;
}
@ -130,6 +139,7 @@ bool HDF5_File_Reader::ReadMesh(float** lines, unsigned int* numLines, int &mesh
else
{
cerr << "HDF5_File_Reader::ReadMesh: no falid mesh information found" << endl;
H5Fclose(hdf5_file);
return false;
}
for (int n=0;n<3;++n)
@ -142,12 +152,14 @@ bool HDF5_File_Reader::ReadMesh(float** lines, unsigned int* numLines, int &mesh
{
cerr << "HDF5_File_Reader::ReadMesh: mesh dimension error" << endl;
delete[] dims;
H5Fclose(hdf5_file);
return false;
}
numLines[n]=dims[0];
delete[] dims;
lines[n]=data;
}
H5Fclose(hdf5_file);
return true;
}
@ -164,20 +176,28 @@ unsigned int HDF5_File_Reader::GetNumTimeSteps()
}
if (H5Lexists(hdf5_file, "/FieldData/TD", H5P_DEFAULT)<0)
{
H5Fclose(hdf5_file);
return 0;
}
hid_t TD_grp = H5Gopen(hdf5_file, "/FieldData/TD" );
if (TD_grp<0)
{
cerr << "HDF5_File_Reader::GetNumTimeSteps: can't open group ""/FieldData/TD""" << endl;
H5Fclose(hdf5_file);
return 0;
}
hsize_t numObj;
if (H5Gget_num_objs(TD_grp,&numObj)<0)
{
cerr << "HDF5_File_Reader::GetNumTimeSteps: can't read number of datasets" << endl;
H5Gclose(TD_grp);
H5Fclose(hdf5_file);
return 0;
}
H5Gclose(TD_grp);
H5Fclose(hdf5_file);
return numObj;
}
@ -196,18 +216,22 @@ bool HDF5_File_Reader::ReadTimeSteps(vector<unsigned int> &timestep, vector<stri
if (H5Lexists(hdf5_file, "/FieldData/TD", H5P_DEFAULT)<0)
{
cerr << "HDF5_File_Reader::ReadTimeSteps: can't open ""/FieldData/TD""" << endl;
H5Fclose(hdf5_file);
return false;
}
hid_t TD_grp = H5Gopen(hdf5_file, "/FieldData/TD" );
if (TD_grp<0)
{
cerr << "HDF5_File_Reader::ReadTimeSteps: can't open ""/FieldData/TD""" << endl;
H5Fclose(hdf5_file);
return false;
}
hsize_t numObj;
if (H5Gget_num_objs(TD_grp,&numObj)<0)
{
cerr << "HDF5_File_Reader::ReadTimeSteps: can't read number of datasets" << endl;
H5Gclose(TD_grp);
H5Fclose(hdf5_file);
return false;
}
char name[100];
@ -220,6 +244,8 @@ bool HDF5_File_Reader::ReadTimeSteps(vector<unsigned int> &timestep, vector<stri
if (H5Gget_objtype_by_idx(TD_grp, n) != H5G_DATASET)
{
cerr << "HDF5_File_Reader::ReadTimeSteps: invalid timestep found!" << endl;
H5Gclose(TD_grp);
H5Fclose(hdf5_file);
return false;
}
ssize_t name_size = H5Gget_objname_by_idx(TD_grp, n, name, 100 );
@ -233,9 +259,13 @@ bool HDF5_File_Reader::ReadTimeSteps(vector<unsigned int> &timestep, vector<stri
else
{
cerr << "HDF5_File_Reader::ReadTimeSteps: invalid timestep format found!" << endl;
H5Gclose(TD_grp);
H5Fclose(hdf5_file);
return false;
}
}
H5Gclose(TD_grp);
H5Fclose(hdf5_file);
return true;
}
@ -255,12 +285,14 @@ float**** HDF5_File_Reader::GetTDVectorData(size_t idx, float &time, unsigned in
if (H5Lexists(hdf5_file, "/FieldData/TD", H5P_DEFAULT)<0)
{
cerr << "HDF5_File_Reader::GetTDVectorData: can't open ""/FieldData/TD""" << endl;
H5Fclose(hdf5_file);
return NULL;
}
hid_t TD_grp = H5Gopen(hdf5_file, "/FieldData/TD" );
if (TD_grp<0)
{
cerr << "HDF5_File_Reader::GetTDVectorData: can't open ""/FieldData/TD""" << endl;
H5Fclose(hdf5_file);
return NULL;
}
@ -268,14 +300,22 @@ float**** HDF5_File_Reader::GetTDVectorData(size_t idx, float &time, unsigned in
if (H5Gget_num_objs(TD_grp,&numObj)<0)
{
cerr << "HDF5_File_Reader::GetTDVectorData: can't read number of datasets" << endl;
H5Gclose(TD_grp);
H5Fclose(hdf5_file);
return NULL;
}
if (idx>=numObj)
{
H5Gclose(TD_grp);
H5Fclose(hdf5_file);
return NULL;
}
if (H5Gget_objtype_by_idx(TD_grp, idx) != H5G_DATASET)
{
cerr << "HDF5_File_Reader::GetTDVectorData: invalid timestep found!" << endl;
H5Gclose(TD_grp);
H5Fclose(hdf5_file);
return NULL;
}
@ -287,11 +327,16 @@ float**** HDF5_File_Reader::GetTDVectorData(size_t idx, float &time, unsigned in
if (attr<0)
{
cerr << "HDF5_File_Reader::GetTDVectorData: time attribute not found!" << endl;
H5Gclose(TD_grp);
H5Fclose(hdf5_file);
return NULL;
}
if (H5Aread(attr, H5T_NATIVE_FLOAT, &time)<0)
{
cerr << "HDF5_File_Reader::GetTDVectorData: can't read time attribute!" << endl;
H5Aclose(attr);
H5Gclose(TD_grp);
H5Fclose(hdf5_file);
return NULL;
}
@ -303,12 +348,18 @@ float**** HDF5_File_Reader::GetTDVectorData(size_t idx, float &time, unsigned in
{
cerr << "HDF5_File_Reader::GetTDVectorData: data dimension invalid" << endl;
delete[] dims;
H5Aclose(attr);
H5Gclose(TD_grp);
H5Fclose(hdf5_file);
return NULL;
}
if (dims[0]!=3)
{
cerr << "HDF5_File_Reader::GetTDVectorData: vector data dimension invalid" << endl;
delete[] dims;
H5Aclose(attr);
H5Gclose(TD_grp);
H5Fclose(hdf5_file);
return NULL;
}
data_size[0]=dims[3];
@ -326,6 +377,9 @@ float**** HDF5_File_Reader::GetTDVectorData(size_t idx, float &time, unsigned in
field[d][i][j][k]=data[pos++];
}
delete[] data;
H5Aclose(attr);
H5Gclose(TD_grp);
H5Fclose(hdf5_file);
return field;
}

View File

@ -61,11 +61,13 @@ hid_t HDF5_File_Writer::OpenGroup(hid_t hdf5_file, string group)
for (size_t n=0;n<results.size();++n)
{
hid_t old_grp = grp;
if (!results.at(n).empty())
{
if (H5Lexists(grp, results.at(n).c_str(), H5P_DEFAULT))
{
grp = H5Gopen(grp, results.at(n).c_str());
H5Gclose(old_grp);
if (grp<0)
{
cerr << "HDF5_File_Writer::OpenGroup: Error, failed to open existing group" << endl;
@ -75,6 +77,7 @@ hid_t HDF5_File_Writer::OpenGroup(hid_t hdf5_file, string group)
else
{
grp = H5Gcreate(grp,results.at(n).c_str(),0);
H5Gclose(old_grp);
if (grp<0)
{
cerr << "HDF5_File_Writer::OpenGroup: Error, creating group """ << group << """ failed" << endl;
@ -114,6 +117,7 @@ bool HDF5_File_Writer::WriteRectMesh(unsigned int const* numLines, float const*
if (H5Lexists(hdf5_file, "/Mesh", H5P_DEFAULT))
{
cerr << "HDF5_File_Writer::WriteRectMesh: Error, group ""/Mesh"" already exists" << endl;
H5Fclose(hdf5_file);
return false;
}
@ -121,6 +125,7 @@ bool HDF5_File_Writer::WriteRectMesh(unsigned int const* numLines, float const*
if (mesh_grp<0)
{
cerr << "HDF5_File_Writer::WriteRectMesh: Error, creating group ""/Mesh"" failed" << endl;
H5Fclose(hdf5_file);
return false;
}
@ -156,10 +161,17 @@ bool HDF5_File_Writer::WriteRectMesh(unsigned int const* numLines, float const*
{
cerr << "HDF5_File_Writer::WriteRectMesh: Error, writing to dataset failed" << endl;
delete[] array;
H5Dclose(dataset);
H5Sclose(space);
H5Gclose(mesh_grp);
H5Fclose(hdf5_file);
return false;
}
delete[] array;
H5Dclose(dataset);
H5Sclose(space);
}
H5Gclose(mesh_grp);
H5Fclose(hdf5_file);
return true;
}
@ -266,6 +278,12 @@ bool HDF5_File_Writer::WriteData(std::string dataSetName, float const* field_buf
}
hid_t group = OpenGroup(hdf5_file,m_Group);
if (group<0)
{
cerr << "HDF5_File_Writer::WriteData: Error opening group" << endl;
H5Fclose(hdf5_file);
return false;
}
hsize_t dims[dim];
for (size_t n=0;n<dim;++n)
@ -275,8 +293,15 @@ bool HDF5_File_Writer::WriteData(std::string dataSetName, float const* field_buf
if (H5Dwrite(dataset, H5T_NATIVE_FLOAT, space, H5P_DEFAULT, H5P_DEFAULT, field_buf))
{
cerr << "HDF5_File_Writer::WriteData: Error, writing to dataset failed" << endl;
H5Dclose(dataset);
H5Sclose(space);
H5Gclose(group);
H5Fclose(hdf5_file);
return false;
}
H5Dclose(dataset);
H5Sclose(space);
H5Gclose(group);
H5Fclose(hdf5_file);
return true;
}
@ -298,12 +323,14 @@ bool HDF5_File_Writer::WriteAtrribute(std::string locName, std::string attr_name
if (H5Lexists(hdf5_file, locName.c_str(), H5P_DEFAULT)<0)
{
cerr << "HDF5_File_Writer::WriteAtrribute: Error, failed to find location: """ << locName << """" << endl;
H5Fclose(hdf5_file);
return false;
}
hid_t loc = H5Oopen(hdf5_file, locName.c_str(), H5P_DEFAULT);
if (loc<0)
{
cerr << "HDF5_File_Writer::WriteAtrribute: Error, failed to open location: """ << locName << """" << endl;
H5Fclose(hdf5_file);
return false;
}
@ -314,6 +341,9 @@ bool HDF5_File_Writer::WriteAtrribute(std::string locName, std::string attr_name
if (attribute_id<0)
{
cerr << "HDF5_File_Writer::WriteAtrribute: Error, failed to create the attrbute" << endl;
H5Sclose(dataspace_id);
H5Oclose(loc);
H5Fclose(hdf5_file);
return false;
}
@ -321,8 +351,16 @@ bool HDF5_File_Writer::WriteAtrribute(std::string locName, std::string attr_name
if (H5Awrite(attribute_id, mem_type, value)<0)
{
cerr << "HDF5_File_Writer::WriteAtrribute: Error, failed to write the attrbute" << endl;
H5Aclose(attribute_id);
H5Sclose(dataspace_id);
H5Oclose(loc);
H5Fclose(hdf5_file);
return false;
}
H5Aclose(attribute_id);
H5Sclose(dataspace_id);
H5Oclose(loc);
H5Fclose(hdf5_file);
return true;
}