32 #include "config_hdf5.h"
39 #include "InternalErr.h"
48 HDF5CFStr::HDF5CFStr(
const string &n,
const string &d,
const string &h5_varname)
49 : Str(n, d), varname(h5_varname)
53 HDF5CFStr::~HDF5CFStr()
56 BaseType *HDF5CFStr::ptr_duplicate()
61 bool HDF5CFStr::read()
64 BESDEBUG(
"h5",
"Coming to HDF5CFStr read "<<endl);
71 if ((fileid = H5Fopen(dataset().c_str(),H5F_ACC_RDONLY,H5P_DEFAULT))<0) {
73 eherr <<
"HDF5 File " << dataset()
74 <<
" cannot be opened. "<<endl;
75 throw InternalErr (__FILE__, __LINE__, eherr.str ());
78 if ((dsetid = H5Dopen(fileid,varname.c_str(),H5P_DEFAULT))<0) {
81 eherr <<
"HDF5 dataset " << name()
82 <<
" cannot be opened. "<<endl;
83 throw InternalErr (__FILE__, __LINE__, eherr.str ());
86 if ((dspace = H5Dget_space(dsetid))<0) {
91 eherr <<
"Space id of the HDF5 dataset " << name()
92 <<
" cannot be obtained. "<<endl;
93 throw InternalErr (__FILE__, __LINE__, eherr.str ());
96 if (H5S_SCALAR != H5Sget_simple_extent_type(dspace)) {
101 eherr <<
" The HDF5 dataset " << name()
102 <<
" is not scalar. "<<endl;
103 throw InternalErr (__FILE__, __LINE__, eherr.str ());
108 if ((dtypeid = H5Dget_type(dsetid)) < 0) {
114 eherr <<
"Obtaining the datatype of the HDF5 dataset " << name()
116 throw InternalErr (__FILE__, __LINE__, eherr.str ());
120 if ((memtype = H5Tget_native_type(dtypeid, H5T_DIR_ASCEND))<0) {
127 eherr <<
"Obtaining the memory type of the HDF5 dataset " << name()
129 throw InternalErr (__FILE__, __LINE__, eherr.str ());
133 htri_t is_vlen_str = H5Tis_variable_str(dtypeid);
134 if (is_vlen_str > 0) {
135 size_t ty_size = H5Tget_size(memtype);
143 eherr <<
"Cannot obtain the size of the fixed size HDF5 string of the dataset "
145 throw InternalErr (__FILE__, __LINE__, eherr.str ());
147 vector <char> strval;
148 strval.resize(ty_size);
150 read_ret = H5Dread(dsetid,memtype,H5S_ALL,H5S_ALL,H5P_DEFAULT,(
void*)&strval[0]);
159 eherr <<
"Cannot read the HDF5 dataset " << name()
160 <<
" with the type of the HDF5 variable length string "<<endl;
161 throw InternalErr (__FILE__, __LINE__, eherr.str ());
164 char*temp_bp = &strval[0];
165 char*onestring = NULL;
166 string final_str =
"";
168 onestring = *(
char**)temp_bp;
170 final_str =string(onestring);
176 herr_t ret_vlen_claim = 0;
177 ret_vlen_claim = H5Dvlen_reclaim(memtype,dspace,H5P_DEFAULT,(
void*)&strval[0]);
178 if (ret_vlen_claim < 0){
185 eherr <<
"Cannot reclaim the memory buffer of the HDF5 variable length string of the dataset "
187 throw InternalErr (__FILE__, __LINE__, eherr.str ());
195 if (
true == HDF5RequestHandler::get_drop_long_string()) {
196 if( final_str.size() > NC_JAVA_STR_SIZE_LIMIT)
199 set_value(final_str);
202 else if (0 == is_vlen_str) {
203 size_t ty_size = H5Tget_size(dtypeid);
211 eherr <<
"Cannot obtain the size of the fixed size HDF5 string of the dataset "
213 throw InternalErr (__FILE__, __LINE__, eherr.str ());
216 vector <char> strval;
217 strval.resize(1+ty_size);
219 read_ret = H5Dread(dsetid,memtype,H5S_ALL,H5S_ALL,H5P_DEFAULT,(
void*)&strval[0]);
228 eherr <<
"Cannot read the HDF5 dataset " << name()
229 <<
" with the type of the fixed size HDF5 string "<<endl;
230 throw InternalErr (__FILE__, __LINE__, eherr.str ());
233 string total_string(strval.begin(),strval.end());
238 if (H5Tget_strpad(dtypeid) == H5T_STR_NULLTERM)
239 temp_pos = total_string.find_first_of(
'\0');
240 else if (H5Tget_strpad(dtypeid) == H5T_STR_SPACEPAD)
241 temp_pos = total_string.find_last_not_of(
' ')+1;
243 temp_pos = total_string.find_last_not_of(
'0')+1;
245 string trim_string = total_string.substr(0,temp_pos);
250 if (
true == HDF5RequestHandler::get_drop_long_string()) {
251 if( trim_string.size() > NC_JAVA_STR_SIZE_LIMIT)
254 set_value(trim_string);
264 throw InternalErr (__FILE__, __LINE__,
"H5Tis_variable_str returns negative value" );