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 20518 2009-03-05 23:39:46Z 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 
00076 Str::Str(const string &n, const string &d)
00077     : BaseType(n, d, dods_str_c), _buf("")
00078 {}
00079 
00080 Str::Str(const Str &copy_from) : BaseType(copy_from)
00081 {
00082     _buf = copy_from._buf;
00083 }
00084 
00085 BaseType *
00086 Str::ptr_duplicate()
00087 {
00088     return new Str(*this);
00089 }
00090 
00091 Str &
00092 Str::operator=(const Str &rhs)
00093 {
00094     if (this == &rhs)
00095         return *this;
00096 
00097     // Call BaseType::operator=.
00098     dynamic_cast<BaseType &>(*this) = rhs;
00099 
00100     _buf = rhs._buf;
00101 
00102     return *this;
00103 }
00104 
00105 unsigned int
00106 Str::length()
00107 {
00108     return _buf.length();
00109 }
00110 
00111 unsigned int
00112 Str::width()
00113 {
00114     return sizeof(string);
00115 }
00116 
00117 bool
00118 Str::serialize(ConstraintEvaluator &eval, DDS &dds,
00119                Marshaller &m, bool ce_eval)
00120 {
00121 
00122     DBG(cerr << "Entering (" << this->name() << " [" << this << "])" << endl);
00123 
00124     dds.timeout_on();
00125 
00126     if (!read_p())
00127         read();
00128 
00129 #if EVAL
00130     if (ce_eval && !eval.eval_selection(dds, dataset()))
00131         return true;
00132 #endif
00133 
00134     dds.timeout_off();
00135 
00136     m.put_str( _buf ) ;
00137 
00138     DBG(cerr << "Exiting: buf = " << _buf << endl);
00139 
00140     return true;
00141 }
00142 
00143 // deserialize the string on stdin and put the result in BUF.
00144 
00145 bool
00146 Str::deserialize(UnMarshaller &um, DDS *, bool)
00147 {
00148     um.get_str( _buf ) ;
00149 
00150     return false;
00151 }
00152 
00162 unsigned int
00163 Str::buf2val(void **val)
00164 {
00165     // Jose Garcia
00166     // The same comment justifying throwing an Error in val2buf applies here.
00167     if (!val)
00168         throw InternalErr(__FILE__, __LINE__,
00169                           "No place to store a reference to the data.");
00170     // If *val is null, then the caller has not allocated storage for the
00171     // value; we must. If there is storage there, assume it is a string and
00172     // assign _buf's value to that storage.
00173     if (!*val)
00174         *val = new string(_buf);
00175     else
00176         *static_cast<string*>(*val) = _buf;
00177 
00178     return sizeof(string*);
00179 }
00180 
00190 unsigned int
00191 Str::val2buf(void *val, bool)
00192 {
00193     // Jose Garcia
00194     // This method is public therefore and I believe it has being designed
00195     // to be use by read which must be implemented on the surrogated library,
00196     // thus if the pointer val is NULL, is an Internal Error.
00197     if (!val)
00198         throw InternalErr(__FILE__, __LINE__, "NULL pointer.");
00199 
00200     _buf = *static_cast<string*>(val);
00201 
00202     return sizeof(string*);
00203 }
00204 
00209 bool
00210 Str::set_value(const string &value)
00211 {
00212     _buf = value;
00213     set_read_p(true);
00214 
00215     return true;
00216 }
00217 
00220 string
00221 Str::value() const
00222 {
00223     return _buf;
00224 }
00225 //#if FILE_METHODS
00226 void
00227 Str::print_val(FILE *out, string space, bool print_decl_p)
00228 {
00229     if (print_decl_p) {
00230         print_decl(out, space, false);
00231         fprintf(out, " = \"%s\";\n", escattr(_buf).c_str()) ;
00232     }
00233     else
00234         fprintf(out, "\"%s\"", escattr(_buf).c_str()) ;
00235 }
00236 //#endif
00237 void
00238 Str::print_val(ostream &out, string space, bool print_decl_p)
00239 {
00240     if (print_decl_p) {
00241         print_decl(out, space, false);
00242         out << " = \"" << escattr(_buf) << "\";\n" ;
00243     }
00244     else
00245         out << "\"" << escattr(_buf) << "\"" ;
00246 }
00247 
00248 bool
00249 Str::ops(BaseType *b, int op)
00250 {
00251     // Extract the Byte arg's value.
00252     if (!read_p() && !read()) {
00253         // Jose Garcia
00254         // Since the read method is virtual and implemented outside
00255         // libdap++ if we cannot 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__, "This value was not read!");
00259     }
00260 
00261     // Extract the second arg's value.
00262     if (!b || !b->read_p() && !b->read()) {
00263         // Jose Garcia
00264         // Since the read method is virtual and implemented outside
00265         // libdap++ if we cannot read the data that is the problem
00266         // of the user or of whoever wrote the surrogate library
00267         // implemeting read therefore it is an internal error.
00268         throw InternalErr(__FILE__, __LINE__, "Argument value was not read!");
00269     }
00270 
00271     switch (b->type()) {
00272     case dods_str_c:
00273         return rops<string, string, StrCmp<string, string> >
00274                (_buf, dynamic_cast<Str *>(b)->_buf, op);
00275     case dods_url_c:
00276         return rops<string, string, StrCmp<string, string> >
00277                (_buf, dynamic_cast<Url *>(b)->_buf, op);
00278     default:
00279         return false;
00280     }
00281 }
00282 
00291 void
00292 Str::dump(ostream &strm) const
00293 {
00294     strm << DapIndent::LMarg << "Str::dump - ("
00295     << (void *)this << ")" << endl ;
00296     DapIndent::Indent() ;
00297     BaseType::dump(strm) ;
00298     strm << DapIndent::LMarg << "value: " << _buf << endl ;
00299     DapIndent::UnIndent() ;
00300 }
00301 
00302 } // namespace libdap
00303 

Generated on Wed Apr 15 19:20:23 2009 for libdap++ by  doxygen 1.4.7