Code

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