NeoMutt
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
  • 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 53 of file atoi.c.

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

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

143{
144 return str_atol_clamp(str, dst, LONG_MIN, LONG_MAX);
145}
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:53
+ 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 160 of file atoi.c.

161{
162 long l;
163 const char *res = str_atol_clamp(str, &l, SHRT_MIN, SHRT_MAX);
164 if (dst)
165 {
166 *dst = res ? l : 0;
167 }
168 return res;
169}
+ 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 187 of file atoi.c.

188{
189 long l;
190 const char *res = str_atol_clamp(str, &l, INT_MIN, INT_MAX);
191 if (dst)
192 {
193 *dst = res ? l : 0;
194 }
195 return res;
196}
+ 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 213 of file atoi.c.

214{
215 unsigned long long l;
216 const char *res = str_atoull_clamp(str, &l, UINT_MAX);
217 if (dst)
218 {
219 *dst = res ? l : 0;
220 }
221 return res;
222}
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:98
+ 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 239 of file atoi.c.

240{
241 unsigned long long l;
242 const char *res = str_atoull_clamp(str, &l, ULONG_MAX);
243 if (dst)
244 {
245 *dst = res ? l : 0;
246 }
247 return res;
248}
+ 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 265 of file atoi.c.

266{
267 unsigned long long l;
268 const char *res = str_atoull_clamp(str, &l, USHRT_MAX);
269 if (dst)
270 {
271 *dst = res ? l : 0;
272 }
273 return res;
274}
+ 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
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 291 of file atoi.c.

292{
293 return str_atoull_clamp(str, dst, ULLONG_MAX);
294}
+ Here is the call graph for this function:
+ Here is the caller graph for this function: