Code

utils: move gcmp_list_from_path() to db_completion.c
[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 "i18n.h"
23 #include <stdlib.h>
24 #include <string.h>
26 GList *
27 string_list_free(GList *string_list)
28 {
29         g_list_free_full(string_list, g_free);
30         return NULL;
31 }
33 GList *
34 string_list_find(GList *string_list, const gchar *str)
35 {
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;
42         }
43         return NULL;
44 }
46 GList *
47 string_list_remove(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                         g_free(list->data);
54                         list->data = NULL;
55                         return g_list_delete_link(string_list, list);
56                 }
57                 list = list->next;
58         }
59         return list;
60 }
62 void
63 format_duration_short(char *buffer, size_t length, unsigned duration)
64 {
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);
72 }
74 void
75 format_duration_long(char *p, size_t length, unsigned long duration)
76 {
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;
87         }
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;
98         }
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;
109         }
111         g_snprintf(p, length, "%02lu:%02lu:%02lu", duration / 3600,
112                    duration % 3600 / 60, duration % 3600 % 60);