Pristine Ack-5.5
[Ack-5.5.git] / modules / src / string / str2long.c
1 /* $Id: str2long.c,v 1.6 1994/06/24 11:22:47 ceriel Exp $ */
2 /*
3  * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
4  * See the copyright notice in the ACK home directory, in the file "Copyright".
5  */
6 /* str2long()
7 */
8
9 #include "ack_string.h"
10
11 value(c, b)
12         char c;
13         int b;
14 {
15         register int ch;
16
17         ch = c - '0';
18         if ((unsigned) ch <= 9) return ch;
19         ch = c - 'A';
20         if ((unsigned) ch <= 5) return ch + 10;
21         ch = c - 'a';
22         if ((unsigned) ch <= 5) return ch + 10;
23         return b;
24 }
25
26 long
27 str2long(str, base)
28         register char *str;
29         int base;
30 {
31         int minus = 0, d;
32         long l = 0;
33
34         if (*str == '-') {
35                 minus++;
36                 str++;
37         }
38         while ((d = value(*str++, base)) < base)
39                 l = base * l + d;
40         return minus ? -l : l;
41 }