utils: expose pw_split_ip

This commit is contained in:
Wim Taymans 2022-12-12 16:45:30 +01:00
parent 72f5dbe6a6
commit 87d2719148
3 changed files with 34 additions and 20 deletions

View file

@ -97,6 +97,37 @@ char **pw_split_strv(const char *str, const char *delimiter, int max_tokens, int
return arr.data; return arr.data;
} }
/** Split a string in-place based on delimiters
* \param str a string to split
* \param delimiter delimiter characters to split on
* \param max_tokens the max number of tokens to split
* \param[out] tokens an array to hold up to \a max_tokens of strings
* \return the number of tokens in \a tokens
*
* \a str will be modified in-place so that \a tokens will contain zero terminated
* strings split at \a delimiter characters.
*/
SPA_EXPORT
int pw_split_ip(char *str, const char *delimiter, int max_tokens, char *tokens[])
{
const char *state = NULL;
char *s, *t;
size_t len, l2;
int n = 0;
s = (char *)pw_split_walk(str, delimiter, &len, &state);
while (s && n + 1 < max_tokens) {
t = (char*)pw_split_walk(str, delimiter, &l2, &state);
s[len] = '\0';
tokens[n++] = s;
s = t;
len = l2;
}
if (s)
tokens[n++] = s;
return n;
}
/** Free a NULL terminated array of strings /** Free a NULL terminated array of strings
* \param str a NULL terminated array of string * \param str a NULL terminated array of string
* *

View file

@ -58,6 +58,9 @@ pw_split_walk(const char *str, const char *delimiter, size_t *len, const char **
char ** char **
pw_split_strv(const char *str, const char *delimiter, int max_tokens, int *n_tokens); pw_split_strv(const char *str, const char *delimiter, int max_tokens, int *n_tokens);
int
pw_split_ip(char *str, const char *delimiter, int max_tokens, char *tokens[]);
void void
pw_free_strv(char **str); pw_free_strv(char **str);

View file

@ -142,26 +142,6 @@ static struct global * obj_global(struct remote_data *rd, uint32_t id);
static int children_of(struct remote_data *rd, uint32_t parent_id, static int children_of(struct remote_data *rd, uint32_t parent_id,
const char *child_type, uint32_t **children); const char *child_type, uint32_t **children);
static int pw_split_ip(char *str, const char *delimiter, int max_tokens, char *tokens[])
{
const char *state = NULL;
char *s, *t;
size_t len, l2;
int n = 0;
s = (char *)pw_split_walk(str, delimiter, &len, &state);
while (s && n + 1 < max_tokens) {
t = (char*)pw_split_walk(str, delimiter, &l2, &state);
s[len] = '\0';
tokens[n++] = s;
s = t;
len = l2;
}
if (s)
tokens[n++] = s;
return n;
}
static void print_properties(struct spa_dict *props, char mark, bool header) static void print_properties(struct spa_dict *props, char mark, bool header)
{ {
const struct spa_dict_item *item; const struct spa_dict_item *item;