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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 2576 Revision 2577
14#include "rc.h" 14#include "rc.h"
15#include "rc-misc.h" 15#include "rc-misc.h"
16 16
17char **rc_strlist_add (char **list, const char *item) 17char **rc_strlist_add (char **list, const char *item)
18{ 18{
19 char **newlist; 19 char **newlist;
20 int i = 0; 20 int i = 0;
21 21
22 if (! item) 22 if (! item)
23 return (list); 23 return (list);
24 24
25 while (list && list[i]) 25 while (list && list[i])
26 i++; 26 i++;
27 27
28 newlist = rc_xrealloc (list, sizeof (char *) * (i + 2)); 28 newlist = rc_xrealloc (list, sizeof (char *) * (i + 2));
29 newlist[i] = rc_xstrdup (item); 29 newlist[i] = rc_xstrdup (item);
30 newlist[i + 1] = NULL; 30 newlist[i + 1] = NULL;
31 31
32 return (newlist); 32 return (newlist);
33} 33}
34 34
35static char **_rc_strlist_addsort (char **list, const char *item, 35static char **_rc_strlist_addsort (char **list, const char *item,
36 int (*sortfunc) (const char *s1, 36 int (*sortfunc) (const char *s1,
37 const char *s2), 37 const char *s2),
38 bool uniq) 38 bool uniq)
39{ 39{
40 char **newlist; 40 char **newlist;
41 int i = 0; 41 int i = 0;
42 char *tmp1; 42 char *tmp1;
43 char *tmp2; 43 char *tmp2;
44 44
45 if (! item) 45 if (! item)
46 return (list); 46 return (list);
47 47
48 while (list && list[i]) 48 while (list && list[i]) {
49 {
50 if (uniq && strcmp (list[i], item) == 0) 49 if (uniq && strcmp (list[i], item) == 0)
51 return (list); 50 return (list);
52 i++; 51 i++;
53 } 52 }
54 53
55 newlist = rc_xrealloc (list, sizeof (char *) * (i + 2)); 54 newlist = rc_xrealloc (list, sizeof (char *) * (i + 2));
56 55
57 if (! i) 56 if (! i)
58 newlist[i] = NULL; 57 newlist[i] = NULL;
59 newlist[i + 1] = NULL; 58 newlist[i + 1] = NULL;
60 59
61 i = 0; 60 i = 0;
62 while (newlist[i] && sortfunc (newlist[i], item) < 0) 61 while (newlist[i] && sortfunc (newlist[i], item) < 0)
63 i++; 62 i++;
64 63
65 tmp1 = newlist[i]; 64 tmp1 = newlist[i];
66 newlist[i] = rc_xstrdup (item); 65 newlist[i] = rc_xstrdup (item);
67 do 66 do {
68 { 67 i++;
69 i++;
70 tmp2 = newlist[i]; 68 tmp2 = newlist[i];
71 newlist[i] = tmp1; 69 newlist[i] = tmp1;
72 tmp1 = tmp2; 70 tmp1 = tmp2;
73 } while (tmp1); 71 } while (tmp1);
74 72
75 return (newlist); 73 return (newlist);
76} 74}
77 75
78char **rc_strlist_addsort (char **list, const char *item) 76char **rc_strlist_addsort (char **list, const char *item)
79{ 77{
80 return (_rc_strlist_addsort (list, item, strcoll, false)); 78 return (_rc_strlist_addsort (list, item, strcoll, false));
81} 79}
82 80
83char **rc_strlist_addsortc (char **list, const char *item) 81char **rc_strlist_addsortc (char **list, const char *item)
84{ 82{
85 return (_rc_strlist_addsort (list, item, strcmp, false)); 83 return (_rc_strlist_addsort (list, item, strcmp, false));
86} 84}
87 85
88char **rc_strlist_addsortu (char **list, const char *item) 86char **rc_strlist_addsortu (char **list, const char *item)
89{ 87{
90 return (_rc_strlist_addsort (list, item, strcmp, true)); 88 return (_rc_strlist_addsort (list, item, strcmp, true));
91} 89}
92 90
93char **rc_strlist_delete (char **list, const char *item) 91char **rc_strlist_delete (char **list, const char *item)
94{ 92{
95 int i = 0; 93 int i = 0;
96 94
97 if (!list || ! item) 95 if (!list || ! item)
98 return (list); 96 return (list);
99 97
100 while (list[i]) 98 while (list[i])
101 if (! strcmp (list[i], item)) 99 if (! strcmp (list[i], item)) {
102 { 100 free (list[i]);
103 free (list[i]); 101 do {
104 do 102 list[i] = list[i + 1];
105 { 103 i++;
106 list[i] = list[i + 1]; 104 } while (list[i]);
107 i++; 105 }
108 } while (list[i]);
109 }
110 106
111 return (list); 107 return (list);
112} 108}
113 109
114void rc_strlist_reverse (char **list) 110void rc_strlist_reverse (char **list)
115{ 111{
116 char *item; 112 char *item;
117 int i = 0; 113 int i = 0;
118 int j = 0; 114 int j = 0;
119 115
120 if (! list) 116 if (! list)
121 return; 117 return;
122 118
123 while (list[j]) 119 while (list[j])
124 j++; 120 j++;
125 j--; 121 j--;
126 122
127 while (i < j && list[i] && list[j]) 123 while (i < j && list[i] && list[j]) {
128 {
129 item = list[i]; 124 item = list[i];
130 list[i] = list[j]; 125 list[i] = list[j];
131 list[j] = item; 126 list[j] = item;
132 i++; 127 i++;
133 j--; 128 j--;
134 } 129 }
135} 130}
136 131
137void rc_strlist_free (char **list) 132void rc_strlist_free (char **list)
138{ 133{
139 int i = 0; 134 int i = 0;
140 135
141 if (! list) 136 if (! list)
142 return; 137 return;
143 138
144 while (list[i]) 139 while (list[i]) {
145 {
146 free (list[i]); 140 free (list[i]);
147 list[i++] = NULL; 141 list[i++] = NULL;
148 } 142 }
149 143
150 free (list); 144 free (list);
151} 145}

Legend:
Removed from v.2576  
changed lines
  Added in v.2577

  ViewVC Help
Powered by ViewVC 1.1.20