Str.cc
// -*- mode: c++; c-basic-offset:4 -*-
// This file is part of libdap, A C++ implementation of the OPeNDAP Data
// Access Protocol.
// Copyright (c) 2002,2003 OPeNDAP, Inc.
// Author: James Gallagher <jgallagher@opendap.org>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
// (c) COPYRIGHT URI/MIT 1994-1999
// Please read the full copyright statement in the file COPYRIGHT_URI.
//
// Authors:
// jhrg,jimg James Gallagher <jgallagher@gso.uri.edu>
// Implementation for Str.
//
// jhrg 9/7/94
#include "config.h"
#include <sstream>
#include "Byte.h"
#if 0
#include "Int16.h"
#include "UInt16.h"
#include "Int32.h"
#include "UInt32.h"
#include "Float32.h"
#include "Float64.h"
#endif
#include "Str.h"
#include "Url.h"
#if 0
#include "Array.h"
#include "Structure.h"
#include "Sequence.h"
#include "Grid.h"
#endif
#include "DDS.h"
#include "Marshaller.h"
#include "UnMarshaller.h"
#include "DMR.h"
#include "D4StreamMarshaller.h"
#include "D4StreamUnMarshaller.h"
#include "util.h"
#if 0
#include "parser.h"
#endif
#include "Operators.h"
#include "InternalErr.h"
#include "escaping.h"
#include "debug.h"
#include "DapIndent.h"
using std::cerr;
using std::endl;
namespace libdap {
Str::Str(const string &n) : BaseType(n, dods_str_c), d_buf("")
{}
Str::Str(const string &n, const string &d) : BaseType(n, d, dods_str_c), d_buf("")
{}
Str::Str(const Str &copy_from) : BaseType(copy_from)
{
d_buf = copy_from.d_buf;
}
BaseType *
Str::ptr_duplicate()
{
return new Str(*this);
}
Str &
Str::operator=(const Str &rhs)
{
if (this == &rhs)
return *this;
BaseType::operator=(rhs);
d_buf = rhs.d_buf;
return *this;
}
int
Str::length() const
{
return (int)d_buf.length();
}
int64_t
Str::length_ll() const
{
return (int64_t)d_buf.length();
}
bool
Str::serialize(ConstraintEvaluator &eval, DDS &dds, Marshaller &m, bool ce_eval)
{
DBG(cerr << "Entering (" << this->name() << " [" << this << "])" << endl);
#if USE_LOCAL_TIMEOUT_SCHEME
dds.timeout_on();
#endif
if (!read_p())
read();
if (ce_eval && !eval.eval_selection(dds, dataset()))
return true;
#if USE_LOCAL_TIMEOUT_SCHEME
dds.timeout_off();
#endif
m.put_str( d_buf ) ;
DBG(cerr << "Exiting: buf = " << d_buf << endl);
return true;
}
// deserialize the string on stdin and put the result in BUF.
bool
Str::deserialize(UnMarshaller &um, DDS *, bool)
{
um.get_str( d_buf ) ;
return false;
}
void
Str::compute_checksum(Crc32 &checksum)
{
checksum.AddData(reinterpret_cast<const uint8_t*>(d_buf.data()), d_buf.length());
}
void
Str::serialize(D4StreamMarshaller &m, DMR &, /*ConstraintEvaluator &,*/ bool)
{
if (!read_p())
read(); // read() throws Error
m.put_str( d_buf ) ;
}
void
Str::deserialize(D4StreamUnMarshaller &um, DMR &)
{
um.get_str( d_buf ) ;
}
unsigned int
Str::buf2val(void **val)
{
// Jose Garcia
// The same comment justifying throwing an Error in val2buf applies here.
if (!val)
throw InternalErr(__FILE__, __LINE__,
"No place to store a reference to the data.");
// If *val is null, then the caller has not allocated storage for the
// value; we must. If there is storage there, assume it is a string and
// assign d_buf's value to that storage.
if (!*val)
*val = new string(d_buf);
else
*static_cast<string*>(*val) = d_buf;
return sizeof(string*);
}
unsigned int
Str::val2buf(void *val, bool)
{
// Jose Garcia
// This method is public therefore and I believe it has being designed
// to be use by read which must be implemented on the surrogated library,
// thus if the pointer val is NULL, is an Internal Error.
if (!val)
throw InternalErr(__FILE__, __LINE__, "NULL pointer.");
d_buf = *static_cast<string*>(val);
return sizeof(string*);
}
bool
Str::set_value(const string &value)
{
d_buf = value;
set_read_p(true);
return true;
}
string
Str::value() const
{
return d_buf;
}
void
Str::print_val(FILE *out, string space, bool print_decl_p)
{
ostringstream oss;
print_val(oss, space, print_decl_p);
fwrite(oss.str().data(), sizeof(char), oss.str().length(), out);
}
string
Str::esc_string_variable_value(const string &s)
{
return escattr(s);
}
void
Str::print_val(ostream &out, string space, bool print_decl_p)
{
if (print_decl_p) {
print_decl(out, space, false);
out << " = \"" << esc_string_variable_value(d_buf) << "\";" << endl;
}
else {
out << "\"" << esc_string_variable_value(d_buf) << "\"";
}
}
bool
Str::ops(BaseType *b, int op)
{
// Extract the Byte arg's value.
if (!read_p() && !read()) {
// Jose Garcia
// Since the read method is virtual and implemented outside
// libdap++ if we cannot read the data that is the problem
// of the user or of whoever wrote the surrogate library
// implementing read therefore it is an internal error.
throw InternalErr(__FILE__, __LINE__, "This value was not read!");
}
// Extract the second arg's value.
if (!b || !(b->read_p() || b->read())) {
// Jose Garcia
// Since the read method is virtual and implemented outside
// libdap++ if we cannot read the data that is the problem
// of the user or of whoever wrote the surrogate library
// implementing read therefore it is an internal error.
throw InternalErr(__FILE__, __LINE__, "Argument value was not read!");
}
return d4_ops(b, op);
}
bool Str::d4_ops(BaseType *b, int op)
{
switch (b->type()) {
case dods_byte_c:
case dods_int8_c:
case dods_int16_c:
case dods_uint16_c:
case dods_int32_c:
case dods_uint32_c:
case dods_int64_c:
case dods_uint64_c:
case dods_float32_c:
case dods_float64_c:
throw Error(malformed_expr, "Relational operators can only compare compatible types (string, number).");
case dods_str_c:
return StrCmp<string, string>(op, d_buf, static_cast<Str*>(b)->value());
case dods_url_c:
return StrCmp<string, string>(op, d_buf, static_cast<Url*>(b)->value());
default:
throw Error(malformed_expr, "Relational operators only work with scalar types.");
}
}
void
Str::dump(ostream &strm) const
{
strm << DapIndent::LMarg << "Str::dump - ("
<< (void *)this << ")" << endl ;
DapIndent::Indent() ;
BaseType::dump(strm) ;
strm << DapIndent::LMarg << "value: " << d_buf << endl ;
DapIndent::UnIndent() ;
}
} // namespace libdap
Crc32
Definition crc.h:79
Crc32::AddData
void AddData(const uint8_t *pData, const uint32_t length)
Definition crc.h:100
libdap::BaseType
The basic data type for the DODS DAP types.
Definition BaseType.h:120
libdap::BaseType::read
virtual bool read()
Read data into a local buffer.
Definition BaseType.cc:905
libdap::BaseType::name
virtual string name() const
Returns the name of the class instance.
Definition BaseType.cc:317
libdap::BaseType::print_decl
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
libdap::BaseType::read_p
virtual bool read_p()
Has this variable been read?
Definition BaseType.cc:477
libdap::BaseType::set_read_p
virtual void set_read_p(bool state)
Sets the value of the read_p property.
Definition BaseType.cc:513
libdap::BaseType::dataset
virtual string dataset() const
Returns the name of the dataset used to create this instance.
Definition BaseType.cc:355
libdap::BaseType::dump
void dump(ostream &strm) const override
dumps information about this object
Definition BaseType.cc:287
libdap::BaseType::type
virtual Type type() const
Returns the type of the class instance.
Definition BaseType.cc:362
libdap::ConstraintEvaluator
Evaluate a constraint expression.
Definition ConstraintEvaluator.h:43
libdap::ConstraintEvaluator::eval_selection
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 ...
Definition ConstraintEvaluator.cc:329
libdap::D4StreamMarshaller
Marshaller that knows how to marshal/serialize dap data objects to a C++ iostream using DAP4's receiv...
Definition D4StreamMarshaller.h:71
libdap::D4StreamUnMarshaller
Read data from the stream made by D4StreamMarshaller.
Definition D4StreamUnMarshaller.h:65
libdap::DDS
Definition DDS.h:178
libdap::DMR
Definition DMR.h:58
libdap::Error
A class for error processing.
Definition Error.h:94
libdap::InternalErr
A class for software fault reporting.
Definition InternalErr.h:65
libdap::Marshaller
abstract base class used to marshal/serialize dap data objects
Definition Marshaller.h:50
libdap::Str
Holds character string data.
Definition Str.h:62
libdap::Str::buf2val
unsigned int buf2val(void **val) override
Definition Str.cc:210
libdap::Str::compute_checksum
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
libdap::Str::length_ll
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
libdap::Str::Str
Str(const string &n)
Definition Str.cc:92
libdap::Str::d4_ops
bool d4_ops(BaseType *b, int op) override
Definition Str.cc:335
libdap::Str::length
int length() const override
How many elements are in this variable? Uses -1 in places.
Definition Str.cc:127
libdap::Str::serialize
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
libdap::Str::deserialize
bool deserialize(UnMarshaller &um, DDS *dds, bool reuse=false) override
Receive data from the net.
Definition Str.cc:164
libdap::Str::value
virtual string value() const
Definition Str.cc:268
libdap::Str::dump
void dump(ostream &strm) const override
dumps information about this object
Definition Str.cc:367
libdap::Str::ops
bool ops(BaseType *b, int op) override
Evaluate relational operators.
Definition Str.cc:307
libdap::Str::ptr_duplicate
BaseType * ptr_duplicate() override
Definition Str.cc:111
libdap::Str::val2buf
unsigned int val2buf(void *val, bool reuse=false) override
Definition Str.cc:238
libdap::Str::set_value
virtual bool set_value(const string &value)
Definition Str.cc:257
libdap::Str::print_val
void print_val(FILE *out, string space="", bool print_decl_p=true) override
Prints the value of the variable.
Definition Str.cc:274
libdap::Str::esc_string_variable_value
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
libdap::UnMarshaller
abstract base class used to unmarshall/deserialize dap data objects
Definition UnMarshaller.h:55
libdap::Url
Holds an Internet address (URL).
Definition Url.h:56
libdap
top level DAP object to house generic methods
Definition AlarmHandler.h:36
libdap::escattr
string escattr(string s)
Definition escaping.cc:368