Code

utils: move format_duration_*() to time_format.c
[ncmpc.git] / src / strfsong.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 "strfsong.h"
21 #include "charset.h"
22 #include "time_format.h"
24 #include <mpd/client.h>
26 #include <string.h>
28 static const gchar *
29 skip(const gchar * p)
30 {
31         gint stack = 0;
33         while (*p != '\0') {
34                 if (*p == '[')
35                         stack++;
36                 if (*p == '#' && p[1] != '\0') {
37                         /* skip escaped stuff */
38                         ++p;
39                 } else if (stack) {
40                         if(*p == ']') stack--;
41                 } else {
42                         if(*p == '&' || *p == '|' || *p == ']') {
43                                 break;
44                         }
45                 }
46                 ++p;
47         }
49         return p;
50 }
52 #ifndef NCMPC_MINI
54 static char *
55 concat_tag_values(const char *a, const char *b)
56 {
57         return g_strconcat(a, ", ", b, NULL);
58 }
60 static char *
61 song_more_tag_values(const struct mpd_song *song, enum mpd_tag_type tag,
62                      const char *first)
63 {
64         const char *p = mpd_song_get_tag(song, tag, 1);
65         if (p == NULL)
66                 return NULL;
68         char *buffer = concat_tag_values(first, p);
69         for (unsigned i = 2; (p = mpd_song_get_tag(song, tag, i)) != NULL;
70              ++i) {
71                 char *prev = buffer;
72                 buffer = concat_tag_values(buffer, p);
73                 g_free(prev);
74         }
76         return buffer;
77 }
79 #endif /* !NCMPC_MINI */
81 static char *
82 song_tag_locale(const struct mpd_song *song, enum mpd_tag_type tag)
83 {
84         const char *value = mpd_song_get_tag(song, tag, 0);
85         if (value == NULL)
86                 return NULL;
88 #ifndef NCMPC_MINI
89         char *all = song_more_tag_values(song, tag, value);
90         if (all != NULL)
91                 value = all;
92 #endif /* !NCMPC_MINI */
94         char *result = utf8_to_locale(value);
96 #ifndef NCMPC_MINI
97         g_free(all);
98 #endif /* !NCMPC_MINI */
100         return result;
103 static gsize
104 _strfsong(gchar *s,
105           gsize max,
106           const gchar *format,
107           const struct mpd_song *song,
108           const gchar **last)
110         bool found = false;
111         /* "missed" helps handling the case of mere literal text like
112            found==true instead of found==false. */
113         bool missed = false;
115         s[0] = '\0';
117         if (song == NULL)
118                 return 0;
120         const char *p;
121         size_t length = 0;
122         for (p = format; *p != '\0' && length<max;) {
123                 /* OR */
124                 if (p[0] == '|') {
125                         ++p;
126                         if(missed && !found) {
127                                 s[0] = '\0';
128                                 length = 0;
129                                 missed = false;
130                         } else {
131                                 p = skip(p);
132                         }
133                         continue;
134                 }
136                 /* AND */
137                 if (p[0] == '&') {
138                         ++p;
139                         if(missed && !found) {
140                                 p = skip(p);
141                         } else {
142                                 found = false;
143                                 missed = false;
144                         }
145                         continue;
146                 }
148                 /* EXPRESSION START */
149                 if (p[0] == '[') {
150                         char *temp = g_malloc0(max);
151                         if( _strfsong(temp, max, p+1, song, &p) >0 ) {
152                                 g_strlcat(s, temp, max);
153                                 length = strlen(s);
154                                 found = true;
155                         } else {
156                                 missed = true;
157                         }
158                         g_free(temp);
159                         continue;
160                 }
162                 /* EXPRESSION END */
163                 if (p[0] == ']') {
164                         if(last) *last = p+1;
165                         if(missed && !found && length) {
166                                 s[0] = '\0';
167                                 length = 0;
168                         }
169                         return length;
170                 }
172                 /* pass-through non-escaped portions of the format string */
173                 if (p[0] != '#' && p[0] != '%' && length<max) {
174                         s[length++] = *p;
175                         s[length] = '\0';
176                         p++;
177                         continue;
178                 }
180                 /* let the escape character escape itself */
181                 if (p[0] == '#' && p[1] != '\0' && length<max) {
182                         s[length++] = *(p+1);
183                         s[length] = '\0';
184                         p+=2;
185                         continue;
186                 }
188                 /* advance past the esc character */
190                 /* find the extent of this format specifier (stop at \0, ' ', or esc) */
191                 char *temp = NULL;
192                 const char *end = p + 1;
193                 while(*end >= 'a' && *end <= 'z') {
194                         end++;
195                 }
196                 size_t n = end - p + 1;
197                 if(*end != '%')
198                         n--;
199                 else if (strncmp("%file%", p, n) == 0)
200                         temp = utf8_to_locale(mpd_song_get_uri(song));
201                 else if (strncmp("%artist%", p, n) == 0) {
202                         temp = song_tag_locale(song, MPD_TAG_ARTIST);
203                         if (temp == NULL) {
204                                 temp = song_tag_locale(song, MPD_TAG_PERFORMER);
205                                 if (temp == NULL)
206                                         temp = song_tag_locale(song, MPD_TAG_COMPOSER);
207                         }
208                 } else if (strncmp("%albumartist", p, n) == 0)
209                         temp = song_tag_locale(song, MPD_TAG_ALBUM_ARTIST);
210                 else if (strncmp("%composer%", p, n) == 0)
211                         temp = song_tag_locale(song, MPD_TAG_COMPOSER);
212                 else if (strncmp("%performer%", p, n) == 0)
213                         temp = song_tag_locale(song, MPD_TAG_PERFORMER);
214                 else if (strncmp("%title%", p, n) == 0) {
215                         temp = song_tag_locale(song, MPD_TAG_TITLE);
216                         if (temp == NULL)
217                                 temp = song_tag_locale(song, MPD_TAG_NAME);
218                 } else if (strncmp("%album%", p, n) == 0)
219                         temp = song_tag_locale(song, MPD_TAG_ALBUM);
220                 else if (strncmp("%shortalbum%", p, n) == 0) {
221                         temp = song_tag_locale(song, MPD_TAG_ALBUM);
222                         if (temp) {
223                                 gchar *temp2 = g_strndup(temp, 25);
224                                 if (strlen(temp) > 25) {
225                                         temp2[24] = '.';
226                                         temp2[23] = '.';
227                                         temp2[22] = '.';
228                                 }
229                                 g_free(temp);
230                                 temp = temp2;
231                         }
232                 }
233                 else if (strncmp("%track%", p, n) == 0)
234                         temp = song_tag_locale(song, MPD_TAG_TRACK);
235                 else if (strncmp("%disc%", p, n) == 0)
236                         temp = song_tag_locale(song, MPD_TAG_DISC);
237                 else if (strncmp("%name%", p, n) == 0)
238                         temp = song_tag_locale(song, MPD_TAG_NAME);
239                 else if (strncmp("%date%", p, n) == 0)
240                         temp = song_tag_locale(song, MPD_TAG_DATE);
241                 else if (strncmp("%genre%", p, n) == 0)
242                         temp = song_tag_locale(song, MPD_TAG_GENRE);
243                 else if (strncmp("%shortfile%", p, n) == 0) {
244                         const char *uri = mpd_song_get_uri(song);
245                         if (strstr(uri, "://") != NULL)
246                                 temp = utf8_to_locale(uri);
247                         else
248                                 temp = utf8_to_locale(g_basename(uri));
249                 } else if (strncmp("%time%", p, n) == 0) {
250                         unsigned duration = mpd_song_get_duration(song);
252                         if (duration > 0)  {
253                                 char buffer[32];
254                                 format_duration_short(buffer, sizeof(buffer),
255                                                       duration);
256                                 temp = g_strdup(buffer);
257                         }
258                 }
260                 if( temp == NULL) {
261                         gsize templen=n;
262                         /* just pass-through any unknown specifiers (including esc) */
263                         if( length+templen > max )
264                                 templen = max-length;
265                         gchar *ident = g_strndup(p, templen);
266                         g_strlcat(s, ident, max);
267                         length+=templen;
268                         g_free(ident);
270                         missed = true;
271                 } else {
272                         gsize templen = strlen(temp);
274                         found = true;
275                         if( length+templen > max )
276                                 templen = max-length;
277                         g_strlcat(s, temp, max);
278                         length+=templen;
279                         g_free(temp);
280                 }
282                 /* advance past the specifier */
283                 p += n;
284         }
286         if(last) *last = p;
288         return length;
291 gsize
292 strfsong(gchar *s, gsize max, const gchar *format,
293          const struct mpd_song *song)
295         return _strfsong(s, max, format, song, NULL);