Asterisk - The Open Source Telephony Project  18.5.0
conversions.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2017, Digium, Inc.
5  *
6  * See http://www.asterisk.org for more information about
7  * the Asterisk project. Please do not directly contact
8  * any of the maintainers of this project for assistance;
9  * the project provides a web site, mailing lists and IRC
10  * channels for your use.
11  *
12  * This program is free software, distributed under the terms of
13  * the GNU General Public License Version 2. See the LICENSE file
14  * at the top of the source tree.
15  */
16 
17 /*! \file
18  *
19  * \brief Conversion utility functions
20  */
21 
22 /*** MODULEINFO
23  <support_level>core</support_level>
24  ***/
25 
26 #include <ctype.h>
27 #include <errno.h>
28 #include <limits.h>
29 #include <inttypes.h>
30 #include <stdio.h>
31 
32 #include "asterisk/conversions.h"
33 
34 static int str_is_negative(const char **str)
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 }
43 
44 int ast_str_to_int(const char *str, int *res)
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 }
55 
56 int ast_str_to_uint(const char *str, unsigned int *res)
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 }
67 
68 int ast_str_to_long(const char *str, long *res)
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 }
79 
80 int ast_str_to_ulong(const char *str, unsigned long *res)
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 }
91 
92 int ast_str_to_imax(const char *str, intmax_t *res)
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 }
118 
119 int ast_str_to_umax(const char *str, uintmax_t *res)
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 }
int ast_str_to_long(const char *str, long *res)
Convert the given string to a signed long.
Definition: conversions.c:68
int ast_str_to_int(const char *str, int *res)
Convert the given string to a signed integer.
Definition: conversions.c:44
Definition: ast_expr2.c:325
int ast_str_to_ulong(const char *str, unsigned long *res)
Convert the given string to an unsigned long.
Definition: conversions.c:80
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
Conversion utility functions.
int errno
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
int ast_str_to_uint(const char *str, unsigned int *res)
Convert the given string to an unsigned integer.
Definition: conversions.c:56
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