Byte.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 Byte.
//
// jhrg 9/7/94
//#define DODS_DEBUG
#include "config.h"
#include <sstream>
#include "Byte.h" // synonymous with UInt8 and Char
#include "Int8.h"
#include "Int16.h"
#include "UInt16.h"
#include "Int32.h"
#include "UInt32.h"
#include "Int64.h"
#include "UInt64.h"
#include "Float32.h"
#include "Float64.h"
#include "Str.h"
#include "Url.h"
#include "DDS.h"
#include "Operators.h"
#include "Marshaller.h"
#include "UnMarshaller.h"
#include "DMR.h"
#include "D4Attributes.h"
#include "D4StreamMarshaller.h"
#include "D4StreamUnMarshaller.h"
#include "debug.h"
#include "util.h"
#include "parser.h"
#include "dods-limits.h"
#include "InternalErr.h"
using std::cerr;
using std::endl;
namespace libdap {
Byte::Byte(const string & n): BaseType(n, dods_byte_c), d_buf(0)
{}
Byte::Byte(const string &n, const string &d): BaseType(n, d, dods_byte_c), d_buf(0)
{}
Byte::Byte(const Byte & copy_from): BaseType(copy_from)
{
d_buf = copy_from.d_buf;
}
BaseType *Byte::ptr_duplicate()
{
return new Byte(*this);
}
Byte & Byte::operator=(const Byte & rhs)
{
if (this == &rhs)
return *this;
dynamic_cast < BaseType & >(*this) = rhs;
d_buf = rhs.d_buf;
return *this;
}
unsigned int Byte::width(bool) const
{
return sizeof(dods_byte);
}
bool Byte::serialize(ConstraintEvaluator & eval, DDS & dds, Marshaller &m, bool ce_eval)
{
#if USE_LOCAL_TIMEOUT_SCHEME
dds.timeout_on();
#endif
if (!read_p())
read(); // read() throws Error and InternalErr
if (ce_eval && !eval.eval_selection(dds, dataset()))
return true;
#if USE_LOCAL_TIMEOUT_SCHEME
dds.timeout_off();
#endif
m.put_byte( d_buf ) ;
return true;
}
bool Byte::deserialize(UnMarshaller &um, DDS *, bool)
{
um.get_byte( d_buf ) ;
return false;
}
void
Byte::compute_checksum(Crc32 &checksum)
{
checksum.AddData(reinterpret_cast<uint8_t*>(&d_buf), sizeof(d_buf));
}
void
Byte::serialize(D4StreamMarshaller &m, DMR &, /*ConstraintEvaluator &,*/ bool)
{
if (!read_p())
read(); // read() throws Error
m.put_byte( d_buf ) ;
}
void
Byte::deserialize(D4StreamUnMarshaller &um, DMR &)
{
um.get_byte( d_buf ) ;
}
unsigned int Byte::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 surrogate library,
// thus if the pointer val is NULL, is an Internal Error.
if (!val)
throw InternalErr("the incoming pointer does not contain any data.");
d_buf = *(dods_byte *) val;
return width();
}
unsigned int Byte::buf2val(void **val)
{
// Jose Garcia
// The same comment justifying throwing an Error in val2buf applies here.
if (!val)
throw InternalErr("NULL pointer");
if (!*val)
*val = new dods_byte;
*(dods_byte *) * val = d_buf;
return width();
}
bool Byte::set_value(dods_byte value)
{
d_buf = value;
set_read_p(true);
return true;
}
dods_byte Byte::value() const
{
return d_buf;
}
void Byte::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);
}
void Byte::print_val(ostream &out, string space, bool print_decl_p)
{
if (print_decl_p) {
print_decl(out, space, false);
out << " = " << (int) d_buf << ";\n";
}
else
out << (int) d_buf;
}
bool Byte::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("This value 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("This value not read!");
}
// By using the same operator code numbers for both the DAP2 and DAP4
// parser/evaluator we can use the same evaluation code.
return d4_ops(b, op);
}
bool Byte::d4_ops(BaseType *b, int op)
{
switch (b->type()) {
case dods_int8_c:
return USCmp<dods_byte, dods_int8>(op, d_buf, static_cast<Int8*>(b)->value());
case dods_byte_c:
return Cmp<dods_byte, dods_byte>(op, d_buf, static_cast<Byte*>(b)->value());
case dods_int16_c:
return USCmp<dods_byte, dods_int16>(op, d_buf, static_cast<Int16*>(b)->value());
case dods_uint16_c:
return Cmp<dods_byte, dods_uint16>(op, d_buf, static_cast<UInt16*>(b)->value());
case dods_int32_c:
return USCmp<dods_byte, dods_int32>(op, d_buf, static_cast<Int32*>(b)->value());
case dods_uint32_c:
return Cmp<dods_byte, dods_uint32>(op, d_buf, static_cast<UInt32*>(b)->value());
case dods_int64_c:
return USCmp<dods_byte, dods_int64>(op, d_buf, static_cast<Int64*>(b)->value());
case dods_uint64_c:
return Cmp<dods_byte, dods_uint64>(op, d_buf, static_cast<UInt64*>(b)->value());
case dods_float32_c:
return USCmp<dods_byte, dods_float32>(op, d_buf, static_cast<Float32*>(b)->value());
case dods_float64_c:
return USCmp<dods_byte, dods_float64>(op, d_buf, static_cast<Float64*>(b)->value());
case dods_str_c:
case dods_url_c:
throw Error(malformed_expr, "Relational operators can only compare compatible types (number, string).");
default:
throw Error(malformed_expr, "Relational operators only work with scalar types.");
}
}
std::vector<BaseType *> *
Byte::transform_to_dap2(AttrTable *parent_attr_table)
{
DBG(cerr << __func__ << "() - BEGIN" << endl;);
vector<BaseType *> *vec = BaseType::transform_to_dap2(parent_attr_table);
if(vec->size()!=1){
ostringstream oss;
oss << __func__ << "() - Something Bad Happened. This transform should produce only ";
oss << " a single BaseType yet it produced " << vec->size();
throw new Error(internal_error,oss.str());
}
BaseType *dest = (*vec)[0];
DBG(cerr << __func__ << "() - type(): " << type() << endl;);
DBG(cerr << __func__ << "() - dest->type(): " << dest->type() << endl;);
// This little bit of magic ensures that the DAP4 shenanigans
// in which UInt8, Char , and Byte are synonymous is reduced
// to the DAP2 simplicity of Byte.
if(type()!=dods_byte_c){
dest->set_type(dods_byte_c);
}
DBG (dest->get_attr_table().print(cerr););
DBG(cerr << __func__ << "() - END" << endl;);
return vec;
}
void Byte::dump(ostream & strm) const
{
strm << DapIndent::LMarg << "Byte::dump - ("
<< (void *) this << ")" << endl;
DapIndent::Indent();
BaseType::dump(strm);
strm << DapIndent::LMarg << "value: " << d_buf << endl;
DapIndent::UnIndent();
}
} // namespace libdap
libdap::BaseType::read
virtual bool read()
Read data into a local buffer.
Definition: BaseType.cc:890
libdap::Int8
Holds an 8-bit signed integer value.
Definition: Int8.h:42
libdap::Int64
Holds a64-bit signed integer.
Definition: Int64.h:49
libdap::BaseType::read_p
virtual bool read_p()
Has this variable been read?
Definition: BaseType.cc:471
libdap::UnMarshaller
abstract base class used to unmarshall/deserialize dap data objects
Definition: UnMarshaller.h:54
libdap::Byte::transform_to_dap2
virtual std::vector< BaseType * > * transform_to_dap2(AttrTable *parent_attr_table)
DAP4 to DAP2 transform.
Definition: Byte.cc:345
libdap::Byte::buf2val
virtual unsigned int buf2val(void **val)
Reads the class data.
Definition: Byte.cc:214
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:994
libdap::BaseType::dump
virtual void dump(ostream &strm) const
dumps information about this object
Definition: BaseType.cc:282
libdap::AttrTable
Contains the attributes for a dataset.
Definition: AttrTable.h:142
libdap::Byte::deserialize
virtual bool deserialize(UnMarshaller &um, DDS *, bool)
Deserialize the char on stdin and put the result in _BUF.
Definition: Byte.cc:159
libdap::Byte::Byte
Byte(const string &n)
The Byte constructor.
Definition: Byte.cc:85
libdap::Byte::serialize
virtual bool serialize(ConstraintEvaluator &eval, DDS &dds, Marshaller &m, bool ce_eval)
Definition: Byte.cc:138
libdap::D4StreamUnMarshaller
Read data from the stream made by D4StreamMarshaller.
Definition: D4StreamUnMarshaller.h:65
libdap::UInt16
Holds an unsigned 16-bit integer.
Definition: UInt16.h:57
Crc32
Definition: crc.h:76
libdap::BaseType::BaseType
BaseType(const string &n, const Type &t, bool is_dap4=false)
The BaseType constructor.
Definition: BaseType.cc:125
libdap::Byte::value
virtual dods_byte value() const
Definition: Byte.cc:243
libdap::Float32
Holds a 32-bit floating point value.
Definition: Float32.h:61
libdap
Definition: AlarmHandler.h:35
libdap::InternalErr
A class for software fault reporting.
Definition: InternalErr.h:64
libdap::Byte::val2buf
virtual unsigned int val2buf(void *val, bool reuse=false)
Definition: Byte.cc:200
libdap::DMR
Definition: DMR.h:55
libdap::D4StreamMarshaller
Marshaller that knows how to marshal/serialize dap data objects to a C++ iostream using DAP4&#39;s receiv...
Definition: D4StreamMarshaller.h:71
libdap::Int16
Holds a 16-bit signed integer value.
Definition: Int16.h:59
libdap::DDS
Definition: DDS.h:180
libdap::BaseType::type
virtual Type type() const
Returns the type of the class instance.
Definition: BaseType.cc:356
libdap::Byte::d4_ops
virtual bool d4_ops(BaseType *b, int op)
Definition: Byte.cc:295
libdap::BaseType::set_read_p
virtual void set_read_p(bool state)
Sets the value of the read_p property.
Definition: BaseType.cc:507
libdap::Byte::set_value
virtual bool set_value(const dods_byte value)
Definition: Byte.cc:233
libdap::Byte::width
virtual unsigned int width(bool constrained=false) const
How many bytes does this use Return the number of bytes of storage this variable uses. For scalar types, this is pretty simple (an int32 uses 4 bytes, etc.). For arrays and Constructors, it is a bit more complex. Note that a scalar String variable uses sizeof(String*) bytes, not the length of the string. In other words, the value returned is independent of the type. Also note width() of a String array returns the number of elements in the array times sizeof(String*). That is, each different array size is a different data type.
Definition: Byte.cc:123
libdap::UInt64
Holds a 64-bit unsigned integer.
Definition: UInt64.h:49
Crc32::AddData
void AddData(const uint8_t *pData, const uint32_t length)
Definition: crc.h:98
libdap::Byte::print_val
virtual void print_val(FILE *out, string space="", bool print_decl_p=true)
Prints the value of the variable.
Definition: Byte.cc:248
libdap::BaseType::transform_to_dap2
virtual std::vector< BaseType * > * transform_to_dap2(AttrTable *parent_attr_table)
DAP4 to DAP2 transform.
Definition: BaseType.cc:258
libdap::ConstraintEvaluator
Evaluate a constraint expression.
Definition: ConstraintEvaluator.h:42
libdap::BaseType
The basic data type for the DODS DAP types.
Definition: BaseType.h:117
libdap::Marshaller
abstract base class used to marshal/serialize dap data objects
Definition: Marshaller.h:50
libdap::Float64
Holds a 64-bit (double precision) floating point value.
Definition: Float64.h:60
libdap::Byte
Holds a single byte.
Definition: Byte.h:60
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:331
libdap::Byte::ops
virtual bool ops(BaseType *b, int op)
Evaluate relational operators.
Definition: Byte.cc:265
libdap::Error
A class for error processing.
Definition: Error.h:90
libdap::Byte::compute_checksum
virtual void compute_checksum(Crc32 &checksum)
include the data for this variable in the checksum DAP4 includes a checksum with every data response...
Definition: Byte.cc:167
libdap::UInt32
Holds a 32-bit unsigned integer.
Definition: UInt32.h:59
libdap::Byte::dump
virtual void dump(ostream &strm) const
dumps information about this object
Definition: Byte.cc:380
libdap::Int32
Holds a 32-bit signed integer.
Definition: Int32.h:65
libdap::Byte::ptr_duplicate
virtual BaseType * ptr_duplicate()
Definition: Byte.cc:106
libdap::BaseType::dataset
virtual string dataset() const
Returns the name of the dataset used to create this instance.
Definition: BaseType.cc:349