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