util: add api for iterating through components of a path string
This commit is contained in:
parent
451a074fd5
commit
82919e3d31
16
util.c
16
util.c
|
@ -284,6 +284,22 @@ char *split_spaces(const char *c, size_t *l, char **state) {
|
||||||
return (char*) current;
|
return (char*) current;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Split a path into filenames. */
|
||||||
|
char *split_slash(const char *c, size_t *l, char **state) {
|
||||||
|
char *current;
|
||||||
|
|
||||||
|
current = *state ? *state : (char*) c;
|
||||||
|
|
||||||
|
if (!*current || *c == 0)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
current += strspn(current, "/");
|
||||||
|
*l = strcspn(current, "/");
|
||||||
|
*state = current+*l;
|
||||||
|
|
||||||
|
return (char*) current;
|
||||||
|
}
|
||||||
|
|
||||||
/* Split a string into words, but consider strings enclosed in '' and
|
/* Split a string into words, but consider strings enclosed in '' and
|
||||||
* "" as words even if they include spaces. */
|
* "" as words even if they include spaces. */
|
||||||
char *split_quoted(const char *c, size_t *l, char **state) {
|
char *split_quoted(const char *c, size_t *l, char **state) {
|
||||||
|
|
4
util.h
4
util.h
|
@ -97,6 +97,7 @@ int safe_atolli(const char *s, long long int *ret_i);
|
||||||
|
|
||||||
char *split_spaces(const char *c, size_t *l, char **state);
|
char *split_spaces(const char *c, size_t *l, char **state);
|
||||||
char *split_quoted(const char *c, size_t *l, char **state);
|
char *split_quoted(const char *c, size_t *l, char **state);
|
||||||
|
char *split_slash(const char *c, size_t *l, char **state);
|
||||||
|
|
||||||
#define FOREACH_WORD(word, length, s, state) \
|
#define FOREACH_WORD(word, length, s, state) \
|
||||||
for ((state) = NULL, (word) = split_spaces((s), &(l), &(state)); (word); (word) = split_spaces((s), &(l), &(state)))
|
for ((state) = NULL, (word) = split_spaces((s), &(l), &(state)); (word); (word) = split_spaces((s), &(l), &(state)))
|
||||||
|
@ -104,6 +105,9 @@ char *split_quoted(const char *c, size_t *l, char **state);
|
||||||
#define FOREACH_WORD_QUOTED(word, length, s, state) \
|
#define FOREACH_WORD_QUOTED(word, length, s, state) \
|
||||||
for ((state) = NULL, (word) = split_quoted((s), &(l), &(state)); (word); (word) = split_quoted((s), &(l), &(state)))
|
for ((state) = NULL, (word) = split_quoted((s), &(l), &(state)); (word); (word) = split_quoted((s), &(l), &(state)))
|
||||||
|
|
||||||
|
#define FOREACH_WORD_SLASH(word, length, s, state) \
|
||||||
|
for ((state) = NULL, (word) = split_slash((s), &(l), &(state)); (word); (word) = split_slash((s), &(l), &(state)))
|
||||||
|
|
||||||
pid_t get_parent_of_pid(pid_t pid, pid_t *ppid);
|
pid_t get_parent_of_pid(pid_t pid, pid_t *ppid);
|
||||||
|
|
||||||
int write_one_line_file(const char *fn, const char *line);
|
int write_one_line_file(const char *fn, const char *line);
|
||||||
|
|
Loading…
Reference in a new issue