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