bes Updated for version 3.21.1
The Backend Server (BES) is the lower two tiers of the Hyrax data server
HDF5Str.cc
Go to the documentation of this file.
1// This file is part of hdf5_handler a HDF5 file handler for the OPeNDAP
2// data server.
3
4// Author: Hyo-Kyung Lee <hyoklee@hdfgroup.org> and Kent Yang
5// <myang6@hdfgroup.org>
6
7// Copyright (c) 2009-2023 The HDF Group, Inc. and OPeNDAP, Inc.
8//
9// This is free software; you can redistribute it and/or modify it under the
10// terms of the GNU Lesser General Public License as published by the Free
11// Software Foundation; either version 2.1 of the License, or (at your
12// option) any later version.
13//
14// This software is distributed in the hope that it will be useful, but
15// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17// License for more details.
18//
19// You should have received a copy of the GNU Lesser General Public
20// License along with this library; if not, write to the Free Software
21// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22//
23// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
24// You can contact The HDF Group, Inc. at 410 E University Ave,
25// Suite 200, Champaign, IL 61820
26
40
41
42#include "config_hdf5.h"
43
44
45#include <string>
46#include <ctype.h>
47#include <memory>
48
49#include <libdap/InternalErr.h>
50
51#include "h5dds.h"
52#include "HDF5Str.h"
53#include "BESDebug.h"
54
55using namespace std;
56using namespace libdap;
57
58HDF5Str::HDF5Str(const string & n, const string &vpath, const string &d)
59 : Str(n,d),var_path(vpath)
60{
61}
62
64{
65 auto HDF5Str_unique = make_unique<HDF5Str>(*this);
66 return HDF5Str_unique.release();
67}
68
70{
71
72 if (read_p())
73 return true;
74
75 hid_t file_id = H5Fopen(dataset().c_str(),H5F_ACC_RDONLY,H5P_DEFAULT);
76 if(file_id < 0) {
77 throw InternalErr(__FILE__,__LINE__, "Fail to obtain the HDF5 file ID .");
78 }
79
80 hid_t dset_id = -1;
81 if(true == is_dap4())
82 dset_id = H5Dopen2(file_id,var_path.c_str(),H5P_DEFAULT);
83 else
84 dset_id = H5Dopen2(file_id,name().c_str(),H5P_DEFAULT);
85
86 if(dset_id < 0) {
87 H5Fclose(file_id);
88 throw InternalErr(__FILE__,__LINE__, "Fail to obtain the datatype .");
89 }
90
91 hid_t dtypeid = H5Dget_type(dset_id);
92 if(dtypeid < 0) {
93 H5Dclose(dset_id);
94 H5Fclose(file_id);
95 throw InternalErr(__FILE__,__LINE__, "Fail to obtain the datatype .");
96 }
97
98 size_t size = H5Tget_size(dtypeid);
99
100 if (size == 0) {
101 H5Tclose(dtypeid);
102 H5Dclose(dset_id);
103 H5Fclose(file_id);
104 throw InternalErr(__FILE__, __LINE__, "cannot return the size of datatype");
105 }
106 try {
107
108 if(H5Tis_variable_str(dtypeid) >0){
109 vector<string>finstrval;
110 finstrval.resize(1);
111 read_vlen_string(dset_id,1,nullptr,nullptr,nullptr,finstrval);
112 string strval = finstrval[0];
113 set_value(strval);
114 }
115
116 else {
117 vector<char>chr;
118 chr.resize(size+1);
119 get_data(dset_id, (void *) chr.data());
120 set_read_p(true);
121 string str(chr.begin(),chr.end());
122 set_value(str);
123 }
124 H5Tclose(dtypeid);
125 H5Dclose(dset_id);
126 H5Fclose(file_id);
127
128 }
129
130 catch(...) {
131 H5Tclose(dtypeid);
132 H5Dclose(dset_id);
133 H5Fclose(file_id);
134 throw;
135 }
136
137
138 return true;
139}
140
This class that translates HDF5 string into DAP string for the default option.
bool read() override
Reads HDF5 string data into local buffer.
Definition HDF5Str.cc:69
libdap::BaseType * ptr_duplicate() override
Definition HDF5Str.cc:63
HDF5Str(const std::string &n, const std::string &vpath, const std::string &d)
Constructor.
Definition HDF5Str.cc:58
void get_data(hid_t dset, void *buf)
Data structure and retrieval processing header for the default option.