str2ul.cc
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <errno.h>
00026 #include <cstdlib>
00027
00028 #include <limits.h>
00029
00030 #include "str.h"
00031
00044 unsigned long suplib::str2ul( const char* txt, int base ) throw( Exception ) {
00045
00046
00047 if ( NULL == txt || '\0' == *txt )
00048 throw Exception( "suplib::strol( ) : Empty field" );
00049
00050 char* ptr = NULL;
00051 long result = ::strtoul( txt, &ptr, base );
00052
00053 if ( (result == LONG_MAX || result == LONG_MIN) && errno == ERANGE )
00054 throw Exception( "suplib::str2ul( " + string( txt ) +
00055 ", int ) : Possibly under/over flow" );
00056
00057 if ( '\0' != *ptr )
00058 throw Exception( "suplib::str2ul( " + string( txt ) +
00059 ", int ) : Not a number, problem starting at `" +
00060 string( ptr ) + "'" );
00061
00062 return result;
00063
00064 }