Asterisk - The Open Source Telephony Project GIT-master-f36a736
Functions
conversions.c File Reference

Conversion utility functions. More...

#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <inttypes.h>
#include <stdio.h>
#include "asterisk/conversions.h"
Include dependency graph for conversions.c:

Go to the source code of this file.

Functions

int ast_str_to_imax (const char *str, intmax_t *res)
 Convert the given string to a signed max size integer. More...
 
int ast_str_to_int (const char *str, int *res)
 Convert the given string to a signed integer. More...
 
int ast_str_to_long (const char *str, long *res)
 Convert the given string to a signed long. More...
 
int ast_str_to_uint (const char *str, unsigned int *res)
 Convert the given string to an unsigned integer. More...
 
int ast_str_to_ulong (const char *str, unsigned long *res)
 Convert the given string to an unsigned long. More...
 
int ast_str_to_umax (const char *str, uintmax_t *res)
 Convert the given string to an unsigned max size integer. More...
 
static int str_is_negative (const char **str)
 

Detailed Description

Conversion utility functions.

Definition in file conversions.c.

Function Documentation

◆ ast_str_to_imax()

int ast_str_to_imax ( const char *  str,
intmax_t *  res 
)

Convert the given string to a signed max size integer.

This function will return failure for the following reasons:

The given string to convert is NULL The given string to convert is empty. The given string to convert contains non numeric values Once converted the number is out of range (less than INTMAX_MIN or greater than INTMAX_MAX)

Parameters
strThe string to convert
[out]resThe converted value
Returns
-1 if it fails to convert, 0 on success

Definition at line 92 of file conversions.c.

93{
94 char *end;
95 intmax_t val;
96
97 if (!str) {
98 return -1;
99 }
100
101 errno = 0;
102 val = strtoimax(str, &end, 10);
103
104 /*
105 * If str equals end then no digits were found. If end is not pointing to
106 * a null character then the string contained some numbers that could be
107 * converted, but some characters that could not, which we'll consider
108 * invalid.
109 */
110 if (str == end || *end != '\0' || (errno == ERANGE &&
111 (val == INTMAX_MIN || val == INTMAX_MAX))) {
112 return -1;
113 }
114
115 *res = val;
116 return 0;
117}
const char * str
Definition: app_jack.c:147
char * end
Definition: eagi_proxy.c:73
int errno
Definition: ast_expr2.c:325

References end, errno, and str.

Referenced by ast_str_to_int(), ast_str_to_long(), and AST_TEST_DEFINE().

◆ ast_str_to_int()

int ast_str_to_int ( const char *  str,
int *  res 
)

Convert the given string to a signed integer.

This function will return failure for the following reasons:

The given string to convert is NULL The given string to convert is empty. The given string to convert contains non numeric values Once converted the number is out of range (less than INT_MIN or greater than INT_MAX)

Parameters
strThe string to convert
[out]resThe converted value
Returns
-1 if it fails to convert, 0 on success

Definition at line 44 of file conversions.c.

45{
46 intmax_t val;
47
49 return -1;
50 }
51
52 *res = val;
53 return 0;
54}
int ast_str_to_imax(const char *str, intmax_t *res)
Convert the given string to a signed max size integer.
Definition: conversions.c:92

References ast_str_to_imax(), and str.

Referenced by AST_TEST_DEFINE(), category_set_sublevels(), detect_write(), dtmfstore_exec(), freq_parser(), json_decode_read(), parse_node(), pbx_builtin_saynumber(), pbx_builtin_sayordinal(), read_sf_exec(), response_code_validator(), sayfile_exec(), scan_exec(), sendsf_exec(), set_id_from_oli(), and wait_exec().

◆ ast_str_to_long()

int ast_str_to_long ( const char *  str,
long *  res 
)

Convert the given string to a signed long.

This function will return failure for the following reasons:

The given string to convert is NULL The given string to convert is empty. The given string to convert contains non numeric values Once converted the number is out of range (less than LONG_MIN or greater than LONG_MAX)

Parameters
strThe string to convert
[out]resThe converted value
Returns
-1 if it fails to convert, 0 on success

Definition at line 68 of file conversions.c.

69{
70 intmax_t val;
71
73 return -1;
74 }
75
76 *res = val;
77 return 0;
78}

References ast_str_to_imax(), and str.

Referenced by AST_TEST_DEFINE().

◆ ast_str_to_uint()

int ast_str_to_uint ( const char *  str,
unsigned int *  res 
)

Convert the given string to an unsigned integer.

This function will return failure for the following reasons:

The given string to convert is NULL The given string to convert is empty. The given string to convert is negative (starts with a '-') The given string to convert contains non numeric values Once converted the number is out of range (greater than UINT_MAX)

Parameters
strThe string to convert
[out]resThe converted value
Returns
-1 if it fails to convert, 0 on success

Definition at line 56 of file conversions.c.

57{
58 uintmax_t val;
59
60 if (ast_str_to_umax(str, &val) || val > UINT_MAX) {
61 return -1;
62 }
63
64 *res = val;
65 return 0;
66}
int ast_str_to_umax(const char *str, uintmax_t *res)
Convert the given string to an unsigned max size integer.
Definition: conversions.c:119

References ast_str_to_umax(), and str.

Referenced by add_cert_expiration_to_astdb(), AST_TEST_DEFINE(), func_get_parkingslot_channel(), func_read(), and stream_echo_exec().

◆ ast_str_to_ulong()

int ast_str_to_ulong ( const char *  str,
unsigned long *  res 
)

Convert the given string to an unsigned long.

This function will return failure for the following reasons:

The given string to convert is NULL The given string to convert is empty. The given string to convert is negative (starts with a '-') The given string to convert contains non numeric values Once converted the number is out of range (greater than ULONG_MAX)

Parameters
strThe string to convert
[out]resThe converted value
Returns
-1 if it fails to convert, 0 on success

Definition at line 80 of file conversions.c.

81{
82 uintmax_t val;
83
84 if (ast_str_to_umax(str, &val) || val > ULONG_MAX) {
85 return -1;
86 }
87
88 *res = val;
89 return 0;
90}

References ast_str_to_umax(), and str.

Referenced by AST_TEST_DEFINE(), and is_cert_cache_entry_expired().

◆ ast_str_to_umax()

int ast_str_to_umax ( const char *  str,
uintmax_t *  res 
)

Convert the given string to an unsigned max size integer.

This function will return failure for the following reasons:

The given string to convert is NULL The given string to convert is empty. The given string to convert is negative (starts with a '-') The given string to convert contains non numeric values Once converted the number is out of range (greater than UINTMAX_MAX)

Parameters
strThe string to convert
[out]resThe converted value
Returns
-1 if it fails to convert, 0 on success

Definition at line 119 of file conversions.c.

120{
121 char *end;
122 uintmax_t val;
123
124 if (!str || str_is_negative(&str)) {
125 return -1;
126 }
127
128 errno = 0;
129 val = strtoumax(str, &end, 10);
130
131 /*
132 * If str equals end then no digits were found. If end is not pointing to
133 * a null character then the string contained some numbers that could be
134 * converted, but some characters that could not, which we'll consider
135 * invalid.
136 */
137 if ((str == end || *end != '\0' || (errno == ERANGE && val == UINTMAX_MAX))) {
138 return -1;
139 }
140
141 *res = val;
142 return 0;
143}
static int str_is_negative(const char **str)
Definition: conversions.c:34

References end, errno, str, and str_is_negative().

Referenced by ast_str_to_uint(), ast_str_to_ulong(), AST_TEST_DEFINE(), create_foo_type_message(), mailbox_to_num(), and validate_data().

◆ str_is_negative()

static int str_is_negative ( const char **  str)
static

Definition at line 34 of file conversions.c.

35{
36 /*
37 * Ignore any preceding white space. It's okay to move the pointer here
38 * since the converting function would do the same, i.e. skip white space.
39 */
40 while (isspace(**str)) ++*str;
41 return **str == '-';
42}

References str.

Referenced by ast_str_to_umax().