Code

strfsong: add a '\0' after copied chars; avoid memset
[ncmpc.git] / src / strfsong.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2010 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 "utils.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         char *buffer, *prev;
67         if (p == NULL)
68                 return NULL;
70         buffer = concat_tag_values(first, p);
71         for (unsigned i = 2; (p = mpd_song_get_tag(song, tag, i)) != NULL;
72              ++i) {
73                 prev = buffer;
74                 buffer = concat_tag_values(buffer, p);
75                 g_free(prev);
76         }
78         return buffer;
79 }
81 #endif /* !NCMPC_MINI */
83 static char *
84 song_tag_locale(const struct mpd_song *song, enum mpd_tag_type tag)
85 {
86         const char *value = mpd_song_get_tag(song, tag, 0);
87         char *result;
88 #ifndef NCMPC_MINI
89         char *all;
90 #endif /* !NCMPC_MINI */
92         if (value == NULL)
93                 return NULL;
95 #ifndef NCMPC_MINI
96         all = song_more_tag_values(song, tag, value);
97         if (all != NULL)
98                 value = all;
99 #endif /* !NCMPC_MINI */
101         result = utf8_to_locale(value);
103 #ifndef NCMPC_MINI
104         g_free(all);
105 #endif /* !NCMPC_MINI */
107         return result;
110 static gsize
111 _strfsong(gchar *s,
112           gsize max,
113           const gchar *format,
114           const struct mpd_song *song,
115           const gchar **last)
117         const gchar *p, *end;
118         gchar *temp;
119         gsize n, length = 0;
120         gboolean found = FALSE;
122         s[0] = '\0';
124         if (song == NULL)
125                 return 0;
127         for (p = format; *p != '\0' && length<max;) {
128                 /* OR */
129                 if (p[0] == '|') {
130                         ++p;
131                         if(!found) {
132                                 s[0] = '\0';
133                                 length = 0;
134                         } else {
135                                 p = skip(p);
136                         }
137                         continue;
138                 }
140                 /* AND */
141                 if (p[0] == '&') {
142                         ++p;
143                         if(!found) {
144                                 p = skip(p);
145                         } else {
146                                 found = FALSE;
147                         }
148                         continue;
149                 }
151                 /* EXPRESSION START */
152                 if (p[0] == '[') {
153                         temp = g_malloc0(max);
154                         if( _strfsong(temp, max, p+1, song, &p) >0 ) {
155                                 g_strlcat(s, temp, max);
156                                 length = strlen(s);
157                                 found = TRUE;
158                         }
159                         g_free(temp);
160                         continue;
161                 }
163                 /* EXPRESSION END */
164                 if (p[0] == ']') {
165                         if(last) *last = p+1;
166                         if(!found && length) {
167                                 s[0] = '\0';
168                                 length = 0;
169                         }
170                         return length;
171                 }
173                 /* pass-through non-escaped portions of the format string */
174                 if (p[0] != '#' && p[0] != '%' && length<max) {
175                         s[length++] = *p;
176                         s[length] = '\0';
177                         p++;
178                         continue;
179                 }
181                 /* let the escape character escape itself */
182                 if (p[0] == '#' && p[1] != '\0' && length<max) {
183                         s[length++] = *(p+1);
184                         s[length] = '\0';
185                         p+=2;
186                         continue;
187                 }
189                 /* advance past the esc character */
191                 /* find the extent of this format specifier (stop at \0, ' ', or esc) */
192                 temp = NULL;
193                 end  = p+1;
194                 while(*end >= 'a' && *end <= 'z') {
195                         end++;
196                 }
197                 n = end - p + 1;
198                 if(*end != '%')
199                         n--;
200                 else if (strncmp("%file%", p, n) == 0)
201                         temp = utf8_to_locale(mpd_song_get_uri(song));
202                 else if (strncmp("%artist%", p, n) == 0)
203                         temp = song_tag_locale(song, MPD_TAG_ARTIST);
204                 else if (strncmp("%title%", p, n) == 0)
205                         temp = song_tag_locale(song, MPD_TAG_TITLE);
206                 else if (strncmp("%album%", p, n) == 0)
207                         temp = song_tag_locale(song, MPD_TAG_ALBUM);
208                 else if (strncmp("%shortalbum%", p, n) == 0) {
209                         temp = song_tag_locale(song, MPD_TAG_ALBUM);
210                         if (temp) {
211                                 gchar *temp2 = g_strndup(temp, 25);
212                                 if (strlen(temp) > 25) {
213                                         temp2[24] = '.';
214                                         temp2[23] = '.';
215                                         temp2[22] = '.';
216                                 }
217                                 g_free(temp);
218                                 temp = temp2;
219                         }
220                 }
221                 else if (strncmp("%track%", p, n) == 0)
222                         temp = song_tag_locale(song, MPD_TAG_TRACK);
223                 else if (strncmp("%name%", p, n) == 0)
224                         temp = song_tag_locale(song, MPD_TAG_NAME);
225                 else if (strncmp("%date%", p, n) == 0)
226                         temp = song_tag_locale(song, MPD_TAG_DATE);
227                 else if (strncmp("%genre%", p, n) == 0)
228                         temp = song_tag_locale(song, MPD_TAG_GENRE);
229                 else if (strncmp("%shortfile%", p, n) == 0) {
230                         const char *uri = mpd_song_get_uri(song);
231                         if (strstr(uri, "://") != NULL)
232                                 temp = utf8_to_locale(uri);
233                         else
234                                 temp = utf8_to_locale(g_basename(uri));
235                 } else if (strncmp("%time%", p, n) == 0) {
236                         unsigned duration = mpd_song_get_duration(song);
238                         if (duration > 0)  {
239                                 char buffer[32];
240                                 format_duration_short(buffer, sizeof(buffer),
241                                                       duration);
242                                 temp = g_strdup(buffer);
243                         }
244                 }
246                 if( temp == NULL) {
247                         gsize templen=n;
248                         /* just pass-through any unknown specifiers (including esc) */
249                         if( length+templen > max )
250                                 templen = max-length;
251                         gchar *ident = g_strndup(p, templen);
252                         g_strlcat(s, ident, max);
253                         length+=templen;
254                         g_free(ident);
255                 } else {
256                         gsize templen = strlen(temp);
258                         found = TRUE;
259                         if( length+templen > max )
260                                 templen = max-length;
261                         g_strlcat(s, temp, max);
262                         length+=templen;
263                         g_free(temp);
264                 }
266                 /* advance past the specifier */
267                 p += n;
268         }
270         if(last) *last = p;
272         return length;
275 gsize
276 strfsong(gchar *s, gsize max, const gchar *format,
277          const struct mpd_song *song)
279         return _strfsong(s, max, format, song, NULL);