mirror of
https://github.com/AuxXxilium/eudev.git
synced 2025-01-18 10:07:46 +07:00
util: add split_spaces() call
This commit is contained in:
parent
ed5bcfbe3c
commit
a41e8209be
19
util.c
19
util.c
@ -145,3 +145,22 @@ int safe_atoi(const char *s, int *ret_i) {
|
||||
*ret_i = (unsigned) l;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* What is interpreted as whitespace? */
|
||||
#define WHITESPACE " \t\n"
|
||||
|
||||
/* Split a string into words. */
|
||||
char *split_spaces(const char *c, size_t *l, char **state) {
|
||||
char *current;
|
||||
|
||||
current = *state ? *state : (char*) c;
|
||||
|
||||
if (!*current || *c == 0)
|
||||
return NULL;
|
||||
|
||||
current += strspn(current, WHITESPACE);
|
||||
*l = strcspn(current, WHITESPACE);
|
||||
*state = current+*l;
|
||||
|
||||
return (char*) current;
|
||||
}
|
||||
|
5
util.h
5
util.h
@ -59,4 +59,9 @@ int parse_boolean(const char *v);
|
||||
int safe_atou(const char *s, unsigned *ret_u);
|
||||
int safe_atoi(const char *s, int *ret_i);
|
||||
|
||||
char *split_spaces(const char *c, size_t *l, char **state);
|
||||
|
||||
#define FOREACH_WORD(word, length, s, state) \
|
||||
for ((state) = NULL, (word) = split_spaces((s), &(l), &(state)); (word); (word) = split_spaces((s), &(l), &(state)))
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user