libdap++  Updated for version 3.13.3
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 
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 "Marshaller.h"
56 #include "UnMarshaller.h"
57 
58 #include "DDS.h"
59 #include "util.h"
60 #include "parser.h"
61 #include "Operators.h"
62 #include "InternalErr.h"
63 #include "escaping.h"
64 #include "debug.h"
65 
66 
67 using std::cerr;
68 using std::endl;
69 
70 namespace libdap {
71 
80 Str::Str(const string &n) : BaseType(n, dods_str_c), d_buf("")
81 {}
82 
90 Str::Str(const string &n, const string &d)
91  : BaseType(n, d, dods_str_c), d_buf("")
92 {}
93 
94 Str::Str(const Str &copy_from) : BaseType(copy_from)
95 {
96  d_buf = copy_from.d_buf;
97 }
98 
99 BaseType *
101 {
102  return new Str(*this);
103 }
104 
105 Str &
106 Str::operator=(const Str &rhs)
107 {
108  if (this == &rhs)
109  return *this;
110 
111  // Call BaseType::operator=.
112  dynamic_cast<BaseType &>(*this) = rhs;
113 
114  d_buf = rhs.d_buf;
115 
116  return *this;
117 }
118 
119 unsigned int
121 {
122  return d_buf.length();
123 }
124 
125 unsigned int
127 {
128  return sizeof(string);
129 }
130 
131 bool
133  Marshaller &m, bool ce_eval)
134 {
135 
136  DBG(cerr << "Entering (" << this->name() << " [" << this << "])" << endl);
137 
138  dds.timeout_on();
139 
140  if (!read_p())
141  read();
142 
143 #if EVAL
144  if (ce_eval && !eval.eval_selection(dds, dataset()))
145  return true;
146 #endif
147 
148  dds.timeout_off();
149 
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 
176 unsigned int
177 Str::buf2val(void **val)
178 {
179  // Jose Garcia
180  // The same comment justifying throwing an Error in val2buf applies here.
181  if (!val)
182  throw InternalErr(__FILE__, __LINE__,
183  "No place to store a reference to the data.");
184  // If *val is null, then the caller has not allocated storage for the
185  // value; we must. If there is storage there, assume it is a string and
186  // assign d_buf's value to that storage.
187  if (!*val)
188  *val = new string(d_buf);
189  else
190  *static_cast<string*>(*val) = d_buf;
191 
192  return sizeof(string*);
193 }
194 
204 unsigned int
205 Str::val2buf(void *val, bool)
206 {
207  // Jose Garcia
208  // This method is public therefore and I believe it has being designed
209  // to be use by read which must be implemented on the surrogated library,
210  // thus if the pointer val is NULL, is an Internal Error.
211  if (!val)
212  throw InternalErr(__FILE__, __LINE__, "NULL pointer.");
213 
214  d_buf = *static_cast<string*>(val);
215 
216  return sizeof(string*);
217 }
218 
223 bool
224 Str::set_value(const string &value)
225 {
226  d_buf = value;
227  set_read_p(true);
228 
229  return true;
230 }
231 
234 string
235 Str::value() const
236 {
237  return d_buf;
238 }
239 
240 void
241 Str::print_val(FILE *out, string space, bool print_decl_p)
242 {
243  ostringstream oss;
244  print_val(oss, space, print_decl_p);
245  fwrite(oss.str().data(), sizeof(char), oss.str().length(), out);
246 }
247 
248 void
249 Str::print_val(ostream &out, string space, bool print_decl_p)
250 {
251  if (print_decl_p) {
252  print_decl(out, space, false);
253  out << " = \"" << escattr(d_buf) << "\";\n" ;
254  }
255  else
256  out << "\"" << escattr(d_buf) << "\"" ;
257 }
258 
259 bool
260 Str::ops(BaseType *b, int op)
261 {
262  // Extract the Byte arg's value.
263  if (!read_p() && !read()) {
264  // Jose Garcia
265  // Since the read method is virtual and implemented outside
266  // libdap++ if we cannot read the data that is the problem
267  // of the user or of whoever wrote the surrogate library
268  // implemeting read therefore it is an internal error.
269  throw InternalErr(__FILE__, __LINE__, "This value was not read!");
270  }
271 
272  // Extract the second arg's value.
273  if (!b || !(b->read_p() || b->read())) {
274  // Jose Garcia
275  // Since the read method is virtual and implemented outside
276  // libdap++ if we cannot read the data that is the problem
277  // of the user or of whoever wrote the surrogate library
278  // implemeting read therefore it is an internal error.
279  throw InternalErr(__FILE__, __LINE__, "Argument value was not read!");
280  }
281 
282  switch (b->type()) {
283  case dods_str_c:
284  return StrCmp<string, string>(op, d_buf, static_cast<Str*>(b)->value());
285  case dods_url_c:
286  return StrCmp<string, string>(op, d_buf, static_cast<Url*>(b)->value());
287  default:
288  return false;
289  }
290 }
291 
300 void
301 Str::dump(ostream &strm) const
302 {
303  strm << DapIndent::LMarg << "Str::dump - ("
304  << (void *)this << ")" << endl ;
306  BaseType::dump(strm) ;
307  strm << DapIndent::LMarg << "value: " << d_buf << endl ;
309 }
310 
311 } // namespace libdap
312 
virtual bool read()
Read data into a local buffer.
Definition: BaseType.cc:923
Holds an Internet address (URL).
Definition: Url.h:63
virtual bool read_p()
Has this variable been read?
Definition: BaseType.cc:579
virtual void print_val(FILE *out, string space="", bool print_decl_p=true)
Prints the value of the variable.
Definition: Str.cc:241
static void UnIndent()
Definition: DapIndent.cc:51
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:985
virtual unsigned int val2buf(void *val, bool reuse=false)
Definition: Str.cc:205
virtual void put_str(const string &val)=0
virtual unsigned int width(bool constrained=false)
Definition: Str.cc:126
virtual BaseType * ptr_duplicate()
Definition: Str.cc:100
Str(const string &n)
Definition: Str.cc:80
void timeout_off()
Definition: DDS.cc:862
Str & operator=(const Str &rhs)
Definition: Str.cc:106
Type type() const
Returns the type of the class instance.
Definition: BaseType.cc:282
virtual bool serialize(ConstraintEvaluator &eval, DDS &dds, Marshaller &m, bool ce_eval=true)
Move data to the net.
Definition: Str.cc:132
string d_buf
Definition: Str.h:66
A class for software fault reporting.
Definition: InternalErr.h:64
string dataset() const
Returns the name of the dataset used to create this instance.
Definition: BaseType.cc:275
bool eval_selection(DDS &dds, const string &dataset)
Evaluate a boolean-valued constraint expression. This is main method for the evaluator ans is called ...
virtual void get_str(string &val)=0
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
#define DBG(x)
Definition: debug.h:58
virtual string value() const
Definition: Str.cc:235
static void Indent()
Definition: DapIndent.cc:45
virtual void dump(ostream &strm) const
dumps information about this object
Definition: BaseType.cc:230
virtual void set_read_p(bool state)
Sets the value of the read_p property.
Definition: BaseType.cc:618
virtual bool ops(BaseType *b, int op)
Evaluate relational operators.
Definition: Str.cc:260
virtual bool set_value(const string &value)
Definition: Str.cc:224
string name() const
Returns the name of the class instance.
Definition: BaseType.cc:254
void timeout_on()
Definition: DDS.cc:854
Evaluate a constraint expression.
static ostream & LMarg(ostream &strm)
Definition: DapIndent.cc:80
The basic data type for the DODS DAP types.
Definition: BaseType.h:199
abstract base class used to marshal/serialize dap data objects
Definition: Marshaller.h:53
virtual unsigned int buf2val(void **val)
Definition: Str.cc:177
virtual void dump(ostream &strm) const
dumps information about this object
Definition: Str.cc:301
unsigned int length()
Definition: Str.cc:120
string escattr(string s)
Definition: escaping.cc:368