libdap++ Updated for version 3.8.2
|
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 BaseType. 00033 // 00034 // jhrg 9/6/94 00035 00036 #include "config.h" 00037 00038 #include <cstdio> // for stdin and stdout 00039 00040 #include <sstream> 00041 #include <string> 00042 00043 //#define DODS_DEBUG 00044 00045 #include "BaseType.h" 00046 #include "Byte.h" 00047 #include "Int16.h" 00048 #include "UInt16.h" 00049 #include "Int32.h" 00050 #include "UInt32.h" 00051 #include "Float32.h" 00052 #include "Float64.h" 00053 #include "Str.h" 00054 #include "Url.h" 00055 #include "Array.h" 00056 #include "Structure.h" 00057 #include "Sequence.h" 00058 #include "Grid.h" 00059 00060 #include "InternalErr.h" 00061 00062 #include "util.h" 00063 #include "escaping.h" 00064 00065 #include "debug.h" 00066 00067 using namespace std; 00068 00069 namespace libdap { 00070 00071 // Protected copy mfunc 00072 00079 void 00080 BaseType::_duplicate(const BaseType &bt) 00081 { 00082 DBG(cerr << "BaseType::_duplicate: " << bt._name << " send_p: " 00083 << bt._send_p << endl); 00084 _name = bt._name; 00085 _type = bt._type; 00086 _dataset = bt._dataset; 00087 _read_p = bt._read_p; // added, reza 00088 _send_p = bt._send_p; // added, reza 00089 d_in_selection = bt.d_in_selection; 00090 _synthesized_p = bt._synthesized_p; // 5/11/2001 jhrg 00091 00092 d_parent = bt.d_parent; // copy pointers 6/4/2001 jhrg 00093 00094 d_attr = bt.d_attr; // Deep copy. 00095 } 00096 00097 // Public mfuncs 00098 00110 BaseType::BaseType(const string &n, const Type &t) 00111 : _name(n), _type(t), _dataset(""), _read_p(false), _send_p(false), 00112 d_in_selection(false), _synthesized_p(false), d_parent(0) 00113 {} 00114 00128 BaseType::BaseType(const string &n, const string &d, const Type &t) 00129 : _name(n), _type(t), _dataset(d), _read_p(false), _send_p(false), 00130 d_in_selection(false), _synthesized_p(false), d_parent(0) 00131 {} 00132 00134 BaseType::BaseType(const BaseType ©_from) : DapObj() 00135 { 00136 _duplicate(copy_from); 00137 } 00138 00139 BaseType::~BaseType() 00140 { 00141 DBG(cerr << "Entering ~BaseType (" << this << ")" << endl); 00142 DBG(cerr << "Exiting ~BaseType" << endl); 00143 } 00144 00145 BaseType & 00146 BaseType::operator=(const BaseType &rhs) 00147 { 00148 if (this == &rhs) 00149 return *this; 00150 00151 _duplicate(rhs); 00152 00153 return *this; 00154 } 00155 00160 string 00161 BaseType::toString() 00162 { 00163 ostringstream oss; 00164 oss << "BaseType (" << this << "):" << endl 00165 << " _name: " << _name << endl 00166 << " _type: " << type_name() << endl 00167 << " _dataset: " << _dataset << endl 00168 << " _read_p: " << _read_p << endl 00169 << " _send_p: " << _send_p << endl 00170 << " _synthesized_p: " << _synthesized_p << endl 00171 << " d_parent: " << d_parent << endl 00172 << " d_attr: " << hex << &d_attr << dec << endl; 00173 00174 return oss.str(); 00175 } 00176 00185 void 00186 BaseType::dump(ostream &strm) const 00187 { 00188 strm << DapIndent::LMarg << "BaseType::dump - (" 00189 << (void *)this << ")" << endl ; 00190 DapIndent::Indent() ; 00191 00192 strm << DapIndent::LMarg << "name: " << _name << endl ; 00193 strm << DapIndent::LMarg << "type: " << type_name() << endl ; 00194 strm << DapIndent::LMarg << "dataset: " << _dataset << endl ; 00195 strm << DapIndent::LMarg << "read_p: " << _read_p << endl ; 00196 strm << DapIndent::LMarg << "send_p: " << _send_p << endl ; 00197 strm << DapIndent::LMarg << "synthesized_p: " << _synthesized_p << endl ; 00198 strm << DapIndent::LMarg << "parent: " << (void *)d_parent << endl ; 00199 strm << DapIndent::LMarg << "attributes: " << endl ; 00200 DapIndent::Indent() ; 00201 d_attr.dump(strm) ; 00202 DapIndent::UnIndent() ; 00203 00204 DapIndent::UnIndent() ; 00205 } 00206 00209 string 00210 BaseType::name() const 00211 { 00212 return _name; 00213 } 00214 00216 void 00217 BaseType::set_name(const string &n) 00218 { 00219 string name = n; 00220 _name = www2id(name); // www2id writes into its param. 00221 } 00222 00230 string 00231 BaseType::dataset() const 00232 { 00233 return _dataset; 00234 } 00235 00237 Type 00238 BaseType::type() const 00239 { 00240 return _type; 00241 } 00242 00244 void 00245 BaseType::set_type(const Type &t) 00246 { 00247 _type = t; 00248 } 00249 00251 string 00252 BaseType::type_name() const 00253 { 00254 switch (_type) { 00255 case dods_null_c: 00256 return string("Null"); 00257 case dods_byte_c: 00258 return string("Byte"); 00259 case dods_int16_c: 00260 return string("Int16"); 00261 case dods_uint16_c: 00262 return string("UInt16"); 00263 case dods_int32_c: 00264 return string("Int32"); 00265 case dods_uint32_c: 00266 return string("UInt32"); 00267 case dods_float32_c: 00268 return string("Float32"); 00269 case dods_float64_c: 00270 return string("Float64"); 00271 case dods_str_c: 00272 return string("String"); 00273 case dods_url_c: 00274 return string("Url"); 00275 case dods_array_c: 00276 return string("Array"); 00277 case dods_structure_c: 00278 return string("Structure"); 00279 case dods_sequence_c: 00280 return string("Sequence"); 00281 case dods_grid_c: 00282 return string("Grid"); 00283 default: 00284 cerr << "BaseType::type_name: Undefined type" << endl; 00285 return string(""); 00286 } 00287 } 00288 00294 bool 00295 BaseType::is_simple_type() 00296 { 00297 switch (type()) { 00298 case dods_null_c: 00299 case dods_byte_c: 00300 case dods_int16_c: 00301 case dods_uint16_c: 00302 case dods_int32_c: 00303 case dods_uint32_c: 00304 case dods_float32_c: 00305 case dods_float64_c: 00306 case dods_str_c: 00307 case dods_url_c: 00308 return true; 00309 00310 case dods_array_c: 00311 case dods_structure_c: 00312 case dods_sequence_c: 00313 case dods_grid_c: 00314 return false; 00315 } 00316 00317 return false; 00318 } 00319 00323 bool 00324 BaseType::is_vector_type() 00325 { 00326 switch (type()) { 00327 case dods_null_c: 00328 case dods_byte_c: 00329 case dods_int16_c: 00330 case dods_uint16_c: 00331 case dods_int32_c: 00332 case dods_uint32_c: 00333 case dods_float32_c: 00334 case dods_float64_c: 00335 case dods_str_c: 00336 case dods_url_c: 00337 return false; 00338 00339 case dods_array_c: 00340 return true; 00341 00342 case dods_structure_c: 00343 case dods_sequence_c: 00344 case dods_grid_c: 00345 return false; 00346 } 00347 00348 return false; 00349 } 00350 00355 bool 00356 BaseType::is_constructor_type() 00357 { 00358 switch (type()) { 00359 case dods_null_c: 00360 case dods_byte_c: 00361 case dods_int16_c: 00362 case dods_uint16_c: 00363 case dods_int32_c: 00364 case dods_uint32_c: 00365 case dods_float32_c: 00366 case dods_float64_c: 00367 case dods_str_c: 00368 case dods_url_c: 00369 case dods_array_c: 00370 return false; 00371 00372 case dods_structure_c: 00373 case dods_sequence_c: 00374 case dods_grid_c: 00375 return true; 00376 } 00377 00378 return false; 00379 } 00380 00406 int 00407 BaseType::element_count(bool) 00408 { 00409 return 1; 00410 } 00411 00415 bool 00416 BaseType::synthesized_p() 00417 { 00418 return _synthesized_p; 00419 } 00420 00426 void 00427 BaseType::set_synthesized_p(bool state) 00428 { 00429 _synthesized_p = state; 00430 } 00431 00432 // Return the state of _read_p (true if the value of the variable has been 00433 // read (and is in memory) false otherwise). 00434 00443 bool 00444 BaseType::read_p() 00445 { 00446 return _read_p; 00447 } 00448 00482 void 00483 BaseType::set_read_p(bool state) 00484 { 00485 if (! _synthesized_p) { 00486 DBG(cerr << "Changing read_p state of " << name() << " to " 00487 << state << endl); 00488 _read_p = state; 00489 } 00490 } 00491 00502 bool 00503 BaseType::send_p() 00504 { 00505 return _send_p; 00506 } 00507 00516 void 00517 BaseType::set_send_p(bool state) 00518 { 00519 DBG(cerr << "Calling BaseType::set_send_p() for: " << this->name() 00520 << endl); 00521 _send_p = state; 00522 } 00523 00524 00530 AttrTable & 00531 BaseType::get_attr_table() 00532 { 00533 return d_attr; 00534 } 00535 00538 void 00539 BaseType::set_attr_table(const AttrTable &at) 00540 { 00541 d_attr = at; 00542 } 00543 00570 void 00571 BaseType::transfer_attributes(AttrTable *at_container) 00572 { 00573 AttrTable *at = at_container->get_attr_table(name()); 00574 00575 DBG(cerr << "In BaseType::transfer_attributes; processing " << name() << endl); 00576 00577 if (at) { 00578 at->set_is_global_attribute(false); 00579 DBG(cerr << "Processing AttrTable: " << at->get_name() << endl); 00580 00581 AttrTable::Attr_iter at_p = at->attr_begin(); 00582 while (at_p != at->attr_end()) { 00583 DBG(cerr << "About to append " << endl); 00584 DBG(cerr << "attr name,type:" << at->get_name(at_p) << ", " << at->get_type(at_p) << endl); 00585 00586 if (at->get_attr_type(at_p) == Attr_container) 00587 get_attr_table().append_container(new AttrTable(*at->get_attr_table(at_p)), 00588 at->get_name(at_p)); 00589 else 00590 get_attr_table().append_attr(at->get_name(at_p), 00591 at->get_type(at_p), at->get_attr_vector(at_p)); 00592 00593 at_p++; 00594 } 00595 } 00596 } 00597 00609 bool 00610 BaseType::is_in_selection() 00611 { 00612 return d_in_selection; 00613 } 00614 00624 void 00625 BaseType::set_in_selection(bool state) 00626 { 00627 d_in_selection = state; 00628 } 00629 00630 // Protected method. 00637 void 00638 BaseType::set_parent(BaseType *parent) 00639 { 00640 if (!dynamic_cast<Constructor *>(parent) 00641 && !dynamic_cast<Vector *>(parent)) 00642 throw InternalErr("Call to set_parent with incorrect variable type."); 00643 00644 d_parent = parent; 00645 } 00646 00647 // Public method. 00648 00654 BaseType * 00655 BaseType::get_parent() 00656 { 00657 return d_parent; 00658 } 00659 00660 // Documented in the header file. 00661 BaseType * 00662 BaseType::var(const string &/*name*/, bool /*exact_match*/, btp_stack */*s*/) 00663 { 00664 return static_cast<BaseType *>(0); 00665 } 00666 00683 BaseType * 00684 BaseType::var(const string &, btp_stack &) 00685 { 00686 return static_cast<BaseType *>(0); 00687 } 00688 00718 void 00719 BaseType::add_var(BaseType *, Part) 00720 { 00721 throw InternalErr(__FILE__, __LINE__, "BaseType::add_var unimplemented"); 00722 } 00723 00789 bool 00790 BaseType::read() 00791 { 00792 if (_read_p) 00793 return false; 00794 00795 throw InternalErr("Unimplemented BaseType::read() method called."); 00796 } 00797 00798 void 00799 BaseType::intern_data(ConstraintEvaluator &, DDS &dds) 00800 { 00801 dds.timeout_on(); 00802 DBG(cerr << "BaseType::intern_data: " << name() << endl); 00803 if (!read_p()) 00804 read(); // read() throws Error and InternalErr 00805 00806 dds.timeout_off(); 00807 } 00808 00809 #if FILE_METHODS 00810 00852 void 00853 BaseType::print_decl(FILE *out, string space, bool print_semi, 00854 bool constraint_info, bool constrained) 00855 { 00856 // if printing the constrained declaration, exit if this variable was not 00857 // selected. 00858 if (constrained && !send_p()) 00859 return; 00860 00861 fprintf(out, "%s%s %s", space.c_str(), type_name().c_str(), 00862 id2www(_name).c_str()) ; 00863 00864 if (constraint_info) { 00865 if (send_p()) 00866 fprintf(out, ": Send True") ; 00867 else 00868 fprintf(out, ": Send False") ; 00869 } 00870 00871 if (print_semi) 00872 fprintf(out, ";\n") ; 00873 } 00874 #endif 00875 00918 void 00919 BaseType::print_decl(ostream &out, string space, bool print_semi, 00920 bool constraint_info, bool constrained) 00921 { 00922 // if printing the constrained declaration, exit if this variable was not 00923 // selected. 00924 if (constrained && !send_p()) 00925 return; 00926 00927 out << space << type_name() << " " << id2www(_name) ; 00928 00929 if (constraint_info) { 00930 if (send_p()) 00931 out << ": Send True" ; 00932 else 00933 out << ": Send False" ; 00934 } 00935 00936 if (print_semi) 00937 out << ";\n" ; 00938 } 00939 00940 #if FILE_METHODS 00941 00947 void 00948 BaseType::print_xml(FILE *out, string space, bool constrained) 00949 { 00950 if (constrained && !send_p()) 00951 return; 00952 00953 fprintf(out, "%s<%s", space.c_str(), type_name().c_str()); 00954 if (!_name.empty()) 00955 fprintf(out, " name=\"%s\"", id2xml(_name).c_str()); 00956 00957 if (get_attr_table().get_size() > 0) { 00958 fprintf(out, ">\n"); // close the variable's tag 00959 get_attr_table().print_xml(out, space + " ", constrained); 00960 // After attributes, print closing tag 00961 fprintf(out, "%s</%s>\n", space.c_str(), type_name().c_str()); 00962 } 00963 else { 00964 fprintf(out, "/>\n"); // no attributes; just close tag. 00965 } 00966 } 00967 #endif 00968 00975 void 00976 BaseType::print_xml(ostream &out, string space, bool constrained) 00977 { 00978 if (constrained && !send_p()) 00979 return; 00980 00981 out << space << "<" << type_name() ; 00982 if (!_name.empty()) 00983 out << " name=\"" << id2xml(_name) << "\"" ; 00984 00985 if (get_attr_table().get_size() > 0) { 00986 out << ">\n" ; 00987 get_attr_table().print_xml(out, space + " ", constrained); 00988 // After attributes, print closing tag 00989 out << space << "</" << type_name() << ">\n" ; 00990 } 00991 else { 00992 out << "/>\n" ; 00993 } 00994 } 00995 00996 // Compares the object's current state with the semantics of a particular 00997 // type. This will typically be defined in ctor classes (which have 00998 // complicated semantics). For BaseType, an object is semantically correct if 00999 // it has both a non-null name and type. 01000 // 01001 // NB: This is not the same as an invariant -- during the parse objects exist 01002 // but have no name. Also, the bool ALL defaults to false for BaseType. It is 01003 // used by children of CtorType. 01004 // 01005 // Returns: true if the object is semantically correct, false otherwise. 01006 01035 bool 01036 BaseType::check_semantics(string &msg, bool) 01037 { 01038 bool sem = (_type != dods_null_c && _name.length()); 01039 01040 if (!sem) 01041 msg = "Every variable must have both a name and a type\n"; 01042 01043 return sem; 01044 } 01045 01080 bool 01081 BaseType::ops(BaseType *, int) 01082 { 01083 // Even though ops is a public method, it can never be called because 01084 // they will never have a BaseType object since this class is abstract, 01085 // however any of the child classes could by mistake call BaseType::ops 01086 // so this is an internal error. Jose Garcia 01087 throw InternalErr(__FILE__, __LINE__, "Unimplemented operator."); 01088 } 01089 01090 } // namespace libdap