00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
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
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 ©_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
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
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
00158
00159 if (!val)
00160 throw InternalErr(__FILE__, __LINE__,
00161 "No place to store a reference to the data.");
00162
00163
00164
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
00186
00187
00188
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
00233 if (!read_p() && !read(dataset)) {
00234
00235
00236
00237
00238
00239 throw InternalErr(__FILE__, __LINE__, "This value was not read!");
00240 }
00241
00242
00243 if (!b->read_p() && !b->read(dataset)) {
00244
00245
00246
00247
00248
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