libdap Updated for version 3.21.1
libdap4 is an implementation of OPeNDAP's DAP protocol.
Str.cc
Go to the documentation of this file.
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#include "config.h"
37
38#include <sstream>
39
40#include "Byte.h"
41#if 0
42#include "Float32.h"
43#include "Float64.h"
44#include "Int16.h"
45#include "Int32.h"
46#include "UInt16.h"
47#include "UInt32.h"
48#endif
49#include "Str.h"
50#include "Url.h"
51
52#if 0
53#include "Array.h"
54#include "Grid.h"
55#include "Sequence.h"
56#include "Structure.h"
57#endif
58
59#include "DDS.h"
60#include "Marshaller.h"
61#include "UnMarshaller.h"
62
63#include "D4StreamMarshaller.h"
65#include "DMR.h"
66
67#include "util.h"
68
69#if 0
70#include "parser.h"
71#endif
72#include "DapIndent.h"
73#include "InternalErr.h"
74#include "Operators.h"
75#include "debug.h"
76#include "escaping.h"
77
78using std::cerr;
79using std::endl;
80
81namespace libdap {
82
91Str::Str(const string &n) : BaseType(n, dods_str_c), d_buf("") {}
92
100Str::Str(const string &n, const string &d) : BaseType(n, d, dods_str_c), d_buf("") {}
101
102Str::Str(const Str &copy_from) : BaseType(copy_from) { d_buf = copy_from.d_buf; }
103
104BaseType *Str::ptr_duplicate() { return new Str(*this); }
105
106Str &Str::operator=(const Str &rhs) {
107 if (this == &rhs)
108 return *this;
110 d_buf = rhs.d_buf;
111 return *this;
112}
113
114int Str::length() const { return (int)d_buf.length(); }
115
116int64_t Str::length_ll() const { return (int64_t)d_buf.length(); }
117
118bool Str::serialize(ConstraintEvaluator &eval, DDS &dds, Marshaller &m, bool ce_eval) {
119
120 DBG(cerr << "Entering (" << this->name() << " [" << this << "])" << endl);
121#if USE_LOCAL_TIMEOUT_SCHEME
122 dds.timeout_on();
123#endif
124 if (!read_p())
125 read();
126
127 if (ce_eval && !eval.eval_selection(dds, dataset()))
128 return true;
129#if USE_LOCAL_TIMEOUT_SCHEME
130 dds.timeout_off();
131#endif
132 m.put_str(d_buf);
133
134 DBG(cerr << "Exiting: buf = " << d_buf << endl);
135
136 return true;
137}
138
139// deserialize the string on stdin and put the result in BUF.
140
142 um.get_str(d_buf);
143
144 return false;
145}
146
148 checksum.AddData(reinterpret_cast<const uint8_t *>(d_buf.data()), d_buf.length());
149}
150
159void Str::serialize(D4StreamMarshaller &m, DMR &, /*ConstraintEvaluator &,*/ bool) {
160 if (!read_p())
161 read(); // read() throws Error
162
163 m.put_str(d_buf);
164}
165
167
177unsigned int Str::buf2val(void **val) {
178 // Jose Garcia
179 // The same comment justifying throwing an Error in val2buf applies here.
180 if (!val)
181 throw InternalErr(__FILE__, __LINE__, "No place to store a reference to the data.");
182 // If *val is null, then the caller has not allocated storage for the
183 // value; we must. If there is storage there, assume it is a string and
184 // assign d_buf's value to that storage.
185 if (!*val)
186 *val = new string(d_buf);
187 else
188 *static_cast<string *>(*val) = d_buf;
189
190 return sizeof(string *);
191}
192
202unsigned int Str::val2buf(void *val, bool) {
203 // Jose Garcia
204 // This method is public therefore and I believe it has being designed
205 // to be use by read which must be implemented on the surrogated library,
206 // thus if the pointer val is NULL, is an Internal Error.
207 if (!val)
208 throw InternalErr(__FILE__, __LINE__, "NULL pointer.");
209
210 d_buf = *static_cast<string *>(val);
211
212 return sizeof(string *);
213}
214
219bool Str::set_value(const string &value) {
220 d_buf = value;
221 set_read_p(true);
222
223 return true;
224}
225
228string Str::value() const { return d_buf; }
229
230void Str::print_val(FILE *out, string space, bool print_decl_p) {
231 ostringstream oss;
232 print_val(oss, space, print_decl_p);
233 fwrite(oss.str().data(), sizeof(char), oss.str().length(), out);
234}
235
243string Str::esc_string_variable_value(const string &s) { return escattr(s); }
244
245void Str::print_val(ostream &out, string space, bool print_decl_p) {
246 if (print_decl_p) {
247 print_decl(out, space, false);
248 out << " = \"" << esc_string_variable_value(d_buf) << "\";" << endl;
249 } else {
250 out << "\"" << esc_string_variable_value(d_buf) << "\"";
251 }
252}
253
254bool Str::ops(BaseType *b, int op) {
255 // Extract the Byte arg's value.
256 if (!read_p() && !read()) {
257 // Jose Garcia
258 // Since the read method is virtual and implemented outside
259 // libdap++ if we cannot read the data that is the problem
260 // of the user or of whoever wrote the surrogate library
261 // implementing read therefore it is an internal error.
262 throw InternalErr(__FILE__, __LINE__, "This value was not read!");
263 }
264
265 // Extract the second arg's value.
266 if (!b || !(b->read_p() || b->read())) {
267 // Jose Garcia
268 // Since the read method is virtual and implemented outside
269 // libdap++ if we cannot read the data that is the problem
270 // of the user or of whoever wrote the surrogate library
271 // implementing read therefore it is an internal error.
272 throw InternalErr(__FILE__, __LINE__, "Argument value was not read!");
273 }
274
275 return d4_ops(b, op);
276}
277
281bool Str::d4_ops(BaseType *b, int op) {
282 switch (b->type()) {
283 case dods_byte_c:
284 case dods_int8_c:
285 case dods_int16_c:
286 case dods_uint16_c:
287 case dods_int32_c:
288 case dods_uint32_c:
289 case dods_int64_c:
290 case dods_uint64_c:
291 case dods_float32_c:
292 case dods_float64_c:
293 throw Error(malformed_expr, "Relational operators can only compare compatible types (string, number).");
294 case dods_str_c:
295 return StrCmp<string, string>(op, d_buf, static_cast<Str *>(b)->value());
296 case dods_url_c:
297 return StrCmp<string, string>(op, d_buf, static_cast<Url *>(b)->value());
298 default:
299 throw Error(malformed_expr, "Relational operators only work with scalar types.");
300 }
301}
302
311void Str::dump(ostream &strm) const {
312 strm << DapIndent::LMarg << "Str::dump - (" << (void *)this << ")" << endl;
314 BaseType::dump(strm);
315 strm << DapIndent::LMarg << "value: " << d_buf << endl;
317}
318
319} // namespace libdap
#define malformed_expr
(400)
Definition Error.h:66
Definition crc.h:43
void AddData(const uint8_t *pData, const uint32_t length)
Definition crc.h:64
BaseType & operator=(const BaseType &rhs)
Definition BaseType.cc:159
virtual bool read()
Read data into a local buffer.
Definition BaseType.cc:775
virtual string name() const
Returns the name of the class instance.
Definition BaseType.cc:296
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:868
virtual bool read_p()
Has this variable been read?
Definition BaseType.cc:410
virtual void set_read_p(bool state)
Sets the value of the read_p property.
Definition BaseType.cc:442
virtual string dataset() const
Returns the name of the dataset used to create this instance.
Definition BaseType.cc:326
void dump(ostream &strm) const override
dumps information about this object
Definition BaseType.cc:269
BaseType(const string &n, const Type &t, bool is_dap4=false)
The BaseType constructor.
Definition BaseType.cc:124
virtual Type type() const
Returns the type of the class instance.
Definition BaseType.cc:329
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...
virtual void put_str(const string &val)
Read data from the stream made by D4StreamMarshaller.
virtual void get_str(string &val)
void timeout_off()
Definition DDS.cc:695
void timeout_on()
Definition DDS.cc:687
static ostream & LMarg(ostream &strm)
Definition DapIndent.cc:61
static void Indent()
Definition DapIndent.cc:44
static void UnIndent()
Definition DapIndent.cc:46
A class for error processing.
Definition Error.h:92
A class for software fault reporting.
Definition InternalErr.h:61
abstract base class used to marshal/serialize dap data objects
Definition Marshaller.h:50
virtual void put_str(const std::string &val)=0
Str & operator=(const Str &rhs)
Definition Str.cc:106
unsigned int buf2val(void **val) override
Definition Str.cc:177
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:147
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:116
Str(const string &n)
Definition Str.cc:91
bool d4_ops(BaseType *b, int op) override
Definition Str.cc:281
int length() const override
How many elements are in this variable? Uses -1 in places.
Definition Str.cc:114
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:118
bool deserialize(UnMarshaller &um, DDS *dds, bool reuse=false) override
Receive data from the net.
Definition Str.cc:141
virtual string value() const
Definition Str.cc:228
void dump(ostream &strm) const override
dumps information about this object
Definition Str.cc:311
bool ops(BaseType *b, int op) override
Evaluate relational operators.
Definition Str.cc:254
BaseType * ptr_duplicate() override
Definition Str.cc:104
unsigned int val2buf(void *val, bool reuse=false) override
Definition Str.cc:202
virtual bool set_value(const string &value)
Definition Str.cc:219
void print_val(FILE *out, string space="", bool print_decl_p=true) override
Prints the value of the variable.
Definition Str.cc:230
string d_buf
Definition Str.h:63
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:243
abstract base class used to unmarshall/deserialize dap data objects
virtual void get_str(string &val)=0
Holds an Internet address (URL).
Definition Url.h:55
#define DBG(x)
Definition debug.h:58
top level DAP object to house generic methods
Definition AISConnect.cc:30
@ dods_uint32_c
Definition Type.h:100
@ dods_int16_c
Definition Type.h:97
@ dods_byte_c
Definition Type.h:96
@ dods_int32_c
Definition Type.h:99
@ dods_url_c
Definition Type.h:104
@ dods_int8_c
Definition Type.h:115
@ dods_float32_c
Definition Type.h:101
@ dods_int64_c
Definition Type.h:118
@ dods_uint64_c
Definition Type.h:119
@ dods_uint16_c
Definition Type.h:98
@ dods_float64_c
Definition Type.h:102
@ dods_str_c
Definition Type.h:103
bool StrCmp(int op, T1 v1, T2 v2)
Definition Operators.h:166
string escattr(string s)
Definition escaping.cc:342