options: remove double-semicolon
[ncmpc.git] / src / utils.c
blobb748580a05cb0b81f396c8dc413215699f02bcf3
1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2010 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
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.
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.
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.
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)
33 GList *list = g_list_first(string_list);
35 while (list) {
36 g_free(list->data);
37 list->data = NULL;
38 list = list->next;
41 g_list_free(string_list);
42 return NULL;
45 GList *
46 string_list_find(GList *string_list, const gchar *str)
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;
55 return NULL;
58 GList *
59 string_list_remove(GList *string_list, const gchar *str)
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);
69 list = list->next;
71 return list;
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)
79 struct mpd_connection *connection;
80 struct mpd_entity *entity;
82 connection = mpdclient_get_connection(c);
83 if (connection == NULL)
84 return list;
86 mpd_send_list_meta(connection, path);
88 while ((entity = mpd_recv_entity(connection)) != NULL) {
89 char *name;
91 if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_DIRECTORY &&
92 types & GCMP_TYPE_DIR) {
93 const struct mpd_directory *dir =
94 mpd_entity_get_directory(entity);
95 gchar *tmp = utf8_to_locale(mpd_directory_get_path(dir));
96 gsize size = strlen(tmp)+2;
98 name = g_malloc(size);
99 g_strlcpy(name, tmp, size);
100 g_strlcat(name, "/", size);
101 g_free(tmp);
102 } else if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG &&
103 types & GCMP_TYPE_FILE) {
104 const struct mpd_song *song =
105 mpd_entity_get_song(entity);
106 name = utf8_to_locale(mpd_song_get_uri(song));
107 } else if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_PLAYLIST &&
108 types & GCMP_TYPE_PLAYLIST) {
109 const struct mpd_playlist *playlist =
110 mpd_entity_get_playlist(entity);
111 name = utf8_to_locale(mpd_playlist_get_path(playlist));
112 } else {
113 mpd_entity_free(entity);
114 continue;
117 list = g_list_append(list, name);
118 mpd_entity_free(entity);
121 return list;
124 void
125 format_duration_short(char *buffer, size_t length, unsigned duration)
127 if (duration < 3600)
128 g_snprintf(buffer, length,
129 "%i:%02i", duration / 60, duration % 60);
130 else
131 g_snprintf(buffer, length,
132 "%i:%02i:%02i", duration / 3600,
133 (duration % 3600) / 60, duration % 60);
136 void
137 format_duration_long(char *p, size_t length, unsigned long duration)
139 const char *year = _("year");
140 const char *years = _("years");
141 const char *week = _("week");
142 const char *weeks = _("weeks");
143 const char *day = _("day");
144 const char *days = _("days");
145 unsigned bytes_written = 0;
147 if (duration / 31536000 > 0) {
148 if (duration / 31536000 == 1)
149 bytes_written = g_snprintf(p, length, "%d %s, ", 1, year);
150 else
151 bytes_written = g_snprintf(p, length, "%lu %s, ", duration / 31536000, years);
152 duration %= 31536000;
153 length -= bytes_written;
154 p += bytes_written;
156 if (duration / 604800 > 0) {
157 if (duration / 604800 == 1)
158 bytes_written = g_snprintf(p, length, "%d %s, ", 1, week);
159 else
160 bytes_written = g_snprintf(p, length, "%lu %s, ", duration / 604800, weeks);
161 duration %= 604800;
162 length -= bytes_written;
163 p += bytes_written;
165 if (duration / 86400 > 0) {
166 if (duration / 86400 == 1)
167 bytes_written = g_snprintf(p, length, "%d %s, ", 1, day);
168 else
169 bytes_written = g_snprintf(p, length, "%lu %s, ", duration / 86400, days);
170 duration %= 86400;
171 length -= bytes_written;
172 p += bytes_written;
175 g_snprintf(p, length, "%02lu:%02lu:%02lu", duration / 3600,
176 duration % 3600 / 60, duration % 3600 % 60);