extract-word: modernize extract_many_words
This commit is contained in:
+13
-22
@@ -244,52 +244,43 @@ int extract_first_word_and_warn(
|
||||
* Let's make sure that ExtractFlags fits into an unsigned int. */
|
||||
assert_cc(sizeof(enum ExtractFlags) <= sizeof(unsigned));
|
||||
|
||||
int extract_many_words(const char **p, const char *separators, unsigned flags, ...) {
|
||||
int extract_many_words_internal(const char **p, const char *separators, unsigned flags, ...) {
|
||||
va_list ap;
|
||||
char **l;
|
||||
int n = 0, i, c, r;
|
||||
unsigned n = 0;
|
||||
int r;
|
||||
|
||||
/* Parses a number of words from a string, stripping any
|
||||
* quotes if necessary. */
|
||||
/* Parses a number of words from a string, stripping any quotes if necessary. */
|
||||
|
||||
assert(p);
|
||||
|
||||
/* Count how many words are expected */
|
||||
va_start(ap, flags);
|
||||
for (;;) {
|
||||
if (!va_arg(ap, char **))
|
||||
break;
|
||||
while (va_arg(ap, char**))
|
||||
n++;
|
||||
}
|
||||
va_end(ap);
|
||||
|
||||
if (n <= 0)
|
||||
if (n == 0)
|
||||
return 0;
|
||||
|
||||
/* Read all words into a temporary array */
|
||||
l = newa0(char*, n);
|
||||
for (c = 0; c < n; c++) {
|
||||
char **l = newa0(char*, n);
|
||||
unsigned c;
|
||||
|
||||
for (c = 0; c < n; c++) {
|
||||
r = extract_first_word(p, &l[c], separators, flags);
|
||||
if (r < 0) {
|
||||
free_many_charp(l, c);
|
||||
return r;
|
||||
}
|
||||
|
||||
if (r == 0)
|
||||
break;
|
||||
}
|
||||
|
||||
/* If we managed to parse all words, return them in the passed
|
||||
* in parameters */
|
||||
/* If we managed to parse all words, return them in the passed in parameters */
|
||||
va_start(ap, flags);
|
||||
for (i = 0; i < n; i++) {
|
||||
char **v;
|
||||
|
||||
v = va_arg(ap, char **);
|
||||
assert(v);
|
||||
|
||||
*v = l[i];
|
||||
FOREACH_ARRAY(i, l, n) {
|
||||
char **v = ASSERT_PTR(va_arg(ap, char**));
|
||||
*v = *i;
|
||||
}
|
||||
va_end(ap);
|
||||
|
||||
|
||||
@@ -19,4 +19,7 @@ typedef enum ExtractFlags {
|
||||
|
||||
int extract_first_word(const char **p, char **ret, const char *separators, ExtractFlags flags);
|
||||
int extract_first_word_and_warn(const char **p, char **ret, const char *separators, ExtractFlags flags, const char *unit, const char *filename, unsigned line, const char *rvalue);
|
||||
int extract_many_words(const char **p, const char *separators, unsigned flags, ...) _sentinel_;
|
||||
|
||||
int extract_many_words_internal(const char **p, const char *separators, unsigned flags, ...) _sentinel_;
|
||||
#define extract_many_words(p, separators, flags, ...) \
|
||||
extract_many_words_internal(p, separators, flags, ##__VA_ARGS__, NULL)
|
||||
|
||||
+1
-1
@@ -358,7 +358,7 @@ int strv_split_colon_pairs(char ***t, const char *s) {
|
||||
|
||||
const char *p = tuple;
|
||||
r = extract_many_words(&p, ":", EXTRACT_CUNESCAPE|EXTRACT_UNESCAPE_SEPARATORS,
|
||||
&first, &second, NULL);
|
||||
&first, &second);
|
||||
if (r < 0)
|
||||
return r;
|
||||
if (r == 0)
|
||||
|
||||
@@ -1429,7 +1429,7 @@ static int get_timezones_from_zone1970_tab(char ***ret) {
|
||||
|
||||
/* Line format is:
|
||||
* 'country codes' 'coordinates' 'timezone' 'comments' */
|
||||
r = extract_many_words(&p, NULL, 0, &cc, &co, &tz, NULL);
|
||||
r = extract_many_words(&p, NULL, 0, &cc, &co, &tz);
|
||||
if (r < 0)
|
||||
continue;
|
||||
|
||||
@@ -1474,7 +1474,7 @@ static int get_timezones_from_tzdata_zi(char ***ret) {
|
||||
* Link line format is:
|
||||
* 'Link' 'target' 'alias'
|
||||
* See 'man zic' for more detail. */
|
||||
r = extract_many_words(&p, NULL, 0, &type, &f1, &f2, NULL);
|
||||
r = extract_many_words(&p, NULL, 0, &type, &f1, &f2);
|
||||
if (r < 0)
|
||||
continue;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user