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