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 16088 2007-03-28 21:42:19Z jimg $"
00041     };
00042 
00043 #include <stdlib.h>
00044 
00045 #include "Str.h"
00046 #include "DDS.h"
00047 #include "util.h"
00048 #include "parser.h"
00049 //#include "ce_expr.tab.h"
00050 #include "Operators.h"
00051 #include "InternalErr.h"
00052 #include "escaping.h"
00053 #include "debug.h"
00054 
00055 
00056 using std::cerr;
00057 using std::endl;
00058 
00067 Str::Str(const string &n) : BaseType(n, dods_str_c), _buf("")
00068 {}
00069 
00070 Str::Str(const Str &copy_from) : BaseType(copy_from)
00071 {
00072     _buf = copy_from._buf;
00073 }
00074 
00075 BaseType *
00076 Str::ptr_duplicate()
00077 {
00078     return new Str(*this);
00079 }
00080 
00081 Str &
00082 Str::operator=(const Str &rhs)
00083 {
00084     if (this == &rhs)
00085         return *this;
00086 
00087     // Call BaseType::operator=.
00088     dynamic_cast<BaseType &>(*this) = rhs;
00089 
00090     _buf = rhs._buf;
00091 
00092     return *this;
00093 }
00094 
00095 unsigned int
00096 Str::length()
00097 {
00098     return _buf.length();
00099 }
00100 
00101 unsigned int
00102 Str::width()
00103 {
00104     return sizeof(string);
00105 }
00106 
00107 bool
00108 Str::serialize(const string &dataset, ConstraintEvaluator &eval, DDS &dds,
00109                XDR *sink, bool ce_eval)
00110 {
00111 
00112     DBG(cerr << "Entering (" << this->name() << " [" << this << "])" << endl);
00113 
00114     dds.timeout_on();
00115 
00116     if (!read_p())
00117         read(dataset);
00118 
00119 #if EVAL
00120     if (ce_eval && !eval.eval_selection(dds, dataset))
00121         return true;
00122 #endif
00123 
00124     dds.timeout_off();
00125 
00126     if (!xdr_str(sink, _buf))
00127         throw Error("Network I/O Error. Could not send string data.\nThis may be due to a bug in libdap, on the server or a\nproblem with the network connection.");
00128 
00129     DBG(cerr << "Exiting: buf = " << _buf << endl);
00130 
00131     return true;
00132 }
00133 
00134 // deserialize the string on stdin and put the result in BUF.
00135 
00136 bool
00137 Str::deserialize(XDR *source, DDS *, bool)
00138 {
00139     if (xdr_str(source, _buf) != 1)
00140         throw Error("Network I/O Error. Could not read string data. This may be due to a\nbug in libdap or a problem with the network connection.");
00141 
00142     return false;
00143 }
00144 
00154 unsigned int
00155 Str::buf2val(void **val)
00156 {
00157     // Jose Garcia
00158     // The same comment justifying throwing an Error in val2buf applies here.
00159     if (!val)
00160         throw InternalErr(__FILE__, __LINE__,
00161                           "No place to store a reference to the data.");
00162     // If *val is null, then the caller has not allocated storage for the
00163     // value; we must. If there is storage there, assume it is a string and
00164     // assign _buf's value to that storage.
00165     if (!*val)
00166         *val = new string(_buf);
00167     else
00168         *static_cast<string*>(*val) = _buf;
00169 
00170     return sizeof(string*);
00171 }
00172 
00182 unsigned int
00183 Str::val2buf(void *val, bool)
00184 {
00185     // Jose Garcia
00186     // This method is public therefore and I believe it has being designed
00187     // to be use by read which must be implemented on the surrogated library,
00188     // thus if the pointer val is NULL, is an Internal Error.
00189     if (!val)
00190         throw InternalErr(__FILE__, __LINE__, "NULL pointer.");
00191 
00192     _buf = *static_cast<string*>(val);
00193 
00194     return sizeof(string*);
00195 }
00196 
00201 bool
00202 Str::set_value(const string &value)
00203 {
00204     _buf = value;
00205     set_read_p(true);
00206 
00207     return true;
00208 }
00209 
00212 string
00213 Str::value() const
00214 {
00215     return _buf;
00216 }
00217 
00218 void
00219 Str::print_val(FILE *out, string space, bool print_decl_p)
00220 {
00221     if (print_decl_p) {
00222         print_decl(out, space, false);
00223         fprintf(out, " = \"%s\";\n", escattr(_buf).c_str()) ;
00224     }
00225     else
00226         fprintf(out, "\"%s\"", escattr(_buf).c_str()) ;
00227 }
00228 
00229 bool
00230 Str::ops(BaseType *b, int op, const string &dataset)
00231 {
00232     // Extract the Byte arg's value.
00233     if (!read_p() && !read(dataset)) {
00234         // Jose Garcia
00235         // Since the read method is virtual and implemented outside
00236         // libdap++ if we can not read the data that is the problem
00237         // of the user or of whoever wrote the surrogate library
00238         // implemeting read therefore it is an internal error.
00239         throw InternalErr(__FILE__, __LINE__, "This value was not read!");
00240     }
00241 
00242     // Extract the second arg's value.
00243     if (!b->read_p() && !b->read(dataset)) {
00244         // Jose Garcia
00245         // Since the read method is virtual and implemented outside
00246         // libdap++ if we can not read the data that is the problem
00247         // of the user or of whoever wrote the surrogate library
00248         // implemeting read therefore it is an internal error.
00249         throw InternalErr(__FILE__, __LINE__, "Argument value was not read!");
00250     }
00251 
00252     switch (b->type()) {
00253     case dods_str_c:
00254         return rops<string, string, StrCmp<string, string> >
00255                (_buf, dynamic_cast<Str *>(b)->_buf, op);
00256     case dods_url_c:
00257         return rops<string, string, StrCmp<string, string> >
00258                (_buf, dynamic_cast<Url *>(b)->_buf, op);
00259     default:
00260         return false;
00261     }
00262 }
00263 
00272 void
00273 Str::dump(ostream &strm) const
00274 {
00275     strm << DapIndent::LMarg << "Str::dump - ("
00276     << (void *)this << ")" << endl ;
00277     DapIndent::Indent() ;
00278     BaseType::dump(strm) ;
00279     strm << DapIndent::LMarg << "value: " << _buf << endl ;
00280     DapIndent::UnIndent() ;
00281 }
00282 

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