summaryrefslogtreecommitdiffstats
path: root/lib/stringlist.c
blob: fc86640b94a8af4acca3c04f44774005a8ec129b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <common.h>
#include <xfuncs.h>
#include <malloc.h>
#include <errno.h>
#include <string.h>
#include <stringlist.h>

static int string_list_compare(struct list_head *a, struct list_head *b)
{
	char *astr, *bstr;
	astr = (char *)list_entry(a, struct string_list, list)->str;
	bstr = (char *)list_entry(b, struct string_list, list)->str;

	return strcmp(astr, bstr);
}

int string_list_add(struct string_list *sl, const char *str)
{
	struct string_list *new;

	new = xmalloc(sizeof(*new));
	new->str = xstrdup(str);

	list_add_tail(&new->list, &sl->list);

	return 0;
}

int string_list_add_asprintf(struct string_list *sl, const char *fmt, ...)
{
	struct string_list *new;
	va_list args;

	new = xmalloc(sizeof(*new));

	va_start(args, fmt);

	new->str = bvasprintf(fmt, args);

	va_end(args);

	if (!new->str) {
		free(new);
		return -ENOMEM;
	}

	list_add_tail(&new->list, &sl->list);

	return 0;
}

int string_list_add_sorted(struct string_list *sl, const char *str)
{
	struct string_list *new;

	new = xmalloc(sizeof(*new));
	new->str = xstrdup(str);

	list_add_sort(&new->list, &sl->list, string_list_compare);

	return 0;
}

int string_list_add_sort_uniq(struct string_list *sl, const char *str)
{
	struct string_list *new, *entry = sl;

	string_list_for_each_entry(entry, sl) {
		int cmp = strcmp(entry->str, str);

		if (cmp < 0)
			continue;
		if (cmp == 0)
			return 0;

		break;
	}

	new = xmalloc(sizeof(*new));
	new->str = xstrdup(str);

	list_add_tail(&new->list, &entry->list);

	return 0;
}

int string_list_contains(struct string_list *sl, const char *str)
{
	struct string_list *entry;

	string_list_for_each_entry(entry, sl) {
		if (!strcmp(str, entry->str))
			return 1;
	}

	return 0;
}

char *string_list_join(const struct string_list *sl, const char *joinstr)
{
	struct string_list *entry;
	size_t len = 0;
	size_t joinstr_len = strlen(joinstr);
	char *str, *next;

	string_list_for_each_entry(entry, sl)
		len += strlen(entry->str) + joinstr_len;

	if (len == 0)
		return strdup("");

	str = malloc(len + 1);
	if (!str)
		return NULL;

	next = str;

	string_list_for_each_entry(entry, sl) {
		next = stpcpy(next, entry->str);
		next = stpcpy(next, joinstr);
	}

	next[-joinstr_len] = '\0';

	return str;
}

void string_list_print_by_column(struct string_list *sl)
{
	int len = 0, num, i;
	struct string_list *entry;

	string_list_for_each_entry(entry, sl) {
		int l = strlen(entry->str) + 4;
		if (l > len)
			len = l;
	}

	if (!len)
		return;

	num = 80 / (len + 1);
	if (num == 0)
		num = 1;

	i = 0;
	string_list_for_each_entry(entry, sl) {
		if (!(++i % num))
			printf("%s\n", entry->str);
		else
			printf("%-*s", len, entry->str);
	}
	if (i % num)
		printf("\n");
}