Skip to content

Commit 75629be

Browse files
committed
fix: move ReadDataSet out of public header to avoid leaking hdf5.h dependency
ReadDataSet is a private implementation detail used only within ReadHDF5. Moving it to an anonymous namespace in the .cpp keeps hid_t (and therefore <hdf5.h>) out of the installed public header, so consumers of CSPropDiscMaterial.h no longer need HDF5 headers on their include path. The hid_t fix (preventing 64-bit truncation of HDF5 type IDs) is preserved. Signed-off-by: Thorsten Liebig <thorsten.liebig@gmx.de> Generated-by: Claude Sonnet 4.6
1 parent 77b8519 commit 75629be

2 files changed

Lines changed: 83 additions & 84 deletions

File tree

src/CSPropDiscMaterial.cpp

Lines changed: 83 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,89 @@
2626
#include "ParameterCoord.h"
2727
#include "CSPropDiscMaterial.h"
2828

29+
namespace {
30+
void* ReadDataSet(std::string filename, std::string d_name, hid_t type_id, int &rank, unsigned int &size, bool debug=false)
31+
{
32+
herr_t status;
33+
H5T_class_t class_id;
34+
size_t type_size;
35+
rank = -1;
36+
37+
hid_t file_id = H5Fopen( filename.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT );
38+
if (file_id < 0)
39+
{
40+
if (debug)
41+
std::cerr << __func__ << ": Failed to open file, skipping..." << std::endl;
42+
H5Fclose(file_id);
43+
return NULL;
44+
}
45+
46+
if (H5Lexists(file_id, d_name.c_str(), H5P_DEFAULT)<=0)
47+
{
48+
if (debug)
49+
std::cerr << __func__ << ": Warning, dataset: \"" << d_name << "\" not found... skipping" << std::endl;
50+
H5Fclose(file_id);
51+
return NULL;
52+
}
53+
54+
status = H5LTget_dataset_ndims(file_id, d_name.c_str(), &rank);
55+
if (status < 0)
56+
{
57+
if (debug)
58+
std::cerr << __func__ << ": Warning, failed to read dimension for dataset: \"" << d_name << "\" skipping..." << std::endl;
59+
H5Fclose(file_id);
60+
return NULL;
61+
}
62+
63+
hsize_t* dims = new hsize_t[rank];
64+
status = H5LTget_dataset_info( file_id, d_name.c_str(), dims, &class_id, &type_size);
65+
if (status < 0)
66+
{
67+
if (debug)
68+
std::cerr << __func__ << ": Warning, failed to read dataset info: \"" << d_name << "\" skipping..." << std::endl;
69+
H5Fclose(file_id);
70+
return NULL;
71+
}
72+
73+
size = 1;
74+
for (int n=0;n<rank;++n)
75+
size*=dims[n];
76+
delete[] dims; dims = NULL;
77+
78+
void* data;
79+
if (type_id==H5T_NATIVE_FLOAT)
80+
data = (void*) new float[size];
81+
else if (type_id==H5T_NATIVE_INT)
82+
data = (void*) new int[size];
83+
else if (type_id==H5T_NATIVE_UINT8)
84+
data = (void*) new uint8[size];
85+
else
86+
{
87+
std::cerr << __func__ << ": Error, unknown data type" << std::endl;
88+
H5Fclose(file_id);
89+
return NULL;
90+
}
91+
92+
status = H5LTread_dataset( file_id, d_name.c_str(), type_id, data );
93+
if (status < 0)
94+
{
95+
if (debug)
96+
std::cerr << __func__ << ": Warning, failed to read dataset: \"" << d_name << "\" skipping..." << std::endl;
97+
if (type_id==H5T_NATIVE_FLOAT)
98+
delete[] (float*)data;
99+
else if (type_id==H5T_NATIVE_INT)
100+
delete[] (int*)data;
101+
else if (type_id==H5T_NATIVE_UINT8)
102+
delete[] (uint8*)data;
103+
H5Fclose(file_id);
104+
return NULL;
105+
}
106+
107+
H5Fclose(file_id);
108+
return data;
109+
}
110+
} // namespace
111+
29112
CSPropDiscMaterial::CSPropDiscMaterial(ParameterSet* paraSet) : CSPropMaterial(paraSet)
30113
{
31114
Type=(CSProperties::PropertyType)(DISCRETE_MATERIAL | MATERIAL);
@@ -280,88 +363,6 @@ bool CSPropDiscMaterial::ReadFile()
280363
return false;
281364
}
282365

283-
void *CSPropDiscMaterial::ReadDataSet(std::string filename, std::string d_name, hid_t type_id, int &rank, unsigned int &size, bool debug)
284-
{
285-
herr_t status;
286-
H5T_class_t class_id;
287-
size_t type_size;
288-
rank = -1;
289-
290-
// open hdf5 file
291-
hid_t file_id = H5Fopen( filename.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT );
292-
if (file_id < 0)
293-
{
294-
if (debug)
295-
std::cerr << __func__ << ": Failed to open file, skipping..." << std::endl;
296-
H5Fclose(file_id);
297-
return NULL;
298-
}
299-
300-
if (H5Lexists(file_id, d_name.c_str(), H5P_DEFAULT)<=0)
301-
{
302-
if (debug)
303-
std::cerr << __func__ << ": Warning, dataset: \"" << d_name << "\" not found... skipping" << std::endl;
304-
H5Fclose(file_id);
305-
return NULL;
306-
}
307-
308-
status = H5LTget_dataset_ndims(file_id, d_name.c_str(), &rank);
309-
if (status < 0)
310-
{
311-
if (debug)
312-
std::cerr << __func__ << ": Warning, failed to read dimension for dataset: \"" << d_name << "\" skipping..." << std::endl;
313-
H5Fclose(file_id);
314-
return NULL;
315-
}
316-
317-
hsize_t* dims = new hsize_t[rank];
318-
status = H5LTget_dataset_info( file_id, d_name.c_str(), dims, &class_id, &type_size);
319-
if (status < 0)
320-
{
321-
if (debug)
322-
std::cerr << __func__ << ": Warning, failed to read dataset info: \"" << d_name << "\" skipping..." << std::endl;
323-
H5Fclose(file_id);
324-
return NULL;
325-
}
326-
327-
size = 1;
328-
for (int n=0;n<rank;++n)
329-
size*=dims[n];
330-
delete[] dims; dims = NULL;
331-
332-
void* data;
333-
if (type_id==H5T_NATIVE_FLOAT)
334-
data = (void*) new float[size];
335-
else if (type_id==H5T_NATIVE_INT)
336-
data = (void*) new int[size];
337-
else if (type_id==H5T_NATIVE_UINT8)
338-
data = (void*) new uint8[size];
339-
else
340-
{
341-
std::cerr << __func__ << ": Error, unknown data type" << std::endl;
342-
H5Fclose(file_id);
343-
return NULL;
344-
}
345-
346-
status = H5LTread_dataset( file_id, d_name.c_str(), type_id, data );
347-
if (status < 0)
348-
{
349-
if (debug)
350-
std::cerr << __func__ << ": Warning, failed to read dataset: \"" << d_name << "\" skipping..." << std::endl;
351-
if (type_id==H5T_NATIVE_FLOAT)
352-
delete[] (float*)data;
353-
else if (type_id==H5T_NATIVE_INT)
354-
delete[] (int*)data;
355-
else if (type_id==H5T_NATIVE_UINT8)
356-
delete[] (uint8*)data;
357-
H5Fclose(file_id);
358-
return NULL;
359-
}
360-
361-
H5Fclose(file_id);
362-
return data;
363-
}
364-
365366
bool CSPropDiscMaterial::ReadHDF5( std::string filename )
366367
{
367368
m_Filename = filename;

src/CSPropDiscMaterial.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
#pragma once
1919

20-
#include <hdf5.h>
2120
#include "CSProperties.h"
2221
#include "CSPropMaterial.h"
2322

@@ -110,6 +109,5 @@ class CSXCAD_EXPORT CSPropDiscMaterial : public CSPropMaterial
110109

111110
void EnsureFileLoaded();
112111
bool ReadHDF5(std::string filename);
113-
void* ReadDataSet(std::string filename, std::string d_name, hid_t type_id, int &rank, unsigned int &size, bool debug=false);
114112
};
115113

0 commit comments

Comments
 (0)