utils: move gcmp_list_from_path() to db_completion.c
[ncmpc.git] / src / utils.c
blob8f867ad36b87130d339b803bdde1ca2f4d366238
1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2017 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 "i18n.h"
23 #include <stdlib.h>
24 #include <string.h>
26 GList *
27 string_list_free(GList *string_list)
29 g_list_free_full(string_list, g_free);
30 return NULL;
33 GList *
34 string_list_find(GList *string_list, const gchar *str)
36 GList *list = g_list_first(string_list);
38 while(list) {
39 if( strcmp(str, (gchar *) list->data) == 0 )
40 return list;
41 list = list->next;
43 return NULL;
46 GList *
47 string_list_remove(GList *string_list, const gchar *str)
49 GList *list = g_list_first(string_list);
51 while(list) {
52 if( strcmp(str, (gchar *) list->data) == 0 ) {
53 g_free(list->data);
54 list->data = NULL;
55 return g_list_delete_link(string_list, list);
57 list = list->next;
59 return list;
62 void
63 format_duration_short(char *buffer, size_t length, unsigned duration)
65 if (duration < 3600)
66 g_snprintf(buffer, length,
67 "%i:%02i", duration / 60, duration % 60);
68 else
69 g_snprintf(buffer, length,
70 "%i:%02i:%02i", duration / 3600,
71 (duration % 3600) / 60, duration % 60);
74 void
75 format_duration_long(char *p, size_t length, unsigned long duration)
77 unsigned bytes_written = 0;
79 if (duration / 31536000 > 0) {
80 if (duration / 31536000 == 1)
81 bytes_written = g_snprintf(p, length, "%d %s, ", 1, _("year"));
82 else
83 bytes_written = g_snprintf(p, length, "%lu %s, ", duration / 31536000, _("years"));
84 duration %= 31536000;
85 length -= bytes_written;
86 p += bytes_written;
88 if (duration / 604800 > 0) {
89 if (duration / 604800 == 1)
90 bytes_written = g_snprintf(p, length, "%d %s, ",
91 1, _("week"));
92 else
93 bytes_written = g_snprintf(p, length, "%lu %s, ",
94 duration / 604800, _("weeks"));
95 duration %= 604800;
96 length -= bytes_written;
97 p += bytes_written;
99 if (duration / 86400 > 0) {
100 if (duration / 86400 == 1)
101 bytes_written = g_snprintf(p, length, "%d %s, ",
102 1, _("day"));
103 else
104 bytes_written = g_snprintf(p, length, "%lu %s, ",
105 duration / 86400, _("days"));
106 duration %= 86400;
107 length -= bytes_written;
108 p += bytes_written;
111 g_snprintf(p, length, "%02lu:%02lu:%02lu", duration / 3600,
112 duration % 3600 / 60, duration % 3600 % 60);