NeoMutt  2023-03-22
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 "atoi.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 51 of file atoi.c.

52{
53 if (dst)
54 {
55 *dst = 0;
56 }
57
58 if (!str || (*str == '\0'))
59 {
60 return NULL;
61 }
62
63 char *e = NULL;
64 errno = 0;
65 long res = strtol(str, &e, 10);
66 if ((e == str) || (((res == LONG_MIN) || (res == LONG_MAX)) && (errno == ERANGE)) ||
67 (res < lmin) || (res > lmax))
68 {
69 return NULL;
70 }
71
72 if (dst)
73 {
74 *dst = res;
75 }
76
77 return e;
78}
+ 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 94 of file atoi.c.

96{
97 if (dst)
98 {
99 *dst = 0;
100 }
101
102 if (!str || (*str == '\0'))
103 {
104 return str;
105 }
106
107 char *e = NULL;
108 errno = 0;
109 unsigned long long res = strtoull(str, &e, 10);
110 if ((e == str) || ((res == ULLONG_MAX) && (errno == ERANGE)) || (res > ullmax))
111 {
112 return NULL;
113 }
114
115 if (dst)
116 {
117 *dst = res;
118 }
119
120 return e;
121}
+ 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 136 of file atoi.c.

137{
138 return str_atol_clamp(str, dst, LONG_MIN, LONG_MAX);
139}
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:51
+ 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 154 of file atoi.c.

155{
156 long l;
157 const char *res = str_atol_clamp(str, &l, SHRT_MIN, SHRT_MAX);
158 if (dst)
159 {
160 *dst = res ? l : 0;
161 }
162 return res;
163}
+ 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 179 of file atoi.c.

180{
181 long l;
182 const char *res = str_atol_clamp(str, &l, INT_MIN, INT_MAX);
183 if (dst)
184 {
185 *dst = res ? l : 0;
186 }
187 return res;
188}
+ 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 203 of file atoi.c.

204{
205 unsigned long long l;
206 const char *res = str_atoull_clamp(str, &l, UINT_MAX);
207 if (dst)
208 {
209 *dst = res ? l : 0;
210 }
211 return res;
212}
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:94
+ 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 227 of file atoi.c.

228{
229 unsigned long long l;
230 const char *res = str_atoull_clamp(str, &l, ULONG_MAX);
231 if (dst)
232 {
233 *dst = res ? l : 0;
234 }
235 return res;
236}
+ 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 251 of file atoi.c.

252{
253 unsigned long long l;
254 const char *res = str_atoull_clamp(str, &l, USHRT_MAX);
255 if (dst)
256 {
257 *dst = res ? l : 0;
258 }
259 return res;
260}
+ 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 275 of file atoi.c.

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