libdap Updated for version 3.21.0
libdap4 is an implementation of OPeNDAP's DAP protocol.
Str.cc
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) 2002,2003 OPeNDAP, Inc.
8// Author: 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// (c) COPYRIGHT URI/MIT 1994-1999
27// Please read the full copyright statement in the file COPYRIGHT_URI.
28//
29// Authors:
30// jhrg,jimg James Gallagher <jgallagher@gso.uri.edu>
31
32// Implementation for Str.
33//
34// jhrg 9/7/94
35
36
37#include "config.h"
38
39#include <sstream>
40
41#include "Byte.h"
42#if 0
43#include "Int16.h"
44#include "UInt16.h"
45#include "Int32.h"
46#include "UInt32.h"
47#include "Float32.h"
48#include "Float64.h"
49#endif
50#include "Str.h"
51#include "Url.h"
52
53#if 0
54#include "Array.h"
55#include "Structure.h"
56#include "Sequence.h"
57#include "Grid.h"
58#endif
59
60#include "DDS.h"
61#include "Marshaller.h"
62#include "UnMarshaller.h"
63
64#include "DMR.h"
65#include "D4StreamMarshaller.h"
66#include "D4StreamUnMarshaller.h"
67
68#include "util.h"
69
70#if 0
71#include "parser.h"
72#endif
73#include "Operators.h"
74#include "InternalErr.h"
75#include "escaping.h"
76#include "debug.h"
77#include "DapIndent.h"
78
79using std::cerr;
80using std::endl;
81
82namespace libdap {
83
92Str::Str(const string &n) : BaseType(n, dods_str_c), d_buf("")
93{}
94
102Str::Str(const string &n, const string &d) : BaseType(n, d, dods_str_c), d_buf("")
103{}
104
105Str::Str(const Str &copy_from) : BaseType(copy_from)
106{
107 d_buf = copy_from.d_buf;
108}
109
110BaseType *
112{
113 return new Str(*this);
114}
115
116Str &
117Str::operator=(const Str &rhs)
118{
119 if (this == &rhs)
120 return *this;
121 BaseType::operator=(rhs);
122 d_buf = rhs.d_buf;
123 return *this;
124}
125
126int
128{
129 return (int)d_buf.length();
130}
131
132int64_t
134{
135 return (int64_t)d_buf.length();
136}
137
138bool
139Str::serialize(ConstraintEvaluator &eval, DDS &dds, Marshaller &m, bool ce_eval)
140{
141
142 DBG(cerr << "Entering (" << this->name() << " [" << this << "])" << endl);
143#if USE_LOCAL_TIMEOUT_SCHEME
144 dds.timeout_on();
145#endif
146 if (!read_p())
147 read();
148
149 if (ce_eval && !eval.eval_selection(dds, dataset()))
150 return true;
151#if USE_LOCAL_TIMEOUT_SCHEME
152 dds.timeout_off();
153#endif
154 m.put_str( d_buf ) ;
155
156 DBG(cerr << "Exiting: buf = " << d_buf << endl);
157
158 return true;
159}
160
161// deserialize the string on stdin and put the result in BUF.
162
163bool
165{
166 um.get_str( d_buf ) ;
167
168 return false;
169}
170
171void
173{
174 checksum.AddData(reinterpret_cast<const uint8_t*>(d_buf.data()), d_buf.length());
175}
176
185void
186Str::serialize(D4StreamMarshaller &m, DMR &, /*ConstraintEvaluator &,*/ bool)
187{
188 if (!read_p())
189 read(); // read() throws Error
190
191 m.put_str( d_buf ) ;
192}
193
194void
196{
197 um.get_str( d_buf ) ;
198}
199
209unsigned int
210Str::buf2val(void **val)
211{
212 // Jose Garcia
213 // The same comment justifying throwing an Error in val2buf applies here.
214 if (!val)
215 throw InternalErr(__FILE__, __LINE__,
216 "No place to store a reference to the data.");
217 // If *val is null, then the caller has not allocated storage for the
218 // value; we must. If there is storage there, assume it is a string and
219 // assign d_buf's value to that storage.
220 if (!*val)
221 *val = new string(d_buf);
222 else
223 *static_cast<string*>(*val) = d_buf;
224
225 return sizeof(string*);
226}
227
237unsigned int
238Str::val2buf(void *val, bool)
239{
240 // Jose Garcia
241 // This method is public therefore and I believe it has being designed
242 // to be use by read which must be implemented on the surrogated library,
243 // thus if the pointer val is NULL, is an Internal Error.
244 if (!val)
245 throw InternalErr(__FILE__, __LINE__, "NULL pointer.");
246
247 d_buf = *static_cast<string*>(val);
248
249 return sizeof(string*);
250}
251
256bool
257Str::set_value(const string &value)
258{
259 d_buf = value;
260 set_read_p(true);
261
262 return true;
263}
264
267string
269{
270 return d_buf;
271}
272
273void
274Str::print_val(FILE *out, string space, bool print_decl_p)
275{
276 ostringstream oss;
277 print_val(oss, space, print_decl_p);
278 fwrite(oss.str().data(), sizeof(char), oss.str().length(), out);
279}
280
288string
290{
291 return escattr(s);
292}
293
294void
295Str::print_val(ostream &out, string space, bool print_decl_p)
296{
297 if (print_decl_p) {
298 print_decl(out, space, false);
299 out << " = \"" << esc_string_variable_value(d_buf) << "\";" << endl;
300 }
301 else {
302 out << "\"" << esc_string_variable_value(d_buf) << "\"";
303 }
304}
305
306bool
308{
309 // Extract the Byte arg's value.
310 if (!read_p() && !read()) {
311 // Jose Garcia
312 // Since the read method is virtual and implemented outside
313 // libdap++ if we cannot read the data that is the problem
314 // of the user or of whoever wrote the surrogate library
315 // implementing read therefore it is an internal error.
316 throw InternalErr(__FILE__, __LINE__, "This value was not read!");
317 }
318
319 // Extract the second arg's value.
320 if (!b || !(b->read_p() || b->read())) {
321 // Jose Garcia
322 // Since the read method is virtual and implemented outside
323 // libdap++ if we cannot read the data that is the problem
324 // of the user or of whoever wrote the surrogate library
325 // implementing read therefore it is an internal error.
326 throw InternalErr(__FILE__, __LINE__, "Argument value was not read!");
327 }
328
329 return d4_ops(b, op);
330}
331
335bool Str::d4_ops(BaseType *b, int op)
336{
337 switch (b->type()) {
338 case dods_byte_c:
339 case dods_int8_c:
340 case dods_int16_c:
341 case dods_uint16_c:
342 case dods_int32_c:
343 case dods_uint32_c:
344 case dods_int64_c:
345 case dods_uint64_c:
346 case dods_float32_c:
347 case dods_float64_c:
348 throw Error(malformed_expr, "Relational operators can only compare compatible types (string, number).");
349 case dods_str_c:
350 return StrCmp<string, string>(op, d_buf, static_cast<Str*>(b)->value());
351 case dods_url_c:
352 return StrCmp<string, string>(op, d_buf, static_cast<Url*>(b)->value());
353 default:
354 throw Error(malformed_expr, "Relational operators only work with scalar types.");
355 }
356}
357
366void
367Str::dump(ostream &strm) const
368{
369 strm << DapIndent::LMarg << "Str::dump - ("
370 << (void *)this << ")" << endl ;
371 DapIndent::Indent() ;
372 BaseType::dump(strm) ;
373 strm << DapIndent::LMarg << "value: " << d_buf << endl ;
374 DapIndent::UnIndent() ;
375}
376
377} // namespace libdap
378
Definition crc.h:77
void AddData(const uint8_t *pData, const uint32_t length)
Definition crc.h:98
The basic data type for the DODS DAP types.
Definition BaseType.h:120
virtual bool read()
Read data into a local buffer.
Definition BaseType.cc:905
virtual string name() const
Returns the name of the class instance.
Definition BaseType.cc:317
virtual void print_decl(FILE *out, string space=" ", bool print_semi=true, bool constraint_info=false, bool constrained=false)
Print an ASCII representation of the variable structure.
Definition BaseType.cc:1009
virtual bool read_p()
Has this variable been read?
Definition BaseType.cc:477
virtual void set_read_p(bool state)
Sets the value of the read_p property.
Definition BaseType.cc:513
virtual string dataset() const
Returns the name of the dataset used to create this instance.
Definition BaseType.cc:355
void dump(ostream &strm) const override
dumps information about this object
Definition BaseType.cc:287
Evaluate a constraint expression.
bool eval_selection(DDS &dds, const std::string &dataset)
Evaluate a boolean-valued constraint expression. This is main method for the evaluator and is called ...
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.
A class for error processing.
Definition Error.h:94
A class for software fault reporting.
Definition InternalErr.h:65
abstract base class used to marshal/serialize dap data objects
Definition Marshaller.h:50
Holds character string data.
Definition Str.h:62
unsigned int buf2val(void **val) override
Definition Str.cc:210
void compute_checksum(Crc32 &checksum) override
include the data for this variable in the checksum DAP4 includes a checksum with every data response....
Definition Str.cc:172
int64_t length_ll() const override
Get the number of elements in this variable This version of the function deprecates length() which is...
Definition Str.cc:133
Str(const string &n)
Definition Str.cc:92
bool d4_ops(BaseType *b, int op) override
Definition Str.cc:335
int length() const override
How many elements are in this variable? Uses -1 in places.
Definition Str.cc:127
bool serialize(ConstraintEvaluator &eval, DDS &dds, Marshaller &m, bool ce_eval=true) override
Move data to the net, then remove them from the object.
Definition Str.cc:139
bool deserialize(UnMarshaller &um, DDS *dds, bool reuse=false) override
Receive data from the net.
Definition Str.cc:164
virtual string value() const
Definition Str.cc:268
void dump(ostream &strm) const override
dumps information about this object
Definition Str.cc:367
bool ops(BaseType *b, int op) override
Evaluate relational operators.
Definition Str.cc:307
BaseType * ptr_duplicate() override
Definition Str.cc:111
unsigned int val2buf(void *val, bool reuse=false) override
Definition Str.cc:238
virtual bool set_value(const string &value)
Definition Str.cc:257
void print_val(FILE *out, string space="", bool print_decl_p=true) override
Prints the value of the variable.
Definition Str.cc:274
virtual string esc_string_variable_value(const string &s)
Escape non-printable characters and quotes from a Str variable value. The value printed is used mostl...
Definition Str.cc:289
abstract base class used to unmarshall/deserialize dap data objects
Holds an Internet address (URL).
Definition Url.h:56
top level DAP object to house generic methods
Definition AISConnect.cc:30
string escattr(string s)
Definition escaping.cc:368