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 17002 2007-08-27 19:16:51Z pwest $"
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                Marshaller &m, 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     m.put_str( _buf ) ;
00127 
00128     DBG(cerr << "Exiting: buf = " << _buf << endl);
00129 
00130     return true;
00131 }
00132 
00133 // deserialize the string on stdin and put the result in BUF.
00134 
00135 bool
00136 Str::deserialize(UnMarshaller &um, DDS *, bool)
00137 {
00138     um.get_str( _buf ) ;
00139 
00140     return false;
00141 }
00142 
00152 unsigned int
00153 Str::buf2val(void **val)
00154 {
00155     // Jose Garcia
00156     // The same comment justifying throwing an Error in val2buf applies here.
00157     if (!val)
00158         throw InternalErr(__FILE__, __LINE__,
00159                           "No place to store a reference to the data.");
00160     // If *val is null, then the caller has not allocated storage for the
00161     // value; we must. If there is storage there, assume it is a string and
00162     // assign _buf's value to that storage.
00163     if (!*val)
00164         *val = new string(_buf);
00165     else
00166         *static_cast<string*>(*val) = _buf;
00167 
00168     return sizeof(string*);
00169 }
00170 
00180 unsigned int
00181 Str::val2buf(void *val, bool)
00182 {
00183     // Jose Garcia
00184     // This method is public therefore and I believe it has being designed
00185     // to be use by read which must be implemented on the surrogated library,
00186     // thus if the pointer val is NULL, is an Internal Error.
00187     if (!val)
00188         throw InternalErr(__FILE__, __LINE__, "NULL pointer.");
00189 
00190     _buf = *static_cast<string*>(val);
00191 
00192     return sizeof(string*);
00193 }
00194 
00199 bool
00200 Str::set_value(const string &value)
00201 {
00202     _buf = value;
00203     set_read_p(true);
00204 
00205     return true;
00206 }
00207 
00210 string
00211 Str::value() const
00212 {
00213     return _buf;
00214 }
00215 
00216 void
00217 Str::print_val(FILE *out, string space, bool print_decl_p)
00218 {
00219     if (print_decl_p) {
00220         print_decl(out, space, false);
00221         fprintf(out, " = \"%s\";\n", escattr(_buf).c_str()) ;
00222     }
00223     else
00224         fprintf(out, "\"%s\"", escattr(_buf).c_str()) ;
00225 }
00226 
00227 void
00228 Str::print_val(ostream &out, string space, bool print_decl_p)
00229 {
00230     if (print_decl_p) {
00231         print_decl(out, space, false);
00232         out << " = \"" << escattr(_buf) << "\";\n" ;
00233     }
00234     else
00235         out << "\"" << escattr(_buf) << "\"" ;
00236 }
00237 
00238 bool
00239 Str::ops(BaseType *b, int op, const string &dataset)
00240 {
00241     // Extract the Byte arg's value.
00242     if (!read_p() && !read(dataset)) {
00243         // Jose Garcia
00244         // Since the read method is virtual and implemented outside
00245         // libdap++ if we can not read the data that is the problem
00246         // of the user or of whoever wrote the surrogate library
00247         // implemeting read therefore it is an internal error.
00248         throw InternalErr(__FILE__, __LINE__, "This value was not read!");
00249     }
00250 
00251     // Extract the second arg's value.
00252     if (!b->read_p() && !b->read(dataset)) {
00253         // Jose Garcia
00254         // Since the read method is virtual and implemented outside
00255         // libdap++ if we can not read the data that is the problem
00256         // of the user or of whoever wrote the surrogate library
00257         // implemeting read therefore it is an internal error.
00258         throw InternalErr(__FILE__, __LINE__, "Argument value was not read!");
00259     }
00260 
00261     switch (b->type()) {
00262     case dods_str_c:
00263         return rops<string, string, StrCmp<string, string> >
00264                (_buf, dynamic_cast<Str *>(b)->_buf, op);
00265     case dods_url_c:
00266         return rops<string, string, StrCmp<string, string> >
00267                (_buf, dynamic_cast<Url *>(b)->_buf, op);
00268     default:
00269         return false;
00270     }
00271 }
00272 
00281 void
00282 Str::dump(ostream &strm) const
00283 {
00284     strm << DapIndent::LMarg << "Str::dump - ("
00285     << (void *)this << ")" << endl ;
00286     DapIndent::Indent() ;
00287     BaseType::dump(strm) ;
00288     strm << DapIndent::LMarg << "value: " << _buf << endl ;
00289     DapIndent::UnIndent() ;
00290 }
00291 

Generated on Fri Nov 30 10:49:12 2007 for libdap++ by  doxygen 1.5.1