PipeWire  0.3.40
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 
54 static inline bool spa_streq(const char *s1, const char *s2)
55 {
56  return SPA_LIKELY(s1 && s2) ? strcmp(s1, s2) == 0 : s1 == s2;
57 }
58 
64 static inline bool spa_strneq(const char *s1, const char *s2, size_t len)
65 {
66  return SPA_LIKELY(s1 && s2) ? strncmp(s1, s2, len) == 0 : s1 == s2;
67 }
68 
69 
75 static inline bool spa_strstartswith(const char *s, const char *prefix)
76 {
77  if (SPA_UNLIKELY(s == NULL))
78  return false;
79 
80  spa_assert_se(prefix);
81 
82  return strncmp(s, prefix, strlen(prefix)) == 0;
83 }
84 
85 
91 static inline bool spa_strendswith(const char *s, const char *suffix)
92 {
93  size_t l1, l2;
94 
95  if (SPA_UNLIKELY(s == NULL))
96  return false;
97 
98  spa_assert_se(suffix);
99 
100  l1 = strlen(s);
101  l2 = strlen(suffix);
102  return l1 >= l2 && spa_streq(s + l1 - l2, suffix);
103 }
104 
113 static inline bool spa_atoi32(const char *str, int32_t *val, int base)
114 {
115  char *endptr;
116  long v;
117 
118  if (!str || *str =='\0')
119  return false;
120 
121  errno = 0;
122  v = strtol(str, &endptr, base);
123  if (errno != 0 || *endptr != '\0')
124  return false;
125 
126  if (v != (int32_t)v)
127  return false;
128 
129  *val = v;
130  return true;
131 }
132 
141 static inline bool spa_atou32(const char *str, uint32_t *val, int base)
142 {
143  char *endptr;
144  unsigned long long v;
145 
146  if (!str || *str =='\0')
147  return false;
148 
149  errno = 0;
150  v = strtoull(str, &endptr, base);
151  if (errno != 0 || *endptr != '\0')
152  return false;
153 
154  if (v != (uint32_t)v)
155  return false;
156 
157  *val = v;
158  return true;
159 }
160 
169 static inline bool spa_atoi64(const char *str, int64_t *val, int base)
170 {
171  char *endptr;
172  long long v;
173 
174  if (!str || *str =='\0')
175  return false;
176 
177  errno = 0;
178  v = strtoll(str, &endptr, base);
179  if (errno != 0 || *endptr != '\0')
180  return false;
181 
182  *val = v;
183  return true;
184 }
185 
194 static inline bool spa_atou64(const char *str, uint64_t *val, int base)
195 {
196  char *endptr;
197  unsigned long long v;
198 
199  if (!str || *str =='\0')
200  return false;
201 
202  errno = 0;
203  v = strtoull(str, &endptr, base);
204  if (errno != 0 || *endptr != '\0')
205  return false;
206 
207  *val = v;
208  return true;
209 }
210 
217 static inline bool spa_atob(const char *str)
218 {
219  return spa_streq(str, "true") || spa_streq(str, "1");
220 }
221 
230 SPA_PRINTF_FUNC(3, 0)
231 static inline int spa_vscnprintf(char *buffer, size_t size, const char *format, va_list args)
232 {
233  int r;
234 
235  spa_assert_se((ssize_t)size > 0);
236 
237  r = vsnprintf(buffer, size, format, args);
238  if (SPA_UNLIKELY(r < 0))
239  buffer[0] = '\0';
240  if (SPA_LIKELY(r < (ssize_t)size))
241  return r;
242  return size - 1;
243 }
244 
253 SPA_PRINTF_FUNC(3, 4)
254 static inline int spa_scnprintf(char *buffer, size_t size, const char *format, ...)
255 {
256  int r;
257  va_list args;
258 
259  va_start(args, format);
260  r = spa_vscnprintf(buffer, size, format, args);
261  va_end(args);
262 
263  return r;
264 }
265 
273 static inline bool spa_atof(const char *str, float *val)
274 {
275  char *endptr;
276  float v;
277 
278  if (!str || *str =='\0')
279  return false;
280 
281  errno = 0;
282  v = strtof(str, &endptr);
283  if (errno != 0 || *endptr != '\0')
284  return false;
285 
286  *val = v;
287  return true;
288 }
289 
297 static inline bool spa_atod(const char *str, double *val)
298 {
299  char *endptr;
300  double v;
301 
302  if (!str || *str =='\0')
303  return false;
304 
305  errno = 0;
306  v = strtod(str, &endptr);
307  if (errno != 0 || *endptr != '\0')
308  return false;
309 
310  *val = v;
311  return true;
312 }
313 
318 #ifdef __cplusplus
319 } /* extern "C" */
320 #endif
321 
322 #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:302
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:199
static bool spa_strstartswith(const char *s, const char *prefix)
Definition: string.h:80
static bool spa_atob(const char *str)
Convert str to a boolean.
Definition: string.h:222
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:174
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:146
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:118
static bool spa_strendswith(const char *s, const char *suffix)
Definition: string.h:96
static bool spa_strneq(const char *s1, const char *s2, size_t len)
Definition: string.h:69
static int spa_vscnprintf(char *buffer, size_t size, const char *format, va_list args)
Definition: string.h:236
static bool spa_streq(const char *s1, const char *s2)
Definition: string.h:59
static bool spa_atof(const char *str, float *val)
Convert str to a float and store the result in val.
Definition: string.h:278
static int spa_scnprintf(char *buffer, size_t size, const char *format,...)
Definition: string.h:259
#define spa_assert_se(expr)
Definition: defs.h:340
#define SPA_LIKELY(x)
Definition: defs.h:308
#define SPA_PRINTF_FUNC(fmt, arg1)
Definition: defs.h:260
#define SPA_UNLIKELY(x)
Definition: defs.h:310