libdap  Updated for version 3.20.6
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 #include "Int16.h"
43 #include "UInt16.h"
44 #include "Int32.h"
45 #include "UInt32.h"
46 #include "Float32.h"
47 #include "Float64.h"
48 #include "Str.h"
49 #include "Url.h"
50 #include "Array.h"
51 #include "Structure.h"
52 #include "Sequence.h"
53 #include "Grid.h"
54 
55 #include "DDS.h"
56 #include "Marshaller.h"
57 #include "UnMarshaller.h"
58 
59 #include "DMR.h"
60 #include "D4StreamMarshaller.h"
61 #include "D4StreamUnMarshaller.h"
62 
63 #include "util.h"
64 #include "parser.h"
65 #include "Operators.h"
66 #include "InternalErr.h"
67 #include "escaping.h"
68 #include "debug.h"
69 #include "DapIndent.h"
70 
71 using std::cerr;
72 using std::endl;
73 
74 namespace libdap {
75 
84 Str::Str(const string &n) : BaseType(n, dods_str_c), d_buf("")
85 {}
86 
94 Str::Str(const string &n, const string &d) : BaseType(n, d, dods_str_c), d_buf("")
95 {}
96 
97 Str::Str(const Str &copy_from) : BaseType(copy_from)
98 {
99  d_buf = copy_from.d_buf;
100 }
101 
102 BaseType *
104 {
105  return new Str(*this);
106 }
107 
108 Str &
109 Str::operator=(const Str &rhs)
110 {
111  if (this == &rhs)
112  return *this;
113 
114  // Call BaseType::operator=.
115  dynamic_cast<BaseType &>(*this) = rhs;
116 
117  d_buf = rhs.d_buf;
118 
119  return *this;
120 }
121 
122 int
123 Str::length() const
124 {
125  return d_buf.length();
126 }
127 
128 unsigned int
129 Str::width(bool) const
130 {
131  return sizeof(string);
132 }
133 
134 bool
135 Str::serialize(ConstraintEvaluator &eval, DDS &dds, Marshaller &m, bool ce_eval)
136 {
137 
138  DBG(cerr << "Entering (" << this->name() << " [" << this << "])" << endl);
139 #if USE_LOCAL_TIMEOUT_SCHEME
140  dds.timeout_on();
141 #endif
142  if (!read_p())
143  read();
144 
145  if (ce_eval && !eval.eval_selection(dds, dataset()))
146  return true;
147 #if USE_LOCAL_TIMEOUT_SCHEME
148  dds.timeout_off();
149 #endif
150  m.put_str( d_buf ) ;
151 
152  DBG(cerr << "Exiting: buf = " << d_buf << endl);
153 
154  return true;
155 }
156 
157 // deserialize the string on stdin and put the result in BUF.
158 
159 bool
161 {
162  um.get_str( d_buf ) ;
163 
164  return false;
165 }
166 
167 void
169 {
170  checksum.AddData(reinterpret_cast<const uint8_t*>(d_buf.data()), d_buf.length());
171 }
172 
181 void
182 Str::serialize(D4StreamMarshaller &m, DMR &, /*ConstraintEvaluator &,*/ bool)
183 {
184  if (!read_p())
185  read(); // read() throws Error
186 
187  m.put_str( d_buf ) ;
188 }
189 
190 void
192 {
193  um.get_str( d_buf ) ;
194 }
195 
205 unsigned int
206 Str::buf2val(void **val)
207 {
208  // Jose Garcia
209  // The same comment justifying throwing an Error in val2buf applies here.
210  if (!val)
211  throw InternalErr(__FILE__, __LINE__,
212  "No place to store a reference to the data.");
213  // If *val is null, then the caller has not allocated storage for the
214  // value; we must. If there is storage there, assume it is a string and
215  // assign d_buf's value to that storage.
216  if (!*val)
217  *val = new string(d_buf);
218  else
219  *static_cast<string*>(*val) = d_buf;
220 
221  return sizeof(string*);
222 }
223 
233 unsigned int
234 Str::val2buf(void *val, bool)
235 {
236  // Jose Garcia
237  // This method is public therefore and I believe it has being designed
238  // to be use by read which must be implemented on the surrogated library,
239  // thus if the pointer val is NULL, is an Internal Error.
240  if (!val)
241  throw InternalErr(__FILE__, __LINE__, "NULL pointer.");
242 
243  d_buf = *static_cast<string*>(val);
244 
245  return sizeof(string*);
246 }
247 
252 bool
253 Str::set_value(const string &value)
254 {
255  d_buf = value;
256  set_read_p(true);
257 
258  return true;
259 }
260 
263 string
264 Str::value() const
265 {
266  return d_buf;
267 }
268 
269 void
270 Str::print_val(FILE *out, string space, bool print_decl_p)
271 {
272  ostringstream oss;
273  print_val(oss, space, print_decl_p);
274  fwrite(oss.str().data(), sizeof(char), oss.str().length(), out);
275 }
276 
277 void
278 Str::print_val(ostream &out, string space, bool print_decl_p)
279 {
280  if (print_decl_p) {
281  print_decl(out, space, false);
282  out << " = \"" << escattr(d_buf) << "\";\n" ;
283  }
284  else
285  out << "\"" << escattr(d_buf) << "\"" ;
286 }
287 
288 bool
289 Str::ops(BaseType *b, int op)
290 {
291  // Extract the Byte arg's value.
292  if (!read_p() && !read()) {
293  // Jose Garcia
294  // Since the read method is virtual and implemented outside
295  // libdap++ if we cannot read the data that is the problem
296  // of the user or of whoever wrote the surrogate library
297  // implemeting read therefore it is an internal error.
298  throw InternalErr(__FILE__, __LINE__, "This value was not read!");
299  }
300 
301  // Extract the second arg's value.
302  if (!b || !(b->read_p() || b->read())) {
303  // Jose Garcia
304  // Since the read method is virtual and implemented outside
305  // libdap++ if we cannot read the data that is the problem
306  // of the user or of whoever wrote the surrogate library
307  // implemeting read therefore it is an internal error.
308  throw InternalErr(__FILE__, __LINE__, "Argument value was not read!");
309  }
310 
311  return d4_ops(b, op);
312 }
313 
317 bool Str::d4_ops(BaseType *b, int op)
318 {
319  switch (b->type()) {
320  case dods_byte_c:
321  case dods_int8_c:
322  case dods_int16_c:
323  case dods_uint16_c:
324  case dods_int32_c:
325  case dods_uint32_c:
326  case dods_int64_c:
327  case dods_uint64_c:
328  case dods_float32_c:
329  case dods_float64_c:
330  throw Error(malformed_expr, "Relational operators can only compare compatible types (string, number).");
331  case dods_str_c:
332  return StrCmp<string, string>(op, d_buf, static_cast<Str*>(b)->value());
333  case dods_url_c:
334  return StrCmp<string, string>(op, d_buf, static_cast<Url*>(b)->value());
335  default:
336  throw Error(malformed_expr, "Relational operators only work with scalar types.");
337  }
338 }
339 
348 void
349 Str::dump(ostream &strm) const
350 {
351  strm << DapIndent::LMarg << "Str::dump - ("
352  << (void *)this << ")" << endl ;
353  DapIndent::Indent() ;
354  BaseType::dump(strm) ;
355  strm << DapIndent::LMarg << "value: " << d_buf << endl ;
356  DapIndent::UnIndent() ;
357 }
358 
359 } // namespace libdap
360 
virtual bool read()
Read data into a local buffer.
Definition: BaseType.cc:899
Holds an Internet address (URL).
Definition: Url.h:68
virtual bool read_p()
Has this variable been read?
Definition: BaseType.cc:480
virtual string name() const
Returns the name of the class instance.
Definition: BaseType.cc:320
virtual void print_val(FILE *out, string space="", bool print_decl_p=true)
Prints the value of the variable.
Definition: Str.cc:270
abstract base class used to unmarshall/deserialize dap data objects
Definition: UnMarshaller.h:54
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:1003
virtual void dump(ostream &strm) const
dumps information about this object
Definition: BaseType.cc:291
virtual unsigned int val2buf(void *val, bool reuse=false)
Definition: Str.cc:234
virtual BaseType * ptr_duplicate()
Definition: Str.cc:103
Read data from the stream made by D4StreamMarshaller.
Definition: crc.h:76
BaseType(const string &n, const Type &t, bool is_dap4=false)
The BaseType constructor.
Definition: BaseType.cc:126
Str(const string &n)
Definition: Str.cc:84
virtual bool serialize(ConstraintEvaluator &eval, DDS &dds, Marshaller &m, bool ce_eval=true)
Move data to the net, then remove them from the object.
Definition: Str.cc:135
top level DAP object to house generic methods
Definition: AISConnect.cc:30
A class for software fault reporting.
Definition: InternalErr.h:64
Holds character string data.
Definition: Str.h:62
virtual bool deserialize(UnMarshaller &um, DDS *dds, bool reuse=false)
Receive data from the net.
Definition: Str.cc:160
virtual void dump(ostream &strm) const
dumps information about this object
Definition: Str.cc:349
Marshaller that knows how to marshal/serialize dap data objects to a C++ iostream using DAP4&#39;s receiv...
virtual Type type() const
Returns the type of the class instance.
Definition: BaseType.cc:365
virtual void set_read_p(bool state)
Sets the value of the read_p property.
Definition: BaseType.cc:516
virtual bool ops(BaseType *b, int op)
Evaluate relational operators.
Definition: Str.cc:289
virtual bool set_value(const string &value)
Definition: Str.cc:253
void AddData(const uint8_t *pData, const uint32_t length)
Definition: crc.h:98
virtual void compute_checksum(Crc32 &checksum)
include the data for this variable in the checksum DAP4 includes a checksum with every data response...
Definition: Str.cc:168
virtual bool d4_ops(BaseType *b, int op)
Definition: Str.cc:317
Evaluate a constraint expression.
The basic data type for the DODS DAP types.
Definition: BaseType.h:117
abstract base class used to marshal/serialize dap data objects
Definition: Marshaller.h:50
virtual string value() const
Definition: Str.cc:264
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 ...
virtual unsigned int width(bool constrained=false) const
How many bytes does this variable use Return the number of bytes of storage this variable uses...
Definition: Str.cc:129
A class for error processing.
Definition: Error.h:92
virtual int length() const
How many elements are in this variable.
Definition: Str.cc:123
virtual unsigned int buf2val(void **val)
Definition: Str.cc:206
virtual string dataset() const
Returns the name of the dataset used to create this instance.
Definition: BaseType.cc:358
string escattr(string s)
Definition: escaping.cc:368