Code

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