2 Copyright (C) 2006 by Jonas Kramer
3 Copyright (C) 2006 by Bart Trojanowski <bart@jukie.net>
5 Published under the terms of the GNU General Public License (GPL).
15 #include <sys/types.h>
20 #include "compatibility.h"
24 /* Counts the elements of a NULL-terminated array of strings. */
25 unsigned count(char ** list
) {
29 while(list
[n
] != NULL
)
35 /* Appends a string to a NULL-terminated array of strings. */
36 char ** append(char ** list
, const char * string
) {
37 unsigned size
= count(list
);
39 list
= realloc(list
, sizeof(char *) * (size
+ 2));
41 list
[size
++] = strdup(string
);
48 Merge two arrays of strings. If the third parameter is zero,
49 the elements of the second array and the array itself are freed.
51 char ** merge(char ** list
, char ** appendix
, int keep
) {
52 unsigned size
= count(list
), i
;
54 for(i
= 0; appendix
&& appendix
[i
] != NULL
; ++i
) {
55 list
= realloc(list
, sizeof(char *) * (size
+ 2));
56 list
[size
++] = strdup(appendix
[i
]);
63 if(appendix
!= NULL
&& !keep
)
69 /* Free a NULL-terminated array of strings. */
70 void purge(char ** list
) {
74 while(list
[i
] != NULL
)
82 Merge strings of an array to one big string. If the second argument is
83 false, the list is purged.
85 char * join(char ** list
, int keep
) {
86 unsigned i
= 0, length
= 0;
90 while(list
[i
] != NULL
) {
91 result
= realloc(result
, sizeof(char) * (length
+ strlen(list
[i
]) + 1));
93 strcpy(result
+ length
, list
[i
]);
94 length
+= strlen(list
[i
]);