/[baselayout]/trunk/src/librc-strlist.c
Gentoo

Contents of /trunk/src/librc-strlist.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2550 - (show annotations) (download) (as text)
Fri Apr 6 01:04:07 2007 UTC (7 years, 5 months ago) by uberlord
File MIME type: text/x-csrc
File size: 2236 byte(s)
Misc fixes, plugged a memory leak in runscript.c and use va_copy to avoid nasty segfaults
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 <stdbool.h>
11 #include <stdlib.h>
12 #include <string.h>
13
14 #include "rc.h"
15 #include "rc-misc.h"
16
17 char **rc_strlist_add (char **list, const char *item)
18 {
19 char **newlist;
20 int i = 0;
21
22 if (! item)
23 return (list);
24
25 while (list && list[i])
26 i++;
27
28 newlist = rc_xrealloc (list, sizeof (char *) * (i + 2));
29 newlist[i] = rc_xstrdup (item);
30 newlist[i + 1] = NULL;
31
32 return (newlist);
33 }
34
35 static char **_rc_strlist_addsort (char **list, const char *item,
36 int (*sortfunc) (const char *s1,
37 const char *s2))
38 {
39 char **newlist;
40 int i = 0;
41 char *tmp1;
42 char *tmp2;
43
44 if (! item)
45 return (list);
46
47 while (list && list[i])
48 i++;
49
50 newlist = rc_xrealloc (list, sizeof (char *) * (i + 2));
51
52 if (! i)
53 newlist[i] = NULL;
54 newlist[i + 1] = NULL;
55
56 i = 0;
57 while (newlist[i] && sortfunc (newlist[i], item) < 0)
58 i++;
59
60 tmp1 = newlist[i];
61 newlist[i] = rc_xstrdup (item);
62 do
63 {
64 i++;
65 tmp2 = newlist[i];
66 newlist[i] = tmp1;
67 tmp1 = tmp2;
68 } while (tmp1);
69
70 return (newlist);
71 }
72
73 char **rc_strlist_addsort (char **list, const char *item)
74 {
75 return (_rc_strlist_addsort (list, item, strcoll));
76 }
77
78 char **rc_strlist_addsortc (char **list, const char *item)
79 {
80 return (_rc_strlist_addsort (list, item, strcmp));
81 }
82
83 char **rc_strlist_delete (char **list, const char *item)
84 {
85 int i = 0;
86
87 if (!list || ! item)
88 return (list);
89
90 while (list[i])
91 if (! strcmp (list[i], item))
92 {
93 free (list[i]);
94 do
95 {
96 list[i] = list[i + 1];
97 i++;
98 } while (list[i]);
99 }
100
101 return (list);
102 }
103
104 void rc_strlist_reverse (char **list)
105 {
106 char *item;
107 int i = 0;
108 int j = 0;
109
110 if (! list)
111 return;
112
113 while (list[j])
114 j++;
115 j--;
116
117 while (i < j && list[i] && list[j])
118 {
119 item = list[i];
120 list[i] = list[j];
121 list[j] = item;
122 i++;
123 j--;
124 }
125 }
126
127 void rc_strlist_free (char **list)
128 {
129 int i = 0;
130
131 if (! list)
132 return;
133
134 while (list[i])
135 {
136 free (list[i]);
137 list[i++] = NULL;
138 }
139
140 free (list);
141 }

  ViewVC Help
Powered by ViewVC 1.1.20