libdap++  Updated for version 3.11.7
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 static char rcsid[] not_used =
40  {"$Id: Str.cc 21699 2009-11-05 00:06:01Z jimg $"
41  };
42 
43 #include "Byte.h"
44 #include "Int16.h"
45 #include "UInt16.h"
46 #include "Int32.h"
47 #include "UInt32.h"
48 #include "Float32.h"
49 #include "Float64.h"
50 #include "Str.h"
51 #include "Url.h"
52 #include "Array.h"
53 #include "Structure.h"
54 #include "Sequence.h"
55 #include "Grid.h"
56 
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), _buf("")
81 {}
82 
90 Str::Str(const string &n, const string &d)
91  : BaseType(n, d, dods_str_c), _buf("")
92 {}
93 
94 Str::Str(const Str &copy_from) : BaseType(copy_from)
95 {
96  _buf = copy_from._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  _buf = rhs._buf;
115 
116  return *this;
117 }
118 
119 unsigned int
121 {
122  return _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( _buf ) ;
151 
152  DBG(cerr << "Exiting: buf = " << _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( _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 _buf's value to that storage.
187  if (!*val)
188  *val = new string(_buf);
189  else
190  *static_cast<string*>(*val) = _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  _buf = *static_cast<string*>(val);
215 
216  return sizeof(string*);
217 }
218 
223 bool
224 Str::set_value(const string &value)
225 {
226  _buf = value;
227  set_read_p(true);
228 
229  return true;
230 }
231 
234 string
235 Str::value() const
236 {
237  return _buf;
238 }
239 
240 #if FILE_METHODS
241 void
242 Str::print_val(FILE *out, string space, bool print_decl_p)
243 {
244  if (print_decl_p) {
245  print_decl(out, space, false);
246  fprintf(out, " = \"%s\";\n", escattr(_buf).c_str()) ;
247  }
248  else
249  fprintf(out, "\"%s\"", escattr(_buf).c_str()) ;
250 }
251 #endif
252 
253 void
254 Str::print_val(ostream &out, string space, bool print_decl_p)
255 {
256  if (print_decl_p) {
257  print_decl(out, space, false);
258  out << " = \"" << escattr(_buf) << "\";\n" ;
259  }
260  else
261  out << "\"" << escattr(_buf) << "\"" ;
262 }
263 
264 bool
265 Str::ops(BaseType *b, int op)
266 {
267  // Extract the Byte arg's value.
268  if (!read_p() && !read()) {
269  // Jose Garcia
270  // Since the read method is virtual and implemented outside
271  // libdap++ if we cannot read the data that is the problem
272  // of the user or of whoever wrote the surrogate library
273  // implemeting read therefore it is an internal error.
274  throw InternalErr(__FILE__, __LINE__, "This value was not read!");
275  }
276 
277  // Extract the second arg's value.
278  if (!b || !(b->read_p() || b->read())) {
279  // Jose Garcia
280  // Since the read method is virtual and implemented outside
281  // libdap++ if we cannot read the data that is the problem
282  // of the user or of whoever wrote the surrogate library
283  // implemeting read therefore it is an internal error.
284  throw InternalErr(__FILE__, __LINE__, "Argument value was not read!");
285  }
286 
287  switch (b->type()) {
288  case dods_str_c:
289  return rops<string, string, StrCmp<string, string> >
290  (_buf, dynamic_cast<Str *>(b)->_buf, op);
291  case dods_url_c:
292  return rops<string, string, StrCmp<string, string> >
293  (_buf, dynamic_cast<Url *>(b)->_buf, op);
294  default:
295  return false;
296  }
297 }
298 
307 void
308 Str::dump(ostream &strm) const
309 {
310  strm << DapIndent::LMarg << "Str::dump - ("
311  << (void *)this << ")" << endl ;
313  BaseType::dump(strm) ;
314  strm << DapIndent::LMarg << "value: " << _buf << endl ;
316 }
317 
318 } // namespace libdap
319