| 1 |
/*
|
| 2 |
librc-strlist.h
|
| 3 |
String list functions for using char ** arrays
|
| 4 |
|
| 5 |
Copyright 2007 Gentoo Foundation
|
| 6 |
Based on a previous implementation by Martin Schlemmer
|
| 7 |
Released under the GPLv2
|
| 8 |
*/
|
| 9 |
|
| 10 |
#include "librc.h"
|
| 11 |
|
| 12 |
static char **_rc_strlist_add (char **list, const char *item, bool uniq)
|
| 13 |
{
|
| 14 |
char **newlist;
|
| 15 |
int i = 0;
|
| 16 |
|
| 17 |
if (! item)
|
| 18 |
return (list);
|
| 19 |
|
| 20 |
while (list && list[i]) {
|
| 21 |
if (uniq && strcmp (list[i], item) == 0)
|
| 22 |
return (list);
|
| 23 |
i++;
|
| 24 |
}
|
| 25 |
|
| 26 |
newlist = rc_xrealloc (list, sizeof (char *) * (i + 2));
|
| 27 |
newlist[i] = rc_xstrdup (item);
|
| 28 |
newlist[i + 1] = NULL;
|
| 29 |
|
| 30 |
return (newlist);
|
| 31 |
}
|
| 32 |
|
| 33 |
char **rc_strlist_add (char **list, const char *item)
|
| 34 |
{
|
| 35 |
return (_rc_strlist_add (list, item, false));
|
| 36 |
}
|
| 37 |
librc_hidden_def(rc_strlist_add)
|
| 38 |
|
| 39 |
char **rc_strlist_addu (char **list, const char *item)
|
| 40 |
{
|
| 41 |
return (_rc_strlist_add (list, item, true));
|
| 42 |
}
|
| 43 |
librc_hidden_def(rc_strlist_addu)
|
| 44 |
|
| 45 |
static char **_rc_strlist_addsort (char **list, const char *item,
|
| 46 |
int (*sortfunc) (const char *s1,
|
| 47 |
const char *s2),
|
| 48 |
bool uniq)
|
| 49 |
{
|
| 50 |
char **newlist;
|
| 51 |
int i = 0;
|
| 52 |
char *tmp1;
|
| 53 |
char *tmp2;
|
| 54 |
|
| 55 |
if (! item)
|
| 56 |
return (list);
|
| 57 |
|
| 58 |
while (list && list[i]) {
|
| 59 |
if (uniq && strcmp (list[i], item) == 0)
|
| 60 |
return (list);
|
| 61 |
i++;
|
| 62 |
}
|
| 63 |
|
| 64 |
newlist = rc_xrealloc (list, sizeof (char *) * (i + 2));
|
| 65 |
|
| 66 |
if (! i)
|
| 67 |
newlist[i] = NULL;
|
| 68 |
newlist[i + 1] = NULL;
|
| 69 |
|
| 70 |
i = 0;
|
| 71 |
while (newlist[i] && sortfunc (newlist[i], item) < 0)
|
| 72 |
i++;
|
| 73 |
|
| 74 |
tmp1 = newlist[i];
|
| 75 |
newlist[i] = rc_xstrdup (item);
|
| 76 |
do {
|
| 77 |
i++;
|
| 78 |
tmp2 = newlist[i];
|
| 79 |
newlist[i] = tmp1;
|
| 80 |
tmp1 = tmp2;
|
| 81 |
} while (tmp1);
|
| 82 |
|
| 83 |
return (newlist);
|
| 84 |
}
|
| 85 |
|
| 86 |
char **rc_strlist_addsort (char **list, const char *item)
|
| 87 |
{
|
| 88 |
return (_rc_strlist_addsort (list, item, strcoll, false));
|
| 89 |
}
|
| 90 |
librc_hidden_def(rc_strlist_addsort)
|
| 91 |
|
| 92 |
char **rc_strlist_addsortc (char **list, const char *item)
|
| 93 |
{
|
| 94 |
return (_rc_strlist_addsort (list, item, strcmp, false));
|
| 95 |
}
|
| 96 |
librc_hidden_def(rc_strlist_addsortc)
|
| 97 |
|
| 98 |
char **rc_strlist_addsortu (char **list, const char *item)
|
| 99 |
{
|
| 100 |
return (_rc_strlist_addsort (list, item, strcmp, true));
|
| 101 |
}
|
| 102 |
librc_hidden_def(rc_strlist_addsortu)
|
| 103 |
|
| 104 |
char **rc_strlist_delete (char **list, const char *item)
|
| 105 |
{
|
| 106 |
int i = 0;
|
| 107 |
|
| 108 |
if (!list || ! item)
|
| 109 |
return (list);
|
| 110 |
|
| 111 |
while (list[i])
|
| 112 |
if (! strcmp (list[i], item)) {
|
| 113 |
free (list[i]);
|
| 114 |
do {
|
| 115 |
list[i] = list[i + 1];
|
| 116 |
i++;
|
| 117 |
} while (list[i]);
|
| 118 |
}
|
| 119 |
|
| 120 |
return (list);
|
| 121 |
}
|
| 122 |
librc_hidden_def(rc_strlist_delete)
|
| 123 |
|
| 124 |
char **rc_strlist_join (char **this, char **that)
|
| 125 |
{
|
| 126 |
char **newlist;
|
| 127 |
int i = 0;
|
| 128 |
int j = 0;
|
| 129 |
|
| 130 |
if (! this && that)
|
| 131 |
return (that);
|
| 132 |
if (! that && this)
|
| 133 |
return (this);
|
| 134 |
if (! that && ! this)
|
| 135 |
return (NULL);
|
| 136 |
|
| 137 |
while (this[i])
|
| 138 |
i++;
|
| 139 |
|
| 140 |
while (that[j])
|
| 141 |
j++;
|
| 142 |
|
| 143 |
newlist = rc_xrealloc (this, sizeof (char *) * (i + j + 1));
|
| 144 |
|
| 145 |
j = 0;
|
| 146 |
while (that[j]) {
|
| 147 |
newlist[i] = that[j];
|
| 148 |
i++;
|
| 149 |
j++;
|
| 150 |
}
|
| 151 |
newlist[i] = NULL;
|
| 152 |
|
| 153 |
return (newlist);
|
| 154 |
}
|
| 155 |
librc_hidden_def(rc_strlist_join)
|
| 156 |
|
| 157 |
void rc_strlist_reverse (char **list)
|
| 158 |
{
|
| 159 |
char *item;
|
| 160 |
int i = 0;
|
| 161 |
int j = 0;
|
| 162 |
|
| 163 |
if (! list)
|
| 164 |
return;
|
| 165 |
|
| 166 |
while (list[j])
|
| 167 |
j++;
|
| 168 |
j--;
|
| 169 |
|
| 170 |
while (i < j && list[i] && list[j]) {
|
| 171 |
item = list[i];
|
| 172 |
list[i] = list[j];
|
| 173 |
list[j] = item;
|
| 174 |
i++;
|
| 175 |
j--;
|
| 176 |
}
|
| 177 |
}
|
| 178 |
librc_hidden_def(rc_strlist_reverse)
|
| 179 |
|
| 180 |
void rc_strlist_free (char **list)
|
| 181 |
{
|
| 182 |
int i = 0;
|
| 183 |
|
| 184 |
if (! list)
|
| 185 |
return;
|
| 186 |
|
| 187 |
while (list[i]) {
|
| 188 |
free (list[i]);
|
| 189 |
list[i++] = NULL;
|
| 190 |
}
|
| 191 |
|
| 192 |
free (list);
|
| 193 |
}
|
| 194 |
librc_hidden_def(rc_strlist_free)
|