Code

include cleanup
[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     {
37       g_free(list->data);
38       list->data=NULL;
39       list=list->next;
40     }
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;
80   
81   if( (filelist=mpdclient_filelist_get(c, path)) == NULL )
82     return list;
83   D("retrieved filelist!\n");
84   flist = filelist->list;
85   while( flist )
86     {
87       filelist_entry_t *entry = flist->data;
88       mpd_InfoEntity *entity = entry ? entry->entity : NULL;
89       char *name = NULL;
90       
91       if( entity && entity->type==MPD_INFO_ENTITY_TYPE_DIRECTORY && 
92           types & GCMP_TYPE_DIR) 
93         {
94           mpd_Directory *dir = entity->info.directory;
95           gchar *tmp = utf8_to_locale(dir->path);
96           gsize size = strlen(tmp)+2;
97           
98           name = g_malloc(size);
99           g_strlcpy(name, tmp, size);
100           g_strlcat(name, "/", size);
101           g_free(tmp);
102         }
103       else if( entity && entity->type==MPD_INFO_ENTITY_TYPE_SONG &&
104                types & GCMP_TYPE_FILE )
105         {
106           mpd_Song *song = entity->info.song;
107           name = utf8_to_locale(song->file);
108         }
109       else if( entity && entity->type==MPD_INFO_ENTITY_TYPE_PLAYLISTFILE &&
110                types & GCMP_TYPE_PLAYLIST )
111         {
112           mpd_PlaylistFile *plf = entity->info.playlistFile;
113           name = utf8_to_locale(plf->path);
114         }
116       if( name )
117         list = g_list_append(list, name);
119       flist = flist->next;
120     }
121   mpdclient_filelist_free(filelist);
122   return list;