Asterisk - The Open Source Telephony Project  GIT-master-b7027de
Functions
conversions.h File Reference

Conversion utility functions. More...

#include <stdint.h>

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...
 

Detailed Description

Conversion utility functions.

Definition in file conversions.h.

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
res[out] The converted value
Returns
-1 if it fails to convert, 0 on success

Definition at line 92 of file conversions.c.

References end, and errno.

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

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, 0);
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 }
Definition: ast_expr2.c:325
const char * str
Definition: app_jack.c:147
char * end
Definition: eagi_proxy.c:73
int errno

◆ 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
res[out] The converted value
Returns
-1 if it fails to convert, 0 on success

Definition at line 44 of file conversions.c.

References ast_str_to_imax().

Referenced by AST_TEST_DEFINE(), and category_set_sublevels().

45 {
46  intmax_t val;
47 
48  if (ast_str_to_imax(str, &val) || val < INT_MIN || val > INT_MAX) {
49  return -1;
50  }
51 
52  *res = val;
53  return 0;
54 }
Definition: ast_expr2.c:325
const char * str
Definition: app_jack.c:147
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

◆ 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
res[out] The converted value
Returns
-1 if it fails to convert, 0 on success

Definition at line 68 of file conversions.c.

References ast_str_to_imax().

Referenced by AST_TEST_DEFINE().

69 {
70  intmax_t val;
71 
72  if (ast_str_to_imax(str, &val) || val < LONG_MIN || val > LONG_MAX) {
73  return -1;
74  }
75 
76  *res = val;
77  return 0;
78 }
Definition: ast_expr2.c:325
const char * str
Definition: app_jack.c:147
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

◆ 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
res[out] The converted value
Returns
-1 if it fails to convert, 0 on success

Definition at line 56 of file conversions.c.

References ast_str_to_umax().

Referenced by AST_TEST_DEFINE(), func_get_parkingslot_channel(), handle_cli_rtcp_set_stats(), set_public_key_expiration(), stir_shaken_read(), and stream_echo_exec().

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 }
Definition: ast_expr2.c:325
const char * str
Definition: app_jack.c:147
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

◆ 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
res[out] The converted value
Returns
-1 if it fails to convert, 0 on success

Definition at line 80 of file conversions.c.

References ast_str_to_umax().

Referenced by AST_TEST_DEFINE(), and public_key_is_expired().

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 }
Definition: ast_expr2.c:325
const char * str
Definition: app_jack.c:147
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

◆ 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
res[out] The converted value
Returns
-1 if it fails to convert, 0 on success

Definition at line 119 of file conversions.c.

References end, errno, 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().

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, 0);
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 }
Definition: ast_expr2.c:325
const char * str
Definition: app_jack.c:147
char * end
Definition: eagi_proxy.c:73
static int str_is_negative(const char **str)
Definition: conversions.c:34
int errno