ReactOS 0.4.16-dev-338-g34e76ad
astoll.c File Reference
#include <schily/mconfig.h>
#include <schily/standard.h>
#include <schily/utypes.h>
#include <schily/schily.h>
#include <schily/errno.h>
Include dependency graph for astoll.c:

Go to the source code of this file.

Macros

#define is_space(c)   ((c) == ' ' || (c) == '\t')
 
#define is_digit(c)   ((c) >= '0' && (c) <= '9')
 
#define is_hex(c)
 
#define is_lower(c)   ((c) >= 'a' && (c) <= 'z')
 
#define is_upper(c)   ((c) >= 'A' && (c) <= 'Z')
 
#define to_lower(c)   (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A'+'a' : (c))
 
#define BASE_MAX   ('z' - 'a' + 10 + 1) /* This is ASCII */
 

Functions

charastoll (const char *s, Llong *l)
 
charastollb (const char *s, Llong *l, int base)
 

Macro Definition Documentation

◆ BASE_MAX

#define BASE_MAX   ('z' - 'a' + 10 + 1) /* This is ASCII */

Definition at line 51 of file astoll.c.

◆ is_digit

#define is_digit (   c)    ((c) >= '0' && (c) <= '9')

Definition at line 39 of file astoll.c.

◆ is_hex

#define is_hex (   c)
Value:
(\
((c) >= 'a' && (c) <= 'f') || \
((c) >= 'A' && (c) <= 'F'))
const GLubyte * c
Definition: glext.h:8905
#define c
Definition: ke_i.h:80

Definition at line 40 of file astoll.c.

◆ is_lower

#define is_lower (   c)    ((c) >= 'a' && (c) <= 'z')

Definition at line 44 of file astoll.c.

◆ is_space

#define is_space (   c)    ((c) == ' ' || (c) == '\t')

Definition at line 38 of file astoll.c.

◆ is_upper

#define is_upper (   c)    ((c) >= 'A' && (c) <= 'Z')

Definition at line 45 of file astoll.c.

◆ to_lower

#define to_lower (   c)    (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A'+'a' : (c))

Definition at line 46 of file astoll.c.

Function Documentation

◆ astoll()

char * astoll ( const char s,
Llong l 
)

Definition at line 56 of file astoll.c.

59{
60 return (astollb(s, l, 0));
61}
char * astollb(const char *s, Llong *l, int base)
Definition: astoll.c:64
r l[0]
Definition: byte_order.h:168
GLdouble s
Definition: gl.h:2039

Referenced by doflag().

◆ astollb()

char * astollb ( const char s,
Llong l,
int  base 
)

Definition at line 64 of file astoll.c.

68{
69 int neg = 0;
70 register ULlong ret = (ULlong)0;
71 ULlong maxmult;
72 ULlong maxval;
73 register int digit;
74 register char c;
75
76 if (base > BASE_MAX || base == 1 || base < 0) {
78 return ((char *)s);
79 }
80
81 while (is_space(*s))
82 s++;
83
84 if (*s == '+') {
85 s++;
86 } else if (*s == '-') {
87 s++;
88 neg++;
89 }
90
91 if (base == 0) {
92 if (*s == '0') {
93 base = 8;
94 s++;
95 if (*s == 'x' || *s == 'X') {
96 s++;
97 base = 16;
98 }
99 } else {
100 base = 10;
101 }
102 }
103 if (neg) {
104 /*
105 * Portable way to compute the positive value of "min-Llong"
106 * as -TYPE_MINVAL(Llong) does not work.
107 */
108 maxval = ((ULlong)(-1 * (TYPE_MINVAL(Llong)+1))) + 1;
109 } else {
110 maxval = TYPE_MAXVAL(Llong);
111 }
112 maxmult = maxval / base;
113 for (; (c = *s) != 0; s++) {
114
115 if (is_digit(c)) {
116 digit = c - '0';
117 } else if (is_lower(c)) {
118 digit = c - 'a' + 10;
119 } else if (is_upper(c)) {
120 digit = c - 'A' + 10;
121 } else {
122 break;
123 }
124
125 if (digit < base) {
126 if (ret > maxmult)
127 goto overflow;
128 ret *= base;
129 if (maxval - ret < digit)
130 goto overflow;
131 ret += digit;
132 } else {
133 break;
134 }
135 }
136 if (neg) {
137 *l = (Llong)-1 * ret;
138 } else {
139 *l = (Llong)ret;
140 }
141 return ((char *)s);
142overflow:
143 for (; (c = *s) != 0; s++) {
144
145 if (is_digit(c)) {
146 digit = c - '0';
147 } else if (is_lower(c)) {
148 digit = c - 'a' + 10;
149 } else if (is_upper(c)) {
150 digit = c - 'A' + 10;
151 } else {
152 break;
153 }
154 if (digit >= base)
155 break;
156 }
157 if (neg) {
158 *l = TYPE_MINVAL(Llong);
159 } else {
160 *l = TYPE_MAXVAL(Llong);
161 }
163 return ((char *)s);
164}
#define EINVAL
Definition: acclib.h:90
#define ERANGE
Definition: acclib.h:92
#define is_lower(c)
Definition: astoll.c:44
#define is_digit(c)
Definition: astoll.c:39
#define BASE_MAX
Definition: astoll.c:51
#define is_space(c)
Definition: astoll.c:38
#define is_upper(c)
Definition: astoll.c:45
EXPORT int seterrno(int err)
Definition: seterrno.c:34
long Llong
Definition: stdint.h:152
unsigned long ULlong
Definition: stdint.h:154
#define TYPE_MAXVAL(t)
Definition: stdint.h:89
#define TYPE_MINVAL(t)
Definition: stdint.h:86
int ret

Referenced by astoll().