2008-03-11 20:38:22 +00:00
|
|
|
#include <common.h>
|
|
|
|
#include <xfuncs.h>
|
|
|
|
#include <malloc.h>
|
2012-04-19 02:36:28 +00:00
|
|
|
#include <errno.h>
|
2008-03-11 20:38:22 +00:00
|
|
|
#include <stringlist.h>
|
|
|
|
|
2011-12-21 08:07:55 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-12-03 14:33:59 +00:00
|
|
|
int string_list_add(struct string_list *sl, const char *str)
|
2008-03-11 20:38:22 +00:00
|
|
|
{
|
|
|
|
struct string_list *new;
|
|
|
|
|
2012-04-19 02:34:11 +00:00
|
|
|
new = xmalloc(sizeof(*new));
|
|
|
|
new->str = xstrdup(str);
|
2008-03-11 20:38:22 +00:00
|
|
|
|
|
|
|
list_add_tail(&new->list, &sl->list);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-19 02:36:28 +00:00
|
|
|
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);
|
|
|
|
|
2016-04-11 14:52:10 +00:00
|
|
|
new->str = bvasprintf(fmt, args);
|
2012-04-19 02:36:28 +00:00
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
if (!new->str) {
|
|
|
|
free(new);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
list_add_tail(&new->list, &sl->list);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-03 14:33:59 +00:00
|
|
|
int string_list_add_sorted(struct string_list *sl, const char *str)
|
2011-12-21 08:07:55 +00:00
|
|
|
{
|
|
|
|
struct string_list *new;
|
|
|
|
|
2012-04-19 02:34:11 +00:00
|
|
|
new = xmalloc(sizeof(*new));
|
|
|
|
new->str = xstrdup(str);
|
2011-12-21 08:07:55 +00:00
|
|
|
|
|
|
|
list_add_sort(&new->list, &sl->list, string_list_compare);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-19 13:34:28 +00:00
|
|
|
int string_list_contains(struct string_list *sl, const char *str)
|
2011-12-21 08:07:55 +00:00
|
|
|
{
|
|
|
|
struct string_list *entry;
|
|
|
|
|
2014-06-24 10:07:00 +00:00
|
|
|
string_list_for_each_entry(entry, sl) {
|
2011-12-21 08:07:55 +00:00
|
|
|
if (!strcmp(str, entry->str))
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-03-11 20:38:22 +00:00
|
|
|
void string_list_print_by_column(struct string_list *sl)
|
|
|
|
{
|
|
|
|
int len = 0, num, i;
|
|
|
|
struct string_list *entry;
|
|
|
|
|
2014-06-24 10:07:00 +00:00
|
|
|
string_list_for_each_entry(entry, sl) {
|
2008-03-11 20:38:22 +00:00
|
|
|
int l = strlen(entry->str) + 4;
|
|
|
|
if (l > len)
|
|
|
|
len = l;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!len)
|
|
|
|
return;
|
|
|
|
|
2009-10-13 14:47:35 +00:00
|
|
|
num = 80 / (len + 1);
|
2012-08-28 15:24:26 +00:00
|
|
|
if (num == 0)
|
|
|
|
num = 1;
|
2008-03-11 20:38:22 +00:00
|
|
|
|
|
|
|
i = 0;
|
2014-06-24 10:07:00 +00:00
|
|
|
string_list_for_each_entry(entry, sl) {
|
2008-03-11 20:38:22 +00:00
|
|
|
if (!(++i % num))
|
2009-10-13 14:47:35 +00:00
|
|
|
printf("%s\n", entry->str);
|
|
|
|
else
|
|
|
|
printf("%-*s", len, entry->str);
|
2008-03-11 20:38:22 +00:00
|
|
|
}
|
|
|
|
if (i % num)
|
|
|
|
printf("\n");
|
|
|
|
}
|