PipeWire  0.3.29
string.h
Go to the documentation of this file.
1 /* Simple Plugin API
2  *
3  * Copyright © 2021 Red Hat, Inc.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24 
25 #ifndef SPA_UTILS_STRING_H
26 #define SPA_UTILS_STRING_H
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 #include <stdbool.h>
33 #include <errno.h>
34 
35 #include <spa/utils/defs.h>
36 
49 /* static */ inline bool spa_streq(const char *s1, const char *s2)
50 {
51  return SPA_LIKELY(s1 && s2) ? strcmp(s1, s2) == 0 : s1 == s2;
52 }
53 
59 /* static */ inline bool spa_strneq(const char *s1, const char *s2, size_t len)
60 {
61  return SPA_LIKELY(s1 && s2) ? strncmp(s1, s2, len) == 0 : s1 == s2;
62 }
63 
72 /* static */ inline bool spa_atoi32(const char *str, int32_t *val, int base)
73 {
74  char *endptr;
75  long v;
76 
77  if (!str || *str =='\0')
78  return false;
79 
80  errno = 0;
81  v = strtol(str, &endptr, base);
82  if (errno != 0 || *endptr != '\0')
83  return false;
84 
85  if (v != (int32_t)v)
86  return false;
87 
88  *val = v;
89  return true;
90 }
91 
100 /* static */ inline bool spa_atou32(const char *str, uint32_t *val, int base)
101 {
102  char *endptr;
103  unsigned long long v;
104 
105  if (!str || *str =='\0')
106  return false;
107 
108  errno = 0;
109  v = strtoull(str, &endptr, base);
110  if (errno != 0 || *endptr != '\0')
111  return false;
112 
113  if (v != (uint32_t)v)
114  return false;
115 
116  *val = v;
117  return true;
118 }
119 
128 /* static */ inline bool spa_atoi64(const char *str, int64_t *val, int base)
129 {
130  char *endptr;
131  long long v;
132 
133  if (!str || *str =='\0')
134  return false;
135 
136  errno = 0;
137  v = strtoll(str, &endptr, base);
138  if (errno != 0 || *endptr != '\0')
139  return false;
140 
141  *val = v;
142  return true;
143 }
144 
153 /* static */ inline bool spa_atou64(const char *str, uint64_t *val, int base)
154 {
155  char *endptr;
156  unsigned long long v;
157 
158  if (!str || *str =='\0')
159  return false;
160 
161  errno = 0;
162  v = strtoull(str, &endptr, base);
163  if (errno != 0 || *endptr != '\0')
164  return false;
165 
166  *val = v;
167  return true;
168 }
169 
176 /* static */ inline bool spa_atob(const char *str)
177 {
178  return spa_streq(str, "true") || spa_streq(str, "1");
179 }
180 
188 /* static */ inline bool spa_atof(const char *str, float *val)
189 {
190  char *endptr;
191  float v;
192 
193  if (!str || *str =='\0')
194  return false;
195 
196  errno = 0;
197  v = strtof(str, &endptr);
198  if (errno != 0 || *endptr != '\0')
199  return false;
200 
201  *val = v;
202  return true;
203 }
204 
212 /* static */ inline bool spa_atod(const char *str, double *val)
213 {
214  char *endptr;
215  double v;
216 
217  if (!str || *str =='\0')
218  return false;
219 
220  errno = 0;
221  v = strtod(str, &endptr);
222  if (errno != 0 || *endptr != '\0')
223  return false;
224 
225  *val = v;
226  return true;
227 }
228 
233 #ifdef __cplusplus
234 } /* extern "C" */
235 #endif
236 
237 #endif /* SPA_UTILS_STRING_H */
bool spa_atof(const char *str, float *val)
Convert str to a float and store the result in val.
Definition: string.h:188
#define SPA_LIKELY(x)
Definition: defs.h:231
bool spa_atou32(const char *str, uint32_t *val, int base)
Convert str to an uint32_t with the given base and store the result in val.
Definition: string.h:100
bool spa_strneq(const char *s1, const char *s2, size_t len)
Definition: string.h:59
bool spa_atob(const char *str)
Convert str to a boolean.
Definition: string.h:176
bool spa_streq(const char *s1, const char *s2)
Definition: string.h:49
bool spa_atoi32(const char *str, int32_t *val, int base)
Convert str to an int32_t with the given base and store the result in val.
Definition: string.h:72
bool spa_atod(const char *str, double *val)
Convert str to a double and store the result in val.
Definition: string.h:212
bool spa_atoi64(const char *str, int64_t *val, int base)
Convert str to an int64_t with the given base and store the result in val.
Definition: string.h:128
bool spa_atou64(const char *str, uint64_t *val, int base)
Convert str to an uint64_t with the given base and store the result in val.
Definition: string.h:153