PipeWire  0.3.30
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 
73 /* static */ inline bool spa_atoi32(const char *str, int32_t *val, int base)
74 {
75  char *endptr;
76  long v;
77 
78  if (!str || *str =='\0')
79  return false;
80 
81  errno = 0;
82  v = strtol(str, &endptr, base);
83  if (errno != 0 || *endptr != '\0')
84  return false;
85 
86  if (v != (int32_t)v)
87  return false;
88 
89  *val = v;
90  return true;
91 }
92 
101 /* static */ inline bool spa_atou32(const char *str, uint32_t *val, int base)
102 {
103  char *endptr;
104  unsigned long long v;
105 
106  if (!str || *str =='\0')
107  return false;
108 
109  errno = 0;
110  v = strtoull(str, &endptr, base);
111  if (errno != 0 || *endptr != '\0')
112  return false;
113 
114  if (v != (uint32_t)v)
115  return false;
116 
117  *val = v;
118  return true;
119 }
120 
129 /* static */ inline bool spa_atoi64(const char *str, int64_t *val, int base)
130 {
131  char *endptr;
132  long long v;
133 
134  if (!str || *str =='\0')
135  return false;
136 
137  errno = 0;
138  v = strtoll(str, &endptr, base);
139  if (errno != 0 || *endptr != '\0')
140  return false;
141 
142  *val = v;
143  return true;
144 }
145 
154 /* static */ inline bool spa_atou64(const char *str, uint64_t *val, int base)
155 {
156  char *endptr;
157  unsigned long long v;
158 
159  if (!str || *str =='\0')
160  return false;
161 
162  errno = 0;
163  v = strtoull(str, &endptr, base);
164  if (errno != 0 || *endptr != '\0')
165  return false;
166 
167  *val = v;
168  return true;
169 }
170 
177 /* static */ inline bool spa_atob(const char *str)
178 {
179  return spa_streq(str, "true") || spa_streq(str, "1");
180 }
181 
190 SPA_PRINTF_FUNC(3, 0)
191 /* static */ inline int spa_vscnprintf(char *buffer, size_t size, const char *format, va_list args)
192 {
193  int r;
194 
195  spa_assert((ssize_t)size > 0);
196 
197  r = vsnprintf(buffer, size, format, args);
198  if (SPA_UNLIKELY(r < 0))
199  buffer[0] = '\0';
200  if (SPA_LIKELY(r < (ssize_t)size))
201  return r;
202  return size - 1;
203 }
204 
213 SPA_PRINTF_FUNC(3, 4)
214 /* static */ inline int spa_scnprintf(char *buffer, size_t size, const char *format, ...)
215 {
216  int r;
217  va_list args;
218 
219  va_start(args, format);
220  r = spa_vscnprintf(buffer, size, format, args);
221  va_end(args);
222 
223  return r;
224 }
225 
233 /* static */ inline bool spa_atof(const char *str, float *val)
234 {
235  char *endptr;
236  float v;
237 
238  if (!str || *str =='\0')
239  return false;
240 
241  errno = 0;
242  v = strtof(str, &endptr);
243  if (errno != 0 || *endptr != '\0')
244  return false;
245 
246  *val = v;
247  return true;
248 }
249 
257 /* static */ inline bool spa_atod(const char *str, double *val)
258 {
259  char *endptr;
260  double v;
261 
262  if (!str || *str =='\0')
263  return false;
264 
265  errno = 0;
266  v = strtod(str, &endptr);
267  if (errno != 0 || *endptr != '\0')
268  return false;
269 
270  *val = v;
271  return true;
272 }
273 
278 #ifdef __cplusplus
279 } /* extern "C" */
280 #endif
281 
282 #endif /* SPA_UTILS_STRING_H */
spa_strneq
bool spa_strneq(const char *s1, const char *s2, size_t len)
Definition: string.h:60
r
static uint32_t int int const char int r
Definition: core.h:341
va_end
va_end(args)
spa_streq
bool spa_streq(const char *s1, const char *s2)
Definition: string.h:50
spa_atou32
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:101
SPA_PRINTF_FUNC
#define SPA_PRINTF_FUNC(fmt, arg1)
Definition: defs.h:204
SPA_LIKELY
#define SPA_LIKELY(x)
Definition: defs.h:232
spa_assert
#define spa_assert(expr)
Definition: defs.h:269
spa_atod
bool spa_atod(const char *str, double *val)
Convert str to a double and store the result in val.
Definition: string.h:257
spa_scnprintf
int spa_scnprintf(char *buffer, size_t size, const char *format,...)
"Safe" version of snprintf.
Definition: string.h:214
spa_vscnprintf
int spa_vscnprintf(char *buffer, size_t size, const char *format, va_list args)
"Safe" version of vsnprintf.
Definition: string.h:191
spa_atof
bool spa_atof(const char *str, float *val)
Convert str to a float and store the result in val.
Definition: string.h:233
va_start
va_start(args, message)
buffer
Definition: filter.c:59
spa_atoi32
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:73
vsnprintf
vsnprintf(buffer, sizeof(buffer), message, args)
spa_atoi64
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:129
defs.h
spa_atou64
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:154
SPA_UNLIKELY
#define SPA_UNLIKELY(x)
Definition: defs.h:233
spa_atob
bool spa_atob(const char *str)
Convert str to a boolean.
Definition: string.h:177
args
static uint32_t int int const char va_list args
Definition: core.h:330