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 >
Wrap
C/C++ Source or Header
|
1994-08-06
|
13KB
|
371 lines
#include <errordesc.h>
#include <stdio.h>
#include <sdai.h>
#include <read_fun.h>
// print Error information for debugging purposes
void PrintErrorState(ErrorDescriptor &err)
{
cout << "** severity: ";
switch(err.severity())
{
case SEVERITY_NULL :
cout << "\n Null\n";
break;
case SEVERITY_USERMSG :
cout << "\n User Message\n";
break;
case SEVERITY_INCOMPLETE :
cout << "\n Incomplete\n";
break;
case SEVERITY_WARNING :
cout << "\n Warning\n";
break;
case SEVERITY_INPUT_ERROR :
cout << "\n Input Error\n";
break;
default:
cout << "\n Other\n";
break;
}
cout << err.DetailMsg() << "\n";
}
// print istream error information for debugging purposes
void IStreamState(istream &in)
{
if( in.good())
{
cerr << "istream GOOD\n" << flush;
}
if( in.fail() )
{
cerr << "istream FAIL\n" << flush;
}
if( in.eof() )
{
cerr << "istream EOF\n" << flush;
}
}
///////////////////////////////////////////////////////////////////////////////
// ReadInteger
// * This function reads an integer if possible
// * If an integer is read it is assigned to val and 1 (true) is returned.
// * If an integer is not read because of an error then val is left unchanged
// and 0 (false) is returned.
// * If there is an error then the ErrorDescriptor err is set accordingly with
// a severity level and error message (no error MESSAGE is set for severity
// incomplete).
// * tokenList contains characters that terminate reading the value.
// * If tokenList is not zero then the istream will be read until a character
// is found matching a character in tokenlist. All values read up to the
// terminating character (delimiter) must be valid or err will be set with an
// appropriate error message. A valid value may still have been assigned
// but it may be followed by garbage thus an error will result. White
// space between the value and the terminating character is not considered
// to be invalid. If tokenList is null then the value must not be followed
// by any characters other than white space (i.e. EOF must happen)
//
///////////////////////////////////////////////////////////////////////////////
int
ReadInteger(SdaiInteger &val, istream &in, ErrorDescriptor *err,
char *tokenList)
{
SdaiInteger i = 0;
in >> ws;
in >> i;
int valAssigned = 0;
// IStreamState(in);
if(!in.fail())
{
valAssigned = 1;
val = i;
}
Severity s = CheckRemainingInput(in, err, "Integer", tokenList);
return valAssigned;
}
///////////////////////////////////////////////////////////////////////////////
// same as above but reads from a const char *
///////////////////////////////////////////////////////////////////////////////
int
ReadInteger(SdaiInteger &val, const char *s, ErrorDescriptor *err,
char *tokenList)
{
istrstream in((char *)s);
return ReadInteger(val, in, err, tokenList);
}
///////////////////////////////////////////////////////////////////////////////
// * attrValue is validated.
// * err is set if there is an error
// * If optional is 1 then a missing value will be valid otherwise severity
// incomplete will be set.
// * If you don\'t know if the value may be optional then set it false and
// check to see if SEVERITY_INCOMPLETE is set. You can change it later to
// SEVERITY_NULL if it is valid for the value to be missing. No error
// 'message' will be associated with the value being missing so you won\'t
// have to worry about undoing an error message.
// * tokenList contains characters that terminate the expected value.
// * If tokenList is not zero then the value is expected to terminate before
// a character found in tokenlist. All values read up to the
// terminating character (delimiter) must be valid or err will be set with an
// appropriate error message. White space between the value and the
// terminating character is not considered to be invalid. If tokenList is
// null then attrValue must only contain a valid value and nothing else
// following.
///////////////////////////////////////////////////////////////////////////////
Severity
IntValidLevel (const char *attrValue, ErrorDescriptor *err,
int clearError, int optional, char *tokenList)
{
if(clearError)
err->ClearErrorMsg();
istrstream in((char *)attrValue);
in >> ws; // skip white space
char c = in.peek();
if(in.eof())
{
if(!optional)
err->GreaterSeverity(SEVERITY_INCOMPLETE);
}
else if(c == '$')
{
if(!optional)
err->GreaterSeverity(SEVERITY_INCOMPLETE);
in >> c;
CheckRemainingInput(in, err, "integer", tokenList);
return err->severity();
}
else
{
SdaiInteger val = 0;
int valAssigned = ReadInteger(val, in, err, tokenList);
if(!valAssigned && !optional)
err->GreaterSeverity(SEVERITY_INCOMPLETE);
}
return err->severity();
}
///////////////////////////////////////////////////////////////////////////////
// ReadReal
// * This function reads a real if possible
// * If a real is read it is assigned to val and 1 (true) is returned.
// * If a real is not read because of an error then val is left unchanged
// and 0 (false) is returned.
// * If there is an error then the ErrorDescriptor err is set accordingly with
// a severity level and error message (no error MESSAGE is set for severity
// incomplete).
// * tokenList contains characters that terminate reading the value.
// * If tokenList is not zero then the istream will be read until a character
// is found matching a character in tokenlist. All values read up to the
// terminating character (delimiter) must be valid or err will be set with an
// appropriate error message. A valid value may still have been assigned
// but it may be followed by garbage thus an error will result. White
// space between the value and the terminating character is not considered
// to be invalid. If tokenList is null then the value must not be followed
// by any characters other than white space (i.e. EOF must happen)
///////////////////////////////////////////////////////////////////////////////
int
ReadReal(SdaiReal &val, istream &in, ErrorDescriptor *err,
char *tokenList)
{
SdaiReal d = 0;
in >> ws;
in >> d;
// IStreamState(in);
int valAssigned = 0;
// PrintErrorState(err);
if(!in.fail())
{
valAssigned = 1;
val = d;
}
Severity s = CheckRemainingInput(in, err, "Real", tokenList);
return valAssigned;
}
///////////////////////////////////////////////////////////////////////////////
// same as above but reads from a const char *
///////////////////////////////////////////////////////////////////////////////
int
ReadReal(SdaiReal &val, const char *s, ErrorDescriptor *err,
char *tokenList)
{
istrstream in((char *)s);
return ReadReal(val, in, err, tokenList);
}
///////////////////////////////////////////////////////////////////////////////
// * attrValue is validated.
// * err is set if there is an error
// * If optional is 1 then a missing value will be valid otherwise severity
// incomplete will be set.
// * If you don\'t know if the value may be optional then set it false and
// check to see if SEVERITY_INCOMPLETE is set. You can change it later to
// SEVERITY_NULL if it is valid for the value to be missing. No error
// 'message' will be associated with the value being missing so you won\'t
// have to worry about undoing an error message.
// * tokenList contains characters that terminate the expected value.
// * If tokenList is not zero then the value is expected to terminate before
// a character found in tokenlist. All values read up to the
// terminating character (delimiter) must be valid or err will be set with an
// appropriate error message. White space between the value and the
// terminating character is not considered to be invalid. If tokenList is
// null then attrValue must only contain a valid value and nothing else
// following.
///////////////////////////////////////////////////////////////////////////////
Severity
RealValidLevel (const char *attrValue, ErrorDescriptor *err,
int clearError, int optional, char *tokenList)
{
if(clearError)
err->ClearErrorMsg();
istrstream in((char *)attrValue);
in >> ws; // skip white space
char c = in.peek();
if(in.eof())
{
if(!optional)
err->GreaterSeverity(SEVERITY_INCOMPLETE);
}
else if(c == '$')
{
if(!optional)
err->GreaterSeverity(SEVERITY_INCOMPLETE);
in >> c;
CheckRemainingInput(in, err, "real", tokenList);
return err->severity();
}
else
{
SdaiReal val = 0;
int valAssigned = ReadReal(val, in, err, tokenList);
if(!valAssigned && !optional)
err->GreaterSeverity(SEVERITY_INCOMPLETE);
}
return err->severity();
}
///////////////////////////////////////////////////////////////////////////////
// ReadNumber - read as a real number
// * This function reads a number if possible
// * If a number is read it is assigned to val and 1 (true) is returned.
// * If a number is not read because of an error then val is left unchanged
// and 0 (false) is returned.
// * If there is an error then the ErrorDescriptor err is set accordingly with
// a severity level and error message (no error MESSAGE is set for severity
// incomplete).
// * tokenList contains characters that terminate reading the value.
// * If tokenList is not zero then the istream will be read until a character
// is found matching a character in tokenlist. All values read up to the
// terminating character (delimiter) must be valid or err will be set with an
// appropriate error message. A valid value may still have been assigned
// but it may be followed by garbage thus an error will result. White
// space between the value and the terminating character is not considered
// to be invalid. If tokenList is null then the value must not be followed
// by any characters other than white space (i.e. EOF must happen)
///////////////////////////////////////////////////////////////////////////////
int
ReadNumber(SdaiReal &val, istream &in, ErrorDescriptor *err,
char *tokenList)
{
SdaiReal d = 0;
in >> ws;
in >> d;
// IStreamState(in);
int valAssigned = 0;
if(!in.fail())
{
valAssigned = 1;
val = d;
}
Severity s = CheckRemainingInput(in, err, "Number", tokenList);
return valAssigned;
}
///////////////////////////////////////////////////////////////////////////////
// same as above but reads from a const char *
///////////////////////////////////////////////////////////////////////////////
int
ReadNumber(SdaiReal &val, const char *s, ErrorDescriptor *err,
char *tokenList)
{
istrstream in((char *)s);
return ReadNumber(val, in, err, tokenList);
}
///////////////////////////////////////////////////////////////////////////////
// * attrValue is validated.
// * err is set if there is an error
// * If optional is 1 then a missing value will be valid otherwise severity
// incomplete will be set.
// * If you don\'t know if the value may be optional then set it false and
// check to see if SEVERITY_INCOMPLETE is set. You can change it later to
// SEVERITY_NULL if it is valid for the value to be missing. No error
// 'message' will be associated with the value being missing so you won\'t
// have to worry about undoing an error message.
// * tokenList contains characters that terminate the expected value.
// * If tokenList is not zero then the value is expected to terminate before
// a character found in tokenlist. All values read up to the
// terminating character (delimiter) must be valid or err will be set with an
// appropriate error message. White space between the value and the
// terminating character is not considered to be invalid. If tokenList is
// null then attrValue must only contain a valid value and nothing else
// following.
///////////////////////////////////////////////////////////////////////////////
Severity
NumberValidLevel (const char *attrValue, ErrorDescriptor *err,
int clearError, int optional, char *tokenList)
{
if(clearError)
err->ClearErrorMsg();
istrstream in((char *)attrValue);
in >> ws; // skip white space
char c = in.peek();
if(in.eof())
{
if(!optional)
err->GreaterSeverity(SEVERITY_INCOMPLETE);
}
else if(c == '$')
{
if(!optional)
err->GreaterSeverity(SEVERITY_INCOMPLETE);
in >> c;
CheckRemainingInput(in, err, "number", tokenList);
return err->severity();
}
else
{
SdaiReal val = 0;
int valAssigned = ReadNumber(val, in, err, tokenList);
if(!valAssigned && !optional)
err->GreaterSeverity(SEVERITY_INCOMPLETE);
}
return err->severity();
}