blob: e7174b21aabf5ec46be65118fdf83f4d777ca455 [file] [log] [blame]
Bryce Harrington82b9f2b2016-08-03 17:40:51 -07001/*
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 <stdlib.h>
Kylie McClain5d72bc42016-09-23 23:40:14 -040031#include <stdint.h>
Bryce Harrington82b9f2b2016-08-03 17:40:51 -070032#include <errno.h>
33#include <assert.h>
Pekka Paalanenbc3c3782021-05-28 14:40:00 +030034#include <stdarg.h>
35#include <stdio.h>
Bryce Harrington82b9f2b2016-08-03 17:40:51 -070036
37/* Convert string to integer
38 *
39 * Parses a base-10 number from the given string. Checks that the
40 * string is not blank, contains only numerical characters, and is
41 * within the range of INT32_MIN to INT32_MAX. If the validation is
42 * successful the result is stored in *value; otherwise *value is
43 * unchanged and errno is set appropriately.
44 *
45 * \return true if the number parsed successfully, false on error
46 */
47static inline bool
48safe_strtoint(const char *str, int32_t *value)
49{
50 long ret;
51 char *end;
52
53 assert(str != NULL);
54
55 errno = 0;
56 ret = strtol(str, &end, 10);
57 if (errno != 0) {
58 return false;
59 } else if (end == str || *end != '\0') {
60 errno = EINVAL;
61 return false;
62 }
63
64 if ((long)((int32_t)ret) != ret) {
65 errno = ERANGE;
66 return false;
67 }
68 *value = (int32_t)ret;
69
70 return true;
71}
72
Pekka Paalanenbc3c3782021-05-28 14:40:00 +030073/**
74 * Exactly like asprintf(), but sets *str_out to NULL if it fails.
75 *
76 * If str_out is NULL, does nothing.
77 */
78static inline void __attribute__ ((format (printf, 2, 3)))
79str_printf(char **str_out, const char *fmt, ...)
80{
81 char *msg;
82 va_list ap;
83 int ret;
84
85 if (!str_out)
86 return;
87
88 va_start(ap, fmt);
89 ret = vasprintf(&msg, fmt, ap);
90 va_end(ap);
91
92 if (ret >= 0)
93 *str_out = msg;
94 else
95 *str_out = NULL;
96}
97
Bryce Harrington82b9f2b2016-08-03 17:40:51 -070098#endif /* WESTON_STRING_HELPERS_H */