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 / UNDEFINE.CPP < prev    next >
C/C++ Source or Header  |  1994-08-06  |  9KB  |  439 lines

  1.  
  2. /*
  3. * NIST STEP Core Class Library
  4. * clstepcore/STEPundefined.cc
  5. * February, 1994
  6. * KC Morris
  7. * David Sauder
  8.  
  9. * Development of this software was funded by the United States Government,
  10. * and is not subject to copyright.
  11. */
  12.  
  13. /* $Id: STEPundefined.cc,v 2.0.1.1 1994/04/05 16:39:53 sauderd Exp $ */
  14.  
  15. #include <stdio.h> // to get the BUFSIZ #define
  16. #include <undefine.h>
  17. #include <attribut.h>
  18.  
  19.  
  20. /******************************************************************
  21.  **    helping functions for reading unknown types        **/
  22.  
  23.  
  24. // return 1 if 'in' points at a quoted quote otherwise return 0
  25.  
  26. int
  27. QuoteInString(istream& in)
  28. {
  29.     char c1, c2;
  30.     if( in.good() )
  31.     {
  32.     in >> c1;
  33.     in >> c2;
  34.     in.putback(c2);
  35.     in.putback(c1);
  36.     if (c1 == STRING_DELIM)
  37.     {
  38.         if(c2 == STRING_DELIM)
  39.         return 1;
  40.     }
  41.     }
  42.     return 0;
  43. }    
  44.  
  45. // assign 's' so that it contains an exchange file format string read from 
  46. // 'in'.  The string 's' is assigned starting at 'index'.  The string 's' is 
  47. // of size 'sSize'.  If assigning the string causes 's' to run out of room, 
  48. // 's' is deleted and reallocated larger and 's' and 'sSize' are returned 
  49. // changed as appropriate.  'index' is changed and returned so that it points
  50. // into 's' past the string that was assigned. 
  51.  
  52. void
  53. PushPastString (istream& in, SCLstring &s, ErrorDescriptor *err)
  54. {
  55.     char messageBuf[BUFSIZ];
  56.     messageBuf[0] = '\0';
  57.  
  58.     char c;
  59.     in >> c;
  60.     while(isspace(c))
  61.     {
  62.     s.Append(c);
  63.     in >> c;
  64.     }
  65.     if(c == STRING_DELIM)
  66.     {
  67.     s.Append(c);
  68.     while(QuoteInString(in))
  69.     {
  70.         in.get(c);
  71.         s.Append(c);
  72.         in.get(c);
  73.         s.Append(c);
  74.     }
  75.     in.get(c);
  76.     //int sk = in.skip(0);
  77.     while( ( c != STRING_DELIM) && !in.eof() )
  78.     {
  79.         s.Append(c);
  80.         while(QuoteInString(in))
  81.         {
  82.         in.get(c);
  83.         s.Append(c);
  84. //        AppendChar(c, index, s, sSize);
  85.         in.get(c);
  86.         s.Append(c);
  87. //        AppendChar(c, index, s, sSize);
  88.         }
  89.         in.get(c);
  90.     }
  91.     //in.skip(sk);
  92.     
  93.     if(c != STRING_DELIM)
  94.     {
  95.         err->GreaterSeverity(SEVERITY_INPUT_ERROR);
  96.         sprintf(messageBuf, "Invalid string value.\n");
  97.         err->AppendToDetailMsg(messageBuf);
  98.         s.Append('\'');
  99.     }
  100.     else
  101.         s.Append(c);
  102.     }
  103. }
  104.  
  105. // assign 's' so that it contains an exchange file format aggregate read from 
  106. // 'in'.  The string 's' is assigned starting at 'index'.  The string 's' is 
  107. // of size 'sSize'.  If assigning the aggregate causes 's' to run out of room, 
  108. // 's' is deleted and reallocated larger and 's' and 'sSize' are returned 
  109. // changed as appropriate.  'index' is changed and returned so that it points
  110. // into 's' past the aggregate that was assigned. 
  111. // This is used to read aggregates that are part of multidimensional 
  112. // aggregates.
  113.  
  114. void
  115. PushPastImbedAggr (istream& in, SCLstring &s, ErrorDescriptor *err)
  116. {
  117.     char messageBuf[BUFSIZ];
  118.     messageBuf[0] = '\0';
  119.  
  120.     char c;
  121.     in.get(c);
  122.     while(isspace(c))
  123.     {
  124.     s.Append(c);
  125.     in.get(c);
  126.     }
  127.  
  128.     if(c == '(')
  129.     {
  130.     s.Append(c);
  131.     in.get(c);
  132.     while( (c != ')') && !in.eof() )
  133.     {
  134.         if ( c == '(' )  
  135.         {
  136.         in.putback(c);
  137.         PushPastImbedAggr(in, s, err);
  138.         }
  139.         else if (c == STRING_DELIM)
  140.         {
  141.         in.putback(c);
  142.         PushPastString(in, s, err);
  143.         }
  144.         else
  145.         s.Append(c);
  146.         in.get(c);
  147.     }    
  148.     if(c != ')')
  149.     {
  150.         err->GreaterSeverity(SEVERITY_INPUT_ERROR);
  151.         sprintf(messageBuf, "Invalid aggregate value.\n");
  152.         err->AppendToDetailMsg(messageBuf);
  153.         s.Append(')');
  154.     }
  155.     else
  156.         s.Append(c);
  157.     }
  158. }
  159.  
  160.  
  161. // assign 's' so that it contains an exchange file format aggregate read from 
  162. // 'in'.  The string 's' is assigned starting at 'index'.  The string 's' is 
  163. // of size 'sSize'.  If assigning the aggregate causes 's' to run out of room, 
  164. // 's' is deleted and reallocated larger and 's' and 'sSize' are returned 
  165. // changed as appropriate.  'index' is changed and returned so that it points
  166. // into 's' past the aggregate that was assigned. 
  167. // This is used to read a single dimensional aggregate (i.e. it is not allowed
  168. // to contain an aggregate as an element.
  169.  
  170. void
  171. PushPastAggr1Dim(istream& in, SCLstring &s, ErrorDescriptor *err)
  172. {
  173.     char messageBuf[BUFSIZ];
  174.     messageBuf[0] = '\0';
  175.  
  176.     char c;
  177.     in.get(c);
  178.     while(isspace(c))
  179.     {
  180.     s.Append(c);
  181.     in.get(c);
  182.     }
  183.  
  184.     if(c == '(')
  185.     {
  186.     s.Append(c);
  187.     in.get(c);
  188.     while( (c != ')') && !in.eof() )
  189.     {
  190.         if ( c == '(' )  
  191.         {
  192.         err->GreaterSeverity(SEVERITY_WARNING);
  193.         sprintf(messageBuf, "Invalid aggregate value.\n");
  194.         err->AppendToDetailMsg(messageBuf);
  195.         }
  196.  
  197.         if (c == STRING_DELIM)
  198.         {
  199.         in.putback(c);
  200.         PushPastString(in, s, err);
  201.         }
  202.         else
  203.         s.Append(c);
  204.         in.get(c);
  205.     }    
  206.     if(c != ')')
  207.     {
  208.         err->GreaterSeverity(SEVERITY_INPUT_ERROR);
  209.         sprintf(messageBuf, "Invalid aggregate value.\n");
  210.         err->AppendToDetailMsg(messageBuf);
  211.         s.Append(')');
  212.     }
  213.     else
  214.         s.Append(c);
  215.     }
  216. }
  217.  
  218.  
  219. Severity 
  220. SCLundefined::StrToVal(const char *s, ErrorDescriptor *err)
  221. {
  222.     val = s;
  223.     return SEVERITY_NULL;
  224. }
  225.  
  226. Severity 
  227. SCLundefined::StrToVal(istream &in, ErrorDescriptor *err)
  228. {
  229.     return STEPread(in, err);
  230. }
  231.  
  232. Severity 
  233. SCLundefined::STEPread(const char *s, ErrorDescriptor *err)
  234. {
  235.     istrstream in((char *) s);
  236.     return STEPread(in, err);
  237. }
  238.  
  239. Severity 
  240. SCLundefined::STEPread(istream &in, ErrorDescriptor *err)
  241. {
  242.     char c = '\0';
  243.     strstream ss;
  244.     SCLstring str;
  245.  
  246.     int terminal = 0;
  247.  
  248.     in >> ws; // skip white space
  249.     in >> c;
  250.     if(c == '$')
  251.     {
  252.     val = "";
  253.     CheckRemainingInput(in, err, "aggregate item", ",)");
  254.     }
  255.     else
  256.     in.putback(c);
  257.  
  258.     while (!terminal)  
  259.     {
  260.     in.get(c);
  261.     switch (c)  
  262.     {
  263.       case '(':
  264.         in.putback(c);
  265.  
  266.         PushPastImbedAggr(in, str, err);
  267.         ss << str.chars();
  268.         break;
  269.  
  270.       case '\'':
  271.         in.putback(c);
  272.  
  273.         PushPastString(in, str, err);
  274.         ss << str.chars();
  275.         break;
  276.  
  277.       case ',':    
  278.         terminal = 1; // it's a STEPattribute separator
  279.         in.putback (c);
  280.         c = '\0';
  281.         break;
  282.  
  283.       case ')':
  284.         in.putback (c);
  285.         terminal = 1; // found a valid delimiter
  286.         break;
  287.  
  288.       case '\0':
  289.       case EOF:
  290.         terminal = 1; // found a valid delimiter
  291.         break;
  292.  
  293.       default:
  294.         ss.put(c);
  295.         break;
  296.     }
  297.  
  298.     if (!in.good ()) {
  299.         terminal =1;
  300.         c = '\0';  
  301.     }
  302. //      if (!in.readable ()) terminal =1;
  303.     }      
  304.  
  305.     ss << ends;
  306.     val = ss.str();
  307.  
  308.     err->GreaterSeverity(SEVERITY_NULL);
  309.     return SEVERITY_NULL;
  310. }
  311.  
  312. const char *
  313. SCLundefined::asStr(SCLstring & s) const
  314. {
  315.     s = val.chars();
  316.     return s.chars();
  317. }
  318.  
  319. const char *
  320. SCLundefined::STEPwrite(SCLstring &s)
  321. {
  322.     if(val.rep())
  323.     {
  324.     s = val.chars();
  325.     }
  326.     else 
  327.     s = "$";
  328.     return s.chars();
  329. }
  330.  
  331. void 
  332. SCLundefined::    STEPwrite (ostream& out)
  333. {
  334.     if(val.rep())
  335.     out << val.chars();
  336.     else 
  337.     out << "$";
  338. }
  339.  
  340. SCLundefined& 
  341. SCLundefined::operator= (const SCLundefined& x)  
  342. {
  343.     SCLstring tmp;
  344.     val = x.asStr(tmp);
  345.     return *this;
  346. }
  347.  
  348. SCLundefined& 
  349. SCLundefined::operator= (const char * str)
  350. {
  351.     if (!str)
  352.     val.set_null();
  353.     else
  354.     val = str;
  355.     return *this;
  356. }
  357.  
  358. SCLundefined::SCLundefined ()  
  359. {
  360. }
  361.  
  362. SCLundefined::~SCLundefined ()  
  363. {
  364. }
  365.  
  366. int
  367. SCLundefined::set_null ()  
  368. {
  369.     val = "";
  370.     return 1;
  371. }
  372.  
  373. int
  374. SCLundefined::is_null ()  
  375. {
  376.     return (!strcmp (val.chars(), ""));
  377.     
  378. }
  379.  
  380.  
  381. /*
  382. int
  383. SCLundefined::STEPread(istream& in )  
  384. {
  385.     char c ='\0';
  386.     char buf [BUFSIZ];
  387.     int i =0;
  388.     int open_paren =0;
  389.     int terminal = 0;
  390.     
  391.     while (!terminal)  
  392.       {
  393.       in >> c;
  394.       switch (c)  
  395.         {
  396.           case '(':
  397.         ++open_paren;
  398.         break;
  399.           case ')':
  400.         if (open_paren)  {
  401.               --open_paren;
  402.               break; 
  403.           }
  404.         // otherwise treat it like a comma
  405.           case ',':
  406.         if (!open_paren)  {
  407.             terminal =1;
  408.             in.putback (c);
  409.             c = '\0';
  410.         }
  411.         
  412.         break;
  413.           case '\0':
  414.         terminal =1;
  415.         break;
  416.         
  417.         }        
  418.  
  419.       if (!in)
  420.       {
  421.           terminal =1;
  422.           c = '\0';  
  423.       }
  424.       if (i < BUFSIZ) buf [i] = c;
  425.  
  426.       // BUG:  read up to BUFSIZ -1 number of characters
  427.       // if more characters, NULL terminate and ignore the rest of input 
  428.       if ((++i == BUFSIZ) && !terminal)  {
  429.           cerr << "WARNING:  information lost -- value of undefined type is too long\n";
  430.           buf [i] = '\0';
  431.       }
  432.  
  433.       }      
  434.     if (i < BUFSIZ) buf [i+1] = '\0';
  435.     val = buf;
  436.     return i;
  437. }
  438. */
  439.