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

  1.  
  2. /*
  3. * NIST STEP Core Class Library
  4. * clstepcore/SdaiBinary.cc
  5. * November, 1994
  6. * David Sauder
  7.  
  8. * Development of this software was funded by the United States Government,
  9. * and is not subject to copyright.
  10. */
  11.  
  12. /* $Id: SdaiBinary.cc,v */
  13.  
  14. #include <Binary.h>
  15.  
  16. extern Severity 
  17. CheckRemainingInput(istream &in, ErrorDescriptor *err, 
  18.             const char *typeName, // used in error message
  19.             const char *tokenList); // e.g. ",)"
  20.  
  21. void 
  22. SdaiBinary::STEPwrite (ostream& out) const
  23. {
  24.     const char *str = 0;
  25.     if (is_null ())
  26.     out << "$";
  27.     else
  28.     {
  29.     out << '\"';
  30.     str = chars ();
  31.     while (*str)
  32.     {
  33.         out << *str;
  34.         str++;
  35.     }
  36.     out << '\"';
  37.     }
  38. }
  39.  
  40. const char * 
  41. SdaiBinary::STEPwrite (SCLstring &s) const
  42. {
  43.     const char *str = 0;
  44.     if (is_null ())
  45.     {
  46. //    s.set_null(); // this would free up space? nope
  47.     s = "$";
  48.     }
  49.     else
  50.     {
  51.     s = "\"";
  52.     str = chars ();
  53.     while (*str)
  54.     {
  55.         s.Append(*str);
  56.         str++;
  57.     }
  58.     s.Append(BINARY_DELIM);
  59.     }
  60.     return s.chars();
  61. }
  62.  
  63. Severity 
  64. SdaiBinary::ReadBinary(istream& in, ErrorDescriptor *err, int AssignVal,
  65.                int needDelims)
  66. {
  67.     if(AssignVal)
  68.     set_null();
  69.  
  70.     SCLstring str;
  71.     char c;
  72.     char messageBuf[512];
  73.     messageBuf[0] = '\0';
  74.  
  75.     int validDelimiters = 1;
  76.  
  77.     in >> ws; // skip white space
  78.  
  79.     if( in.good() )
  80.     {
  81.     in.get(c);
  82.     if( (c == '\"') || isxdigit(c) )
  83.     {
  84.         if( c == '\"' )
  85.         {
  86.         in.get(c); // push past the delimiter
  87.         // since found a valid delimiter it is now invalid until the 
  88.         //   matching ending delim is found
  89.         validDelimiters = 0;
  90.         }
  91.         while(in.good() && isxdigit(c))
  92.         {
  93.         str.Append(c);
  94.         in.get(c);
  95.         }
  96.         if(in.good() && (c != '\"') )
  97.         in.putback(c);
  98.         if( AssignVal && (str.Length() > 0) )
  99.         operator= (str.chars());
  100.  
  101.         if(c == '\"') // if found ending delimiter
  102.         {
  103.         // if expecting delim (i.e. validDelimiter == 0)
  104.         if(!validDelimiters) 
  105.         {
  106.             validDelimiters = 1; // everything is fine
  107.         }
  108.         else // found ending delimiter but no initial delimiter
  109.         {
  110.             validDelimiters = 0;
  111.         }
  112.         }
  113.         // didn't find any delimiters at all and need them.
  114.         else if(needDelims) 
  115.         {
  116.         validDelimiters = 0;
  117.         }
  118.  
  119.         if (!validDelimiters)
  120.         {    
  121.         err->GreaterSeverity(SEVERITY_WARNING);
  122.         if(needDelims)
  123.             sprintf(messageBuf, 
  124.                 "Binary value missing double quote delimiters.\n");
  125.         else
  126.             sprintf(messageBuf, 
  127.                "Mismatched double quote delimiters for binary.\n");
  128.         err->AppendToDetailMsg(messageBuf);
  129.         err->AppendToUserMsg(messageBuf);
  130.         }
  131. /*
  132.         else if ( !isspace(b[0]) && (b[0] != '\0') )
  133.         {    
  134.         err->GreaterSeverity(SEVERITY_WARNING);
  135.         sprintf(messageBuf, "Invalid binary value.\n");
  136.         err->AppendToDetailMsg(messageBuf);
  137.         }
  138. */
  139.     }
  140.     else
  141.     {
  142.         err->GreaterSeverity(SEVERITY_WARNING);
  143.         sprintf(messageBuf, "Invalid binary value.\n");
  144.         err->AppendToDetailMsg(messageBuf);
  145.         err->AppendToUserMsg(messageBuf);
  146.     }
  147.     }
  148.     else
  149.     {
  150.     err->GreaterSeverity(SEVERITY_INCOMPLETE);
  151.     }
  152.     return err->severity();
  153. }
  154.  
  155. Severity
  156. SdaiBinary::StrToVal (const char * s, ErrorDescriptor *err)
  157. {
  158.     istrstream in ((char *)s); // sz defaults to length of s
  159.     return ReadBinary(in, err, 1, 0);
  160. }
  161.  
  162. /*
  163.     operator= (s);
  164.     if (! strcmp (chars (),  s))  return SEVERITY_NULL ; 
  165.     else return SEVERITY_INPUT_ERROR; 
  166. */
  167. /////////////////////////////////////////////////
  168. #ifdef bpzdofbqlvzbfxck
  169.     set_null();
  170.  
  171.     char messageBuf[BUFSIZ];
  172.     messageBuf[0] = '\0';
  173.  
  174.     int validDelimiters = 1;
  175.     char *start = 0;
  176.  
  177.     char *b = s;
  178.     while( *b && isspace(*b) )
  179.     b++;
  180.  
  181.     if( *b )
  182.     {
  183.     if( (b[0] == '\"') || isxdigit(b[0]) )
  184.     {
  185.         if( b[0] == '\"' )
  186.         {
  187.         b++; // push past the delimiter
  188.         // since found a valid delimiter it is now invalid until the 
  189.         //   matching ending delim is found
  190.         validDelimiters = 0;
  191.         }
  192.         if(isxdigit(b[0]))
  193.         start = b;
  194.         while(isxdigit(b[0]))
  195.         {
  196.         b++;
  197.         }
  198.  
  199.         if(b[0] == '\"') // if found ending delimiter
  200.         {
  201.         // if expecting delim (i.e. validDelimiter == 0)
  202.         if(!validDelimiters) 
  203.         {
  204.             validDelimiters = 1; // everything is fine
  205.             b++; // push past closing quote
  206.         }
  207.         else // found ending delimiter but no initial delimiter
  208.         {
  209.             validDelimiters = 0;
  210.         }
  211.         }
  212.         // didn't find any delimiters at all and need them.
  213.         else if(NeedDelims) 
  214.         {
  215.         validDelimiters = 0;
  216.         }
  217.  
  218.         if (!validDelimiters)
  219.         {    
  220.         err->GreaterSeverity(SEVERITY_WARNING);
  221.         sprintf(messageBuf, 
  222.             "Binary value missing double quote delimiters. \n");
  223.         }
  224.         else if ( !isspace(b[0]) && (b[0] != '\0') )
  225.         {    
  226.         err->GreaterSeverity(SEVERITY_WARNING);
  227.         sprintf(messageBuf, "Invalid binary value.\n");
  228.         err->AppendToDetailMsg(messageBuf);
  229.         }
  230.     }
  231.     else
  232.     {
  233.         err->GreaterSeverity(SEVERITY_WARNING);
  234.         sprintf(messageBuf, "Invalid binary value.\n");
  235.         err->AppendToDetailMsg(messageBuf);
  236.     }
  237.     }
  238.     else
  239.     {
  240.     err->GreaterSeverity(SEVERITY_INCOMPLETE);
  241.     }
  242.     return b;
  243. #endif
  244.  
  245. //  STEPread reads a binary in exchange file format
  246. //  delimited by double quotes
  247.  
  248. Severity 
  249. SdaiBinary::STEPread (istream& in, ErrorDescriptor *err)
  250. {
  251.     return ReadBinary(in, err, 1, 1);
  252. /*
  253.     int foundEndQuote = 0; // need so this string is not ok: 'hi''
  254.     set_null ();  // clear the old string
  255.     char c;
  256.     in >> ws; // skip white space
  257.     in >> c;
  258.  
  259.     // remember the current format state to restore the previous settings
  260.     long int flags = in.flags();
  261.     in.unsetf(ios::skipws);
  262.  
  263.     if (c == BINARY_DELIM)
  264.     {
  265.     while( in.good() && in.get(c) && (c != BINARY_DELIM) )
  266.     {
  267.         Append (c);
  268.     }
  269.     Append ('\0');
  270.  
  271.     if(c == BINARY_DELIM)
  272.         return SEVERITY_NULL;
  273.     else
  274.     {    // non-recoverable error
  275.         err->AppendToDetailMsg("Missing closing quote on binary value.\n");
  276.         err->AppendToUserMsg("Missing closing quote on binary value.\n");
  277.         err->GreaterSeverity(SEVERITY_INPUT_ERROR);
  278.         return SEVERITY_INPUT_ERROR;
  279.     }
  280.     }
  281.     //  otherwise there was not a quote
  282.     in.putback (c);
  283.     in.flags(flags); // set the format state back to previous settings
  284.  
  285.     return err -> GreaterSeverity (SEVERITY_INCOMPLETE);  
  286. */
  287. }
  288.  
  289. Severity 
  290. SdaiBinary::STEPread (const char *s, ErrorDescriptor *err)
  291. {
  292.     istrstream in((char *)s);
  293.     return STEPread (in, err);
  294. }
  295.  
  296. ///////////////////////////////////////////////////////////////////////////////
  297. // * attrValue is validated.
  298. // * err is set if there is an error
  299. // * If optional is 1 then a missing value will be valid otherwise severity
  300. //   incomplete will be set.
  301. // * If you don\'t know if the value may be optional then set it false and 
  302. //   check to see if SEVERITY_INCOMPLETE is set. You can change it later to
  303. //   SEVERITY_NULL if it is valid for the value to be missing.  No error 
  304. //   'message' will be associated with the value being missing so you won\'t 
  305. //   have to worry about undoing an error message.
  306. // * tokenList contains characters that terminate the expected value.
  307. // * If tokenList is not zero then the value is expected to terminate before 
  308. //   a character found in tokenlist.  All values read up to the 
  309. //   terminating character (delimiter) must be valid or err will be set with an
  310. //   appropriate error message.  White space between the value and the 
  311. //   terminating character is not considered to be invalid.  If tokenList is 
  312. //   null then attrValue must only contain a valid value and nothing else 
  313. //   following.
  314. ///////////////////////////////////////////////////////////////////////////////
  315.  
  316. Severity 
  317. SdaiBinary::BinaryValidLevel (istream &in, ErrorDescriptor *err,
  318.                 int optional, char *tokenList, 
  319.                 int needDelims, int clearError)
  320. {
  321.     if(clearError)
  322.     err->ClearErrorMsg();
  323.  
  324.     in >> ws; // skip white space
  325.     char c = in.peek();
  326.     if(c == '$' || in.eof())
  327.     {
  328.     if(!optional)
  329.         err->GreaterSeverity(SEVERITY_INCOMPLETE);
  330.     if(in)
  331.         in >> c;
  332.     CheckRemainingInput(in, err, "binary", tokenList);
  333.     return err->severity();
  334.     }
  335.     else
  336.     {
  337.     ErrorDescriptor error;
  338.     ReadBinary(in, &error, 0, needDelims);
  339.     CheckRemainingInput(in, &error, "binary", tokenList);
  340.  
  341.     Severity sev = error.severity();
  342.     if(sev < SEVERITY_INCOMPLETE)
  343.     {
  344.         err->AppendToDetailMsg(error.DetailMsg());
  345.         err->AppendToUserMsg(error.UserMsg());
  346.         err->GreaterSeverity(error.severity());
  347.     }
  348.     else if(sev == SEVERITY_INCOMPLETE && !optional)
  349.         err->GreaterSeverity(SEVERITY_INCOMPLETE);
  350.     }
  351.     return err->severity();
  352. }
  353.  
  354. Severity 
  355. SdaiBinary::BinaryValidLevel (const char *value, ErrorDescriptor *err,
  356.                 int optional, char *tokenList, 
  357.                 int needDelims, int clearError)
  358. {
  359.     istrstream in((char *)value);
  360.     return BinaryValidLevel (in, err, optional, tokenList, 
  361.                  needDelims, clearError);
  362. }
  363.