2007-04-05 16:48:42 +05:30
|
|
|
/*
|
|
|
|
librc-strlist.h
|
|
|
|
String list functions for using char ** arrays
|
|
|
|
|
|
|
|
Copyright 2007 Gentoo Foundation
|
|
|
|
Based on a previous implementation by Martin Schlemmer
|
|
|
|
Released under the GPLv2
|
|
|
|
*/
|
|
|
|
|
2007-04-13 19:38:16 +05:30
|
|
|
#include "librc.h"
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-09-18 17:06:55 +05:30
|
|
|
static char *_rc_strlist_add (char ***list, const char *item, bool uniq)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
char **newlist;
|
2007-09-18 17:06:55 +05:30
|
|
|
char **lst = *list;
|
2007-04-11 18:14:47 +05:30
|
|
|
int i = 0;
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
if (! item)
|
2007-09-18 17:06:55 +05:30
|
|
|
return (NULL);
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-09-18 17:06:55 +05:30
|
|
|
while (lst && lst[i]) {
|
|
|
|
if (uniq && strcmp (lst[i], item) == 0) {
|
|
|
|
errno = EEXIST;
|
|
|
|
return (NULL);
|
|
|
|
}
|
2007-04-11 18:14:47 +05:30
|
|
|
i++;
|
2007-04-27 16:54:05 +05:30
|
|
|
}
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-10-08 16:41:21 +05:30
|
|
|
newlist = xrealloc (lst, sizeof (char *) * (i + 2));
|
2007-10-08 16:46:22 +05:30
|
|
|
newlist[i] = xstrdup (item);
|
2007-04-11 18:14:47 +05:30
|
|
|
newlist[i + 1] = NULL;
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-09-18 17:06:55 +05:30
|
|
|
*list = newlist;
|
|
|
|
return (newlist[i]);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
2007-04-27 16:54:05 +05:30
|
|
|
|
2007-09-18 17:06:55 +05:30
|
|
|
char *rc_strlist_add (char ***list, const char *item)
|
2007-04-27 16:54:05 +05:30
|
|
|
{
|
|
|
|
return (_rc_strlist_add (list, item, false));
|
|
|
|
}
|
2007-04-13 19:38:16 +05:30
|
|
|
librc_hidden_def(rc_strlist_add)
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-09-18 17:06:55 +05:30
|
|
|
char *rc_strlist_addu (char ***list, const char *item)
|
2007-04-27 16:54:05 +05:30
|
|
|
{
|
|
|
|
return (_rc_strlist_add (list, item, true));
|
|
|
|
}
|
|
|
|
librc_hidden_def(rc_strlist_addu)
|
|
|
|
|
2007-09-18 17:06:55 +05:30
|
|
|
static char *_rc_strlist_addsort (char ***list, const char *item,
|
|
|
|
int (*sortfunc) (const char *s1,
|
|
|
|
const char *s2),
|
|
|
|
bool uniq)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
char **newlist;
|
2007-09-18 17:06:55 +05:30
|
|
|
char **lst = *list;
|
2007-04-11 18:14:47 +05:30
|
|
|
int i = 0;
|
|
|
|
char *tmp1;
|
|
|
|
char *tmp2;
|
2007-09-18 17:06:55 +05:30
|
|
|
char *retval;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
if (! item)
|
2007-09-18 17:06:55 +05:30
|
|
|
return (NULL);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-09-18 17:06:55 +05:30
|
|
|
while (lst && lst[i]) {
|
|
|
|
if (uniq && strcmp (lst[i], item) == 0) {
|
|
|
|
errno = EEXIST;
|
|
|
|
return (NULL);
|
|
|
|
}
|
2007-04-11 18:14:47 +05:30
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2007-10-08 16:41:21 +05:30
|
|
|
newlist = xrealloc (lst, sizeof (char *) * (i + 2));
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
if (! i)
|
|
|
|
newlist[i] = NULL;
|
|
|
|
newlist[i + 1] = NULL;
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
while (newlist[i] && sortfunc (newlist[i], item) < 0)
|
|
|
|
i++;
|
|
|
|
|
|
|
|
tmp1 = newlist[i];
|
2007-10-08 16:46:22 +05:30
|
|
|
retval = newlist[i] = xstrdup (item);
|
2007-04-11 18:14:47 +05:30
|
|
|
do {
|
|
|
|
i++;
|
|
|
|
tmp2 = newlist[i];
|
|
|
|
newlist[i] = tmp1;
|
|
|
|
tmp1 = tmp2;
|
|
|
|
} while (tmp1);
|
|
|
|
|
2007-09-18 17:06:55 +05:30
|
|
|
*list = newlist;
|
|
|
|
return (retval);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2007-09-18 17:06:55 +05:30
|
|
|
char *rc_strlist_addsort (char ***list, const char *item)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
return (_rc_strlist_addsort (list, item, strcoll, false));
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
2007-04-13 19:38:16 +05:30
|
|
|
librc_hidden_def(rc_strlist_addsort)
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-09-18 17:06:55 +05:30
|
|
|
char *rc_strlist_addsortc (char ***list, const char *item)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
return (_rc_strlist_addsort (list, item, strcmp, false));
|
2007-04-11 12:45:02 +05:30
|
|
|
}
|
2007-04-13 19:38:16 +05:30
|
|
|
librc_hidden_def(rc_strlist_addsortc)
|
2007-04-11 12:45:02 +05:30
|
|
|
|
2007-09-18 17:06:55 +05:30
|
|
|
char *rc_strlist_addsortu (char ***list, const char *item)
|
2007-04-11 12:45:02 +05:30
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
return (_rc_strlist_addsort (list, item, strcmp, true));
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
2007-04-13 19:38:16 +05:30
|
|
|
librc_hidden_def(rc_strlist_addsortu)
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-09-25 23:02:12 +05:30
|
|
|
bool rc_strlist_delete (char ***list, const char *item)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-09-18 17:06:55 +05:30
|
|
|
char **lst = *list;
|
2007-04-11 18:14:47 +05:30
|
|
|
int i = 0;
|
|
|
|
|
2007-09-18 17:06:55 +05:30
|
|
|
if (!lst || ! item)
|
2007-09-25 23:02:12 +05:30
|
|
|
return (false);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-10-11 15:41:12 +05:30
|
|
|
while (lst[i]) {
|
|
|
|
if (strcmp (lst[i], item) == 0) {
|
2007-09-18 17:06:55 +05:30
|
|
|
free (lst[i]);
|
2007-04-11 18:14:47 +05:30
|
|
|
do {
|
2007-09-18 17:06:55 +05:30
|
|
|
lst[i] = lst[i + 1];
|
2007-04-11 18:14:47 +05:30
|
|
|
i++;
|
2007-09-18 17:06:55 +05:30
|
|
|
} while (lst[i]);
|
2007-10-11 15:41:12 +05:30
|
|
|
return (true);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
2007-10-11 15:41:12 +05:30
|
|
|
i++;
|
|
|
|
}
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-10-11 15:41:12 +05:30
|
|
|
errno = ENOENT;
|
|
|
|
return (false);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
2007-04-13 19:38:16 +05:30
|
|
|
librc_hidden_def(rc_strlist_delete)
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-09-25 21:08:21 +05:30
|
|
|
char *rc_strlist_join (char ***list1, char **list2)
|
2007-04-27 16:54:05 +05:30
|
|
|
{
|
2007-09-18 19:38:56 +05:30
|
|
|
char **lst1 = *list1;
|
2007-04-27 16:54:05 +05:30
|
|
|
char **newlist;
|
|
|
|
int i = 0;
|
|
|
|
int j = 0;
|
|
|
|
|
2007-09-18 21:13:19 +05:30
|
|
|
if (! list2)
|
2007-09-25 21:08:21 +05:30
|
|
|
return (NULL);
|
2007-04-27 16:54:05 +05:30
|
|
|
|
2007-09-18 21:13:19 +05:30
|
|
|
while (lst1 && lst1[i])
|
2007-04-27 16:54:05 +05:30
|
|
|
i++;
|
|
|
|
|
2007-07-13 20:00:33 +05:30
|
|
|
while (list2[j])
|
2007-04-27 16:54:05 +05:30
|
|
|
j++;
|
|
|
|
|
2007-10-08 16:41:21 +05:30
|
|
|
newlist = xrealloc (lst1, sizeof (char *) * (i + j + 1));
|
2007-04-27 16:54:05 +05:30
|
|
|
|
|
|
|
j = 0;
|
2007-07-13 20:00:33 +05:30
|
|
|
while (list2[j]) {
|
|
|
|
newlist[i] = list2[j];
|
2007-09-18 21:13:19 +05:30
|
|
|
/* Take the item off the 2nd list as it's only a shallow copy */
|
|
|
|
list2[j] = NULL;
|
2007-04-27 16:54:05 +05:30
|
|
|
i++;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
newlist[i] = NULL;
|
|
|
|
|
2007-09-18 19:38:56 +05:30
|
|
|
*list1 = newlist;
|
2007-09-25 21:08:21 +05:30
|
|
|
return (newlist[i == 0 ? 0 : i - 1]);
|
2007-04-27 16:54:05 +05:30
|
|
|
}
|
|
|
|
librc_hidden_def(rc_strlist_join)
|
|
|
|
|
2007-04-05 16:48:42 +05:30
|
|
|
void rc_strlist_reverse (char **list)
|
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
char *item;
|
|
|
|
int i = 0;
|
|
|
|
int j = 0;
|
|
|
|
|
|
|
|
if (! list)
|
|
|
|
return;
|
|
|
|
|
|
|
|
while (list[j])
|
|
|
|
j++;
|
|
|
|
j--;
|
|
|
|
|
|
|
|
while (i < j && list[i] && list[j]) {
|
|
|
|
item = list[i];
|
|
|
|
list[i] = list[j];
|
|
|
|
list[j] = item;
|
|
|
|
i++;
|
|
|
|
j--;
|
|
|
|
}
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
2007-04-13 19:38:16 +05:30
|
|
|
librc_hidden_def(rc_strlist_reverse)
|
2007-04-05 16:48:42 +05:30
|
|
|
|
|
|
|
void rc_strlist_free (char **list)
|
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
int i = 0;
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
if (! list)
|
|
|
|
return;
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-09-18 17:06:55 +05:30
|
|
|
while (list[i])
|
|
|
|
free (list[i++]);
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
free (list);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
2007-04-13 19:38:16 +05:30
|
|
|
librc_hidden_def(rc_strlist_free)
|