libdap  Updated for version 3.20.6
libdap4 is an implementation of OPeNDAP's DAP protocol.
D4Opaque.cc
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., 51 Franklin D4Opaqueeet, Fifth Floor, Boston, MA 02110-1301 USA
22 //
23 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
24 
25 //#define DODS_DEBUG
26 
27 #include "config.h"
28 
29 #include <sstream>
30 #include <iterator>
31 
32 #include "D4Opaque.h"
33 
34 #include "DMR.h"
35 #include "D4StreamMarshaller.h"
36 #include "D4StreamUnMarshaller.h"
37 
38 #include "util.h"
39 #include "crc.h"
40 
41 #include "debug.h"
42 #include "DapIndent.h"
43 
44 #undef CLEAR_LOCAL_DATA
45 
46 using namespace std;
47 
48 namespace libdap {
49 
50 D4Opaque &
51 D4Opaque::operator=(const D4Opaque &rhs)
52 {
53  if (this == &rhs)
54  return *this;
55 
56  // Call BaseType::operator=
57  dynamic_cast<BaseType &>(*this) = rhs;
58 
59  d_buf = rhs.d_buf;
60 
61  return *this;
62 }
63 
64 void
65 D4Opaque::clear_local_data()
66 {
67  if (!d_buf.empty()) {
68  d_buf.erase(d_buf.begin(), d_buf.end());
69  d_buf.resize(0);
70  }
71 
72  set_read_p(false);
73 }
74 
75 void
76 D4Opaque::compute_checksum(Crc32 &checksum)
77 {
78  checksum.AddData(&d_buf[0], d_buf.size());
79 }
80 
81 void
82 D4Opaque::serialize(D4StreamMarshaller &m, DMR &, bool)
83 {
84  if (!read_p())
85  read(); // read() throws Error
86 
87  m.put_opaque_dap4( reinterpret_cast<char*>(&d_buf[0]), d_buf.size() ) ;
88 
89 #ifdef CLEAR_LOCAL_DATA
90  clear_local_data();
91 #endif
92 
93 }
94 
95 void
96 D4Opaque::deserialize(D4StreamUnMarshaller &um, DMR &)
97 {
98  um.get_opaque_dap4( d_buf ) ;
99 }
100 
101 unsigned int
102 D4Opaque::buf2val(void **val)
103 {
104  assert(val);
105 
106  // If *val is null, then the caller has not allocated storage for the
107  // value; we must. If there is storage there, assume it is a vector<uint8_t>
108  // (i.e., dods_opaque) and assign d_buf's value to that storage.
109  if (!*val)
110  *val = new vector<uint8_t>;
111  else
112  *static_cast<vector<uint8_t>*>(*val) = d_buf;
113 
114  return sizeof(vector<uint8_t>*);
115 }
116 
117 unsigned int
118 D4Opaque::val2buf(void *val, bool)
119 {
120  assert(val);
121 
122  d_buf = *static_cast<dods_opaque*>(val);
123 
124  return sizeof(dods_opaque*);
125 }
126 
131 bool
132 D4Opaque::set_value(const dods_opaque &value)
133 {
134  d_buf = value;
135  set_read_p(true);
136 
137  return true;
138 }
139 
142 D4Opaque::dods_opaque
143 D4Opaque::value() const
144 {
145  return d_buf;
146 }
147 
148 std::vector<BaseType *> *
149 D4Opaque::transform_to_dap2(AttrTable *){
150  DBG(cerr << __func__ << "() - Transform not implemented DAP4 Opaque type." << endl;);
151  return NULL;
152 }
153 
154 
155 void
156 D4Opaque::print_val(ostream &out, string space, bool print_decl_p)
157 {
158  if (print_decl_p) print_decl(out, space, false);
159 
160  if (d_buf.size()) {
161  // end() - 1 is only OK if size() is > 0
162  std::ostream_iterator<unsigned int> out_it(out, ",");
163  std::copy(d_buf.begin(), d_buf.end() - 1, out_it);
164  out << (unsigned int) d_buf.back(); // can also use: *(d_buf.end()-1);
165  }
166 
167  if (print_decl_p) out << ";" << endl;
168 }
169 
170 void
171 D4Opaque::dump(ostream &strm) const
172 {
173  strm << DapIndent::LMarg << "D4Opaque::dump - ("
174  << (void *)this << ")" << endl ;
175  DapIndent::Indent() ;
176  BaseType::dump(strm) ;
177  //strm << DapIndent::LMarg << "value: " << d_buf << endl ;
178  ostream_iterator<uint8_t> out_it (strm," ");
179  std::copy ( d_buf.begin(), d_buf.end(), out_it );
180 
181  DapIndent::UnIndent() ;
182 }
183 
184 } // namespace libdap
185 
Contains the attributes for a dataset.
Definition: AttrTable.h:142
Read data from the stream made by D4StreamMarshaller.
Definition: crc.h:76
STL namespace.
top level DAP object to house generic methods
Definition: AISConnect.cc:30
virtual void get_opaque_dap4(char **val, int64_t &len)
Marshaller that knows how to marshal/serialize dap data objects to a C++ iostream using DAP4&#39;s receiv...
void AddData(const uint8_t *pData, const uint32_t length)
Definition: crc.h:98