libdap Updated for version 3.21.1
libdap4 is an implementation of OPeNDAP's DAP protocol.
D4Dimensions.cc
Go to the documentation of this file.
1// -*- mode: c++; c-basic-offset:4 -*-
2
3// This file is part of libdap, A C++ implementation of the OPeNDAP Data
4// Access Protocol.
5
6// Copyright (c) 2013 OPeNDAP, Inc.
7// Author: James Gallagher <jgallagher@opendap.org>
8//
9// This library is free software; you can redistribute it and/or
10// modify it under the terms of the GNU Lesser General Public
11// License as published by the Free Software Foundation; either
12// version 2.1 of the License, or (at your option) any later version.
13//
14// This library is distributed in the hope that it will be useful,
15// but WITHOUT ANY WARRANTY; without even the implied warranty of
16// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17// Lesser General Public 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22//
23// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
24
25#include "config.h"
26
27#include <sstream>
28
29#include "D4Dimensions.h"
30#include "D4Group.h"
31#include "XMLWriter.h"
32
33#include "Error.h"
34#include "InternalErr.h"
35
36namespace libdap {
37
38void D4Dimension::set_size(const string &size) {
39 unsigned long value = 0;
40 istringstream iss(size);
41 iss >> value;
42
43 // First test if the stream is OK, then look to see if we read all
44 // of the chars.
45 if (!iss || !iss.eof())
46 throw Error("Invalid value '" + size + "' passed to D4Dimension::set_size.");
47 set_size(value);
48}
49
55 string name = d_name;
56
57 // d_parent is the D4Dimensions container and its parent is the Group where
58 // this Dimension is defined.
59 D4Group *grp = d_parent->parent();
60 while (grp) {
61 // The root group is named "/" (always); this avoids '//name'
62 name = (grp->name() == "/") ? "/" + name : grp->name() + "/" + name;
63
64 if (grp->get_parent())
65 grp = static_cast<D4Group *>(grp->get_parent());
66 else
67 grp = 0;
68 }
69
70 return name;
71}
72
80 if (xmlTextWriterStartElement(xml.get_writer(), (const xmlChar *)"Dimension") < 0)
81 throw InternalErr(__FILE__, __LINE__, "Could not write Dimension element");
82
83 if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar *)"name", (const xmlChar *)d_name.c_str()) < 0)
84 throw InternalErr(__FILE__, __LINE__, "Could not write attribute for name");
85#if 0
86 // Use FQNs when things are referenced, not when they are defined
87 if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "name", (const xmlChar*)fully_qualified_name().c_str()) < 0)
88 throw InternalErr(__FILE__, __LINE__, "Could not write attribute for name");
89#endif
90 ostringstream oss;
91 if (d_constrained)
92 oss << (d_c_stop - d_c_start) / d_c_stride + 1;
93 else
94 oss << d_size;
95 if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar *)"size", (const xmlChar *)oss.str().c_str()) < 0)
96 throw InternalErr(__FILE__, __LINE__, "Could not write attribute for size");
97
98 if (xmlTextWriterEndElement(xml.get_writer()) < 0)
99 throw InternalErr(__FILE__, __LINE__, "Could not end Dimension element");
100}
101
102#if 0
103// Note that in order for this to work the second argument must not be a reference.
104// jhrg 8/20/13
105static bool
106dim_name_eq(D4Dimension *d, const string name)
107{
108 return d->name() == name;
109}
110#endif
111
113#if 0
114 D4DimensionsIter d = find_if(d_dims.begin(), d_dims.end(), bind2nd(ptr_fun(dim_name_eq), name));
115#endif
116 auto d = find_if(d_dims.begin(), d_dims.end(), [name](const D4Dimension *dim) { return name == dim->name(); });
117 return (d != d_dims.end()) ? *d : 0;
118}
119
120void D4Dimensions::print_dap4(XMLWriter &xml, bool constrained) const {
121 D4DimensionsCIter i = d_dims.begin();
122 while (i != d_dims.end()) {
123#if 0
124 if (!constrained || parent()->find_first_var_that_uses_dimension(*i))
125 (*i)->print_dap4(xml);
126#endif
127 if (constrained) {
128 if ((*i)->used_by_projected_var())
129 (*i)->print_dap4(xml);
130 } else {
131 (*i)->print_dap4(xml);
132 }
133 ++i;
134 }
135}
136
137} /* namespace libdap */
virtual string name() const
Returns the name of the class instance.
Definition BaseType.cc:296
virtual BaseType * get_parent() const
Definition BaseType.cc:636
unsigned long long size() const
string name() const
void print_dap4(XMLWriter &xml) const
Print the Dimension declaration. Print the Dimension in a form suitable for use in a Group definition...
void set_size(unsigned long long size)
string fully_qualified_name() const
Get the FQN for the dimension.
vector< D4Dimension * >::iterator D4DimensionsIter
Iterator used for D4Dimensions.
vector< D4Dimension * >::const_iterator D4DimensionsCIter
D4Dimension * find_dim(const string &name)
D4Group * parent() const
void print_dap4(XMLWriter &xml, bool constrained=false) const
A class for error processing.
Definition Error.h:92
A class for software fault reporting.
Definition InternalErr.h:61
xmlTextWriterPtr get_writer() const
Definition XMLWriter.h:55
top level DAP object to house generic methods
Definition AISConnect.cc:30