comparison gui/util/list.c @ 37066:1843c6aaae4d

Add listFree(). Use it in listSet(). (This is the only purpose of use at the moment, but we will need listFree() later which is the reason behind setting the pointer to NULL after freeing.)
author ib
date Thu, 24 Apr 2014 11:56:18 +0000
parents b28b632efeef
children b03e0fd957fd
comparison
equal deleted inserted replaced
37065:b28b632efeef 37066:1843c6aaae4d
24 #include <stdint.h> 24 #include <stdint.h>
25 #include <stdlib.h> 25 #include <stdlib.h>
26 #include <string.h> 26 #include <string.h>
27 27
28 #include "list.h" 28 #include "list.h"
29 #include "mem.h"
29 #include "string.h" 30 #include "string.h"
30 #include "gui/app/gui.h" 31 #include "gui/app/gui.h"
31 32
32 #include "mp_msg.h" 33 #include "mp_msg.h"
33 #include "path.h" 34 #include "path.h"
267 return NULL; 268 return NULL;
268 } 269 }
269 } 270 }
270 271
271 /** 272 /**
272 * @brief Set string list to @a entry. 273 * @brief Free a string list.
273 * 274 *
274 * @param list pointer to the string list 275 * @param list pointer to the string list
275 * @param entry the new (and only) element of the list 276 */
276 * 277 void listFree(char ***list)
277 * @note Actually, a new list will be created and the old list will be freed.
278 */
279 void listSet(char ***list, const char *entry)
280 { 278 {
281 if (*list) { 279 if (*list) {
282 char **l = *list; 280 char **l = *list;
283 281
284 while (*l) { 282 while (*l) {
285 free(*l); 283 free(*l);
286 l++; 284 l++;
287 } 285 }
288 286
289 free(*list); 287 nfree(*list);
290 } 288 }
289 }
290
291 /**
292 * @brief Set string list to @a entry.
293 *
294 * @param list pointer to the string list
295 * @param entry the new (and only) element of the list
296 *
297 * @note Actually, a new list will be created and the old list will be freed.
298 */
299 void listSet(char ***list, const char *entry)
300 {
301 listFree(list);
291 302
292 *list = malloc(2 * sizeof(char *)); 303 *list = malloc(2 * sizeof(char *));
293 304
294 if (*list) { 305 if (*list) {
295 (*list)[0] = gstrdup(entry); 306 (*list)[0] = gstrdup(entry);