NeoMutt  2022-04-29-247-gc6aae8
Teaching an old dog new tricks
DOXYGEN
atoi.c File Reference

Parse a number in a string. More...

#include "config.h"
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
+ Include dependency graph for atoi.c:

Go to the source code of this file.

Functions

static const char * str_atol_clamp (const char *str, long *dst, long lmin, long lmax)
 Convert ASCII string to a long and clamp. More...
 
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. More...
 
const char * mutt_str_atol (const char *str, long *dst)
 Convert ASCII string to a long. More...
 
const char * mutt_str_atos (const char *str, short *dst)
 Convert ASCII string to a short. More...
 
const char * mutt_str_atoi (const char *str, int *dst)
 Convert ASCII string to an integer. More...
 
const char * mutt_str_atoui (const char *str, unsigned int *dst)
 Convert ASCII string to an unsigned integer. More...
 
const char * mutt_str_atoul (const char *str, unsigned long *dst)
 Convert ASCII string to an unsigned long. More...
 
const char * mutt_str_atous (const char *str, unsigned short *dst)
 Convert ASCII string to an unsigned short. More...
 
const char * mutt_str_atoull (const char *str, unsigned long long *dst)
 Convert ASCII string to an unsigned long long. More...
 

Detailed Description

Parse a number in a string.

Authors
  • Pietro Cerutti

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file atoi.c.

Function Documentation

◆ str_atol_clamp()

static const char * str_atol_clamp ( const char *  str,
long *  dst,
long  lmin,
long  lmax 
)
static

Convert ASCII string to a long and clamp.

Parameters
[in]strString to read
[in]lminLower bound
[in]lmaxUpper bound
[out]dstStore the result
Return values
endptr

endptr == NULL -> no conversion happened, or overflow endptr[0] == '\0' -> str was fully converted endptr[0] != '\0' -> endptr points to first non converted char in str

This is a strtol() wrapper with range checking. errno may be set on error, e.g. ERANGE

Definition at line 50 of file atoi.c.

51{
52 if (dst)
53 {
54 *dst = 0;
55 }
56
57 if (!str || (*str == '\0'))
58 {
59 return NULL;
60 }
61
62 char *e = NULL;
63 errno = 0;
64 long res = strtol(str, &e, 10);
65 if ((e == str) || (((res == LONG_MIN) || (res == LONG_MAX)) && (errno == ERANGE)) ||
66 (res < lmin) || (res > lmax))
67 {
68 return NULL;
69 }
70
71 if (dst)
72 {
73 *dst = res;
74 }
75
76 return e;
77}
+ Here is the caller graph for this function:

◆ str_atoull_clamp()

static const char * str_atoull_clamp ( const char *  str,
unsigned long long *  dst,
unsigned long long  ullmax 
)
static

Convert ASCII string to an unsigned long long and clamp.

Parameters
[in]strString to read
[in]ullmaxUpper bound
[out]dstStore the result
Return values
endptr

endptr == NULL -> no conversion happened, or overflow endptr[0] == '\0' -> str was fully converted endptr[0] != '\0' -> endptr points to first non converted char in str

This is a strtoull() wrapper with range checking. errno may be set on error, e.g. ERANGE

Definition at line 93 of file atoi.c.

95{
96 if (dst)
97 {
98 *dst = 0;
99 }
100
101 if (!str || (*str == '\0'))
102 {
103 return str;
104 }
105
106 char *e = NULL;
107 errno = 0;
108 unsigned long long res = strtoull(str, &e, 10);
109 if ((e == str) || ((res == ULLONG_MAX) && (errno == ERANGE)) || (res > ullmax))
110 {
111 return NULL;
112 }
113
114 if (dst)
115 {
116 *dst = res;
117 }
118
119 return e;
120}
+ Here is the caller graph for this function:

◆ mutt_str_atol()

const char * mutt_str_atol ( const char *  str,
long *  dst 
)

Convert ASCII string to a long.

Parameters
[in]strString to read
[out]dstStore the result
Return values
endptr

endptr == NULL -> no conversion happened, or overflow endptr[0] == '\0' -> str was fully converted endptr[0] != '\0' -> endptr points to first non converted char in str

This is a strtol() wrapper with range checking. errno may be set on error, e.g. ERANGE

Definition at line 135 of file atoi.c.

136{
137 return str_atol_clamp(str, dst, LONG_MIN, LONG_MAX);
138}
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:50
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_str_atos()

const char * mutt_str_atos ( const char *  str,
short *  dst 
)

Convert ASCII string to a short.

Parameters
[in]strString to read
[out]dstStore the result
Return values
0Success
-1Error
-2Error, overflow

This is a strtol() wrapper with range checking. If dst is NULL, the string will be tested only (without conversion).

errno may be set on error, e.g. ERANGE

Definition at line 153 of file atoi.c.

154{
155 long l;
156 const char *res = str_atol_clamp(str, &l, SHRT_MIN, SHRT_MAX);
157 if (dst)
158 {
159 *dst = res ? l : 0;
160 }
161 return res;
162}
+ Here is the call graph for this function:

◆ mutt_str_atoi()

const char * mutt_str_atoi ( const char *  str,
int *  dst 
)

Convert ASCII string to an integer.

Parameters
[in]strString to read
[out]dstStore the result
Return values
endptr

endptr == NULL -> no conversion happened, or overflow endptr[0] == '\0' -> str was fully converted endptr[0] != '\0' -> endptr points to first non converted char in str

This is a strtol() wrapper with range checking. If dst is NULL, the string will be tested only (without conversion). errno may be set on error, e.g. ERANGE

Definition at line 178 of file atoi.c.

179{
180 long l;
181 const char *res = str_atol_clamp(str, &l, INT_MIN, INT_MAX);
182 if (dst)
183 {
184 *dst = res ? l : 0;
185 }
186 return res;
187}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_str_atoui()

const char * mutt_str_atoui ( const char *  str,
unsigned int *  dst 
)

Convert ASCII string to an unsigned integer.

Parameters
[in]strString to read
[out]dstStore the result
Return values
endptr

endptr == NULL -> no conversion happened, or overflow endptr[0] == '\0' -> str was fully converted endptr[0] != '\0' -> endptr points to first non converted char in str

Note
This function's return value differs from the other functions. They return -1 if there is input beyond the number.

Definition at line 202 of file atoi.c.

203{
204 unsigned long long l;
205 const char *res = str_atoull_clamp(str, &l, UINT_MAX);
206 if (dst)
207 {
208 *dst = res ? l : 0;
209 }
210 return res;
211}
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:93
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_str_atoul()

const char * mutt_str_atoul ( const char *  str,
unsigned long *  dst 
)

Convert ASCII string to an unsigned long.

Parameters
[in]strString to read
[out]dstStore the result
Return values
endptr

endptr == NULL -> no conversion happened, or overflow endptr[0] == '\0' -> str was fully converted endptr[0] != '\0' -> endptr points to first non converted char in str

Note
This function's return value differs from the other functions. They return -1 if there is input beyond the number.

Definition at line 226 of file atoi.c.

227{
228 unsigned long long l;
229 const char *res = str_atoull_clamp(str, &l, ULONG_MAX);
230 if (dst)
231 {
232 *dst = res ? l : 0;
233 }
234 return res;
235}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_str_atous()

const char * mutt_str_atous ( const char *  str,
unsigned short *  dst 
)

Convert ASCII string to an unsigned short.

Parameters
[in]strString to read
[out]dstStore the result
Return values
endptr

endptr == NULL -> no conversion happened, or overflow endptr[0] == '\0' -> str was fully converted endptr[0] != '\0' -> endptr points to first non converted char in str

Note
This function's return value differs from the other functions. They return -1 if there is input beyond the number.

Definition at line 250 of file atoi.c.

251{
252 unsigned long long l;
253 const char *res = str_atoull_clamp(str, &l, USHRT_MAX);
254 if (dst)
255 {
256 *dst = res ? l : 0;
257 }
258 return res;
259}
+ Here is the call graph for this function:

◆ mutt_str_atoull()

const char * mutt_str_atoull ( const char *  str,
unsigned long long *  dst 
)

Convert ASCII string to an unsigned long long.

Parameters
[in]strString to read
[out]dstStore the result
Return values
endptr

endptr == NULL -> no conversion happened, or overflow endptr[0] == '\0' -> str was fully converted endptr[0] != '\0' -> endptr points to first non converted char in str

Note
This function's return value differs from the other functions. They return -1 if there is input beyond the number.

Definition at line 274 of file atoi.c.

275{
276 return str_atoull_clamp(str, dst, ULLONG_MAX);
277}
+ Here is the call graph for this function:
+ Here is the caller graph for this function: