2 #include "string-list.h"
5 * Parse an argument into a string list. arg should either be a
6 * ':'-separated list of strings, or "-" to indicate an empty string
7 * list (as opposed to "", which indicates a string list containing a
8 * single empty string). list->strdup_strings must be set.
10 void parse_string_list(struct string_list
*list
, const char *arg
)
12 if (!strcmp(arg
, "-"))
15 (void)string_list_split(list
, arg
, ':', -1);
18 void write_list(const struct string_list
*list
)
21 for (i
= 0; i
< list
->nr
; i
++)
22 printf("[%d]: \"%s\"\n", i
, list
->items
[i
].string
);
25 void write_list_compact(const struct string_list
*list
)
31 printf("%s", list
->items
[0].string
);
32 for (i
= 1; i
< list
->nr
; i
++)
33 printf(":%s", list
->items
[i
].string
);
38 int prefix_cb(struct string_list_item
*item
, void *cb_data
)
40 const char *prefix
= (const char *)cb_data
;
41 return !prefixcmp(item
->string
, prefix
);
44 int main(int argc
, char **argv
)
46 if (argc
== 5 && !strcmp(argv
[1], "split")) {
47 struct string_list list
= STRING_LIST_INIT_DUP
;
49 const char *s
= argv
[2];
51 int maxsplit
= atoi(argv
[4]);
53 i
= string_list_split(&list
, s
, delim
, maxsplit
);
56 string_list_clear(&list
, 0);
60 if (argc
== 5 && !strcmp(argv
[1], "split_in_place")) {
61 struct string_list list
= STRING_LIST_INIT_NODUP
;
63 char *s
= xstrdup(argv
[2]);
65 int maxsplit
= atoi(argv
[4]);
67 i
= string_list_split_in_place(&list
, s
, delim
, maxsplit
);
70 string_list_clear(&list
, 0);
75 if (argc
== 4 && !strcmp(argv
[1], "filter")) {
77 * Retain only the items that have the specified prefix.
78 * Arguments: list|- prefix
80 struct string_list list
= STRING_LIST_INIT_DUP
;
81 const char *prefix
= argv
[3];
83 parse_string_list(&list
, argv
[2]);
84 filter_string_list(&list
, 0, prefix_cb
, (void *)prefix
);
85 write_list_compact(&list
);
86 string_list_clear(&list
, 0);
90 fprintf(stderr
, "%s: unknown function name: %s\n", argv
[0],
91 argv
[1] ? argv
[1] : "(there was none)");