Mercurial > mplayer.hg
annotate gui/util/list.c @ 35362:848ca0b6d5ca
Prevent floating point exception.
In case of a dynamic label with no text and width zero
there will be a modulo by zero.
author | ib |
---|---|
date | Fri, 23 Nov 2012 12:30:44 +0000 |
parents | b03481253518 |
children | 02006c5b3b30 |
rev | line source |
---|---|
33741 | 1 /* |
2 * This file is part of MPlayer. | |
3 * | |
4 * MPlayer is free software; you can redistribute it and/or modify | |
5 * it under the terms of the GNU General Public License as published by | |
6 * the Free Software Foundation; either version 2 of the License, or | |
7 * (at your option) any later version. | |
8 * | |
9 * MPlayer is distributed in the hope that it will be useful, | |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 * GNU General Public License for more details. | |
13 * | |
14 * You should have received a copy of the GNU General Public License along | |
15 * with MPlayer; if not, write to the Free Software Foundation, Inc., | |
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
17 */ | |
18 | |
34683 | 19 /** |
20 * @file | |
21 * @brief List management | |
22 */ | |
23 | |
33741 | 24 #include <stdlib.h> |
25 #include <string.h> | |
26 | |
27 #include "list.h" | |
28 #include "string.h" | |
29 | |
34667 | 30 static plItem *plList; |
34664
4df4d842d5fb
Remove global variable pointing to current playlist item.
ib
parents:
34663
diff
changeset
|
31 static plItem *plCurrent; |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
32 |
34668 | 33 static urlItem *urlList; |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
34 |
34683 | 35 /** |
36 * @brief Manage playlists and URL lists. | |
37 * | |
38 * @param cmd task to be performed | |
39 * @param data list item for the task | |
40 * | |
41 * @return pointer to top of list (GET command), | |
42 * pointer to current list item (ITEM command) or | |
43 * NULL (DELETE or unknown command) | |
44 * | |
45 */ | |
34610 | 46 void *listMgr(int cmd, void *data) |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
47 { |
34673 | 48 plItem *pdat = (plItem *)data; |
49 urlItem *udat = (urlItem *)data; | |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
50 |
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
51 switch (cmd) { |
34684 | 52 /* playlist */ |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
53 |
34667 | 54 case PLAYLIST_GET: |
55 | |
56 return plList; | |
57 | |
34681 | 58 case PLAYLIST_ITEM_APPEND: |
34674 | 59 |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
60 if (plList) { |
34673 | 61 plItem *item = plList; |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
62 |
34673 | 63 while (item->next) |
64 item = item->next; | |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
65 |
34673 | 66 item->next = pdat; |
67 pdat->prev = item; | |
68 pdat->next = NULL; | |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
69 } else { |
34674 | 70 pdat->next = pdat->prev = NULL; |
34673 | 71 plCurrent = plList = pdat; |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
72 } |
34674 | 73 |
74 return plCurrent; | |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
75 |
34663 | 76 case PLAYLIST_ITEM_INSERT: |
34675 | 77 |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
78 if (plCurrent) { |
34675 | 79 pdat->next = plCurrent->next; |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
80 |
34673 | 81 if (pdat->next) |
82 pdat->next->prev = pdat; | |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
83 |
34675 | 84 pdat->prev = plCurrent; |
85 plCurrent->next = pdat; | |
86 | |
87 plCurrent = pdat; | |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
88 |
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
89 return plCurrent; |
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
90 } else |
34681 | 91 return listMgr(PLAYLIST_ITEM_APPEND, pdat); |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
92 |
34682
c3ab7bd64ab3
Cosmetic: Arrange listMgr() commands in switch statement.
ib
parents:
34681
diff
changeset
|
93 case PLAYLIST_ITEM_SET_CURR: |
34676
207272df4aef
Cosmetic: Insert some blank lines and remove commented code.
ib
parents:
34675
diff
changeset
|
94 |
34682
c3ab7bd64ab3
Cosmetic: Arrange listMgr() commands in switch statement.
ib
parents:
34681
diff
changeset
|
95 plCurrent = pdat; |
c3ab7bd64ab3
Cosmetic: Arrange listMgr() commands in switch statement.
ib
parents:
34681
diff
changeset
|
96 return plCurrent; |
34676
207272df4aef
Cosmetic: Insert some blank lines and remove commented code.
ib
parents:
34675
diff
changeset
|
97 |
34682
c3ab7bd64ab3
Cosmetic: Arrange listMgr() commands in switch statement.
ib
parents:
34681
diff
changeset
|
98 case PLAYLIST_ITEM_GET_CURR: |
c3ab7bd64ab3
Cosmetic: Arrange listMgr() commands in switch statement.
ib
parents:
34681
diff
changeset
|
99 |
c3ab7bd64ab3
Cosmetic: Arrange listMgr() commands in switch statement.
ib
parents:
34681
diff
changeset
|
100 return plCurrent; |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
101 |
34663 | 102 case PLAYLIST_ITEM_GET_PREV: |
34676
207272df4aef
Cosmetic: Insert some blank lines and remove commented code.
ib
parents:
34675
diff
changeset
|
103 |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
104 if (plCurrent && plCurrent->prev) { |
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
105 plCurrent = plCurrent->prev; |
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
106 return plCurrent; |
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
107 } |
34676
207272df4aef
Cosmetic: Insert some blank lines and remove commented code.
ib
parents:
34675
diff
changeset
|
108 |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
109 return NULL; |
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
110 |
34682
c3ab7bd64ab3
Cosmetic: Arrange listMgr() commands in switch statement.
ib
parents:
34681
diff
changeset
|
111 case PLAYLIST_ITEM_GET_NEXT: |
34676
207272df4aef
Cosmetic: Insert some blank lines and remove commented code.
ib
parents:
34675
diff
changeset
|
112 |
34682
c3ab7bd64ab3
Cosmetic: Arrange listMgr() commands in switch statement.
ib
parents:
34681
diff
changeset
|
113 if (plCurrent && plCurrent->next) { |
c3ab7bd64ab3
Cosmetic: Arrange listMgr() commands in switch statement.
ib
parents:
34681
diff
changeset
|
114 plCurrent = plCurrent->next; |
c3ab7bd64ab3
Cosmetic: Arrange listMgr() commands in switch statement.
ib
parents:
34681
diff
changeset
|
115 return plCurrent; |
c3ab7bd64ab3
Cosmetic: Arrange listMgr() commands in switch statement.
ib
parents:
34681
diff
changeset
|
116 } |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
117 |
34682
c3ab7bd64ab3
Cosmetic: Arrange listMgr() commands in switch statement.
ib
parents:
34681
diff
changeset
|
118 return NULL; |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
119 |
34663 | 120 case PLAYLIST_ITEM_DEL_CURR: |
34677 | 121 |
122 if (plCurrent) { | |
34678 | 123 plItem *curr = plCurrent; |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
124 |
34678 | 125 if (curr->prev) |
126 curr->prev->next = curr->next; | |
127 if (curr->next) | |
128 curr->next->prev = curr->prev; | |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
129 |
34678 | 130 plCurrent = curr->next; |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
131 |
34678 | 132 if (curr == plList) |
133 plList = plCurrent; | |
34677 | 134 |
34678 | 135 free(curr->path); |
136 free(curr->name); | |
137 free(curr); | |
138 } | |
34677 | 139 |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
140 return plCurrent; |
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
141 |
34663 | 142 case PLAYLIST_DELETE: |
34676
207272df4aef
Cosmetic: Insert some blank lines and remove commented code.
ib
parents:
34675
diff
changeset
|
143 |
34602 | 144 while (plList) { |
34673 | 145 plItem *item = plList->next; |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
146 |
34602 | 147 free(plList->path); |
148 free(plList->name); | |
149 free(plList); | |
150 | |
34673 | 151 plList = item; |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
152 } |
34676
207272df4aef
Cosmetic: Insert some blank lines and remove commented code.
ib
parents:
34675
diff
changeset
|
153 |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
154 plCurrent = NULL; |
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
155 return NULL; |
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
156 |
34684 | 157 /* URL list */ |
34668 | 158 |
159 case URLLIST_GET: | |
160 | |
161 return urlList; | |
162 | |
34663 | 163 case URLLIST_ITEM_ADD: |
34679 | 164 |
33748 | 165 if (urlList) { |
34673 | 166 urlItem *item = urlList; |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
167 |
34679 | 168 while (item) { |
169 if (strcmp(udat->url, item->url) == 0) { | |
170 free(udat->url); | |
171 free(udat); | |
172 return NULL; | |
173 } | |
174 | |
175 if (item->next) | |
176 item = item->next; | |
177 else { | |
178 item->next = udat; | |
179 udat->next = NULL; | |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
180 break; |
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
181 } |
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
182 } |
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
183 } else { |
34673 | 184 udat->next = NULL; |
185 urlList = udat; | |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
186 } |
34679 | 187 |
188 return udat; | |
34599 | 189 |
34663 | 190 case URLLIST_DELETE: |
34676
207272df4aef
Cosmetic: Insert some blank lines and remove commented code.
ib
parents:
34675
diff
changeset
|
191 |
34599 | 192 while (urlList) { |
34673 | 193 urlItem *item = urlList->next; |
34601 | 194 |
34599 | 195 free(urlList->url); |
34601 | 196 free(urlList); |
197 | |
34673 | 198 urlList = item; |
34599 | 199 } |
34676
207272df4aef
Cosmetic: Insert some blank lines and remove commented code.
ib
parents:
34675
diff
changeset
|
200 |
34599 | 201 return NULL; |
34680 | 202 |
203 default: | |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
204 |
34680 | 205 return NULL; |
206 } | |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
207 } |
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
208 |
33741 | 209 /** |
34622 | 210 * @brief Set list to @a entry. |
211 * | |
212 * @param list pointer to the char pointer list | |
213 * @param entry the new (and only) element of the list | |
214 * | |
215 * @note Actually, a new list will be created and the old list will be freed. | |
33741 | 216 */ |
34610 | 217 void listSet(char ***list, const char *entry) |
33741 | 218 { |
34622 | 219 if (*list) { |
220 char **l = *list; | |
33741 | 221 |
34622 | 222 while (*l) { |
223 free(*l); | |
224 l++; | |
225 } | |
33741 | 226 |
227 free(*list); | |
228 } | |
229 | |
34623 | 230 *list = malloc(2 * sizeof(char *)); |
34622 | 231 |
232 if (*list) { | |
34623 | 233 (*list)[0] = gstrdup(entry); |
234 (*list)[1] = NULL; | |
34622 | 235 } |
33741 | 236 } |
237 | |
238 /** | |
34627 | 239 * @brief Replace the first element in list that starts with @a search. |
240 * | |
241 * @note If no such element is found, @a replace will be appended. | |
242 * | |
243 * @param list pointer to the char pointer list | |
244 * @param search element to search | |
245 * @param replace replacement element | |
33741 | 246 */ |
34610 | 247 void listRepl(char ***list, const char *search, const char *replace) |
33741 | 248 { |
34627 | 249 int i = 0; |
250 char **org = *list; | |
251 | |
252 if (!replace) | |
253 return; | |
33741 | 254 |
255 if (*list) { | |
34627 | 256 size_t len = (search ? strlen(search) : 0); |
257 | |
33741 | 258 for (i = 0; (*list)[i]; i++) { |
34627 | 259 if (gstrncmp((*list)[i], search, len) == 0) { |
33741 | 260 free((*list)[i]); |
34627 | 261 (*list)[i] = strdup(replace); |
33741 | 262 return; |
263 } | |
264 } | |
265 | |
266 *list = realloc(*list, (i + 2) * sizeof(char *)); | |
267 } else | |
268 *list = malloc(2 * sizeof(char *)); | |
269 | |
34627 | 270 if (!*list) { |
271 *list = org; | |
272 return; | |
273 } | |
274 | |
275 (*list)[i] = strdup(replace); | |
33741 | 276 (*list)[i + 1] = NULL; |
277 } |