Code

colors, utils: use g_list_free_full(g_free)
[ncmpc.git] / src / utils.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2017 The Music Player Daemon Project
3  * Project homepage: http://musicpd.org
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
20 #include "utils.h"
21 #include "options.h"
22 #include "charset.h"
23 #include "i18n.h"
24 #include "mpdclient.h"
26 #include <ctype.h>
27 #include <stdlib.h>
28 #include <string.h>
30 GList *
31 string_list_free(GList *string_list)
32 {
33         g_list_free_full(string_list, g_free);
34         return NULL;
35 }
37 GList *
38 string_list_find(GList *string_list, const gchar *str)
39 {
40         GList *list = g_list_first(string_list);
42         while(list) {
43                 if( strcmp(str, (gchar *) list->data) ==  0 )
44                         return list;
45                 list = list->next;
46         }
47         return NULL;
48 }
50 GList *
51 string_list_remove(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                         g_free(list->data);
58                         list->data = NULL;
59                         return g_list_delete_link(string_list, list);
60                 }
61                 list = list->next;
62         }
63         return list;
64 }
66 /* create a list suitable for GCompletion from path */
67 GList *
68 gcmp_list_from_path(struct mpdclient *c, const gchar *path,
69                     GList *list, gint types)
70 {
71         struct mpd_connection *connection = mpdclient_get_connection(c);
72         if (connection == NULL)
73                 return list;
75         mpd_send_list_meta(connection, path);
77         struct mpd_entity *entity;
78         while ((entity = mpd_recv_entity(connection)) != NULL) {
79                 char *name;
81                 if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_DIRECTORY &&
82                     types & GCMP_TYPE_DIR) {
83                         const struct mpd_directory *dir =
84                                 mpd_entity_get_directory(entity);
85                         gchar *tmp = utf8_to_locale(mpd_directory_get_path(dir));
86                         gsize size = strlen(tmp)+2;
88                         name = g_malloc(size);
89                         g_strlcpy(name, tmp, size);
90                         g_strlcat(name, "/", size);
91                         g_free(tmp);
92                 } else if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG &&
93                            types & GCMP_TYPE_FILE) {
94                         const struct mpd_song *song =
95                                 mpd_entity_get_song(entity);
96                         name = utf8_to_locale(mpd_song_get_uri(song));
97                 } else if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_PLAYLIST &&
98                            types & GCMP_TYPE_PLAYLIST) {
99                         const struct mpd_playlist *playlist =
100                                 mpd_entity_get_playlist(entity);
101                         name = utf8_to_locale(mpd_playlist_get_path(playlist));
102                 } else {
103                         mpd_entity_free(entity);
104                         continue;
105                 }
107                 list = g_list_append(list, name);
108                 mpd_entity_free(entity);
109         }
111         return list;
114 void
115 format_duration_short(char *buffer, size_t length, unsigned duration)
117         if (duration < 3600)
118                 g_snprintf(buffer, length,
119                            "%i:%02i", duration / 60, duration % 60);
120         else
121                 g_snprintf(buffer, length,
122                            "%i:%02i:%02i", duration / 3600,
123                            (duration % 3600) / 60, duration % 60);
126 void
127 format_duration_long(char *p, size_t length, unsigned long duration)
129         unsigned bytes_written = 0;
131         if (duration / 31536000 > 0) {
132                 if (duration / 31536000 == 1)
133                         bytes_written = g_snprintf(p, length, "%d %s, ", 1, _("year"));
134                 else
135                         bytes_written = g_snprintf(p, length, "%lu %s, ", duration / 31536000, _("years"));
136                 duration %= 31536000;
137                 length -= bytes_written;
138                 p += bytes_written;
139         }
140         if (duration / 604800 > 0) {
141                 if (duration / 604800 == 1)
142                         bytes_written = g_snprintf(p, length, "%d %s, ",
143                                                    1, _("week"));
144                 else
145                         bytes_written = g_snprintf(p, length, "%lu %s, ",
146                                                    duration / 604800, _("weeks"));
147                 duration %= 604800;
148                 length -= bytes_written;
149                 p += bytes_written;
150         }
151         if (duration / 86400 > 0) {
152                 if (duration / 86400 == 1)
153                         bytes_written = g_snprintf(p, length, "%d %s, ",
154                                                    1, _("day"));
155                 else
156                         bytes_written = g_snprintf(p, length, "%lu %s, ",
157                                                    duration / 86400, _("days"));
158                 duration %= 86400;
159                 length -= bytes_written;
160                 p += bytes_written;
161         }
163         g_snprintf(p, length, "%02lu:%02lu:%02lu", duration / 3600,
164                    duration % 3600 / 60, duration % 3600 % 60);