PipeWire  0.3.32
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 <stdarg.h>
33 #include <stdbool.h>
34 #include <errno.h>
35 
36 #include <spa/utils/defs.h>
37 
50 /* static */ inline bool spa_streq(const char *s1, const char *s2)
51 {
52  return SPA_LIKELY(s1 && s2) ? strcmp(s1, s2) == 0 : s1 == s2;
53 }
54 
60 /* static */ inline bool spa_strneq(const char *s1, const char *s2, size_t len)
61 {
62  return SPA_LIKELY(s1 && s2) ? strncmp(s1, s2, len) == 0 : s1 == s2;
63 }
64 
65 
71 /* static */ inline bool spa_strendswith(const char *s, const char *suffix)
72 {
73  size_t l1, l2;
74 
75  if (SPA_UNLIKELY(s == NULL))
76  return false;
77 
78  spa_assert(suffix);
79 
80  l1 = strlen(s);
81  l2 = strlen(suffix);
82  return l1 >= l2 && spa_streq(s + l1 - l2, suffix);
83 }
84 
93 /* static */ inline bool spa_atoi32(const char *str, int32_t *val, int base)
94 {
95  char *endptr;
96  long v;
97 
98  if (!str || *str =='\0')
99  return false;
100 
101  errno = 0;
102  v = strtol(str, &endptr, base);
103  if (errno != 0 || *endptr != '\0')
104  return false;
105 
106  if (v != (int32_t)v)
107  return false;
108 
109  *val = v;
110  return true;
111 }
112 
121 /* static */ inline bool spa_atou32(const char *str, uint32_t *val, int base)
122 {
123  char *endptr;
124  unsigned long long v;
125 
126  if (!str || *str =='\0')
127  return false;
128 
129  errno = 0;
130  v = strtoull(str, &endptr, base);
131  if (errno != 0 || *endptr != '\0')
132  return false;
133 
134  if (v != (uint32_t)v)
135  return false;
136 
137  *val = v;
138  return true;
139 }
140 
149 /* static */ inline bool spa_atoi64(const char *str, int64_t *val, int base)
150 {
151  char *endptr;
152  long long v;
153 
154  if (!str || *str =='\0')
155  return false;
156 
157  errno = 0;
158  v = strtoll(str, &endptr, base);
159  if (errno != 0 || *endptr != '\0')
160  return false;
161 
162  *val = v;
163  return true;
164 }
165 
174 /* static */ inline bool spa_atou64(const char *str, uint64_t *val, int base)
175 {
176  char *endptr;
177  unsigned long long v;
178 
179  if (!str || *str =='\0')
180  return false;
181 
182  errno = 0;
183  v = strtoull(str, &endptr, base);
184  if (errno != 0 || *endptr != '\0')
185  return false;
186 
187  *val = v;
188  return true;
189 }
190 
197 /* static */ inline bool spa_atob(const char *str)
198 {
199  return spa_streq(str, "true") || spa_streq(str, "1");
200 }
201 
210 SPA_PRINTF_FUNC(3, 0)
211 /* static */ inline int spa_vscnprintf(char *buffer, size_t size, const char *format, va_list args)
212 {
213  int r;
214 
215  spa_assert((ssize_t)size > 0);
216 
217  r = vsnprintf(buffer, size, format, args);
218  if (SPA_UNLIKELY(r < 0))
219  buffer[0] = '\0';
220  if (SPA_LIKELY(r < (ssize_t)size))
221  return r;
222  return size - 1;
223 }
224 
233 SPA_PRINTF_FUNC(3, 4)
234 /* static */ inline int spa_scnprintf(char *buffer, size_t size, const char *format, ...)
235 {
236  int r;
237  va_list args;
238 
239  va_start(args, format);
240  r = spa_vscnprintf(buffer, size, format, args);
241  va_end(args);
242 
243  return r;
244 }
245 
253 /* static */ inline bool spa_atof(const char *str, float *val)
254 {
255  char *endptr;
256  float v;
257 
258  if (!str || *str =='\0')
259  return false;
260 
261  errno = 0;
262  v = strtof(str, &endptr);
263  if (errno != 0 || *endptr != '\0')
264  return false;
265 
266  *val = v;
267  return true;
268 }
269 
277 /* static */ inline bool spa_atod(const char *str, double *val)
278 {
279  char *endptr;
280  double v;
281 
282  if (!str || *str =='\0')
283  return false;
284 
285  errno = 0;
286  v = strtod(str, &endptr);
287  if (errno != 0 || *endptr != '\0')
288  return false;
289 
290  *val = v;
291  return true;
292 }
293 
298 #ifdef __cplusplus
299 } /* extern "C" */
300 #endif
301 
302 #endif /* SPA_UTILS_STRING_H */
static uint32_t int int const char va_list args
Definition: core.h:330
va_end(args)
vsnprintf(buffer, sizeof(buffer), message, args)
static uint32_t int int const char int r
Definition: core.h:341
va_start(args, message)
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:93
int spa_scnprintf(char *buffer, size_t size, const char *format,...)
"Safe" version of snprintf.
Definition: string.h:234
bool spa_strneq(const char *s1, const char *s2, size_t len)
Definition: string.h:60
bool spa_atod(const char *str, double *val)
Convert str to a double and store the result in val.
Definition: string.h:277
bool spa_streq(const char *s1, const char *s2)
Definition: string.h:50
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:121
int spa_vscnprintf(char *buffer, size_t size, const char *format, va_list args)
"Safe" version of vsnprintf.
Definition: string.h:211
bool spa_atof(const char *str, float *val)
Convert str to a float and store the result in val.
Definition: string.h:253
bool spa_strendswith(const char *s, const char *suffix)
Definition: string.h:71
#define SPA_LIKELY(x)
Definition: defs.h:234
#define SPA_PRINTF_FUNC(fmt, arg1)
Definition: defs.h:205
bool spa_atob(const char *str)
Convert str to a boolean.
Definition: string.h:197
#define SPA_UNLIKELY(x)
Definition: defs.h:235
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:174
#define spa_assert(expr)
Definition: defs.h:288
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:149
Definition: filter.c:59