Code

fd7a0645b249b61865ee0e3b764e03fd1af5ab64
[ncmpc.git] / src / utils.c
1 /* 
2  * $Id$
3  *
4  * (c) 2004 by Kalle Wallin <kaw@linux.se>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  */
21 #include "utils.h"
22 #include "options.h"
23 #include "charset.h"
25 #include <ctype.h>
26 #include <stdlib.h>
27 #include <string.h>
29 GList *
30 string_list_free(GList *string_list)
31 {
32         GList *list = g_list_first(string_list);
34         while (list) {
35                 g_free(list->data);
36                 list->data = NULL;
37                 list = list->next;
38         }
40         g_list_free(string_list);
41         return NULL;
42 }
44 GList *
45 string_list_find(GList *string_list, const gchar *str)
46 {
47         GList *list = g_list_first(string_list);
49         while(list) {
50                 if( strcmp(str, (gchar *) list->data) ==  0 )
51                         return list;
52                 list = list->next;
53         }
54         return NULL;
55 }
57 GList *
58 string_list_remove(GList *string_list, const gchar *str)
59 {
60         GList *list = g_list_first(string_list);
62         while(list) {
63                 if( strcmp(str, (gchar *) list->data) ==  0 ) {
64                         g_free(list->data);
65                         list->data = NULL;
66                         return g_list_delete_link(string_list, list);
67                 }
68                 list = list->next;
69         }
70         return list;
71 }
73 /* create a list suiteble for GCompletion from path */
74 GList *
75 gcmp_list_from_path(mpdclient_t *c, const gchar *path, GList *list, gint types)
76 {
77         guint i;
78         mpdclient_filelist_t *filelist;
80         if ((filelist = mpdclient_filelist_get(c, path)) == NULL)
81                 return list;
83         for (i = 0; i < filelist_length(filelist); ++i) {
84                 struct filelist_entry *entry = filelist_get(filelist, i);
85                 mpd_InfoEntity *entity = entry ? entry->entity : NULL;
86                 char *name = NULL;
88                 if (entity && entity->type==MPD_INFO_ENTITY_TYPE_DIRECTORY &&
89                     types & GCMP_TYPE_DIR) {
90                         mpd_Directory *dir = entity->info.directory;
91                         gchar *tmp = utf8_to_locale(dir->path);
92                         gsize size = strlen(tmp)+2;
94                         name = g_malloc(size);
95                         g_strlcpy(name, tmp, size);
96                         g_strlcat(name, "/", size);
97                         g_free(tmp);
98                 } else if (entity &&
99                            entity->type == MPD_INFO_ENTITY_TYPE_SONG &&
100                            types & GCMP_TYPE_FILE) {
101                         mpd_Song *song = entity->info.song;
102                         name = utf8_to_locale(song->file);
103                 } else if (entity &&
104                            entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE &&
105                            types & GCMP_TYPE_PLAYLIST) {
106                         mpd_PlaylistFile *plf = entity->info.playlistFile;
107                         name = utf8_to_locale(plf->path);
108                 }
110                 if (name)
111                         list = g_list_append(list, name);
112         }
114         filelist_free(filelist);
115         return list;