libdap++ Updated for version 3.12.1

Byte.cc

Go to the documentation of this file.
00001 
00002 // -*- mode: c++; c-basic-offset:4 -*-
00003 
00004 // This file is part of libdap, A C++ implementation of the OPeNDAP Data
00005 // Access Protocol.
00006 
00007 // Copyright (c) 2002,2003 OPeNDAP, Inc.
00008 // Author: James Gallagher <jgallagher@opendap.org>
00009 //
00010 // This library is free software; you can redistribute it and/or
00011 // modify it under the terms of the GNU Lesser General Public
00012 // License as published by the Free Software Foundation; either
00013 // version 2.1 of the License, or (at your option) any later version.
00014 //
00015 // This library is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00023 //
00024 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
00025 
00026 // (c) COPYRIGHT URI/MIT 1994-1999
00027 // Please read the full copyright statement in the file COPYRIGHT_URI.
00028 //
00029 // Authors:
00030 //      jhrg,jimg       James Gallagher <jgallagher@gso.uri.edu>
00031 
00032 // Implementation for Byte.
00033 //
00034 // jhrg 9/7/94
00035 
00036 
00037 #include "config.h"
00038 
00039 #include <sstream>
00040 
00041 #include "Byte.h"           // synonymous with UInt8 and Char
00042 #include "Int8.h"
00043 #include "Int16.h"
00044 #include "UInt16.h"
00045 #include "Int32.h"
00046 #include "UInt32.h"
00047 #include "Int64.h"
00048 #include "UInt64.h"
00049 #include "Float32.h"
00050 #include "Float64.h"
00051 #include "Str.h"
00052 #include "Url.h"
00053 
00054 #include "DDS.h"
00055 #include "Operators.h"
00056 #include "Marshaller.h"
00057 #include "UnMarshaller.h"
00058 
00059 #include "util.h"
00060 #include "parser.h"
00061 #include "dods-limits.h"
00062 #include "InternalErr.h"
00063 
00064 using std::cerr;
00065 using std::endl;
00066 
00067 namespace libdap {
00068 
00078 Byte::Byte(const string & n): BaseType(n, dods_byte_c)
00079 {}
00080 
00091 Byte::Byte(const string &n, const string &d): BaseType(n, d, dods_byte_c)
00092 {}
00093 
00094 Byte::Byte(const Byte & copy_from): BaseType(copy_from)
00095 {
00096     d_buf = copy_from.d_buf;
00097 }
00098 
00099 BaseType *Byte::ptr_duplicate()
00100 {
00101     return new Byte(*this);
00102 }
00103 
00104 Byte & Byte::operator=(const Byte & rhs)
00105 {
00106     if (this == &rhs)
00107         return *this;
00108 
00109     dynamic_cast < BaseType & >(*this) = rhs;
00110 
00111     d_buf = rhs.d_buf;
00112 
00113     return *this;
00114 }
00115 
00116 unsigned int Byte::width(bool)
00117 {
00118     return sizeof(dods_byte);
00119 }
00120 
00131 bool Byte::serialize(ConstraintEvaluator & eval, DDS & dds,
00132                      Marshaller &m, bool ce_eval)
00133 {
00134     dds.timeout_on();
00135 
00136     if (!read_p())
00137         read();          // read() throws Error and InternalErr
00138 
00139 #if EVAL
00140     if (ce_eval && !eval.eval_selection(dds, dataset()))
00141         return true;
00142 #endif
00143 
00144     dds.timeout_off();
00145 
00146     m.put_byte( d_buf ) ;
00147 
00148     return true;
00149 }
00150 
00154 bool Byte::deserialize(UnMarshaller &um, DDS *, bool)
00155 {
00156     um.get_byte( d_buf ) ;
00157 
00158     return false;
00159 }
00160 
00166 unsigned int Byte::val2buf(void *val, bool)
00167 {
00168     // Jose Garcia
00169     // This method is public therefore and I believe it has being designed
00170     // to be use by read which must be implemented on the surrogate library,
00171     // thus if the pointer val is NULL, is an Internal Error.
00172     if (!val)
00173         throw InternalErr("the incoming pointer does not contain any data.");
00174 
00175     d_buf = *(dods_byte *) val;
00176 
00177     return width();
00178 }
00179 
00180 unsigned int Byte::buf2val(void **val)
00181 {
00182     // Jose Garcia
00183     // The same comment justifying throwing an Error in val2buf applies here.
00184     if (!val)
00185         throw InternalErr("NULL pointer");
00186 
00187     if (!*val)
00188         *val = new dods_byte;
00189 
00190     *(dods_byte *) * val = d_buf;
00191 
00192     return width();
00193 }
00194 
00199 bool Byte::set_value(dods_byte value)
00200 {
00201     d_buf = value;
00202     set_read_p(true);
00203 
00204     return true;
00205 }
00206 
00209 dods_byte Byte::value() const
00210 {
00211     return d_buf;
00212 }
00213 
00214 void Byte::print_val(FILE * out, string space, bool print_decl_p)
00215 {
00216     ostringstream oss;
00217     print_val(oss, space, print_decl_p);
00218     fwrite(oss.str().data(), sizeof(char), oss.str().length(), out);
00219 }
00220 
00221 void Byte::print_val(ostream &out, string space, bool print_decl_p)
00222 {
00223     if (print_decl_p) {
00224         print_decl(out, space, false);
00225         out << " = " << (int) d_buf << ";\n";
00226     }
00227     else
00228         out << (int) d_buf;
00229 }
00230 
00231 bool Byte::ops(BaseType * b, int op)
00232 {
00233 
00234     // Extract the Byte arg's value.
00235     if (!read_p() && !read()) {
00236         // Jose Garcia
00237         // Since the read method is virtual and implemented outside
00238         // libdap++ if we cannot read the data that is the problem
00239         // of the user or of whoever wrote the surrogate library
00240         // implementing read therefore it is an internal error.
00241         throw InternalErr("This value not read!");
00242     }
00243     // Extract the second arg's value.
00244     if (!b || !(b->read_p() || b->read())) {
00245         // Jose Garcia
00246         // Since the read method is virtual and implemented outside
00247         // libdap++ if we cannot read the data that is the problem
00248         // of the user or of whoever wrote the surrogate library
00249         // implementing read therefore it is an internal error.
00250         throw InternalErr("This value not read!");
00251     }
00252 
00253     switch (b->type()) {
00254         case dods_int8_c:
00255             return USCmp<dods_byte, dods_int8>(op, d_buf, static_cast<Int8*>(b)->value());
00256         case dods_byte_c:
00257             return Cmp<dods_byte, dods_byte>(op, d_buf, static_cast<Byte*>(b)->value());
00258         case dods_int16_c:
00259             return USCmp<dods_byte, dods_int16>(op, d_buf, static_cast<Int16*>(b)->value());
00260         case dods_uint16_c:
00261             return Cmp<dods_byte, dods_uint16>(op, d_buf, static_cast<UInt16*>(b)->value());
00262         case dods_int32_c:
00263             return USCmp<dods_byte, dods_int32>(op, d_buf, static_cast<Int32*>(b)->value());
00264         case dods_uint32_c:
00265             return Cmp<dods_byte, dods_uint32>(op, d_buf, static_cast<UInt32*>(b)->value());
00266         case dods_int64_c:
00267             return USCmp<dods_byte, dods_int64>(op, d_buf, static_cast<Int64*>(b)->value());
00268         case dods_uint64_c:
00269             return Cmp<dods_byte, dods_uint64>(op, d_buf, static_cast<UInt64*>(b)->value());
00270         case dods_float32_c:
00271             return USCmp<dods_byte, dods_float32>(op, d_buf, static_cast<Float32*>(b)->value());
00272         case dods_float64_c:
00273             return USCmp<dods_byte, dods_float64>(op, d_buf, static_cast<Float64*>(b)->value());
00274         default:
00275             return false;
00276     }
00277 }
00278 
00287 void Byte::dump(ostream & strm) const
00288 {
00289     strm << DapIndent::LMarg << "Byte::dump - ("
00290     << (void *) this << ")" << endl;
00291     DapIndent::Indent();
00292     BaseType::dump(strm);
00293     strm << DapIndent::LMarg << "value: " << d_buf << endl;
00294     DapIndent::UnIndent();
00295 }
00296 
00297 } // namespace libdap