OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
BindShapeFunction.cc
Go to the documentation of this file.
1 
2 // -*- mode: c++; c-basic-offset:4 -*-
3 
4 // This file is part of libdap, A C++ implementation of the OPeNDAP Data
5 // Access Protocol.
6 
7 // Copyright (c) 2013 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 
30 #include <sstream>
31 #include <vector>
32 
33 #include <BaseType.h>
34 #include <Array.h>
35 #include <Str.h>
36 
37 #include <Error.h>
38 #include <DDS.h>
39 
40 #include <debug.h>
41 #include <util.h>
42 
43 #include <BESDebug.h>
44 
45 #include "BindNameFunction.h"
46 
47 namespace libdap {
48 
49 vector<int> parse_dims(const string &shape); // defined in MakeArrayFunction.cc
50 
65 void
66 function_bind_shape(int argc, BaseType * argv[], DDS &, BaseType **btpp)
67 {
68  string info =
69  string("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n") +
70  "<function name=\"make_array\" version=\"1.0\" href=\"http://docs.opendap.org/index.php/Server_Side_Processing_Functions#bind_shape\">\n" +
71  "</function>";
72 
73  if (argc == 0) {
74  Str *response = new Str("info");
75  response->set_value(info);
76  *btpp = response;
77  return;
78  }
79 
80  // Check for two args or more. The first two must be strings.
81  if (argc != 2)
82  throw Error(malformed_expr, "bind_shape(shape,variable) requires two arguments.");
83 
84  // string shape = extract_string_argument(argv[0]);
85  vector<int> dims = parse_dims(extract_string_argument(argv[0]));
86 
87  Array *array = dynamic_cast<Array*>(argv[1]);
88  if (!array)
89  throw Error(malformed_expr, "bind_shape() requires an Array as its second argument.");
90 
91  unsigned long vector_size = array->length();
92 
93  array->clear_all_dims();
94 
95  unsigned long number_of_elements = 1;
96  vector<int>::iterator i = dims.begin();
97  while (i != dims.end()) {
98  number_of_elements *= *i;
99  array->append_dim(*i++);
100  }
101 
102  if (number_of_elements != vector_size)
103  throw Error(malformed_expr, "bind_shape(): The product of the new dimensions must match the size of the vector argument.");
104 
105  *btpp = argv[1];
106 
107  return;
108 }
109 
110 } // namesspace libdap
vector< int > parse_dims(const string &shape)
Parse the shape 'expression'.
void function_bind_shape(int argc, BaseType *argv[], DDS &, BaseType **btpp)
Bind a shape to a DAP2 Array that is a vector.