Michael Haggerty | ff919f9 | 2012-09-12 16:04:43 +0200 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | #include "string-list.h" |
| 3 | |
Michael Haggerty | eb5f0c7 | 2012-09-12 16:04:44 +0200 | [diff] [blame] | 4 | /* |
| 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. |
| 9 | */ |
Ramsay Jones | 6511987 | 2012-09-15 17:18:42 +0100 | [diff] [blame] | 10 | static void parse_string_list(struct string_list *list, const char *arg) |
Michael Haggerty | eb5f0c7 | 2012-09-12 16:04:44 +0200 | [diff] [blame] | 11 | { |
| 12 | if (!strcmp(arg, "-")) |
| 13 | return; |
| 14 | |
| 15 | (void)string_list_split(list, arg, ':', -1); |
| 16 | } |
| 17 | |
Ramsay Jones | 6511987 | 2012-09-15 17:18:42 +0100 | [diff] [blame] | 18 | static void write_list(const struct string_list *list) |
Michael Haggerty | ff919f9 | 2012-09-12 16:04:43 +0200 | [diff] [blame] | 19 | { |
| 20 | int i; |
| 21 | for (i = 0; i < list->nr; i++) |
| 22 | printf("[%d]: \"%s\"\n", i, list->items[i].string); |
| 23 | } |
| 24 | |
Ramsay Jones | 6511987 | 2012-09-15 17:18:42 +0100 | [diff] [blame] | 25 | static void write_list_compact(const struct string_list *list) |
Michael Haggerty | eb5f0c7 | 2012-09-12 16:04:44 +0200 | [diff] [blame] | 26 | { |
| 27 | int i; |
| 28 | if (!list->nr) |
| 29 | printf("-\n"); |
| 30 | else { |
| 31 | printf("%s", list->items[0].string); |
| 32 | for (i = 1; i < list->nr; i++) |
| 33 | printf(":%s", list->items[i].string); |
| 34 | printf("\n"); |
| 35 | } |
| 36 | } |
| 37 | |
Ramsay Jones | 6511987 | 2012-09-15 17:18:42 +0100 | [diff] [blame] | 38 | static int prefix_cb(struct string_list_item *item, void *cb_data) |
Michael Haggerty | eb5f0c7 | 2012-09-12 16:04:44 +0200 | [diff] [blame] | 39 | { |
| 40 | const char *prefix = (const char *)cb_data; |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 41 | return starts_with(item->string, prefix); |
Michael Haggerty | eb5f0c7 | 2012-09-12 16:04:44 +0200 | [diff] [blame] | 42 | } |
| 43 | |
Michael Haggerty | ff919f9 | 2012-09-12 16:04:43 +0200 | [diff] [blame] | 44 | int main(int argc, char **argv) |
| 45 | { |
| 46 | if (argc == 5 && !strcmp(argv[1], "split")) { |
| 47 | struct string_list list = STRING_LIST_INIT_DUP; |
| 48 | int i; |
| 49 | const char *s = argv[2]; |
| 50 | int delim = *argv[3]; |
| 51 | int maxsplit = atoi(argv[4]); |
| 52 | |
| 53 | i = string_list_split(&list, s, delim, maxsplit); |
| 54 | printf("%d\n", i); |
| 55 | write_list(&list); |
| 56 | string_list_clear(&list, 0); |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | if (argc == 5 && !strcmp(argv[1], "split_in_place")) { |
| 61 | struct string_list list = STRING_LIST_INIT_NODUP; |
| 62 | int i; |
| 63 | char *s = xstrdup(argv[2]); |
| 64 | int delim = *argv[3]; |
| 65 | int maxsplit = atoi(argv[4]); |
| 66 | |
| 67 | i = string_list_split_in_place(&list, s, delim, maxsplit); |
| 68 | printf("%d\n", i); |
| 69 | write_list(&list); |
| 70 | string_list_clear(&list, 0); |
| 71 | free(s); |
| 72 | return 0; |
| 73 | } |
| 74 | |
Michael Haggerty | eb5f0c7 | 2012-09-12 16:04:44 +0200 | [diff] [blame] | 75 | if (argc == 4 && !strcmp(argv[1], "filter")) { |
| 76 | /* |
| 77 | * Retain only the items that have the specified prefix. |
| 78 | * Arguments: list|- prefix |
| 79 | */ |
| 80 | struct string_list list = STRING_LIST_INIT_DUP; |
| 81 | const char *prefix = argv[3]; |
| 82 | |
| 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); |
| 87 | return 0; |
| 88 | } |
| 89 | |
Michael Haggerty | 31d5451 | 2012-09-12 16:04:45 +0200 | [diff] [blame] | 90 | if (argc == 3 && !strcmp(argv[1], "remove_duplicates")) { |
| 91 | struct string_list list = STRING_LIST_INIT_DUP; |
| 92 | |
| 93 | parse_string_list(&list, argv[2]); |
| 94 | string_list_remove_duplicates(&list, 0); |
| 95 | write_list_compact(&list); |
| 96 | string_list_clear(&list, 0); |
| 97 | return 0; |
| 98 | } |
| 99 | |
Michael Haggerty | ff919f9 | 2012-09-12 16:04:43 +0200 | [diff] [blame] | 100 | fprintf(stderr, "%s: unknown function name: %s\n", argv[0], |
| 101 | argv[1] ? argv[1] : "(there was none)"); |
| 102 | return 1; |
| 103 | } |