Mercurial > mplayer.hg
annotate gui/util/list.c @ 35493:411875efca3f
Introduce boolean symbolic constants.
Use them wherever suitable and useful to make code easier to read.
author | ib |
---|---|
date | Tue, 04 Dec 2012 18:36:14 +0000 |
parents | 58a221f73d75 |
children | e27855a45128 |
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" | |
35493 | 28 #include "gui/gui.h" |
33741 | 29 #include "string.h" |
30 | |
35376 | 31 #include "mp_msg.h" |
32 #include "path.h" | |
33 | |
34667 | 34 static plItem *plList; |
34664
4df4d842d5fb
Remove global variable pointing to current playlist item.
ib
parents:
34663
diff
changeset
|
35 static plItem *plCurrent; |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
36 |
34668 | 37 static urlItem *urlList; |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
38 |
34683 | 39 /** |
40 * @brief Manage playlists and URL lists. | |
41 * | |
42 * @param cmd task to be performed | |
43 * @param data list item for the task | |
44 * | |
45 * @return pointer to top of list (GET command), | |
46 * pointer to current list item (ITEM command) or | |
47 * NULL (DELETE or unknown command) | |
48 * | |
35487
58a221f73d75
Enhance PLAYLIST_ITEM_GET_POS to provide total number of items.
ib
parents:
35460
diff
changeset
|
49 * @note PLAYLIST_ITEM_GET_POS returns the position number as pointer |
58a221f73d75
Enhance PLAYLIST_ITEM_GET_POS to provide total number of items.
ib
parents:
35460
diff
changeset
|
50 * (if @a data is NULL the last position number, i.e. number of items), |
35458 | 51 * and position 0 means "not found" |
34683 | 52 */ |
34610 | 53 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
|
54 { |
35458 | 55 unsigned int pos; |
34673 | 56 plItem *pdat = (plItem *)data; |
57 urlItem *udat = (urlItem *)data; | |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
58 |
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
59 switch (cmd) { |
34684 | 60 /* playlist */ |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
61 |
34667 | 62 case PLAYLIST_GET: |
63 | |
64 return plList; | |
65 | |
34681 | 66 case PLAYLIST_ITEM_APPEND: |
34674 | 67 |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
68 if (plList) { |
34673 | 69 plItem *item = plList; |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
70 |
34673 | 71 while (item->next) |
72 item = item->next; | |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
73 |
34673 | 74 item->next = pdat; |
75 pdat->prev = item; | |
76 pdat->next = NULL; | |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
77 } else { |
34674 | 78 pdat->next = pdat->prev = NULL; |
34673 | 79 plCurrent = plList = pdat; |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
80 } |
34674 | 81 |
82 return plCurrent; | |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
83 |
34663 | 84 case PLAYLIST_ITEM_INSERT: |
34675 | 85 |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
86 if (plCurrent) { |
34675 | 87 pdat->next = plCurrent->next; |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
88 |
34673 | 89 if (pdat->next) |
90 pdat->next->prev = pdat; | |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
91 |
34675 | 92 pdat->prev = plCurrent; |
93 plCurrent->next = pdat; | |
94 | |
95 plCurrent = pdat; | |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
96 |
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
97 return plCurrent; |
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
98 } else |
34681 | 99 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
|
100 |
35460 | 101 case PLAYLIST_ITEM_FIND: |
102 | |
103 if (plList) { | |
104 plItem *item = plList; | |
105 | |
106 do { | |
107 if (gstrcmp(item->path, pdat->path) == 0 && gstrcmp(item->name, pdat->name) == 0) | |
108 return item; | |
109 | |
110 item = item->next; | |
111 } while (item); | |
112 } | |
113 | |
114 return NULL; | |
115 | |
34682
c3ab7bd64ab3
Cosmetic: Arrange listMgr() commands in switch statement.
ib
parents:
34681
diff
changeset
|
116 case PLAYLIST_ITEM_SET_CURR: |
34676
207272df4aef
Cosmetic: Insert some blank lines and remove commented code.
ib
parents:
34675
diff
changeset
|
117 |
34682
c3ab7bd64ab3
Cosmetic: Arrange listMgr() commands in switch statement.
ib
parents:
34681
diff
changeset
|
118 plCurrent = pdat; |
c3ab7bd64ab3
Cosmetic: Arrange listMgr() commands in switch statement.
ib
parents:
34681
diff
changeset
|
119 return plCurrent; |
34676
207272df4aef
Cosmetic: Insert some blank lines and remove commented code.
ib
parents:
34675
diff
changeset
|
120 |
34682
c3ab7bd64ab3
Cosmetic: Arrange listMgr() commands in switch statement.
ib
parents:
34681
diff
changeset
|
121 case PLAYLIST_ITEM_GET_CURR: |
c3ab7bd64ab3
Cosmetic: Arrange listMgr() commands in switch statement.
ib
parents:
34681
diff
changeset
|
122 |
c3ab7bd64ab3
Cosmetic: Arrange listMgr() commands in switch statement.
ib
parents:
34681
diff
changeset
|
123 return plCurrent; |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
124 |
35458 | 125 case PLAYLIST_ITEM_GET_POS: |
126 | |
127 pos = 0; | |
128 | |
129 if (plList) { | |
130 unsigned int i = 0; | |
131 plItem *item = plList; | |
132 | |
133 do { | |
134 i++; | |
135 | |
136 if (item == pdat) { | |
137 pos = i; | |
138 break; | |
139 } | |
140 | |
141 item = item->next; | |
142 } while (item); | |
35487
58a221f73d75
Enhance PLAYLIST_ITEM_GET_POS to provide total number of items.
ib
parents:
35460
diff
changeset
|
143 |
58a221f73d75
Enhance PLAYLIST_ITEM_GET_POS to provide total number of items.
ib
parents:
35460
diff
changeset
|
144 if (!pdat) |
58a221f73d75
Enhance PLAYLIST_ITEM_GET_POS to provide total number of items.
ib
parents:
35460
diff
changeset
|
145 pos = i; |
35458 | 146 } |
147 | |
148 return (void *)pos; | |
149 | |
34663 | 150 case PLAYLIST_ITEM_GET_PREV: |
34676
207272df4aef
Cosmetic: Insert some blank lines and remove commented code.
ib
parents:
34675
diff
changeset
|
151 |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
152 if (plCurrent && plCurrent->prev) { |
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
153 plCurrent = plCurrent->prev; |
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
154 return plCurrent; |
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
155 } |
34676
207272df4aef
Cosmetic: Insert some blank lines and remove commented code.
ib
parents:
34675
diff
changeset
|
156 |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
157 return NULL; |
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
158 |
34682
c3ab7bd64ab3
Cosmetic: Arrange listMgr() commands in switch statement.
ib
parents:
34681
diff
changeset
|
159 case PLAYLIST_ITEM_GET_NEXT: |
34676
207272df4aef
Cosmetic: Insert some blank lines and remove commented code.
ib
parents:
34675
diff
changeset
|
160 |
34682
c3ab7bd64ab3
Cosmetic: Arrange listMgr() commands in switch statement.
ib
parents:
34681
diff
changeset
|
161 if (plCurrent && plCurrent->next) { |
c3ab7bd64ab3
Cosmetic: Arrange listMgr() commands in switch statement.
ib
parents:
34681
diff
changeset
|
162 plCurrent = plCurrent->next; |
c3ab7bd64ab3
Cosmetic: Arrange listMgr() commands in switch statement.
ib
parents:
34681
diff
changeset
|
163 return plCurrent; |
c3ab7bd64ab3
Cosmetic: Arrange listMgr() commands in switch statement.
ib
parents:
34681
diff
changeset
|
164 } |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
165 |
34682
c3ab7bd64ab3
Cosmetic: Arrange listMgr() commands in switch statement.
ib
parents:
34681
diff
changeset
|
166 return NULL; |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
167 |
34663 | 168 case PLAYLIST_ITEM_DEL_CURR: |
34677 | 169 |
170 if (plCurrent) { | |
34678 | 171 plItem *curr = plCurrent; |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
172 |
34678 | 173 if (curr->prev) |
174 curr->prev->next = curr->next; | |
175 if (curr->next) | |
176 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
|
177 |
34678 | 178 plCurrent = curr->next; |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
179 |
34678 | 180 if (curr == plList) |
181 plList = plCurrent; | |
34677 | 182 |
34678 | 183 free(curr->path); |
184 free(curr->name); | |
185 free(curr); | |
186 } | |
34677 | 187 |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
188 return plCurrent; |
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
189 |
34663 | 190 case PLAYLIST_DELETE: |
34676
207272df4aef
Cosmetic: Insert some blank lines and remove commented code.
ib
parents:
34675
diff
changeset
|
191 |
34602 | 192 while (plList) { |
34673 | 193 plItem *item = plList->next; |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
194 |
34602 | 195 free(plList->path); |
196 free(plList->name); | |
197 free(plList); | |
198 | |
34673 | 199 plList = item; |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
200 } |
34676
207272df4aef
Cosmetic: Insert some blank lines and remove commented code.
ib
parents:
34675
diff
changeset
|
201 |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
202 plCurrent = NULL; |
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
203 return NULL; |
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
204 |
34684 | 205 /* URL list */ |
34668 | 206 |
207 case URLLIST_GET: | |
208 | |
209 return urlList; | |
210 | |
34663 | 211 case URLLIST_ITEM_ADD: |
34679 | 212 |
33748 | 213 if (urlList) { |
34673 | 214 urlItem *item = urlList; |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
215 |
34679 | 216 while (item) { |
217 if (strcmp(udat->url, item->url) == 0) { | |
218 free(udat->url); | |
219 free(udat); | |
220 return NULL; | |
221 } | |
222 | |
223 if (item->next) | |
224 item = item->next; | |
225 else { | |
226 item->next = udat; | |
227 udat->next = NULL; | |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
228 break; |
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
229 } |
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
230 } |
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
231 } else { |
34673 | 232 udat->next = NULL; |
233 urlList = udat; | |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
234 } |
34679 | 235 |
236 return udat; | |
34599 | 237 |
34663 | 238 case URLLIST_DELETE: |
34676
207272df4aef
Cosmetic: Insert some blank lines and remove commented code.
ib
parents:
34675
diff
changeset
|
239 |
34599 | 240 while (urlList) { |
34673 | 241 urlItem *item = urlList->next; |
34601 | 242 |
34599 | 243 free(urlList->url); |
34601 | 244 free(urlList); |
245 | |
34673 | 246 urlList = item; |
34599 | 247 } |
34676
207272df4aef
Cosmetic: Insert some blank lines and remove commented code.
ib
parents:
34675
diff
changeset
|
248 |
34599 | 249 return NULL; |
34680 | 250 |
251 default: | |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
252 |
34680 | 253 return NULL; |
254 } | |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
255 } |
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
256 |
33741 | 257 /** |
34622 | 258 * @brief Set list to @a entry. |
259 * | |
260 * @param list pointer to the char pointer list | |
261 * @param entry the new (and only) element of the list | |
262 * | |
263 * @note Actually, a new list will be created and the old list will be freed. | |
33741 | 264 */ |
34610 | 265 void listSet(char ***list, const char *entry) |
33741 | 266 { |
34622 | 267 if (*list) { |
268 char **l = *list; | |
33741 | 269 |
34622 | 270 while (*l) { |
271 free(*l); | |
272 l++; | |
273 } | |
33741 | 274 |
275 free(*list); | |
276 } | |
277 | |
34623 | 278 *list = malloc(2 * sizeof(char *)); |
34622 | 279 |
280 if (*list) { | |
34623 | 281 (*list)[0] = gstrdup(entry); |
282 (*list)[1] = NULL; | |
34622 | 283 } |
33741 | 284 } |
285 | |
286 /** | |
34627 | 287 * @brief Replace the first element in list that starts with @a search. |
288 * | |
289 * @note If no such element is found, @a replace will be appended. | |
290 * | |
291 * @param list pointer to the char pointer list | |
292 * @param search element to search | |
293 * @param replace replacement element | |
33741 | 294 */ |
34610 | 295 void listRepl(char ***list, const char *search, const char *replace) |
33741 | 296 { |
34627 | 297 int i = 0; |
298 char **org = *list; | |
299 | |
300 if (!replace) | |
301 return; | |
33741 | 302 |
303 if (*list) { | |
34627 | 304 size_t len = (search ? strlen(search) : 0); |
305 | |
33741 | 306 for (i = 0; (*list)[i]; i++) { |
34627 | 307 if (gstrncmp((*list)[i], search, len) == 0) { |
33741 | 308 free((*list)[i]); |
34627 | 309 (*list)[i] = strdup(replace); |
33741 | 310 return; |
311 } | |
312 } | |
313 | |
314 *list = realloc(*list, (i + 2) * sizeof(char *)); | |
315 } else | |
316 *list = malloc(2 * sizeof(char *)); | |
317 | |
34627 | 318 if (!*list) { |
319 *list = org; | |
320 return; | |
321 } | |
322 | |
323 (*list)[i] = strdup(replace); | |
33741 | 324 (*list)[i + 1] = NULL; |
325 } | |
35376 | 326 |
327 /** | |
328 * @brief Append or insert a file to the playlist. | |
329 * | |
330 * @param what file to be added | |
331 * @param how command (#PLAYLIST_ITEM_APPEND or #PLAYLIST_ITEM_INSERT) to be performed | |
332 * | |
35493 | 333 * @return #True (ok) or #False (error) |
35376 | 334 */ |
335 int add_to_gui_playlist(const char *what, int how) | |
336 { | |
35378 | 337 const char *file; |
338 char *path; | |
35376 | 339 plItem *item; |
340 | |
35386 | 341 if (!what || !*what || (how != PLAYLIST_ITEM_APPEND && how != PLAYLIST_ITEM_INSERT)) |
35493 | 342 return False; |
35376 | 343 |
35378 | 344 file = mp_basename(what); |
345 path = strdup(what); | |
35376 | 346 |
35378 | 347 if (file > what) |
348 path[file - what - 1] = 0; | |
35376 | 349 else |
35378 | 350 strcpy(path, "."); |
35376 | 351 |
352 item = calloc(1, sizeof(plItem)); | |
353 | |
354 if (!item) | |
35493 | 355 return False; |
35376 | 356 |
35378 | 357 mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[list] adding %s/%s\n", path, file); |
35376 | 358 |
35378 | 359 item->name = strdup(file); |
360 item->path = path; | |
35376 | 361 |
362 listMgr(how, item); | |
363 | |
35493 | 364 return True; |
35376 | 365 } |