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

Generated on Tue Mar 4 18:01:55 2008 for libdap++ by  doxygen 1.5.1