bes  Updated for version 3.20.6
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 Muqun Yang
5 // <myang6@hdfgroup.org>
6 
7 // Copyright (c) 2009-2016 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 1800 South Oak Street,
25 // Suite 203, Champaign, IL 61820
26 
40 
41 
42 #include "config_hdf5.h"
43 
44 
45 #include <string>
46 #include <ctype.h>
47 
48 #include "InternalErr.h"
49 
50 #include "h5dds.h"
51 #include "HDF5Str.h"
52 #include "BESDebug.h"
53 
54 using namespace std;
55 using namespace libdap;
56 
57 HDF5Str::HDF5Str(const string & n, const string &vpath, const string &d)
58  : Str(n,d),var_path(vpath)
59 {
60 }
61 
63 {
64  return new HDF5Str(*this);
65 }
66 
68 {
69 
70  if (read_p())
71  return true;
72 
73  hid_t file_id = H5Fopen(dataset().c_str(),H5F_ACC_RDONLY,H5P_DEFAULT);
74  if(file_id < 0) {
75  throw InternalErr(__FILE__,__LINE__, "Fail to obtain the HDF5 file ID .");
76  }
77 
78  hid_t dset_id = -1;
79  if(true == is_dap4())
80  dset_id = H5Dopen2(file_id,var_path.c_str(),H5P_DEFAULT);
81  else
82  dset_id = H5Dopen2(file_id,name().c_str(),H5P_DEFAULT);
83 
84  if(dset_id < 0) {
85  H5Fclose(file_id);
86  throw InternalErr(__FILE__,__LINE__, "Fail to obtain the datatype .");
87  }
88 
89  hid_t dtypeid = H5Dget_type(dset_id);
90  if(dtypeid < 0) {
91  H5Dclose(dset_id);
92  H5Fclose(file_id);
93  throw InternalErr(__FILE__,__LINE__, "Fail to obtain the datatype .");
94  }
95 
96  size_t size = H5Tget_size(dtypeid);
97 
98  if (size == 0) {
99  H5Tclose(dtypeid);
100  H5Dclose(dset_id);
101  H5Fclose(file_id);
102  throw InternalErr(__FILE__, __LINE__, "cannot return the size of datatype");
103  }
104  try {
105 
106  if(H5Tis_variable_str(dtypeid) >0){
107  vector<string>finstrval;
108  finstrval.resize(1);
109  read_vlen_string(dset_id,1,NULL,NULL,NULL,finstrval);
110  string strval = finstrval[0];
111  set_value(strval);
112  }
113 
114  else {
115  vector<char>chr;
116  chr.resize(size+1);
117  get_data(dset_id, (void *) &chr[0]);
118  set_read_p(true);
119  string str(chr.begin(),chr.end());
120  set_value(str);
121  }
122  H5Tclose(dtypeid);
123  H5Dclose(dset_id);
124  H5Fclose(file_id);
125 
126  }
127 
128  catch(...) {
129  H5Tclose(dtypeid);
130  H5Dclose(dset_id);
131  H5Fclose(file_id);
132  throw;
133  }
134 
135 
136  return true;
137 }
138 
h5dds.h
Data structure and retrieval processing header for the default option.
get_data
void get_data(hid_t dset, void *buf)
Definition: h5common.cc:50
HDF5Str::HDF5Str
HDF5Str(const std::string &n, const std::string &vpath, const std::string &d)
Constructor.
Definition: HDF5Str.cc:57
libdap
Definition: BESDapFunctionResponseCache.h:35
HDF5Str.h
This class that translates HDF5 string into DAP string for the default option.
HDF5Str::read
virtual bool read()
Reads HDF5 string data into local buffer
Definition: HDF5Str.cc:67
HDF5Str::ptr_duplicate
virtual libdap::BaseType * ptr_duplicate()
Definition: HDF5Str.cc:62