Venga, yo también quiero :)
Código C++:
Ver originalenum class ConvertError
{
NoError,
Overflow,
WrongFormat
};
ConvertError ToInt( std::istream& stream, int& value )
{
ConvertError toReturn = ConvertError::NoError;
bool negate = false;
bool firstChar = true;
value = 0;
bool nextIteration = true;
do
{
char c = stream.peek();
if( c == EOF )
nextIteration = false;
else
{
stream.get(); // discard from the stream
if( c==' ' || c=='\n' || c=='\r' || c=='\t' )
nextIteration = false;
{
int newValue = value*10+c-'0';
if( newValue < value )
{
nextIteration = false;
toReturn = ConvertError::Overflow;
}
value = newValue;
}
else
{
if( (c != '-' && c != '+') || !firstChar )
{
nextIteration = false;
toReturn = ConvertError::WrongFormat;
}
else
negate = (c=='-');
}
}
firstChar = false;
} while(nextIteration);
if( negate )
value *= -1;
return toReturn;
}