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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 static char rcsid[] not_used =
00040     { "$Id: Byte.cc 16612 2007-06-04 22:08:57Z jimg $"
00041     };
00042 
00043 #include <stdlib.h>
00044 
00045 #include "Byte.h"
00046 //#include "Int16.h"
00047 #include "DDS.h"
00048 #include "Operators.h"
00049 
00050 #include "util.h"
00051 #include "parser.h"
00052 #include "dods-limits.h"
00053 #include "InternalErr.h"
00054 
00055 
00056 using std::cerr;
00057 using std::endl;
00058 
00073 Byte::Byte(const string & n): BaseType(n, dods_byte_c)
00074 {}
00075 
00076 Byte::Byte(const Byte & copy_from): BaseType(copy_from)
00077 {
00078     _buf = copy_from._buf;
00079 }
00080 
00081 BaseType *Byte::ptr_duplicate()
00082 {
00083     return new Byte(*this);
00084 }
00085 
00086 Byte & Byte::operator=(const Byte & rhs)
00087 {
00088     if (this == &rhs)
00089         return *this;
00090 
00091     dynamic_cast < BaseType & >(*this) = rhs;
00092 
00093     _buf = rhs._buf;
00094 
00095     return *this;
00096 }
00097 
00098 unsigned int Byte::width()
00099 {
00100     return sizeof(dods_byte);
00101 }
00102 
00116 bool Byte::serialize(const string & dataset, ConstraintEvaluator & eval,
00117                      DDS & dds, XDR * sink, bool ce_eval)
00118 {
00119     dds.timeout_on();
00120 
00121     if (!read_p())
00122         read(dataset);          // read() throws Error and InternalErr
00123 
00124 #if EVAL
00125     if (ce_eval && !eval.eval_selection(dds, dataset))
00126         return true;
00127 #endif
00128 
00129     dds.timeout_off();
00130 
00131     if (!xdr_char(sink, (char *) &_buf))
00132         throw Error("Network I/O Error. Could not send byte data.\nThis may be due to a bug in DODS, on the server or a\nproblem with the network connection.");
00133 
00134     return true;
00135 }
00136 
00140 bool Byte::deserialize(XDR * source, DDS *, bool)
00141 {
00142     if (!xdr_char(source, (char *) &_buf))
00143         throw Error("Network I/O Error. Could not read byte data. This may be due to a\nbug in DODS or a problem with the network connection.");
00144 
00145     return false;
00146 }
00147 
00153 unsigned int Byte::val2buf(void *val, bool)
00154 {
00155     // Jose Garcia
00156     // This method is public therefore and I believe it has being designed
00157     // to be use by read which must be implemented on the surrogate library,
00158     // thus if the pointer val is NULL, is an Internal Error.
00159     if (!val)
00160         throw InternalErr("the incoming pointer does not contain any data.");
00161 
00162     _buf = *(dods_byte *) val;
00163 
00164     return width();
00165 }
00166 
00167 unsigned int Byte::buf2val(void **val)
00168 {
00169     // Jose Garcia
00170     // The same comment justifying throwing an Error in val2buf applies here.
00171     if (!val)
00172         throw InternalErr("NULL pointer");
00173 
00174     if (!*val)
00175         *val = new dods_byte;
00176 
00177     *(dods_byte *) * val = _buf;
00178 
00179     return width();
00180 }
00181 
00186 bool Byte::set_value(dods_byte value)
00187 {
00188     _buf = value;
00189     set_read_p(true);
00190 
00191     return true;
00192 }
00193 
00196 dods_byte Byte::value() const
00197 {
00198     return _buf;
00199 }
00200 
00201 void Byte::print_val(FILE * out, string space, bool print_decl_p)
00202 {
00203     if (print_decl_p) {
00204         print_decl(out, space, false);
00205         fprintf(out, " = %d;\n", (int) _buf);
00206     }
00207     else
00208         fprintf(out, "%d", (int) _buf);
00209 }
00210 
00211 bool Byte::ops(BaseType * b, int op, const string & dataset)
00212 {
00213 
00214     // Extract the Byte arg's value.
00215     if (!read_p() && !read(dataset)) {
00216         cerr << "This value not read!" << endl;
00217         // Jose Garcia
00218         // Since the read method is virtual and implemented outside
00219         // libdap++ if we can not read the data that is the problem
00220         // of the user or of whoever wrote the surrogate library
00221         // implementing read therefore it is an internal error.
00222         throw InternalErr("This value not read!");
00223     }
00224     // Extract the second arg's value.
00225     if (!b->read_p() && !b->read(dataset)) {
00226         cerr << "This value not read!" << endl;
00227         // Jose Garcia
00228         // Since the read method is virtual and implemented outside
00229         // libdap++ if we can not read the data that is the problem
00230         // of the user or of whoever wrote the surrogate library
00231         // implementing read therefore it is an internal error.
00232         throw InternalErr("This value not read!");
00233     }
00234 
00235     switch (b->type()) {
00236     case dods_byte_c:
00237         return rops < dods_byte, dods_byte, Cmp < dods_byte, dods_byte > >
00238                (_buf, dynamic_cast < Byte * >(b)->_buf, op);
00239     case dods_int16_c:
00240         return rops < dods_byte, dods_int16, USCmp < dods_byte,
00241                dods_int16 > > (_buf, dynamic_cast < Int16 * >(b)->_buf, op);
00242     case dods_uint16_c:
00243         return rops < dods_byte, dods_uint16, Cmp < dods_byte,
00244                dods_uint16 > > (_buf, dynamic_cast < UInt16 * >(b)->_buf, op);
00245     case dods_int32_c:
00246         return rops < dods_byte, dods_int32, USCmp < dods_byte,
00247                dods_int32 > > (_buf, dynamic_cast < Int32 * >(b)->_buf, op);
00248     case dods_uint32_c:
00249         return rops < dods_byte, dods_uint32, Cmp < dods_byte,
00250                dods_uint32 > > (_buf, dynamic_cast < UInt32 * >(b)->_buf, op);
00251     case dods_float32_c:
00252         return rops < dods_byte, dods_float32, Cmp < dods_byte,
00253                dods_float32 > > (_buf, dynamic_cast < Float32 * >(b)->_buf,
00254                                  op);
00255     case dods_float64_c:
00256         return rops < dods_byte, dods_float64, Cmp < dods_byte,
00257                dods_float64 > > (_buf, dynamic_cast < Float64 * >(b)->_buf,
00258                                  op);
00259     default:
00260         return false;
00261     }
00262 }
00263 
00272 void Byte::dump(ostream & strm) const
00273 {
00274     strm << DapIndent::LMarg << "Byte::dump - ("
00275     << (void *) this << ")" << endl;
00276     DapIndent::Indent();
00277     BaseType::dump(strm);
00278     strm << DapIndent::LMarg << "value: " << _buf << endl;
00279     DapIndent::UnIndent();
00280 }

Generated on Wed Jun 27 12:56:38 2007 for libdap++ by  doxygen 1.4.7