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 / STR.CPP < prev    next >
Text File  |  1994-08-06  |  9KB  |  283 lines

  1.  
  2. /*
  3. * NIST Utils Class Library
  4. * clutils/Str.cc
  5. * February, 1994
  6. * K. C. 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: Str.cc,v 2.0.1.2 1994/05/10 20:57:05 kc Exp $  */ 
  14.  
  15. #include <Str.h>
  16.  
  17. /******************************************************************
  18.  ** Procedure:  string functions
  19.  ** Description:  These functions take a character or a string and return
  20.  **    a temporary copy of the string with the function applied to it.
  21.  ** Parameters:  
  22.  ** Returns:  temporary copy of characters
  23.  ** Side Effects:  
  24.  ** Status:  complete
  25.  ******************************************************************/
  26.  
  27. char
  28. ToLower (const char c)
  29. {
  30.     if (isupper (c)) return (tolower (c));
  31.     else return (c);
  32.  
  33. }
  34.  
  35. char
  36. ToUpper  (const char c)
  37. {
  38.     if (islower (c)) return (toupper (c));
  39.     else return (c);
  40. }
  41.  
  42. const char *
  43. StrToLower (const char * word, SCLstring &s)
  44. {
  45.     char newword [BUFSIZ];
  46.     int i =0;
  47. //    char ToLower (char c);
  48.     
  49.     while (word [i] != '\0') {
  50.     newword [i] = ToLower (word [i]);
  51.     ++i;    
  52.     }
  53.     newword [i] = '\0';
  54.     s = newword;
  55.     return s.chars();
  56. }
  57.  
  58. const char * 
  59. StrToUpper (const char * word, SCLstring &s)
  60. {
  61.     char newword [BUFSIZ];
  62.     int i =0;
  63. //    char ToUpper (char c);
  64.     
  65.     while (word [i] != '\0') {
  66.     newword [i] = ToUpper (word [i]);
  67.     ++i;
  68.     
  69.     }
  70.     newword [i] = '\0';
  71.     s = newword;
  72.     return s.chars();
  73. }
  74.  
  75. const char * 
  76. StrToConstant (const char * word, SCLstring &s)
  77. {
  78.     char newword [BUFSIZ];
  79.     int i =0;
  80. //    char ToUpper (char c);
  81.     
  82.     while (word [i] != '\0') {
  83.     if (word [i] == '/' || word [i] == '.') newword [i] = '_';
  84.     else newword [i] = ToUpper (word [i]);
  85.     ++i;
  86.     
  87.     }
  88.     newword [i] = '\0';
  89.     s = newword;
  90.     return s.chars();
  91. }
  92.  
  93. /******************************************************************
  94.  ** Procedure:  PrettyName (char * oldname)
  95.  ** Parameters:  oldname
  96.  ** Returns:  a new capitalized name 
  97.  ** Description:  creates a new name with first character's in caps
  98.  ** Side Effects:  PrettyNewName allocates memory for the new name
  99.                    PrettyTmpName returns new name in a static buffer
  100.  ** Status:   OK  7-Oct-1992 kcm 
  101.  ******************************************************************/
  102. const char *
  103. PrettyTmpName (const char * oldname)
  104. {
  105.     int i= 0;
  106.     static char newname [BUFSIZ];
  107.     newname [0] = '\0';
  108.     while (( oldname [i] != '\0') && (i < BUFSIZ)) {
  109.         newname [i] = ToLower (oldname [i]);
  110.         if (oldname [i] == '_')  /*  character is '_'   */  
  111.       {
  112.           ++i;
  113.           newname [i] = ToUpper (oldname [i]);  
  114.       }
  115.         if (oldname [i] != '\0') ++i;
  116.     }
  117.  
  118.     newname [0] = ToUpper (oldname [0]);
  119.     newname [i] = '\0';
  120.     return newname;
  121. }
  122.  
  123.  
  124. char *
  125. PrettyNewName (const char * oldname)
  126. {
  127.     
  128.     char * name = new char [strlen (oldname) +1];
  129.     strcpy (name, PrettyTmpName (oldname));
  130.     return name;
  131. }
  132.  
  133.  
  134.  
  135. // This function is used to check an input stream following a read.  It writes
  136. // error messages in the 'ErrorDescriptor &err' argument as appropriate.
  137. // 'const char *tokenList' argument contains a string made up of delimiters
  138. // that are used to move the file pointer in the input stream to the end of
  139. // the value you are reading (i.e. the ending marked by the presence of the
  140. // delimiter).  The file pointer is moved just prior to the delimiter.  If the 
  141. // tokenList argument is a null pointer then this function expects to find EOF.
  142. //
  143. // If input is being read from a stream then a tokenList should be provided so
  144. // this function can push the file pointer up to but not past the delimiter
  145. // (i.e. not removing the delimiter from the input stream).  If you have a
  146. // string containing a single value and you expect the whole string to contain
  147. // a valid value, you can change the string to an istrstream, read the value 
  148. // then send the istrstream to this function with tokenList set to null 
  149. // and this function will set an error for you if any input remains following
  150. // the value.
  151.  
  152. // If the input stream can be readable again then 
  153. //    - any error states set for the the stream are cleared. 
  154. //    - white space skipped in the input stream
  155. //    - if EOF is encountered it returns
  156. //      otherwise it peeks at the next character
  157. //    - if the tokenList argument exists (i.e. is not null)
  158. //      then if looks to see if the char peeked at is in the tokenList string
  159. //      if it is then no error is set in the ErrorDescriptor
  160. //      if the char peeked at is not in the tokenList string that implies 
  161. //         that there is garbage following the value that was successfully 
  162. //         or unsuccessfully read.  The garbage is read until EOF or a 
  163. //         delimiter in the tokenList is found.
  164. //         - EOF is found you did not recover -> SEVERITY_INPUT_ERROR
  165. //         - delimiter found you recovered successfully => SEVERITY_WARNING
  166. //    - if tokenList does not exist then it expects to find EOF, if it does 
  167. //      not then it is an error but the bad chars are not read since you have
  168. //      no way to know when to stop.
  169.  
  170. Severity 
  171. CheckRemainingInput(istream &in, ErrorDescriptor *err, 
  172.             const char *typeName, // used in error message
  173.             const char *tokenList) // e.g. ",)"
  174. {
  175.     SCLstring skipBuf;
  176.     char name[64];
  177.     name[0] = 0;
  178.  
  179.     // 1. CHECK to see if there is invalid input following what you read.
  180.     //    good or fail means you can still read from the input stream.
  181.  
  182.     if( in.good() || in.fail() )
  183.       // fail means that the input did not match the expected input.
  184.     {   // check for bad input following what you read (or tried to read) but 
  185.     // preceding a delimiter if you are expecting one.
  186.  
  187.     in.clear(); // clear the istreams error
  188.     in >> ws; // skip whitespace
  189.     if(in.eof()) // no input following the desired input (or following the 
  190.     {         // missing desired input)
  191.         return err->severity();
  192.     }
  193.     char c;
  194.     c = in.peek();
  195.  
  196.     if( tokenList ) // are expecting a delim so read till you find it
  197.     {
  198.  
  199.         // 3. FIND a delimiter
  200.  
  201.         if( strchr(tokenList, c) )
  202.         { // next char peeked at was delim expected => success
  203.         return err->severity();
  204.         }
  205.         else // next character was not a delim and thus is garbage
  206.         { // read bad input until you find the delim
  207.         in.get(c);
  208.         while(in && !strchr(tokenList, c) )
  209.         {   // this could chew up the remainder of the input unless you
  210.             // give it the delim to end an entity value and it knows 
  211.             // about strings
  212.             skipBuf.Append(c);
  213.             in.get(c);
  214.         }
  215.         // ENHANCEMENT may want to add the bad data to the err msg.
  216.  
  217.         if(strchr(tokenList, c))
  218.         { // congratulations you recovered
  219.             err->GreaterSeverity(SEVERITY_WARNING);
  220.             sprintf(name, 
  221.                "\tFound invalid %s value...\n", 
  222.                 typeName);
  223.             err->AppendToUserMsg(name);
  224.             err->AppendToDetailMsg(name);
  225.             err->AppendToDetailMsg(
  226.                 "\tdata lost looking for end of attribute: ");
  227.             err->AppendToDetailMsg( skipBuf.chars() );
  228.             err->AppendToDetailMsg( "\n" );
  229.             in.putback(c);
  230.             // invalid input 
  231.             // (though a valid value may have been assigned)
  232.             return err->severity();
  233.         }
  234.         else
  235.         { // could not recover (of course)
  236.             err->GreaterSeverity(SEVERITY_INPUT_ERROR);
  237.             sprintf(name, 
  238.               "Unable to recover from input error while reading %s %s",
  239.                 typeName, "value.\n");
  240.             err->AppendToUserMsg(name);
  241.             err->AppendToDetailMsg(name);
  242.             // invalid input 
  243.             // (though a valid value may have been assigned)
  244.             return err->severity();
  245.         }
  246.         }
  247.     }
  248.     else if(in.good()) // found a char => error 
  249.       // i.e. skipping whitespace did not cause EOF so must have hit a char
  250.     { // remember we are not expecting more input since no tokenList
  251.  
  252.         // or 3. since not expecting a delimiter and there is input there 
  253.         //       is an error
  254.  
  255.         err->GreaterSeverity(SEVERITY_WARNING);
  256.         sprintf(name, "Invalid %s value.\n", typeName);
  257.         err->AppendToUserMsg(name);
  258.         err->AppendToDetailMsg(name);
  259.             // invalid input 
  260.             // (though a valid value may have been assigned)
  261.         return err->severity();
  262.     }
  263.     }
  264.     else if (in.eof())
  265.     {
  266.     // hit EOF when reading for a value 
  267.     // don\'t set any error
  268.     return err->severity();
  269.     }
  270.     else
  271.     { // badbit set (in.bad()) means there was a problem when reading istream
  272.       // this is bad news... it means the input stream is hopelessly messed up
  273.     err->GreaterSeverity(SEVERITY_INPUT_ERROR);
  274.     sprintf(name, "Invalid %s value.\n", typeName);
  275.     err->AppendToUserMsg(name);
  276.     err->AppendToDetailMsg(name);
  277.             // invalid input 
  278.             // (though a valid value may have been assigned)
  279.     return err->severity();
  280.     }
  281.     return err->severity();
  282. }
  283.