NeoMutt  2024-04-25-89-g194907
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
atoi.c
Go to the documentation of this file.
1
30#include "config.h"
31#include <errno.h>
32#include <limits.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include "atoi.h"
36
54static const char *str_atol_clamp(const char *str, long *dst, long lmin, long lmax)
55{
56 if (dst)
57 {
58 *dst = 0;
59 }
60
61 if (!str || (*str == '\0'))
62 {
63 return NULL;
64 }
65
66 char *e = NULL;
67 errno = 0;
68 long res = strtol(str, &e, 10);
69 if ((e == str) || (((res == LONG_MIN) || (res == LONG_MAX)) && (errno == ERANGE)) ||
70 (res < lmin) || (res > lmax))
71 {
72 return NULL;
73 }
74
75 if (dst)
76 {
77 *dst = res;
78 }
79
80 return e;
81}
82
99static const char *str_atoull_clamp(const char *str, unsigned long long *dst,
100 unsigned long long ullmax)
101{
102 if (dst)
103 {
104 *dst = 0;
105 }
106
107 if (!str || (*str == '\0'))
108 {
109 return str;
110 }
111
112 char *e = NULL;
113 errno = 0;
114 unsigned long long res = strtoull(str, &e, 10);
115 if ((e == str) || ((res == ULLONG_MAX) && (errno == ERANGE)) || (res > ullmax))
116 {
117 return NULL;
118 }
119
120 if (dst)
121 {
122 *dst = res;
123 }
124
125 return e;
126}
127
143const char *mutt_str_atol(const char *str, long *dst)
144{
145 return str_atol_clamp(str, dst, LONG_MIN, LONG_MAX);
146}
147
161const char *mutt_str_atos(const char *str, short *dst)
162{
163 long l;
164 const char *res = str_atol_clamp(str, &l, SHRT_MIN, SHRT_MAX);
165 if (dst)
166 {
167 *dst = res ? l : 0;
168 }
169 return res;
170}
171
188const char *mutt_str_atoi(const char *str, int *dst)
189{
190 long l;
191 const char *res = str_atol_clamp(str, &l, INT_MIN, INT_MAX);
192 if (dst)
193 {
194 *dst = res ? l : 0;
195 }
196 return res;
197}
198
214const char *mutt_str_atoui(const char *str, unsigned int *dst)
215{
216 unsigned long long l;
217 const char *res = str_atoull_clamp(str, &l, UINT_MAX);
218 if (dst)
219 {
220 *dst = res ? l : 0;
221 }
222 return res;
223}
224
240const char *mutt_str_atoul(const char *str, unsigned long *dst)
241{
242 unsigned long long l;
243 const char *res = str_atoull_clamp(str, &l, ULONG_MAX);
244 if (dst)
245 {
246 *dst = res ? l : 0;
247 }
248 return res;
249}
250
266const char *mutt_str_atous(const char *str, unsigned short *dst)
267{
268 unsigned long long l;
269 const char *res = str_atoull_clamp(str, &l, USHRT_MAX);
270 if (dst)
271 {
272 *dst = res ? l : 0;
273 }
274 return res;
275}
276
292const char *mutt_str_atoull(const char *str, unsigned long long *dst)
293{
294 return str_atoull_clamp(str, dst, ULLONG_MAX);
295}
static const char * str_atol_clamp(const char *str, long *dst, long lmin, long lmax)
Convert ASCII string to a long and clamp.
Definition: atoi.c:54
const char * mutt_str_atoull(const char *str, unsigned long long *dst)
Convert ASCII string to an unsigned long long.
Definition: atoi.c:292
const char * mutt_str_atous(const char *str, unsigned short *dst)
Convert ASCII string to an unsigned short.
Definition: atoi.c:266
const char * mutt_str_atol(const char *str, long *dst)
Convert ASCII string to a long.
Definition: atoi.c:143
const char * mutt_str_atoul(const char *str, unsigned long *dst)
Convert ASCII string to an unsigned long.
Definition: atoi.c:240
static const char * str_atoull_clamp(const char *str, unsigned long long *dst, unsigned long long ullmax)
Convert ASCII string to an unsigned long long and clamp.
Definition: atoi.c:99
const char * mutt_str_atos(const char *str, short *dst)
Convert ASCII string to a short.
Definition: atoi.c:161
const char * mutt_str_atoui(const char *str, unsigned int *dst)
Convert ASCII string to an unsigned integer.
Definition: atoi.c:214
const char * mutt_str_atoi(const char *str, int *dst)
Convert ASCII string to an integer.
Definition: atoi.c:188
Parse a number in a string.