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 / READ_FUN.CPP < prev    next >
C/C++ Source or Header  |  1994-08-06  |  13KB  |  371 lines

  1.  
  2. #include <errordesc.h>
  3. #include <stdio.h>
  4. #include <sdai.h> 
  5. #include <read_fun.h>
  6.  
  7. // print Error information for debugging purposes
  8. void PrintErrorState(ErrorDescriptor &err)
  9. {
  10.     cout << "** severity: ";
  11.     switch(err.severity())
  12.     {
  13.       case SEVERITY_NULL :
  14.     cout << "\n  Null\n";
  15.     break;
  16.       case SEVERITY_USERMSG :
  17.     cout << "\n  User Message\n";
  18.     break;
  19.       case SEVERITY_INCOMPLETE :
  20.     cout << "\n  Incomplete\n";
  21.     break;
  22.       case SEVERITY_WARNING :
  23.     cout << "\n  Warning\n";
  24.     break;
  25.       case SEVERITY_INPUT_ERROR :
  26.     cout << "\n  Input Error\n";
  27.     break;
  28.       default:
  29.     cout << "\n  Other\n";
  30.     break;
  31.     }
  32.     cout << err.DetailMsg() << "\n";
  33. }
  34.  
  35. // print istream error information for debugging purposes
  36. void IStreamState(istream &in)
  37. {
  38.     if( in.good())
  39.     {
  40.     cerr << "istream GOOD\n" << flush;
  41.     }
  42.     if( in.fail() )
  43.     {
  44.     cerr << "istream FAIL\n" << flush;
  45.     }
  46.     if( in.eof() )
  47.     {
  48.     cerr << "istream EOF\n" << flush;
  49.     }
  50. }
  51.  
  52. ///////////////////////////////////////////////////////////////////////////////
  53. //  ReadInteger
  54. // * This function reads an integer if possible
  55. // * If an integer is read it is assigned to val and 1 (true) is returned.
  56. // * If an integer is not read because of an error then val is left unchanged 
  57. //   and 0 (false) is returned.
  58. // * If there is an error then the ErrorDescriptor err is set accordingly with
  59. //   a severity level and error message (no error MESSAGE is set for severity
  60. //   incomplete).
  61. // * tokenList contains characters that terminate reading the value.
  62. // * If tokenList is not zero then the istream will be read until a character 
  63. //   is found matching a character in tokenlist.  All values read up to the 
  64. //   terminating character (delimiter) must be valid or err will be set with an
  65. //   appropriate error message.  A valid value may still have been assigned 
  66. //   but it may be followed by garbage thus an error will result.  White
  67. //   space between the value and the terminating character is not considered 
  68. //   to be invalid.  If tokenList is null then the value must not be followed
  69. //   by any characters other than white space (i.e. EOF must happen)
  70. //   
  71. ///////////////////////////////////////////////////////////////////////////////
  72.  
  73. int
  74. ReadInteger(SdaiInteger &val, istream &in, ErrorDescriptor *err, 
  75.         char *tokenList)
  76. {
  77.     SdaiInteger i = 0;
  78.     in >> ws;
  79.     in >> i;
  80.  
  81.     int valAssigned = 0;
  82.  
  83. //    IStreamState(in);
  84.  
  85.     if(!in.fail())
  86.     {
  87.     valAssigned = 1;
  88.     val = i;
  89.     }
  90.     Severity s = CheckRemainingInput(in, err, "Integer", tokenList);
  91.     return valAssigned;
  92. }
  93.  
  94. ///////////////////////////////////////////////////////////////////////////////
  95. // same as above but reads from a const char *
  96. ///////////////////////////////////////////////////////////////////////////////
  97.  
  98. int
  99. ReadInteger(SdaiInteger &val, const char *s, ErrorDescriptor *err, 
  100.         char *tokenList)
  101. {
  102.     istrstream in((char *)s);
  103.     return ReadInteger(val, in, err, tokenList);
  104. }
  105.  
  106. ///////////////////////////////////////////////////////////////////////////////
  107. // * attrValue is validated.
  108. // * err is set if there is an error
  109. // * If optional is 1 then a missing value will be valid otherwise severity
  110. //   incomplete will be set.
  111. // * If you don\'t know if the value may be optional then set it false and 
  112. //   check to see if SEVERITY_INCOMPLETE is set. You can change it later to
  113. //   SEVERITY_NULL if it is valid for the value to be missing.  No error 
  114. //   'message' will be associated with the value being missing so you won\'t 
  115. //   have to worry about undoing an error message.
  116. // * tokenList contains characters that terminate the expected value.
  117. // * If tokenList is not zero then the value is expected to terminate before 
  118. //   a character found in tokenlist.  All values read up to the 
  119. //   terminating character (delimiter) must be valid or err will be set with an
  120. //   appropriate error message.  White space between the value and the 
  121. //   terminating character is not considered to be invalid.  If tokenList is 
  122. //   null then attrValue must only contain a valid value and nothing else 
  123. //   following.
  124. ///////////////////////////////////////////////////////////////////////////////
  125.  
  126. Severity 
  127. IntValidLevel (const char *attrValue, ErrorDescriptor *err,
  128.            int clearError, int optional, char *tokenList)
  129. {
  130.     if(clearError)
  131.     err->ClearErrorMsg();
  132.  
  133.     istrstream in((char *)attrValue);
  134.     in >> ws; // skip white space
  135.     char c = in.peek();
  136.     if(in.eof())
  137.     {
  138.     if(!optional)
  139.         err->GreaterSeverity(SEVERITY_INCOMPLETE);
  140.     }
  141.     else if(c == '$')
  142.     {
  143.     if(!optional)
  144.         err->GreaterSeverity(SEVERITY_INCOMPLETE);
  145.     in >> c;
  146.     CheckRemainingInput(in, err, "integer", tokenList);
  147.     return err->severity();
  148.     }
  149.     else
  150.     {
  151.     SdaiInteger val = 0;
  152.     int valAssigned = ReadInteger(val, in, err, tokenList);
  153.     if(!valAssigned && !optional)
  154.         err->GreaterSeverity(SEVERITY_INCOMPLETE);
  155.     }
  156.     return err->severity();
  157. }
  158.  
  159. ///////////////////////////////////////////////////////////////////////////////
  160. //  ReadReal
  161. // * This function reads a real if possible
  162. // * If a real is read it is assigned to val and 1 (true) is returned.
  163. // * If a real is not read because of an error then val is left unchanged 
  164. //   and 0 (false) is returned.
  165. // * If there is an error then the ErrorDescriptor err is set accordingly with
  166. //   a severity level and error message (no error MESSAGE is set for severity
  167. //   incomplete).
  168. // * tokenList contains characters that terminate reading the value.
  169. // * If tokenList is not zero then the istream will be read until a character 
  170. //   is found matching a character in tokenlist.  All values read up to the 
  171. //   terminating character (delimiter) must be valid or err will be set with an
  172. //   appropriate error message.  A valid value may still have been assigned 
  173. //   but it may be followed by garbage thus an error will result.  White
  174. //   space between the value and the terminating character is not considered 
  175. //   to be invalid.  If tokenList is null then the value must not be followed
  176. //   by any characters other than white space (i.e. EOF must happen)
  177. ///////////////////////////////////////////////////////////////////////////////
  178.  
  179. int
  180. ReadReal(SdaiReal &val, istream &in, ErrorDescriptor *err, 
  181.      char *tokenList)
  182. {
  183.     SdaiReal d = 0;
  184.     in >> ws;
  185.     in >> d;
  186.  
  187. //    IStreamState(in);
  188.  
  189.     int valAssigned = 0;
  190. //    PrintErrorState(err);
  191.  
  192.     if(!in.fail())
  193.     {
  194.     valAssigned = 1;
  195.     val = d;
  196.     }
  197.     Severity s = CheckRemainingInput(in, err, "Real", tokenList);
  198.     return valAssigned;
  199. }
  200.  
  201. ///////////////////////////////////////////////////////////////////////////////
  202. // same as above but reads from a const char *
  203. ///////////////////////////////////////////////////////////////////////////////
  204.  
  205. int
  206. ReadReal(SdaiReal &val, const char *s, ErrorDescriptor *err, 
  207.      char *tokenList)
  208. {
  209.     istrstream in((char *)s);
  210.     return ReadReal(val, in, err, tokenList);
  211. }
  212.  
  213. ///////////////////////////////////////////////////////////////////////////////
  214. // * attrValue is validated.
  215. // * err is set if there is an error
  216. // * If optional is 1 then a missing value will be valid otherwise severity
  217. //   incomplete will be set.
  218. // * If you don\'t know if the value may be optional then set it false and 
  219. //   check to see if SEVERITY_INCOMPLETE is set. You can change it later to
  220. //   SEVERITY_NULL if it is valid for the value to be missing.  No error 
  221. //   'message' will be associated with the value being missing so you won\'t 
  222. //   have to worry about undoing an error message.
  223. // * tokenList contains characters that terminate the expected value.
  224. // * If tokenList is not zero then the value is expected to terminate before 
  225. //   a character found in tokenlist.  All values read up to the 
  226. //   terminating character (delimiter) must be valid or err will be set with an
  227. //   appropriate error message.  White space between the value and the 
  228. //   terminating character is not considered to be invalid.  If tokenList is 
  229. //   null then attrValue must only contain a valid value and nothing else 
  230. //   following.
  231. ///////////////////////////////////////////////////////////////////////////////
  232.  
  233. Severity 
  234. RealValidLevel (const char *attrValue, ErrorDescriptor *err,
  235.         int clearError, int optional, char *tokenList)
  236. {
  237.     if(clearError)
  238.     err->ClearErrorMsg();
  239.  
  240.     istrstream in((char *)attrValue);
  241.     in >> ws; // skip white space
  242.     char c = in.peek();
  243.     if(in.eof())
  244.     {
  245.     if(!optional)
  246.         err->GreaterSeverity(SEVERITY_INCOMPLETE);
  247.     }
  248.     else if(c == '$')
  249.     {
  250.     if(!optional)
  251.         err->GreaterSeverity(SEVERITY_INCOMPLETE);
  252.     in >> c;
  253.     CheckRemainingInput(in, err, "real", tokenList);
  254.     return err->severity();
  255.     }
  256.     else
  257.     {
  258.     SdaiReal val = 0;
  259.     int valAssigned = ReadReal(val, in, err, tokenList);
  260.     if(!valAssigned && !optional)
  261.         err->GreaterSeverity(SEVERITY_INCOMPLETE);
  262.     }
  263.     return err->severity();
  264. }
  265.  
  266. ///////////////////////////////////////////////////////////////////////////////
  267. //  ReadNumber - read as a real number
  268. // * This function reads a number if possible
  269. // * If a number is read it is assigned to val and 1 (true) is returned.
  270. // * If a number is not read because of an error then val is left unchanged 
  271. //   and 0 (false) is returned.
  272. // * If there is an error then the ErrorDescriptor err is set accordingly with
  273. //   a severity level and error message (no error MESSAGE is set for severity
  274. //   incomplete).
  275. // * tokenList contains characters that terminate reading the value.
  276. // * If tokenList is not zero then the istream will be read until a character 
  277. //   is found matching a character in tokenlist.  All values read up to the 
  278. //   terminating character (delimiter) must be valid or err will be set with an
  279. //   appropriate error message.  A valid value may still have been assigned 
  280. //   but it may be followed by garbage thus an error will result.  White
  281. //   space between the value and the terminating character is not considered 
  282. //   to be invalid.  If tokenList is null then the value must not be followed
  283. //   by any characters other than white space (i.e. EOF must happen)
  284. ///////////////////////////////////////////////////////////////////////////////
  285.  
  286. int
  287. ReadNumber(SdaiReal &val, istream &in, ErrorDescriptor *err, 
  288.        char *tokenList)
  289. {
  290.     SdaiReal d = 0;
  291.     in >> ws;
  292.     in >> d;
  293.  
  294. //    IStreamState(in);
  295.  
  296.     int valAssigned = 0;
  297.     if(!in.fail())
  298.     {
  299.     valAssigned = 1;
  300.     val = d;
  301.     }
  302.     Severity s = CheckRemainingInput(in, err, "Number", tokenList);
  303.     return valAssigned;
  304. }
  305.  
  306. ///////////////////////////////////////////////////////////////////////////////
  307. // same as above but reads from a const char *
  308. ///////////////////////////////////////////////////////////////////////////////
  309.  
  310. int
  311. ReadNumber(SdaiReal &val, const char *s, ErrorDescriptor *err, 
  312.        char *tokenList)
  313. {
  314.     istrstream in((char *)s);
  315.     return ReadNumber(val, in, err, tokenList);
  316. }
  317.  
  318.  
  319. ///////////////////////////////////////////////////////////////////////////////
  320. // * attrValue is validated.
  321. // * err is set if there is an error
  322. // * If optional is 1 then a missing value will be valid otherwise severity
  323. //   incomplete will be set.
  324. // * If you don\'t know if the value may be optional then set it false and 
  325. //   check to see if SEVERITY_INCOMPLETE is set. You can change it later to
  326. //   SEVERITY_NULL if it is valid for the value to be missing.  No error 
  327. //   'message' will be associated with the value being missing so you won\'t 
  328. //   have to worry about undoing an error message.
  329. // * tokenList contains characters that terminate the expected value.
  330. // * If tokenList is not zero then the value is expected to terminate before 
  331. //   a character found in tokenlist.  All values read up to the 
  332. //   terminating character (delimiter) must be valid or err will be set with an
  333. //   appropriate error message.  White space between the value and the 
  334. //   terminating character is not considered to be invalid.  If tokenList is 
  335. //   null then attrValue must only contain a valid value and nothing else 
  336. //   following.
  337. ///////////////////////////////////////////////////////////////////////////////
  338.  
  339. Severity 
  340. NumberValidLevel (const char *attrValue, ErrorDescriptor *err,
  341.           int clearError, int optional, char *tokenList)
  342. {
  343.     if(clearError)
  344.     err->ClearErrorMsg();
  345.  
  346.     istrstream in((char *)attrValue);
  347.     in >> ws; // skip white space
  348.     char c = in.peek();
  349.     if(in.eof())
  350.     {
  351.     if(!optional)
  352.         err->GreaterSeverity(SEVERITY_INCOMPLETE);
  353.     }
  354.     else if(c == '$')
  355.     {
  356.     if(!optional)
  357.         err->GreaterSeverity(SEVERITY_INCOMPLETE);
  358.     in >> c;
  359.     CheckRemainingInput(in, err, "number", tokenList);
  360.     return err->severity();
  361.     }
  362.     else
  363.     {
  364.     SdaiReal val = 0;
  365.     int valAssigned = ReadNumber(val, in, err, tokenList);
  366.     if(!valAssigned && !optional)
  367.         err->GreaterSeverity(SEVERITY_INCOMPLETE);
  368.     }
  369.     return err->severity();
  370. }
  371.