OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
h5cfdaputil.cc
Go to the documentation of this file.
1 // This file is part of hdf5_handler: an HDF5 file handler for the OPeNDAP
2 // data server.
3 
4 // Copyright (c) 2011-2013 The HDF Group, Inc. and OPeNDAP, Inc.
5 //
6 // This is free software; you can redistribute it and/or modify it under the
7 // terms of the GNU Lesser General Public License as published by the Free
8 // Software Foundation; either version 2.1 of the License, or (at your
9 // option) any later version.
10 //
11 // This software is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 // License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 //
20 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
21 // You can contact The HDF Group, Inc. at 1800 South Oak Street,
22 // Suite 203, Champaign, IL 61820
23 
35 
36 
37 #include "h5cfdaputil.h"
38 
39 bool HDF5CFDAPUtil::check_beskeys(const string key) {
40 
41  bool found = false;
42  string doset ="";
43  const string dosettrue ="true";
44  const string dosetyes = "yes";
45 
46  TheBESKeys::TheKeys()->get_value( key, doset, found ) ;
47  if( true == found ) {
48  doset = BESUtil::lowercase( doset ) ;
49  if( dosettrue == doset || dosetyes == doset )
50  return true;
51  }
52  return false;
53 
54 }
55 
56 
57 string HDF5CFDAPUtil::escattr(string s)
58 {
59  const string printable = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789~`!@#$%^&*()_-+={[}]|\\:;<,>.?/'\"\n\t\r";
60  const string ESC = "\\";
61  const string DOUBLE_ESC = ESC + ESC;
62  const string QUOTE = "\"";
63  const string ESCQUOTE = ESC + QUOTE;
64 
65  // escape \ with a second backslash
66  string::size_type ind = 0;
67  while ((ind = s.find(ESC, ind)) != s.npos) {
68  s.replace(ind, 1, DOUBLE_ESC);
69  ind += DOUBLE_ESC.length();
70  }
71 
72  // escape non-printing characters with octal escape
73  ind = 0;
74  while ((ind = s.find_first_not_of(printable, ind)) != s.npos)
75  s.replace(ind, 1, ESC + octstring(s[ind]));
76 
77 
78  // escape " with backslash
79  ind = 0;
80  while ((ind = s.find(QUOTE, ind)) != s.npos) {
81  s.replace(ind, 1, ESCQUOTE);
82  ind += ESCQUOTE.length();
83  }
84 
85  return s;
86 }
87 
88 string
89 HDF5CFDAPUtil::octstring(unsigned char val)
90 {
91  ostringstream buf;
92  buf << oct << setw(3) << setfill('0')
93  << static_cast<unsigned int>(val);
94 
95  return buf.str();
96 }
97 
98 
100 
101  const string offend_char = "\"";
102  const string replace_str = "&quote";
103  size_t found_quote = 0;
104  size_t start_pos = 0;
105  while (found_quote != string::npos) {
106  found_quote = str.find(offend_char,start_pos);
107  if (found_quote!= string::npos){
108  str.replace(found_quote,offend_char.size(),replace_str);
109  start_pos = found_quote+1;
110  }
111  }
112 }
113 
114 
116 
117  // The list is based on libdap/AttrTable.h.
118  string DAPUNSUPPORTED ="Unsupported";
119  string DAPBYTE ="Byte";
120  string DAPINT16 ="Int16";
121  string DAPUINT16 ="Uint16";
122  string DAPINT32 ="Int32";
123  string DAPUINT32 ="Uint32";
124  string DAPFLOAT32 ="Float32";
125  string DAPFLOAT64 ="Float64";
126  string DAPSTRING = "String";
127 
128  switch (type) {
129 
130  case H5UCHAR:
131  return DAPBYTE;
132 
133  case H5CHAR:
134  return DAPINT16;
135 
136  case H5INT16:
137  return DAPINT16;
138 
139  case H5UINT16:
140  return DAPUINT16;
141 
142  case H5INT32:
143  return DAPINT32;
144 
145  case H5UINT32:
146  return DAPUINT32;
147 
148  case H5FLOAT32:
149  return DAPFLOAT32;
150 
151  case H5FLOAT64:
152  return DAPFLOAT64;
153 
154  case H5FSTRING:
155  case H5VSTRING:
156  return DAPSTRING;
157  case H5INT64:
158  case H5UINT64:
159  case H5REFERENCE:
160  case H5COMPOUND:
161  case H5ARRAY:
162  return DAPUNSUPPORTED;
163 
164  default:
165  return DAPUNSUPPORTED;
166  }
167 
168 }
169 
171 HDF5CFDAPUtil::get_mem_dtype(H5DataType dtype,size_t mem_dtype_size ) {
172 
173  // Currently in addition to "char" to "int16", all other memory datatype will be the same as the datatype.
174  // So we have a short cut for this function
175  return ((H5INT16 == dtype) && (1 == mem_dtype_size))?H5CHAR:dtype;
176 }
177 
178 
179 string
180 HDF5CFDAPUtil:: print_attr(H5DataType type, int loc, void *vals)
181 {
182  ostringstream rep;
183 
184  union {
185  unsigned char* ucp;
186  char *cp;
187  short *sp;
188  unsigned short *usp;
189  int *ip;
190  unsigned int *uip;
191  float *fp;
192  double *dp;
193  } gp;
194 
195  switch (type) {
196 
197  case H5UCHAR:
198  {
199  unsigned char uc;
200  gp.ucp = (unsigned char *) vals;
201 
202  uc = *(gp.ucp+loc);
203  rep << (int)uc;
204 
205  return rep.str();
206  }
207 
208  case H5CHAR:
209  {
210  gp.cp = (char *) vals;
211  char c;
212  c = *(gp.cp+loc);
213  // Since the character may be a special character and DAP may not be able to represent so supposedly we should escape the character
214  // by calling the escattr function. However, HDF5 native char maps to DAP Int16. So the mapping assumes that users will never
215  // use HDF5 native char or HDF5 unsigned native char to represent characters. Instead HDF5 string should be used to represent characters.
216  // So don't do any escaping of H5CHAR for now. KY 2013-10-14
217  rep <<(int)c;
218  return rep.str();
219  }
220 
221 
222  case H5INT16:
223  {
224  gp.sp = (short *) vals;
225  rep<< *(gp.sp+loc);
226  return rep.str();
227  }
228 
229  case H5UINT16:
230 
231  {
232  gp.usp = (unsigned short *) vals;
233  rep << *(gp.usp+loc);
234  return rep.str();
235  }
236 
237 
238  case H5INT32:
239  {
240  gp.ip = (int *) vals;
241  rep << *(gp.ip+loc);
242  return rep.str();
243  }
244 
245  case H5UINT32:
246  {
247  gp.uip = (unsigned int *) vals;
248  rep << *(gp.uip+loc);
249  return rep.str();
250  }
251 
252 
253  case H5FLOAT32:
254  {
255  gp.fp = (float *) vals;
256  rep << showpoint;
257  rep << setprecision(10);
258  rep << *(gp.fp+loc);
259  if (rep.str().find('.') == string::npos
260  && rep.str().find('e') == string::npos)
261  rep << ".";
262  return rep.str();
263  }
264 
265  case H5FLOAT64:
266  {
267  gp.dp = (double *) vals;
268  rep << std::showpoint;
269  rep << std::setprecision(17);
270  rep << *(gp.dp+loc);
271  if (rep.str().find('.') == string::npos
272  && rep.str().find('e') == string::npos)
273  rep << ".";
274  return rep.str();
275  break;
276  }
277  default:
278  return string("UNKNOWN");
279  }
280 
281 }
282 
static string lowercase(const string &s)
Convert a string to all lower case.
Definition: BESUtil.cc:182
static void replace_double_quote(string &str)
Definition: h5cfdaputil.cc:99
static bool check_beskeys(const string key)
Definition: h5cfdaputil.cc:39
static H5DataType get_mem_dtype(H5DataType, size_t)
Definition: h5cfdaputil.cc:171
H5DataType
Definition: HDF5CFUtil.h:54
static string print_attr(H5DataType h5type, int loc, void *vals)
Definition: h5cfdaputil.cc:180
static string print_type(H5DataType h5type)
Definition: h5cfdaputil.cc:115
static string escattr(string s)
A customized escaping function to escape special characters following OPeNDAP's escattr function that...
Definition: h5cfdaputil.cc:57
Helper functions for generating DAS attributes and a function to check BES Key.
void get_value(const string &s, string &val, bool &found)
Retrieve the value of a given key, if set.
Definition: BESKeys.cc:453
static string octstring(unsigned char val)
Helper function for escattr.
Definition: h5cfdaputil.cc:89
static BESKeys * TheKeys()
Definition: TheBESKeys.cc:48