Various utility functions.
More...
|
typedef void(* | pw_destroy_t) (void *object) |
| a function to destroy an item
|
|
|
const char * | pw_split_walk (const char *str, const char *delimiter, size_t *len, const char **state) |
| Split a string based on delimiters.
|
|
char ** | pw_split_strv (const char *str, const char *delimiter, int max_tokens, int *n_tokens) |
| Split a string based on delimiters.
|
|
int | pw_split_ip (char *str, const char *delimiter, int max_tokens, char *tokens[]) |
| Split a string in-place based on delimiters.
|
|
char ** | pw_strv_parse (const char *val, size_t len, int max_tokens, int *n_tokens) |
| Parse an array of strings.
|
|
int | pw_strv_find (char **a, const char *b) |
| Find a string in a NULL terminated array of strings.
|
|
int | pw_strv_find_common (char **a, char **b) |
| Check if two NULL terminated arrays of strings have a common string.
|
|
void | pw_free_strv (char **str) |
| Free a NULL terminated array of strings.
|
|
char * | pw_strip (char *str, const char *whitespace) |
| Strip all whitespace before and after a string.
|
|
ssize_t | pw_getrandom (void *buf, size_t buflen, unsigned int flags) |
| Fill a buffer with random data.
|
|
void | pw_random (void *buf, size_t buflen) |
| Fill a buffer with random data.
|
|
void * | pw_reallocarray (void *ptr, size_t nmemb, size_t size) |
|
Various utility functions.
◆ pw_destroy_t
typedef void(* pw_destroy_t) (void *object) |
a function to destroy an item
◆ strndupa
#define strndupa |
( |
|
s, |
|
|
|
n |
|
) |
| |
◆ strdupa
◆ pw_rand32
#define pw_rand32 |
( |
| ) |
({ uint32_t val; pw_random(&val, sizeof(val)); val; }) |
◆ PW_DEPRECATED
#define PW_DEPRECATED |
( |
|
v | ) |
({ __typeof__(v) _v SPA_DEPRECATED = (v); (void)_v; (v); }) |
◆ pw_split_walk()
const char * pw_split_walk |
( |
const char * |
str, |
|
|
const char * |
delimiter, |
|
|
size_t * |
len, |
|
|
const char ** |
state |
|
) |
| |
Split a string based on delimiters.
- Parameters
-
| str | a string to split |
| delimiter | delimiter characters to split on |
[out] | len | the length of the current string |
[in,out] | state | a state variable |
- Returns
- a string or NULL when the end is reached
Repeatedly call this function to split str into all substrings delimited by delimiter. state should be set to NULL on the first invocation and passed to the function until NULL is returned.
◆ pw_split_strv()
char ** pw_split_strv |
( |
const char * |
str, |
|
|
const char * |
delimiter, |
|
|
int |
max_tokens, |
|
|
int * |
n_tokens |
|
) |
| |
Split a string based on delimiters.
- Parameters
-
| str | a string to split |
| delimiter | delimiter characters to split on |
| max_tokens | the max number of tokens to split |
[out] | n_tokens | the number of tokens |
- Returns
- a NULL terminated array of strings that should be freed with pw_free_strv.
◆ pw_split_ip()
int pw_split_ip |
( |
char * |
str, |
|
|
const char * |
delimiter, |
|
|
int |
max_tokens, |
|
|
char * |
tokens[] |
|
) |
| |
Split a string in-place based on delimiters.
- Parameters
-
| str | a string to split |
| delimiter | delimiter characters to split on |
| max_tokens | the max number of tokens to split |
[out] | tokens | an array to hold up to max_tokens of strings |
- Returns
- the number of tokens in tokens
str will be modified in-place so that tokens will contain zero terminated strings split at delimiter characters.
◆ pw_strv_parse()
char ** pw_strv_parse |
( |
const char * |
val, |
|
|
size_t |
len, |
|
|
int |
max_tokens, |
|
|
int * |
n_tokens |
|
) |
| |
Parse an array of strings.
- Parameters
-
| val | a string to parse |
| len | the length of val |
| max_tokens | the max number of tokens to split |
[out] | n_tokens | the number of tokens, may be NULL |
- Returns
- a NULL terminated array of strings that should be freed with pw_free_strv.
val is parsed using relaxed json syntax.
- Since
- 0.3.84
◆ pw_strv_find()
int pw_strv_find |
( |
char ** |
a, |
|
|
const char * |
b |
|
) |
| |
Find a string in a NULL terminated array of strings.
- Parameters
-
a | a strv to check |
b | the string to find |
- Returns
- the index in a where b is found or < 0 if not.
- Since
- 0.3.84
◆ pw_strv_find_common()
int pw_strv_find_common |
( |
char ** |
a, |
|
|
char ** |
b |
|
) |
| |
Check if two NULL terminated arrays of strings have a common string.
- Parameters
-
a | a strv to check |
b | another strv to check |
- Returns
- the index in a of the first common string or < 0 if not.
- Since
- 0.3.84
◆ pw_free_strv()
void pw_free_strv |
( |
char ** |
str | ) |
|
Free a NULL terminated array of strings.
- Parameters
-
str | a NULL terminated array of string |
Free all the strings in the array and the array
◆ pw_strip()
char * pw_strip |
( |
char * |
str, |
|
|
const char * |
whitespace |
|
) |
| |
Strip all whitespace before and after a string.
- Parameters
-
str | a string to strip |
whitespace | characters to strip |
- Returns
- the stripped part of str
Strip whitespace before and after str. str will be modified.
◆ pw_getrandom()
ssize_t pw_getrandom |
( |
void * |
buf, |
|
|
size_t |
buflen, |
|
|
unsigned int |
flags |
|
) |
| |
Fill a buffer with random data.
- Parameters
-
buf | a buffer to fill |
buflen | the number of bytes to fill |
flags | optional flags |
- Returns
- the number of bytes filled
Fill buf with buflen random bytes.
◆ pw_random()
void pw_random |
( |
void * |
buf, |
|
|
size_t |
buflen |
|
) |
| |
Fill a buffer with random data.
- Parameters
-
buf | a buffer to fill |
buflen | the number of bytes to fill |
Fill buf with buflen random bytes. This functions uses pw_getrandom() but falls back to a pseudo random number generator in case of failure.
◆ pw_reallocarray()
void * pw_reallocarray |
( |
void * |
ptr, |
|
|
size_t |
nmemb, |
|
|
size_t |
size |
|
) |
| |