home *** CD-ROM | disk | FTP | other *** search
/ ftptest.leeds.ac.uk / 2015.02.ftptest.leeds.ac.uk.tar / ftptest.leeds.ac.uk / bionet / CAE-GROUP / SCL-WIN3x / SCL.EXE / SELECT.CPP < prev    next >
C/C++ Source or Header  |  1994-08-06  |  11KB  |  452 lines

  1.  
  2. /*
  3. * NIST STEP Core Class Library
  4. * clstepcore/STEPselect.cc
  5. * February, 1994
  6. * Dave Helfrick
  7. * KC Morris
  8.  
  9. * Development of this software was funded by the United States Government,
  10. * and is not subject to copyright.
  11. */
  12.  
  13. /* $Id: STEPselect.cc,v 2.0.1.2 1994/05/10 21:04:46 kc Exp $ */
  14.  
  15. #include <select.h>
  16. #include <stdio.h> // to get the BUFSIZ #define
  17. #include <ExpDict.h>
  18. #include <strstream.h>
  19. #include <Sentity.h>
  20.  
  21. /**********
  22.     (member) functions for the select class SdaiSelect
  23. **********/
  24. /*
  25. SdaiSelect::SdaiSelect()
  26. {
  27. }
  28. SdaiSelect::SdaiSelect( const SdaiSelect& s )
  29. {
  30. }
  31. */
  32.  
  33. Severity SdaiSelect::severity() const
  34. {        /**  fn Severity  **/
  35.    return _error.severity();
  36. }        /**  end function  **/
  37.  
  38. Severity  SdaiSelect::severity( Severity s )
  39. {        /**  fn Severity  **/
  40.    return _error.severity( s );
  41. }        /**  end function  **/
  42.  
  43. const char * SdaiSelect::Error()
  44. {        /**  fn Error  **/
  45.    return _error.DetailMsg();
  46. }        /**  end function  **/
  47.  
  48. void SdaiSelect::Error( char * e )
  49. {        /**  fn Error  **/
  50.    _error.DetailMsg( e );
  51. }        /**  end function  **/
  52.  
  53. void  SdaiSelect::ClearError()
  54. {        /**  end function  **/
  55.    _error.ClearErrorMsg();
  56. }        /**  end function  **/
  57.  
  58. const TypeDescriptor *
  59. SdaiSelect::CanBe (const char * n) const  
  60. {
  61.     const TypeDescLinkNode * tdn = 
  62.         (const TypeDescLinkNode *) _type -> GetElements ().GetHead ();
  63.     const TypeDescriptor *td = tdn -> TypeDesc ();
  64.  
  65.     SCLstring tmp1;
  66.     SCLstring tmp2;
  67.   
  68.     while (tdn)  {
  69.     td = tdn -> TypeDesc ();
  70.     if (strcmp( (char *)StrToUpper( n, tmp1 ), 
  71.             (char *)StrToUpper( td->Name(), tmp2 ) ))
  72.         ;
  73.     else 
  74.         return td;  // they are the same 
  75.     tdn = (TypeDescLinkNode *) (tdn -> NextNode ());
  76.     }
  77.     return 0;
  78. }
  79.  
  80.  
  81. const TypeDescriptor *
  82. SdaiSelect::CanBe (BASE_TYPE bt) const  
  83. {
  84.   const TypeDescLinkNode * tdn = (const TypeDescLinkNode *) _type -> GetElements ().GetHead ();
  85.   const TypeDescriptor *td = tdn -> TypeDesc ();
  86.  
  87.   while (tdn)  {
  88.     td = tdn -> TypeDesc ();    
  89.     if (td -> NonRefType () == bt)
  90.       return td;  // they are the same 
  91.     tdn = (TypeDescLinkNode *) (tdn -> NextNode ());
  92.   }
  93.   return 0;
  94. }
  95.  
  96. const TypeDescriptor *
  97. SdaiSelect::CanBe (const TypeDescriptor * td) const  
  98. {
  99.   return _type -> CanBe (td);
  100. }
  101.  
  102. SdaiString SdaiSelect::UnderlyingTypeName () const  
  103. {
  104.   return underlying_type-> Name ();
  105. }
  106.  
  107. const TypeDescriptor *  SdaiSelect::CurrentUnderlyingType() const 
  108. {
  109.   return underlying_type;
  110. }
  111.  
  112. const TypeDescriptor * 
  113. SdaiSelect::SetUnderlyingType (const TypeDescriptor * td)
  114. {
  115.   //  don\'t do anything if the descriptor is bad
  116.   if (!td || !(_type -> CanBe (td))) return 0;
  117.  
  118.   base_type = td -> NonRefType ();
  119.   return underlying_type = td;
  120. }
  121.  
  122. int SdaiSelect::exists() const 
  123. {
  124.   return (int) underlying_type;
  125. }
  126.  
  127. void SdaiSelect::nullify() 
  128. {
  129.   underlying_type = 0;
  130. }
  131.  
  132. Severity 
  133. SdaiSelect::SelectValidLevel(const char *attrValue, ErrorDescriptor *err, 
  134.                  InstMgr *im, int clearError)
  135. {
  136.   SdaiSelect * tmp = NewSelect();
  137.   Severity s = SEVERITY_NULL;
  138.   if (CurrentUnderlyingType ())
  139.     s = tmp -> StrToVal 
  140.       (attrValue, CurrentUnderlyingType () -> Name (), err, im);
  141.   else 
  142.     s = tmp -> STEPread (istrstream ((char*)attrValue), err, im);
  143.   delete tmp;
  144.   return s;
  145. }
  146.  
  147. Severity 
  148. SdaiSelect::StrToVal(const char *val, const char *selectType, 
  149.              ErrorDescriptor *err, InstMgr * instances)
  150. {
  151.     if (SetUnderlyingType (CanBe (selectType)))  
  152.  
  153.     //  the underlying type is set to a valid type
  154.     // call read on underlying type in subclass
  155.  
  156.     switch (base_type)  {
  157.       case ENTITY_TYPE:
  158.       {  
  159.         STEPentity * tmp = 
  160.           ReadEntityRef (val, err, ",)", instances, 0);
  161.         if( tmp && ( tmp != ENTITY_NULL ) && AssignEntity(tmp) ) 
  162.           return SEVERITY_NULL;  
  163.         else
  164.         {
  165.         err->AppendToDetailMsg( 
  166.         "Reference to entity that is not a valid type for SELECT.\n" );
  167.         nullify ();       
  168.         err->GreaterSeverity( SEVERITY_WARNING );
  169.         return SEVERITY_WARNING;
  170.         }    
  171.       }
  172.  
  173.         // call StrToVal on the contents
  174.       case STRING_TYPE:
  175.       case AGGREGATE_TYPE:
  176.       case ENUM_TYPE:
  177.       case SELECT_TYPE:
  178.       case BOOLEAN_TYPE:
  179.       case LOGICAL_TYPE:
  180.       {
  181.           err->GreaterSeverity( StrToVal_content (val, instances) );
  182.           if(_error.severity() != SEVERITY_NULL)
  183.           err->AppendFromErrorArg(&_error);
  184.           return err->severity();
  185.       }
  186.  
  187.         // do the same as STEPread_content
  188.       case BINARY_TYPE:
  189.       case NUMBER_TYPE:
  190.       case REAL_TYPE:
  191.       case INTEGER_TYPE:
  192.       default:
  193.       {
  194.           err->GreaterSeverity( STEPread_content( istrstream((char*)val) ) );
  195.           if(_error.severity() != SEVERITY_NULL)
  196.           err->AppendFromErrorArg(&_error);
  197.           return err->severity();
  198.       }
  199.     }
  200.     return SEVERITY_INPUT_ERROR;
  201. }
  202.  
  203. Severity
  204. SdaiSelect::STEPread  (istream& in, ErrorDescriptor *err, InstMgr * instances, 
  205.                int addFileId)
  206. {
  207.     char c ='\0';
  208.     SCLstring tmp;
  209.  
  210.     // find out what case we have
  211.     //  NOTE case C falls out of recursive calls in cases A and B
  212.  
  213.     in >> ws;
  214.     in >> c;
  215. //    c = in.peek();
  216.     if( isalpha(c) ) //  case B 
  217.     {
  218.     //  token is a type name - get the type
  219.       int eot =0;  // end of token flag
  220.     while( (c != '(') && in.good() )
  221.     {
  222.         if (!eot && ! (eot = isspace (c)) ) 
  223.           // as long as eot hasn\'t been reached keep appending
  224.         tmp.Append(c);
  225.         in >> c; 
  226.     }
  227.  
  228.     //  check for valid type and  set the underlying type 
  229.  
  230.     if( SetUnderlyingType( CanBe( tmp.chars() ) ) )
  231.     {  //  assign the value to the underlying type
  232.         in >> ws; // skip white space
  233.         STEPread_content (in, instances, addFileId);
  234. //  STEPread_content uses the ErrorDesc data member from the STEPselect class
  235.         err->AppendToDetailMsg (Error ());
  236.         err->GreaterSeverity( severity () );
  237.         in >> ws >> c;  
  238.         if( c != ')' )
  239.         {
  240.         err->AppendToDetailMsg( 
  241.                 "Missing closing ')' for SELECT type.\n" );
  242.         err->GreaterSeverity( SEVERITY_WARNING );
  243.         in.putback (c);
  244.         return SEVERITY_WARNING;
  245.         }
  246.         return err->severity();
  247.     }
  248.     else // ERROR  -- the type wasn't one of the choices
  249.     {
  250.         if( !in.good() )
  251.         {
  252.         err->GreaterSeverity( SEVERITY_INPUT_ERROR );
  253.         return SEVERITY_INPUT_ERROR;
  254.         }
  255.         else
  256.         {
  257.         err->AppendToDetailMsg (
  258.             "The type name for the SELECT type is not valid.\n");
  259.         err->GreaterSeverity (SEVERITY_WARNING);
  260.         return SEVERITY_WARNING;
  261.         }
  262.     }
  263. /*    if (!in.good())
  264.     {
  265.         err->GreaterSeverity( SEVERITY_INPUT_ERROR );
  266.         return SEVERITY_INPUT_ERROR;
  267.     }
  268. */
  269.     }
  270.     else // case A
  271.     {    //  the type can be determined from the value 
  272.       if (_type && ! _type -> UniqueElements ())  {
  273.     err->AppendToDetailMsg("Type for value of SELECT is ambiguous.\n" );
  274.     err->GreaterSeverity( SEVERITY_WARNING );
  275.       }
  276.     switch (c)
  277.     {
  278.       case '$':  
  279.         nullify ();
  280.         err->GreaterSeverity (SEVERITY_INCOMPLETE);
  281.         return SEVERITY_INCOMPLETE;
  282.  
  283.       case ',':
  284.       case '\0':
  285.         // ERROR  IN INPUT
  286.         in.putback (c);
  287.         err->AppendToDetailMsg( "No value found for SELECT type.\n" );
  288.         err->GreaterSeverity( SEVERITY_WARNING );
  289.         return SEVERITY_WARNING;
  290.  
  291.       case '.': // assign enum
  292.         base_type = ENUM_TYPE;
  293.         break;
  294.         // set the underlying type
  295.         // call STEPread
  296.         // return
  297.  
  298.       case '\'': // assign string
  299.         base_type = STRING_TYPE;
  300.         break;
  301.  
  302.       case '#':
  303.         base_type = ENTITY_TYPE;
  304.         break;
  305.         // call STEPread_reference
  306.         // set the underlying type
  307.  
  308.         // assign entity
  309.         // read the reference
  310.         // match type to underlying type
  311.         // assign the value
  312.         // set the underlying type
  313.  
  314.       case '(':
  315.       {
  316.         char n;
  317.         in >> n;
  318.         in.putback(n);
  319.         if( isalpha(n) )  
  320.         base_type = SELECT_TYPE;
  321.         else 
  322.         base_type = AGGREGATE_TYPE;
  323.         in.putback( n );
  324.         break;
  325.       }
  326.  
  327.       case '0':
  328.       case '1':
  329.       case '2':
  330.       case '3':
  331.       case '4':
  332.       case '5':
  333.       case '6':
  334.       case '7':
  335.       case '8':
  336.       case '9':
  337.       case '-':
  338.         if( CanBe( REAL_TYPE ) ) 
  339.         base_type = REAL_TYPE;
  340.         else     
  341.         base_type = INTEGER_TYPE;
  342.         break;
  343.  
  344.       default:
  345.         // ambiguous - ERROR:  underlying type should have been set
  346.         //         STEPread_error ();
  347.         err->AppendToDetailMsg( 
  348.             "type for SELECT could not be determined from value.\n" );
  349.         nullify ();
  350.         in.putback (c);
  351.         err->GreaterSeverity( SEVERITY_WARNING );
  352.         return SEVERITY_WARNING;
  353.     }
  354.  
  355.     in.putback (c);
  356.  
  357.     // now the type descriptor should be derivable from the base_type
  358.     if( base_type == ENTITY_TYPE ) 
  359.     {  // you don\'t know if this is an ENTITY or a SELECT
  360.         // have to do this here - not in STEPread_content
  361.         STEPentity * tmp = 
  362.         ReadEntityRef (in, err, ",)", instances, addFileId);
  363.         if( tmp && ( tmp != ENTITY_NULL ) && AssignEntity(tmp) ) 
  364.         return SEVERITY_NULL;  
  365.         else
  366.         {
  367.         err->AppendToDetailMsg( 
  368.         "Reference to entity that is not a valid type for SELECT.\n" );
  369.         nullify ();       
  370.         err->GreaterSeverity( SEVERITY_WARNING );
  371.         return SEVERITY_WARNING;
  372.         }    
  373.     }
  374.     else if ( SetUnderlyingType( CanBe(base_type) ) )
  375.         STEPread_content( in, instances, addFileId );
  376.  
  377.     else // ERROR  -- the type wasn\'t one of the choices
  378.     {
  379.         err->AppendToDetailMsg( 
  380.                 "The type of the SELECT type is not valid.\n");
  381.         err->GreaterSeverity( SEVERITY_WARNING );
  382.         return SEVERITY_WARNING;
  383.     }
  384.     }
  385. //    if (!in.good())    severity (SEVERITY_INPUT_ERROR);
  386.     return err->severity ();
  387. }
  388.  
  389.  
  390. void
  391. SdaiSelect::STEPwrite(ostream& out)  const
  392. {
  393.     if (!exists ()) {  
  394.     out << "$";
  395.     return;
  396.     }
  397.     if (_type -> UniqueElements ())
  398.     STEPwrite_content( out ); 
  399.     else
  400.     {
  401.     SCLstring tmp;
  402.     out << StrToUpper( CurrentUnderlyingType()->Name(), tmp ) << "(";
  403.     STEPwrite_content (out); 
  404.     out << ")";
  405.     }
  406. }
  407.  
  408. const char *
  409. SdaiSelect::STEPwrite(SCLstring& s)  const
  410. {
  411.   strstream buf;
  412.   STEPwrite (buf);
  413.   buf << ends;  // have to add the terminating \0 char
  414.   char * tmp;
  415.   tmp = buf.str ();
  416.   s = tmp;
  417.   delete tmp;
  418.   return s;
  419. }
  420.  
  421.  
  422. //SdaiSelect& 
  423. //SdaiSelect::operator= (SdaiSelect& x)  
  424. //{
  425. //    return *this;
  426. //}
  427.  
  428. #ifdef OBSOLETE
  429. char* 
  430. SdaiSelect::asStr()  const
  431. {
  432.   strstream buf;
  433.   STEPwrite (buf);
  434. /*  STEPwrite_content (buf);*/
  435.   buf << ends;  // have to add the terminating \0 char
  436.   return buf.str ();  // need to delete this space
  437. }
  438. #endif
  439.  
  440. int
  441. SdaiSelect::set_null ()  
  442. {
  443.   nullify ();
  444.     return 1;
  445. }
  446.  
  447. int
  448. SdaiSelect::is_null ()  
  449. {
  450.   return (!exists ());
  451. }
  452.