suplibxx  1.3.14
str2l.cc
1 // --8<--8<--8<--8<--
2 //
3 // Copyright (C) 2006 Smithsonian Astrophysical Observatory
4 //
5 // This file is part of suplibxx
6 //
7 // suplibxx is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
11 //
12 // suplibxx is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the
19 // Free Software Foundation, Inc.
20 // 51 Franklin Street, Fifth Floor
21 // Boston, MA 02110-1301, USA
22 //
23 // -->8-->8-->8-->8--
24 
25 #include <errno.h>
26 #include <cstdlib>
27 #include <string>
28 
29 #include <limits.h>
30 
31 #include <Exception/Exception.h>
32 
33 #include "str.h"
34 
47 long suplib::str2l( const char* txt, int base ) {
48 
49  // The field cannot be empty. The user must enter a value.
50  if ( NULL == txt || '\0' == *txt )
51  throw Exception( "suplib::strol( ) : Empty field" );
52 
53  char* ptr = NULL;
54  long result = ::strtol( txt, &ptr, base );
55 
56  if ( (result == LONG_MAX || result == LONG_MIN) && errno == ERANGE )
57  throw Exception( "suplib::str2l( " + std::string( txt ) +
58  ", int ) : Possibly under/over flow" );
59 
60  if ( '\0' != *ptr )
61  throw Exception( "suplib::str2l( " + std::string( txt ) +
62  ", int ) : Not a number, problem starting at `" +
63  std::string( ptr ) + "'" );
64 
65  return result;
66 
67 }
long str2l(const char *txt, int base=10)
convert string to long number
Definition: str2l.cc:47