PipeWire 0.3.65
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
29extern "C" {
30#endif
31
32#include <stdarg.h>
33#include <stdbool.h>
34#include <errno.h>
35#include <stdlib.h>
36#include <locale.h>
37
38#include <spa/utils/defs.h>
39
56static inline bool spa_streq(const char *s1, const char *s2)
57{
58 return SPA_LIKELY(s1 && s2) ? strcmp(s1, s2) == 0 : s1 == s2;
59}
60
66static inline bool spa_strneq(const char *s1, const char *s2, size_t len)
67{
68 return SPA_LIKELY(s1 && s2) ? strncmp(s1, s2, len) == 0 : s1 == s2;
69}
70
77static inline bool spa_strstartswith(const char *s, const char *prefix)
78{
79 if (SPA_UNLIKELY(s == NULL))
80 return false;
81
83
84 return strncmp(s, prefix, strlen(prefix)) == 0;
85}
86
87
93static inline bool spa_strendswith(const char *s, const char *suffix)
94{
95 size_t l1, l2;
96
97 if (SPA_UNLIKELY(s == NULL))
98 return false;
99
100 spa_assert_se(suffix);
101
102 l1 = strlen(s);
103 l2 = strlen(suffix);
104 return l1 >= l2 && spa_streq(s + l1 - l2, suffix);
105}
106
115static inline bool spa_atoi32(const char *str, int32_t *val, int base)
116{
117 char *endptr;
118 long v;
119
120 if (!str || *str =='\0')
121 return false;
122
123 errno = 0;
124 v = strtol(str, &endptr, base);
125 if (errno != 0 || *endptr != '\0')
126 return false;
127
128 if (v != (int32_t)v)
129 return false;
130
131 *val = v;
132 return true;
133}
134
143static inline bool spa_atou32(const char *str, uint32_t *val, int base)
144{
145 char *endptr;
146 unsigned long long v;
147
148 if (!str || *str =='\0')
149 return false;
150
151 errno = 0;
152 v = strtoull(str, &endptr, base);
153 if (errno != 0 || *endptr != '\0')
154 return false;
155
156 if (v != (uint32_t)v)
157 return false;
158
159 *val = v;
160 return true;
161}
162
171static inline bool spa_atoi64(const char *str, int64_t *val, int base)
172{
173 char *endptr;
174 long long v;
175
176 if (!str || *str =='\0')
177 return false;
178
179 errno = 0;
180 v = strtoll(str, &endptr, base);
181 if (errno != 0 || *endptr != '\0')
182 return false;
183
184 *val = v;
185 return true;
186}
187
196static inline bool spa_atou64(const char *str, uint64_t *val, int base)
197{
198 char *endptr;
199 unsigned long long v;
200
201 if (!str || *str =='\0')
202 return false;
203
204 errno = 0;
205 v = strtoull(str, &endptr, base);
206 if (errno != 0 || *endptr != '\0')
207 return false;
208
209 *val = v;
210 return true;
211}
212
219static inline bool spa_atob(const char *str)
220{
221 return spa_streq(str, "true") || spa_streq(str, "1");
222}
223
232SPA_PRINTF_FUNC(3, 0)
233static inline int spa_vscnprintf(char *buffer, size_t size, const char *format, va_list args)
234{
235 int r;
236
237 spa_assert_se((ssize_t)size > 0);
239 r = vsnprintf(buffer, size, format, args);
240 if (SPA_UNLIKELY(r < 0))
241 buffer[0] = '\0';
242 if (SPA_LIKELY(r < (ssize_t)size))
243 return r;
244 return size - 1;
245}
246
255SPA_PRINTF_FUNC(3, 4)
256static inline int spa_scnprintf(char *buffer, size_t size, const char *format, ...)
257{
258 int r;
259 va_list args;
260
261 va_start(args, format);
262 r = spa_vscnprintf(buffer, size, format, args);
263 va_end(args);
264
265 return r;
266}
267
276static inline float spa_strtof(const char *str, char **endptr)
277{
278#ifndef __LOCALE_C_ONLY
279 static locale_t locale = NULL;
280 locale_t prev;
281#endif
282 float v;
283#ifndef __LOCALE_C_ONLY
284 if (SPA_UNLIKELY(locale == NULL))
285 locale = newlocale(LC_ALL_MASK, "C", NULL);
286 prev = uselocale(locale);
287#endif
288 v = strtof(str, endptr);
289#ifndef __LOCALE_C_ONLY
290 uselocale(prev);
291#endif
292 return v;
293}
294
302static inline bool spa_atof(const char *str, float *val)
303{
304 char *endptr;
305 float v;
306
307 if (!str || *str =='\0')
308 return false;
309 errno = 0;
310 v = spa_strtof(str, &endptr);
311 if (errno != 0 || *endptr != '\0')
312 return false;
313
314 *val = v;
315 return true;
316}
317
326static inline double spa_strtod(const char *str, char **endptr)
327{
328#ifndef __LOCALE_C_ONLY
329 static locale_t locale = NULL;
330 locale_t prev;
331#endif
332 double v;
333#ifndef __LOCALE_C_ONLY
334 if (SPA_UNLIKELY(locale == NULL))
335 locale = newlocale(LC_ALL_MASK, "C", NULL);
336 prev = uselocale(locale);
337#endif
338 v = strtod(str, endptr);
339#ifndef __LOCALE_C_ONLY
340 uselocale(prev);
341#endif
342 return v;
343}
344
352static inline bool spa_atod(const char *str, double *val)
353{
354 char *endptr;
355 double v;
356
357 if (!str || *str =='\0')
358 return false;
359
360 errno = 0;
361 v = spa_strtod(str, &endptr);
362 if (errno != 0 || *endptr != '\0')
363 return false;
364
365 *val = v;
366 return true;
367}
368
369static inline char *spa_dtoa(char *str, size_t size, double val)
370{
371 int i, l;
372 l = spa_scnprintf(str, size, "%f", val);
373 for (i = 0; i < l; i++)
374 if (str[i] == ',')
375 str[i] = '.';
376 return str;
377}
378
379struct spa_strbuf {
380 char *buffer;
381 size_t maxsize;
382 size_t pos;
383};
385static inline void spa_strbuf_init(struct spa_strbuf *buf, char *buffer, size_t maxsize)
387 buf->buffer = buffer;
388 buf->maxsize = maxsize;
389 buf->pos = 0;
391
392SPA_PRINTF_FUNC(2, 3)
393static inline int spa_strbuf_append(struct spa_strbuf *buf, const char *fmt, ...)
394{
395 size_t remain = buf->maxsize - buf->pos;
396 ssize_t written;
397 va_list args;
398 va_start(args, fmt);
399 written = vsnprintf(&buf->buffer[buf->pos], remain, fmt, args);
400 va_end(args);
401 if (written > 0)
402 buf->pos += SPA_MIN(remain, (size_t)written);
403 return written;
404}
405
410#ifdef __cplusplus
411} /* extern "C" */
412#endif
413
414#endif /* SPA_UTILS_STRING_H */
spa/utils/defs.h
static bool spa_atod(const char *str, double *val)
Convert str to a double and store the result in val.
Definition: string.h:357
static 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:201
static bool spa_strstartswith(const char *s, const char *prefix)
Definition: string.h:82
static double spa_strtod(const char *str, char **endptr)
Convert str to a double in the C locale.
Definition: string.h:331
static bool spa_atob(const char *str)
Convert str to a boolean.
Definition: string.h:224
static 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:176
static 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:148
static 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:120
static bool spa_strendswith(const char *s, const char *suffix)
Definition: string.h:98
static bool spa_strneq(const char *s1, const char *s2, size_t len)
Definition: string.h:71
static int spa_strbuf_append(struct spa_strbuf *buf, const char *fmt,...)
Definition: string.h:398
static float spa_strtof(const char *str, char **endptr)
Convert str to a float in the C locale.
Definition: string.h:281
static void spa_strbuf_init(struct spa_strbuf *buf, char *buffer, size_t maxsize)
Definition: string.h:390
static int spa_vscnprintf(char *buffer, size_t size, const char *format, va_list args)
Definition: string.h:238
static bool spa_streq(const char *s1, const char *s2)
Definition: string.h:61
static bool spa_atof(const char *str, float *val)
Convert str to a float and store the result in val.
Definition: string.h:307
static int spa_scnprintf(char *buffer, size_t size, const char *format,...)
Definition: string.h:261
static char * spa_dtoa(char *str, size_t size, double val)
Definition: string.h:374
#define SPA_MIN(a, b)
Definition: defs.h:167
#define spa_assert_se(expr)
Definition: defs.h:393
#define SPA_LIKELY(x)
Definition: defs.h:361
#define SPA_PRINTF_FUNC(fmt, arg1)
Definition: defs.h:289
#define SPA_UNLIKELY(x)
Definition: defs.h:363
Definition: string.h:384
size_t pos
Definition: string.h:387
size_t maxsize
Definition: string.h:386
char * buffer
Definition: string.h:385