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 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 ©_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
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
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
00155
00156 if (!val)
00157 throw InternalErr(__FILE__, __LINE__,
00158 "No place to store a reference to the data.");
00159
00160
00161
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
00183
00184
00185
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
00241 if (!read_p() && !read(dataset)) {
00242
00243
00244
00245
00246
00247 throw InternalErr(__FILE__, __LINE__, "This value was not read!");
00248 }
00249
00250
00251 if (!b->read_p() && !b->read(dataset)) {
00252
00253
00254
00255
00256
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 }
00292