Code

utils: pass preallocated buffer to format_duration_long()
[ncmpc.git] / src / utils.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2009 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.
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.
18 */
20 #include "utils.h"
21 #include "options.h"
22 #include "charset.h"
23 #include "i18n.h"
24 #include "mpdclient.h"
25 #include "filelist.h"
27 #include <ctype.h>
28 #include <stdlib.h>
29 #include <string.h>
31 GList *
32 string_list_free(GList *string_list)
33 {
34         GList *list = g_list_first(string_list);
36         while (list) {
37                 g_free(list->data);
38                 list->data = NULL;
39                 list = list->next;
40         }
42         g_list_free(string_list);
43         return NULL;
44 }
46 GList *
47 string_list_find(GList *string_list, const gchar *str)
48 {
49         GList *list = g_list_first(string_list);
51         while(list) {
52                 if( strcmp(str, (gchar *) list->data) ==  0 )
53                         return list;
54                 list = list->next;
55         }
56         return NULL;
57 }
59 GList *
60 string_list_remove(GList *string_list, const gchar *str)
61 {
62         GList *list = g_list_first(string_list);
64         while(list) {
65                 if( strcmp(str, (gchar *) list->data) ==  0 ) {
66                         g_free(list->data);
67                         list->data = NULL;
68                         return g_list_delete_link(string_list, list);
69                 }
70                 list = list->next;
71         }
72         return list;
73 }
75 /* create a list suitable for GCompletion from path */
76 GList *
77 gcmp_list_from_path(struct mpdclient *c, const gchar *path,
78                     GList *list, gint types)
79 {
80         guint i;
81         struct filelist *filelist;
83         if ((filelist = mpdclient_filelist_get(c, path)) == NULL)
84                 return list;
86         for (i = 0; i < filelist_length(filelist); ++i) {
87                 const struct filelist_entry *entry = filelist_get(filelist, i);
88                 const struct mpd_entity *entity = entry ? entry->entity : NULL;
89                 char *name = NULL;
91                 if (entity != NULL &&
92                     mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_DIRECTORY &&
93                     types & GCMP_TYPE_DIR) {
94                         const struct mpd_directory *dir =
95                                 mpd_entity_get_directory(entity);
96                         gchar *tmp = utf8_to_locale(mpd_directory_get_path(dir));
97                         gsize size = strlen(tmp)+2;
99                         name = g_malloc(size);
100                         g_strlcpy(name, tmp, size);
101                         g_strlcat(name, "/", size);
102                         g_free(tmp);
103                 } else if (entity != NULL &&
104                            mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG &&
105                            types & GCMP_TYPE_FILE) {
106                         const struct mpd_song *song =
107                                 mpd_entity_get_song(entity);
108                         name = utf8_to_locale(mpd_song_get_uri(song));
109                 } else if (entity != NULL &&
110                            mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_PLAYLIST &&
111                            types & GCMP_TYPE_PLAYLIST) {
112                         const struct mpd_playlist *playlist =
113                                 mpd_entity_get_playlist(entity);
114                         name = utf8_to_locale(mpd_playlist_get_path(playlist));
115                 }
117                 if (name)
118                         list = g_list_append(list, name);
119         }
121         filelist_free(filelist);
122         return list;
125 void
126 format_duration_long(char *p, size_t length, unsigned long duration)
128         const char *year = _("year");
129         const char *years = _("years");
130         const char *week = _("week");
131         const char *weeks = _("weeks");
132         const char *day = _("day");
133         const char *days = _("days");
134         unsigned bytes_written = 0;
136         if (duration / 31536000 > 0) {
137                 if (duration / 31536000 == 1)
138                         bytes_written = g_snprintf(p, length, "%d %s, ", 1, year);
139                 else
140                         bytes_written = g_snprintf(p, length, "%lu %s, ", duration / 31536000, years);
141                 duration %= 31536000;
142                 length -= bytes_written;
143                 p += bytes_written;
144         }
145         if (duration / 604800 > 0) {
146                 if (duration / 604800 == 1)
147                         bytes_written = g_snprintf(p, length, "%d %s, ", 1, week);
148                 else
149                         bytes_written = g_snprintf(p, length, "%lu %s, ", duration / 604800, weeks);
150                 duration %= 604800;
151                 length -= bytes_written;
152                 p += bytes_written;
153         }
154         if (duration / 86400 > 0) {
155                 if (duration / 86400 == 1)
156                         bytes_written = g_snprintf(p, length, "%d %s, ", 1, day);
157                 else
158                         bytes_written = g_snprintf(p, length, "%lu %s, ", duration / 86400, days);
159                 duration %= 86400;
160                 length -= bytes_written;
161                 p += bytes_written;
162         }
164         g_snprintf(p, length, "%02lu:%02lu:%02lu", duration / 3600,
165                    duration % 3600 / 60, duration % 3600 % 60);