Code

keyboard: implement keyboard_unread() without ungetch()
[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         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 suitable for GCompletion from path */
75 GList *
76 gcmp_list_from_path(struct mpdclient *c, const gchar *path,
77                     GList *list, gint types)
78 {
79         struct mpd_connection *connection = mpdclient_get_connection(c);
80         if (connection == NULL)
81                 return list;
83         mpd_send_list_meta(connection, path);
85         struct mpd_entity *entity;
86         while ((entity = mpd_recv_entity(connection)) != NULL) {
87                 char *name;
89                 if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_DIRECTORY &&
90                     types & GCMP_TYPE_DIR) {
91                         const struct mpd_directory *dir =
92                                 mpd_entity_get_directory(entity);
93                         gchar *tmp = utf8_to_locale(mpd_directory_get_path(dir));
94                         gsize size = strlen(tmp)+2;
96                         name = g_malloc(size);
97                         g_strlcpy(name, tmp, size);
98                         g_strlcat(name, "/", size);
99                         g_free(tmp);
100                 } else if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG &&
101                            types & GCMP_TYPE_FILE) {
102                         const struct mpd_song *song =
103                                 mpd_entity_get_song(entity);
104                         name = utf8_to_locale(mpd_song_get_uri(song));
105                 } else if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_PLAYLIST &&
106                            types & GCMP_TYPE_PLAYLIST) {
107                         const struct mpd_playlist *playlist =
108                                 mpd_entity_get_playlist(entity);
109                         name = utf8_to_locale(mpd_playlist_get_path(playlist));
110                 } else {
111                         mpd_entity_free(entity);
112                         continue;
113                 }
115                 list = g_list_append(list, name);
116                 mpd_entity_free(entity);
117         }
119         return list;
122 void
123 format_duration_short(char *buffer, size_t length, unsigned duration)
125         if (duration < 3600)
126                 g_snprintf(buffer, length,
127                            "%i:%02i", duration / 60, duration % 60);
128         else
129                 g_snprintf(buffer, length,
130                            "%i:%02i:%02i", duration / 3600,
131                            (duration % 3600) / 60, duration % 60);
134 void
135 format_duration_long(char *p, size_t length, unsigned long duration)
137         unsigned bytes_written = 0;
139         if (duration / 31536000 > 0) {
140                 if (duration / 31536000 == 1)
141                         bytes_written = g_snprintf(p, length, "%d %s, ", 1, _("year"));
142                 else
143                         bytes_written = g_snprintf(p, length, "%lu %s, ", duration / 31536000, _("years"));
144                 duration %= 31536000;
145                 length -= bytes_written;
146                 p += bytes_written;
147         }
148         if (duration / 604800 > 0) {
149                 if (duration / 604800 == 1)
150                         bytes_written = g_snprintf(p, length, "%d %s, ",
151                                                    1, _("week"));
152                 else
153                         bytes_written = g_snprintf(p, length, "%lu %s, ",
154                                                    duration / 604800, _("weeks"));
155                 duration %= 604800;
156                 length -= bytes_written;
157                 p += bytes_written;
158         }
159         if (duration / 86400 > 0) {
160                 if (duration / 86400 == 1)
161                         bytes_written = g_snprintf(p, length, "%d %s, ",
162                                                    1, _("day"));
163                 else
164                         bytes_written = g_snprintf(p, length, "%lu %s, ",
165                                                    duration / 86400, _("days"));
166                 duration %= 86400;
167                 length -= bytes_written;
168                 p += bytes_written;
169         }
171         g_snprintf(p, length, "%02lu:%02lu:%02lu", duration / 3600,
172                    duration % 3600 / 60, duration % 3600 % 60);