1    | /***************************************
2    |   $Revision: 1.6 $
3    | 
4    |   Utilities (ut). numconv.c  - library for fast numerical conversions
5    |                                (to[?]/from string).
6    | 
7    |   Status: NOT REVUED, TESTED, 
8    | 
9    |   Design and implementation by: Marek Bukowy
10   | 
11   |   ******************/ /******************
12   |   Copyright (c) 1999,2000,2001,2002               RIPE NCC
13   |  
14   |   All Rights Reserved
15   |   
16   |   Permission to use, copy, modify, and distribute this software and its
17   |   documentation for any purpose and without fee is hereby granted,
18   |   provided that the above copyright notice appear in all copies and that
19   |   both that copyright notice and this permission notice appear in
20   |   supporting documentation, and that the name of the author not be
21   |   used in advertising or publicity pertaining to distribution of the
22   |   software without specific, written prior permission.
23   |   
24   |   THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
25   |   ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
26   |   AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
27   |   DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
28   |   AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
29   |   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
30   |   ***************************************/
31   | 
32   | #include "rip.h"
33   | #include <ctype.h>
34   | 
35   | /* converts QUICKLY a string to integer value, stops when encounters a 
36   |    whitespace. All it expects is an UNSIGNED INTEGER value, 
37   |    so in case it gets a non-digit (and non-whitespace) character it returns -1 
38   |    
39   |    if everything looks fine, it return 0. The converted unsigned int is placed
40   |    into *result;
41   | */
42   | 
43   | int
44   | ut_dec_2_uns(char *cpy, unsigned *result) 
45   | {
46   | register unsigned val=0;
47   | register char *ch=cpy;
48   | 
49   |   while( *ch != '\0') {
50   | 
51   |     if ( ! isdigit(* (unsigned char *) ch)) {       /* make the &*^%#@$%^ */
52   |       if ( isspace(* (unsigned char *) ch)) {       /* gcc happy */
53   |         break;
54   |       }
55   |       else {
56   |         return -1;
57   |       }
58   |     }
59   | 
60   |     val *= 10;
61   |     val += ( *ch - '0' );
62   |     ch++;
63   |   }
64   | 
65   |  *result = val;
66   |  return 0;
67   | }