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 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 ©_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
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
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
00166
00167 if (!val)
00168 throw InternalErr(__FILE__, __LINE__,
00169 "No place to store a reference to the data.");
00170
00171
00172
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
00194
00195
00196
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
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
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
00252 if (!read_p() && !read()) {
00253
00254
00255
00256
00257
00258 throw InternalErr(__FILE__, __LINE__, "This value was not read!");
00259 }
00260
00261
00262 if (!b || !b->read_p() && !b->read()) {
00263
00264
00265
00266
00267
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 }
00303