compositor: Use stdint for specifing integer storage
[src/agl-compositor.git] / shared / string-helpers.h
1 /*
2  * Copyright © 2016 Samsung Electronics Co., Ltd
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial
14  * portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25
26 #ifndef WESTON_STRING_HELPERS_H
27 #define WESTON_STRING_HELPERS_H
28
29 #include <stdbool.h>
30 #include <stdarg.h>
31 #include <stdlib.h>
32 #include <stdint.h>
33 #include <errno.h>
34 #include <assert.h>
35
36 /* Convert string to integer
37  *
38  * Parses a base-10 number from the given string.  Checks that the
39  * string is not blank, contains only numerical characters, and is
40  * within the range of INT32_MIN to INT32_MAX.  If the validation is
41  * successful the result is stored in *value; otherwise *value is
42  * unchanged and errno is set appropriately.
43  *
44  * \return true if the number parsed successfully, false on error
45  */
46 static inline bool
47 safe_strtoint(const char *str, int32_t *value)
48 {
49         long ret;
50         char *end;
51
52         assert(str != NULL);
53
54         errno = 0;
55         ret = strtol(str, &end, 10);
56         if (errno != 0) {
57                 return false;
58         } else if (end == str || *end != '\0') {
59                 errno = EINVAL;
60                 return false;
61         }
62
63         if ((long)((int32_t)ret) != ret) {
64                 errno = ERANGE;
65                 return false;
66         }
67         *value = (int32_t)ret;
68
69         return true;
70 }
71
72 /**
73  * Exactly like asprintf(), but sets *str_out to NULL if it fails.
74  *
75  * If str_out is NULL, does nothing.
76  */
77 static inline void __attribute__ ((format (printf, 2, 3)))
78 str_printf(char **str_out, const char *fmt, ...)
79 {
80         char *msg;
81         va_list ap;
82         int ret;
83
84         if (!str_out)
85                 return;
86
87         va_start(ap, fmt);
88         ret = vasprintf(&msg, fmt, ap);
89         va_end(ap);
90
91         if (ret >= 0)
92                 *str_out = msg;
93         else
94                 *str_out = NULL;
95 }
96
97 #endif /* WESTON_STRING_HELPERS_H */