NeoMutt  2024-11-14-34-g5aaf0d
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
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.
 
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.
 
const char * mutt_str_atol (const char *str, long *dst)
 Convert ASCII string to a long.
 
const char * mutt_str_atos (const char *str, short *dst)
 Convert ASCII string to a short.
 
const char * mutt_str_atoi (const char *str, int *dst)
 Convert ASCII string to an integer.
 
const char * mutt_str_atoui (const char *str, unsigned int *dst)
 Convert ASCII string to an unsigned integer.
 
const char * mutt_str_atoul (const char *str, unsigned long *dst)
 Convert ASCII string to an unsigned long.
 
const char * mutt_str_atous (const char *str, unsigned short *dst)
 Convert ASCII string to an unsigned short.
 
const char * mutt_str_atoull (const char *str, unsigned long long *dst)
 Convert ASCII string to an unsigned long long.
 

Detailed Description

Parse a number in a string.

Authors
  • Richard Russon
  • 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
ptrendptr
Condition Description
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 54 of file atoi.c.

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}
+ 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
ptrendptr
Condition Description
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 99 of file atoi.c.

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}
+ 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
ptrendptr
Condition Description
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 143 of file atoi.c.

144{
145 return str_atol_clamp(str, dst, LONG_MIN, LONG_MAX);
146}
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
+ 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
ptrendptr
Condition Description
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 165 of file atoi.c.

166{
167 long l = 0;
168 const char *res = str_atol_clamp(str, &l, SHRT_MIN, SHRT_MAX);
169 if (dst)
170 {
171 *dst = res ? l : 0;
172 }
173 return res;
174}
+ 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
ptrendptr
Condition Description
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 192 of file atoi.c.

193{
194 long l = 0;
195 const char *res = str_atol_clamp(str, &l, INT_MIN, INT_MAX);
196 if (dst)
197 {
198 *dst = res ? l : 0;
199 }
200 return res;
201}
+ 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
ptrendptr
Condition Description
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 218 of file atoi.c.

219{
220 unsigned long long l = 0;
221 const char *res = str_atoull_clamp(str, &l, UINT_MAX);
222 if (dst)
223 {
224 *dst = res ? l : 0;
225 }
226 return res;
227}
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
+ 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
ptrendptr
Condition Description
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 244 of file atoi.c.

245{
246 unsigned long long l = 0;
247 const char *res = str_atoull_clamp(str, &l, ULONG_MAX);
248 if (dst)
249 {
250 *dst = res ? l : 0;
251 }
252 return res;
253}
+ 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
ptrendptr
Condition Description
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 270 of file atoi.c.

271{
272 unsigned long long l = 0;
273 const char *res = str_atoull_clamp(str, &l, USHRT_MAX);
274 if (dst)
275 {
276 *dst = res ? l : 0;
277 }
278 return res;
279}
+ Here is the call graph for this function:
+ Here is the caller 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
ptrendptr
Condition Description
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 296 of file atoi.c.

297{
298 return str_atoull_clamp(str, dst, ULLONG_MAX);
299}
+ Here is the call graph for this function:
+ Here is the caller graph for this function: