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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2577 - (show annotations) (download) (as text)
Wed Apr 11 12:44:47 2007 UTC (12 years ago) by uberlord
File MIME type: text/x-csrc
File size: 2329 byte(s)
Cuddle up to LKML C style
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 bool uniq)
39 {
40 char **newlist;
41 int i = 0;
42 char *tmp1;
43 char *tmp2;
44
45 if (! item)
46 return (list);
47
48 while (list && list[i]) {
49 if (uniq && strcmp (list[i], item) == 0)
50 return (list);
51 i++;
52 }
53
54 newlist = rc_xrealloc (list, sizeof (char *) * (i + 2));
55
56 if (! i)
57 newlist[i] = NULL;
58 newlist[i + 1] = NULL;
59
60 i = 0;
61 while (newlist[i] && sortfunc (newlist[i], item) < 0)
62 i++;
63
64 tmp1 = newlist[i];
65 newlist[i] = rc_xstrdup (item);
66 do {
67 i++;
68 tmp2 = newlist[i];
69 newlist[i] = tmp1;
70 tmp1 = tmp2;
71 } while (tmp1);
72
73 return (newlist);
74 }
75
76 char **rc_strlist_addsort (char **list, const char *item)
77 {
78 return (_rc_strlist_addsort (list, item, strcoll, false));
79 }
80
81 char **rc_strlist_addsortc (char **list, const char *item)
82 {
83 return (_rc_strlist_addsort (list, item, strcmp, false));
84 }
85
86 char **rc_strlist_addsortu (char **list, const char *item)
87 {
88 return (_rc_strlist_addsort (list, item, strcmp, true));
89 }
90
91 char **rc_strlist_delete (char **list, const char *item)
92 {
93 int i = 0;
94
95 if (!list || ! item)
96 return (list);
97
98 while (list[i])
99 if (! strcmp (list[i], item)) {
100 free (list[i]);
101 do {
102 list[i] = list[i + 1];
103 i++;
104 } while (list[i]);
105 }
106
107 return (list);
108 }
109
110 void rc_strlist_reverse (char **list)
111 {
112 char *item;
113 int i = 0;
114 int j = 0;
115
116 if (! list)
117 return;
118
119 while (list[j])
120 j++;
121 j--;
122
123 while (i < j && list[i] && list[j]) {
124 item = list[i];
125 list[i] = list[j];
126 list[j] = item;
127 i++;
128 j--;
129 }
130 }
131
132 void rc_strlist_free (char **list)
133 {
134 int i = 0;
135
136 if (! list)
137 return;
138
139 while (list[i]) {
140 free (list[i]);
141 list[i++] = NULL;
142 }
143
144 free (list);
145 }

  ViewVC Help
Powered by ViewVC 1.1.20