bes  Updated for version 3.20.6
functions_util.cc
1 
2 // -*- mode: c++; c-basic-offset:4 -*-
3 
4 // This file is part of bes, A C++ implementation of the OPeNDAP
5 // Hyrax data server
6 
7 // Copyright (c) 2015 OPeNDAP, Inc.
8 // Authors: James Gallagher <jgallagher@opendap.org>
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 //
24 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
25 
26 #include "config.h"
27 
28 #include <cassert>
29 #include <string>
30 #include <sstream>
31 #include <vector>
32 
33 #include "BaseType.h"
34 #include "Byte.h"
35 #include "Int16.h"
36 #include "Int32.h"
37 #include "UInt16.h"
38 #include "UInt32.h"
39 #include "Int64.h"
40 #include "UInt64.h"
41 #include "Int8.h"
42 #include "Float32.h"
43 #include "Float64.h"
44 #include "Str.h"
45 #include <Array.h>
46 #include <Error.h>
47 #include <util.h>
48 
49 using namespace std;
50 using namespace libdap;
51 
52 namespace functions {
53 
58 vector<int> parse_dims(const string &shape)
59 {
60  vector<int> dims;
61  istringstream iss(shape);
62  string::size_type pos = 0;
63  do {
64  char brace;
65  iss >> brace;
66  ++pos;
67  // EOF is only found by reading past the last character
68  if (iss.eof()) return dims;
69 
70  if (brace != '[' || iss.fail())
71  throw Error(malformed_expr,
72  "make_array(): Expected a left brace at position " + long_to_string(pos) + " in shape expression: "
73  + shape);
74 
75  int size = 0;
76  iss >> size;
77  ++pos;
78  if (size == 0 || iss.fail())
79  throw Error(malformed_expr,
80  "make_array(): Expected an integer at position " + long_to_string(pos) + " in shape expression: "
81  + shape);
82  dims.push_back(size);
83 
84  iss >> brace;
85  ++pos;
86  if (brace != ']' || iss.fail())
87  throw Error(malformed_expr,
88  "make_array(): Expected a right brace at position " + long_to_string(pos) + " in shape expression: "
89  + shape);
90  } while (!iss.eof());
91 
92  return dims;
93 }
94 
104 void check_number_type_array(BaseType *btp, unsigned int rank /* = 0 */)
105 {
106  if (!btp)
107  throw InternalErr(__FILE__, __LINE__, "roi() function called with null variable.");
108 
109  if (btp->type() != dods_array_c)
110  throw Error("In function roi(): Expected argument '" + btp->name() + "' to be an Array.");
111 
112  Array *a = static_cast<Array *>(btp);
113  if (!a->var()->is_simple_type() || a->var()->type() == dods_str_c || a->var()->type() == dods_url_c)
114  throw Error("In function roi(): Expected argument '" + btp->name() + "' to be an Array of numeric types.");
115 
116  if (rank && !(a->dimensions() == rank || a->dimensions() == rank+1))
117  throw Error("In function roi(): Expected the array '" + a->name() +"' to be rank " + long_to_string(rank) + " or " + long_to_string(rank+1) + ".");
118 }
119 
129 unsigned int extract_uint_value(BaseType *arg)
130 {
131  assert(arg);
132 
133  // Simple types are Byte, ..., Float64, String and Url.
134  if (!arg->is_simple_type() || arg->type() == dods_str_c || arg->type() == dods_url_c)
135  throw Error(malformed_expr, "The function requires a numeric-type argument.");
136 
137  if (!arg->read_p())
138  throw InternalErr(__FILE__, __LINE__,
139  "The Evaluator built an argument list where some constants held no values.");
140 
141  // The types of arguments that the CE Parser will build for numeric
142  // constants are limited to Uint32, Int32 and Float64. See ce_expr.y.
143  // Expanded to work for any numeric type so it can be used for more than
144  // just arguments.
145  switch (arg->type()) {
146  case dods_byte_c:
147  return (unsigned int) (static_cast<Byte*>(arg)->value());
148  case dods_uint16_c:
149  return (unsigned int) (static_cast<UInt16*>(arg)->value());
150  case dods_int16_c:
151  return (unsigned int) (static_cast<Int16*>(arg)->value());
152  case dods_uint32_c:
153  return (unsigned int) (static_cast<UInt32*>(arg)->value());
154  case dods_int32_c:
155  return (unsigned int) (static_cast<Int32*>(arg)->value());
156  case dods_float32_c:
157  return (unsigned int) (static_cast<Float32*>(arg)->value());
158  case dods_float64_c:
159  return (unsigned int)static_cast<Float64*>(arg)->value();
160 
161  // Support for DAP4 types.
162  case dods_uint8_c:
163  return (unsigned int) (static_cast<Byte*>(arg)->value());
164  case dods_int8_c:
165  return (unsigned int) (static_cast<Int8*>(arg)->value());
166  case dods_uint64_c:
167  return (unsigned int) (static_cast<UInt64*>(arg)->value());
168  case dods_int64_c:
169  return (unsigned int) (static_cast<Int64*>(arg)->value());
170 
171  default:
172  throw InternalErr(__FILE__, __LINE__,
173  "The argument list built by the parser contained an unsupported numeric type.");
174  }
175 }
176 
177 } // namespace functions
libdap
Definition: BESDapFunctionResponseCache.h:35
Error