libdap  Updated for version 3.17.0
Byte.cc
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 "DMR.h"
00060 #include "D4StreamMarshaller.h"
00061 #include "D4StreamUnMarshaller.h"
00062 
00063 #include "util.h"
00064 #include "parser.h"
00065 #include "dods-limits.h"
00066 #include "InternalErr.h"
00067 
00068 using std::cerr;
00069 using std::endl;
00070 
00071 namespace libdap {
00072 
00082 Byte::Byte(const string & n): BaseType(n, dods_byte_c), d_buf(0)
00083 {}
00084 
00095 Byte::Byte(const string &n, const string &d): BaseType(n, d, dods_byte_c), d_buf(0)
00096 {}
00097 
00098 Byte::Byte(const Byte & copy_from): BaseType(copy_from)
00099 {
00100     d_buf = copy_from.d_buf;
00101 }
00102 
00103 BaseType *Byte::ptr_duplicate()
00104 {
00105     return new Byte(*this);
00106 }
00107 
00108 Byte & Byte::operator=(const Byte & rhs)
00109 {
00110     if (this == &rhs)
00111         return *this;
00112 
00113     dynamic_cast < BaseType & >(*this) = rhs;
00114 
00115     d_buf = rhs.d_buf;
00116 
00117     return *this;
00118 }
00119 
00120 unsigned int Byte::width(bool) const
00121 {
00122     return sizeof(dods_byte);
00123 }
00124 
00135 bool Byte::serialize(ConstraintEvaluator & eval, DDS & dds, Marshaller &m, bool ce_eval)
00136 {
00137 #if USE_LOCAL_TIMEOUT_SCHEME
00138     dds.timeout_on();
00139 #endif
00140     if (!read_p())
00141         read();          // read() throws Error and InternalErr
00142 
00143     if (ce_eval && !eval.eval_selection(dds, dataset()))
00144         return true;
00145 #if USE_LOCAL_TIMEOUT_SCHEME
00146     dds.timeout_off();
00147 #endif
00148     m.put_byte( d_buf ) ;
00149 
00150     return true;
00151 }
00152 
00156 bool Byte::deserialize(UnMarshaller &um, DDS *, bool)
00157 {
00158     um.get_byte( d_buf ) ;
00159 
00160     return false;
00161 }
00162 
00163 void
00164 Byte::compute_checksum(Crc32 &checksum)
00165 {
00166         checksum.AddData(reinterpret_cast<uint8_t*>(&d_buf), sizeof(d_buf));
00167 }
00168 
00177 void
00178 Byte::serialize(D4StreamMarshaller &m, DMR &, /*ConstraintEvaluator &,*/ bool)
00179 {
00180     if (!read_p())
00181         read();          // read() throws Error
00182 
00183     m.put_byte( d_buf ) ;
00184 }
00185 
00186 void
00187 Byte::deserialize(D4StreamUnMarshaller &um, DMR &)
00188 {
00189     um.get_byte( d_buf ) ;
00190 }
00191 
00197 unsigned int Byte::val2buf(void *val, bool)
00198 {
00199     // Jose Garcia
00200     // This method is public therefore and I believe it has being designed
00201     // to be use by read which must be implemented on the surrogate library,
00202     // thus if the pointer val is NULL, is an Internal Error.
00203     if (!val)
00204         throw InternalErr("the incoming pointer does not contain any data.");
00205 
00206     d_buf = *(dods_byte *) val;
00207 
00208     return width();
00209 }
00210 
00211 unsigned int Byte::buf2val(void **val)
00212 {
00213     // Jose Garcia
00214     // The same comment justifying throwing an Error in val2buf applies here.
00215     if (!val)
00216         throw InternalErr("NULL pointer");
00217 
00218     if (!*val)
00219         *val = new dods_byte;
00220 
00221     *(dods_byte *) * val = d_buf;
00222 
00223     return width();
00224 }
00225 
00230 bool Byte::set_value(dods_byte value)
00231 {
00232     d_buf = value;
00233     set_read_p(true);
00234 
00235     return true;
00236 }
00237 
00240 dods_byte Byte::value() const
00241 {
00242     return d_buf;
00243 }
00244 
00245 void Byte::print_val(FILE * out, string space, bool print_decl_p)
00246 {
00247     ostringstream oss;
00248     print_val(oss, space, print_decl_p);
00249     fwrite(oss.str().data(), sizeof(char), oss.str().length(), out);
00250 }
00251 
00252 void Byte::print_val(ostream &out, string space, bool print_decl_p)
00253 {
00254     if (print_decl_p) {
00255         print_decl(out, space, false);
00256         out << " = " << (int) d_buf << ";\n";
00257     }
00258     else
00259         out << (int) d_buf;
00260 }
00261 
00262 bool Byte::ops(BaseType * b, int op)
00263 {
00264 
00265     // Extract the Byte arg's value.
00266     if (!read_p() && !read()) {
00267         // Jose Garcia
00268         // Since the read method is virtual and implemented outside
00269         // libdap++ if we cannot read the data that is the problem
00270         // of the user or of whoever wrote the surrogate library
00271         // implementing read therefore it is an internal error.
00272         throw InternalErr("This value not read!");
00273     }
00274     // Extract the second arg's value.
00275     if (!b || !(b->read_p() || b->read())) {
00276         // Jose Garcia
00277         // Since the read method is virtual and implemented outside
00278         // libdap++ if we cannot read the data that is the problem
00279         // of the user or of whoever wrote the surrogate library
00280         // implementing read therefore it is an internal error.
00281         throw InternalErr("This value not read!");
00282     }
00283 
00284     switch (b->type()) {
00285         case dods_int8_c:
00286             return USCmp<dods_byte, dods_int8>(op, d_buf, static_cast<Int8*>(b)->value());
00287         case dods_byte_c:
00288             return Cmp<dods_byte, dods_byte>(op, d_buf, static_cast<Byte*>(b)->value());
00289         case dods_int16_c:
00290             return USCmp<dods_byte, dods_int16>(op, d_buf, static_cast<Int16*>(b)->value());
00291         case dods_uint16_c:
00292             return Cmp<dods_byte, dods_uint16>(op, d_buf, static_cast<UInt16*>(b)->value());
00293         case dods_int32_c:
00294             return USCmp<dods_byte, dods_int32>(op, d_buf, static_cast<Int32*>(b)->value());
00295         case dods_uint32_c:
00296             return Cmp<dods_byte, dods_uint32>(op, d_buf, static_cast<UInt32*>(b)->value());
00297         case dods_int64_c:
00298             return USCmp<dods_byte, dods_int64>(op, d_buf, static_cast<Int64*>(b)->value());
00299         case dods_uint64_c:
00300             return Cmp<dods_byte, dods_uint64>(op, d_buf, static_cast<UInt64*>(b)->value());
00301         case dods_float32_c:
00302             return USCmp<dods_byte, dods_float32>(op, d_buf, static_cast<Float32*>(b)->value());
00303         case dods_float64_c:
00304             return USCmp<dods_byte, dods_float64>(op, d_buf, static_cast<Float64*>(b)->value());
00305         default:
00306             return false;
00307     }
00308 }
00309 
00318 void Byte::dump(ostream & strm) const
00319 {
00320     strm << DapIndent::LMarg << "Byte::dump - ("
00321     << (void *) this << ")" << endl;
00322     DapIndent::Indent();
00323     BaseType::dump(strm);
00324     strm << DapIndent::LMarg << "value: " << d_buf << endl;
00325     DapIndent::UnIndent();
00326 }
00327 
00328 } // namespace libdap