libdap++ Updated for version 3.8.2

Str.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 Str.
00033 //
00034 // jhrg 9/7/94
00035 
00036 
00037 #include "config.h"
00038 
00039 static char rcsid[] not_used =
00040     {"$Id: Str.cc 21699 2009-11-05 00:06:01Z jimg $"
00041     };
00042 
00043 #include "Byte.h"
00044 #include "Int16.h"
00045 #include "UInt16.h"
00046 #include "Int32.h"
00047 #include "UInt32.h"
00048 #include "Float32.h"
00049 #include "Float64.h"
00050 #include "Str.h"
00051 #include "Url.h"
00052 #include "Array.h"
00053 #include "Structure.h"
00054 #include "Sequence.h"
00055 #include "Grid.h"
00056 
00057 
00058 #include "DDS.h"
00059 #include "util.h"
00060 #include "parser.h"
00061 #include "Operators.h"
00062 #include "InternalErr.h"
00063 #include "escaping.h"
00064 #include "debug.h"
00065 
00066 
00067 using std::cerr;
00068 using std::endl;
00069 
00070 namespace libdap {
00071 
00080 Str::Str(const string &n) : BaseType(n, dods_str_c), _buf("")
00081 {}
00082 
00090 Str::Str(const string &n, const string &d)
00091     : BaseType(n, d, dods_str_c), _buf("")
00092 {}
00093 
00094 Str::Str(const Str &copy_from) : BaseType(copy_from)
00095 {
00096     _buf = copy_from._buf;
00097 }
00098 
00099 BaseType *
00100 Str::ptr_duplicate()
00101 {
00102     return new Str(*this);
00103 }
00104 
00105 Str &
00106 Str::operator=(const Str &rhs)
00107 {
00108     if (this == &rhs)
00109         return *this;
00110 
00111     // Call BaseType::operator=.
00112     dynamic_cast<BaseType &>(*this) = rhs;
00113 
00114     _buf = rhs._buf;
00115 
00116     return *this;
00117 }
00118 
00119 unsigned int
00120 Str::length()
00121 {
00122     return _buf.length();
00123 }
00124 
00125 unsigned int
00126 Str::width()
00127 {
00128     return sizeof(string);
00129 }
00130 
00131 bool
00132 Str::serialize(ConstraintEvaluator &eval, DDS &dds,
00133                Marshaller &m, bool ce_eval)
00134 {
00135 
00136     DBG(cerr << "Entering (" << this->name() << " [" << this << "])" << endl);
00137 
00138     dds.timeout_on();
00139 
00140     if (!read_p())
00141         read();
00142 
00143 #if EVAL
00144     if (ce_eval && !eval.eval_selection(dds, dataset()))
00145         return true;
00146 #endif
00147 
00148     dds.timeout_off();
00149 
00150     m.put_str( _buf ) ;
00151 
00152     DBG(cerr << "Exiting: buf = " << _buf << endl);
00153 
00154     return true;
00155 }
00156 
00157 // deserialize the string on stdin and put the result in BUF.
00158 
00159 bool
00160 Str::deserialize(UnMarshaller &um, DDS *, bool)
00161 {
00162     um.get_str( _buf ) ;
00163 
00164     return false;
00165 }
00166 
00176 unsigned int
00177 Str::buf2val(void **val)
00178 {
00179     // Jose Garcia
00180     // The same comment justifying throwing an Error in val2buf applies here.
00181     if (!val)
00182         throw InternalErr(__FILE__, __LINE__,
00183                           "No place to store a reference to the data.");
00184     // If *val is null, then the caller has not allocated storage for the
00185     // value; we must. If there is storage there, assume it is a string and
00186     // assign _buf's value to that storage.
00187     if (!*val)
00188         *val = new string(_buf);
00189     else
00190         *static_cast<string*>(*val) = _buf;
00191 
00192     return sizeof(string*);
00193 }
00194 
00204 unsigned int
00205 Str::val2buf(void *val, bool)
00206 {
00207     // Jose Garcia
00208     // This method is public therefore and I believe it has being designed
00209     // to be use by read which must be implemented on the surrogated library,
00210     // thus if the pointer val is NULL, is an Internal Error.
00211     if (!val)
00212         throw InternalErr(__FILE__, __LINE__, "NULL pointer.");
00213 
00214     _buf = *static_cast<string*>(val);
00215 
00216     return sizeof(string*);
00217 }
00218 
00223 bool
00224 Str::set_value(const string &value)
00225 {
00226     _buf = value;
00227     set_read_p(true);
00228 
00229     return true;
00230 }
00231 
00234 string
00235 Str::value() const
00236 {
00237     return _buf;
00238 }
00239 
00240 #if FILE_METHODS
00241 void
00242 Str::print_val(FILE *out, string space, bool print_decl_p)
00243 {
00244     if (print_decl_p) {
00245         print_decl(out, space, false);
00246         fprintf(out, " = \"%s\";\n", escattr(_buf).c_str()) ;
00247     }
00248     else
00249         fprintf(out, "\"%s\"", escattr(_buf).c_str()) ;
00250 }
00251 #endif
00252 
00253 void
00254 Str::print_val(ostream &out, string space, bool print_decl_p)
00255 {
00256     if (print_decl_p) {
00257         print_decl(out, space, false);
00258         out << " = \"" << escattr(_buf) << "\";\n" ;
00259     }
00260     else
00261         out << "\"" << escattr(_buf) << "\"" ;
00262 }
00263 
00264 bool
00265 Str::ops(BaseType *b, int op)
00266 {
00267     // Extract the Byte arg's value.
00268     if (!read_p() && !read()) {
00269         // Jose Garcia
00270         // Since the read method is virtual and implemented outside
00271         // libdap++ if we cannot read the data that is the problem
00272         // of the user or of whoever wrote the surrogate library
00273         // implemeting read therefore it is an internal error.
00274         throw InternalErr(__FILE__, __LINE__, "This value was not read!");
00275     }
00276 
00277     // Extract the second arg's value.
00278     if (!b || !(b->read_p() || b->read())) {
00279         // Jose Garcia
00280         // Since the read method is virtual and implemented outside
00281         // libdap++ if we cannot read the data that is the problem
00282         // of the user or of whoever wrote the surrogate library
00283         // implemeting read therefore it is an internal error.
00284         throw InternalErr(__FILE__, __LINE__, "Argument value was not read!");
00285     }
00286 
00287     switch (b->type()) {
00288     case dods_str_c:
00289         return rops<string, string, StrCmp<string, string> >
00290                (_buf, dynamic_cast<Str *>(b)->_buf, op);
00291     case dods_url_c:
00292         return rops<string, string, StrCmp<string, string> >
00293                (_buf, dynamic_cast<Url *>(b)->_buf, op);
00294     default:
00295         return false;
00296     }
00297 }
00298 
00307 void
00308 Str::dump(ostream &strm) const
00309 {
00310     strm << DapIndent::LMarg << "Str::dump - ("
00311     << (void *)this << ")" << endl ;
00312     DapIndent::Indent() ;
00313     BaseType::dump(strm) ;
00314     strm << DapIndent::LMarg << "value: " << _buf << endl ;
00315     DapIndent::UnIndent() ;
00316 }
00317 
00318 } // namespace libdap
00319