Code

code style, indent with tabs VI
[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 "ncmpc.h"
23 #include "options.h"
24 #include "support.h"
26 #include <ctype.h>
27 #include <stdlib.h>
28 #include <string.h>
30 GList *
31 string_list_free(GList *string_list)
32 {
33         GList *list = g_list_first(string_list);
35         while (list) {
36                 g_free(list->data);
37                 list->data = NULL;
38                 list = list->next;
39         }
41         g_list_free(string_list);
42         return NULL;
43 }
45 GList *
46 string_list_find(GList *string_list, const gchar *str)
47 {
48         GList *list = g_list_first(string_list);
50         while(list) {
51                 if( strcmp(str, (gchar *) list->data) ==  0 )
52                         return list;
53                 list = list->next;
54         }
55         return NULL;
56 }
58 GList *
59 string_list_remove(GList *string_list, const gchar *str)
60 {
61         GList *list = g_list_first(string_list);
63         while(list) {
64                 if( strcmp(str, (gchar *) list->data) ==  0 ) {
65                         g_free(list->data);
66                         list->data = NULL;
67                         return g_list_delete_link(string_list, list);
68                 }
69                 list = list->next;
70         }
71         return list;
72 }
74 /* create a list suiteble for GCompletion from path */
75 GList *
76 gcmp_list_from_path(mpdclient_t *c, const gchar *path, GList *list, gint types)
77 {
78         GList *flist = NULL;
79         mpdclient_filelist_t *filelist;
81         if ((filelist = mpdclient_filelist_get(c, path)) == NULL)
82                 return list;
84         D("retrieved filelist!\n");
85         flist = filelist->list;
86         while (flist) {
87                 filelist_entry_t *entry = flist->data;
88                 mpd_InfoEntity *entity = entry ? entry->entity : NULL;
89                 char *name = NULL;
91                 if (entity && entity->type==MPD_INFO_ENTITY_TYPE_DIRECTORY &&
92                     types & GCMP_TYPE_DIR) {
93                         mpd_Directory *dir = entity->info.directory;
94                         gchar *tmp = utf8_to_locale(dir->path);
95                         gsize size = strlen(tmp)+2;
97                         name = g_malloc(size);
98                         g_strlcpy(name, tmp, size);
99                         g_strlcat(name, "/", size);
100                         g_free(tmp);
101                 } else if (entity &&
102                            entity->type == MPD_INFO_ENTITY_TYPE_SONG &&
103                            types & GCMP_TYPE_FILE) {
104                         mpd_Song *song = entity->info.song;
105                         name = utf8_to_locale(song->file);
106                 } else if (entity &&
107                            entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE &&
108                            types & GCMP_TYPE_PLAYLIST) {
109                         mpd_PlaylistFile *plf = entity->info.playlistFile;
110                         name = utf8_to_locale(plf->path);
111                 }
113                 if (name)
114                         list = g_list_append(list, name);
116                 flist = flist->next;
117         }
119         mpdclient_filelist_free(filelist);
120         return list;