Chameleon

Chameleon Svn Source Tree

Root/branches/xZenu/src/modules/klibc/strntoumax.c

Source at commit 1295 created 12 years 8 months ago.
By meklort, Remove libsaio.h from klibc
1/*
2 * strntoumax.c
3 *
4 * The strntoumax() function and associated
5 */
6#include <stdlib.h>
7#include <stdint.h>
8#include <ctype.h>
9
10static inline int digitval(int ch)
11{
12if (ch >= '0' && ch <= '9') {
13return ch - '0';
14} else if (ch >= 'A' && ch <= 'Z') {
15return ch - 'A' + 10;
16} else if (ch >= 'a' && ch <= 'z') {
17return ch - 'a' + 10;
18} else {
19return -1;
20}
21}
22
23uintmax_t strntoumax(const char *nptr, char **endptr, int base, size_t n)
24{
25int minus = 0;
26uintmax_t v = 0;
27int d;
28
29while (n && isspace((unsigned char)*nptr)) {
30nptr++;
31n--;
32}
33
34/* Single optional + or - */
35if (n) {
36char c = *nptr;
37if (c == '-' || c == '+') {
38minus = (c == '-');
39nptr++;
40n--;
41}
42}
43
44if (base == 0) {
45if (n >= 2 && nptr[0] == '0' &&
46 (nptr[1] == 'x' || nptr[1] == 'X')) {
47n -= 2;
48nptr += 2;
49base = 16;
50} else if (n >= 1 && nptr[0] == '0') {
51n--;
52nptr++;
53base = 8;
54} else {
55base = 10;
56}
57} else if (base == 16) {
58if (n >= 2 && nptr[0] == '0' &&
59 (nptr[1] == 'x' || nptr[1] == 'X')) {
60n -= 2;
61nptr += 2;
62}
63}
64
65while (n && (d = digitval(*nptr)) >= 0 && d < base) {
66v = v * base + d;
67n--;
68nptr++;
69}
70
71if (endptr)
72*endptr = (char *)nptr;
73
74return minus ? -v : v;
75}
76

Archive Download this file

Revision: 1295