demux: adaptive: handle obsolete http header line folding
[vlc.git] / src / text / strings.c
blobeae4461006b66d7a9b64f5fac2ba57a37898b72f
1 /*****************************************************************************
2 * strings.c: String related functions
3 *****************************************************************************
4 * Copyright (C) 2006 VLC authors and VideoLAN
5 * Copyright (C) 2008-2009 Rémi Denis-Courmont
6 * $Id$
8 * Authors: Antoine Cellerier <dionoea at videolan dot org>
9 * Daniel Stranger <vlc at schmaller dot de>
10 * Rémi Denis-Courmont <rem # videolan org>
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU Lesser General Public License as published by
14 * the Free Software Foundation; either version 2.1 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with this program; if not, write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 /*****************************************************************************
28 * Preamble
29 *****************************************************************************/
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
34 #include <vlc_common.h>
35 #include <assert.h>
37 /* Needed by vlc_strftime */
38 #include <time.h>
39 #include <limits.h>
40 #include <math.h>
41 #include <string.h>
42 #ifndef HAVE_STRCOLL
43 # define strcoll strcasecmp
44 #endif
46 /* Needed by vlc_strfinput */
47 #include <vlc_input.h>
48 #include <vlc_meta.h>
49 #include <vlc_aout.h>
50 #include <vlc_memstream.h>
52 #include <vlc_strings.h>
53 #include <vlc_charset.h>
54 #include <vlc_arrays.h>
55 #include <libvlc.h>
56 #include <errno.h>
58 static const struct xml_entity_s
60 char psz_entity[8];
61 char psz_char[4];
62 } xml_entities[] = {
63 /* Important: this list has to be in alphabetical order (psz_entity-wise) */
64 { "AElig;", "Æ" },
65 { "Aacute;", "Á" },
66 { "Acirc;", "Â" },
67 { "Agrave;", "À" },
68 { "Aring;", "Å" },
69 { "Atilde;", "Ã" },
70 { "Auml;", "Ä" },
71 { "Ccedil;", "Ç" },
72 { "Dagger;", "‡" },
73 { "ETH;", "Ð" },
74 { "Eacute;", "É" },
75 { "Ecirc;", "Ê" },
76 { "Egrave;", "È" },
77 { "Euml;", "Ë" },
78 { "Iacute;", "Í" },
79 { "Icirc;", "Î" },
80 { "Igrave;", "Ì" },
81 { "Iuml;", "Ï" },
82 { "Ntilde;", "Ñ" },
83 { "OElig;", "Œ" },
84 { "Oacute;", "Ó" },
85 { "Ocirc;", "Ô" },
86 { "Ograve;", "Ò" },
87 { "Oslash;", "Ø" },
88 { "Otilde;", "Õ" },
89 { "Ouml;", "Ö" },
90 { "Scaron;", "Š" },
91 { "THORN;", "Þ" },
92 { "Uacute;", "Ú" },
93 { "Ucirc;", "Û" },
94 { "Ugrave;", "Ù" },
95 { "Uuml;", "Ü" },
96 { "Yacute;", "Ý" },
97 { "Yuml;", "Ÿ" },
98 { "aacute;", "á" },
99 { "acirc;", "â" },
100 { "acute;", "´" },
101 { "aelig;", "æ" },
102 { "agrave;", "à" },
103 { "amp;", "&" },
104 { "apos;", "'" },
105 { "aring;", "å" },
106 { "atilde;", "ã" },
107 { "auml;", "ä" },
108 { "bdquo;", "„" },
109 { "brvbar;", "¦" },
110 { "ccedil;", "ç" },
111 { "cedil;", "¸" },
112 { "cent;", "¢" },
113 { "circ;", "ˆ" },
114 { "copy;", "©" },
115 { "curren;", "¤" },
116 { "dagger;", "†" },
117 { "deg;", "°" },
118 { "divide;", "÷" },
119 { "eacute;", "é" },
120 { "ecirc;", "ê" },
121 { "egrave;", "è" },
122 { "eth;", "ð" },
123 { "euml;", "ë" },
124 { "euro;", "€" },
125 { "frac12;", "½" },
126 { "frac14;", "¼" },
127 { "frac34;", "¾" },
128 { "gt;", ">" },
129 { "hellip;", "…" },
130 { "iacute;", "í" },
131 { "icirc;", "î" },
132 { "iexcl;", "¡" },
133 { "igrave;", "ì" },
134 { "iquest;", "¿" },
135 { "iuml;", "ï" },
136 { "laquo;", "«" },
137 { "ldquo;", "“" },
138 { "lsaquo;", "‹" },
139 { "lsquo;", "‘" },
140 { "lt;", "<" },
141 { "macr;", "¯" },
142 { "mdash;", "—" },
143 { "micro;", "µ" },
144 { "middot;", "·" },
145 { "nbsp;", "\xc2\xa0" },
146 { "ndash;", "–" },
147 { "not;", "¬" },
148 { "ntilde;", "ñ" },
149 { "oacute;", "ó" },
150 { "ocirc;", "ô" },
151 { "oelig;", "œ" },
152 { "ograve;", "ò" },
153 { "ordf;", "ª" },
154 { "ordm;", "º" },
155 { "oslash;", "ø" },
156 { "otilde;", "õ" },
157 { "ouml;", "ö" },
158 { "para;", "¶" },
159 { "permil;", "‰" },
160 { "plusmn;", "±" },
161 { "pound;", "£" },
162 { "quot;", "\"" },
163 { "raquo;", "»" },
164 { "rdquo;", "”" },
165 { "reg;", "®" },
166 { "rsaquo;", "›" },
167 { "rsquo;", "’" },
168 { "sbquo;", "‚" },
169 { "scaron;", "š" },
170 { "sect;", "§" },
171 { "shy;", "­" },
172 { "sup1;", "¹" },
173 { "sup2;", "²" },
174 { "sup3;", "³" },
175 { "szlig;", "ß" },
176 { "thorn;", "þ" },
177 { "tilde;", "˜" },
178 { "times;", "×" },
179 { "trade;", "™" },
180 { "uacute;", "ú" },
181 { "ucirc;", "û" },
182 { "ugrave;", "ù" },
183 { "uml;", "¨" },
184 { "uuml;", "ü" },
185 { "yacute;", "ý" },
186 { "yen;", "¥" },
187 { "yuml;", "ÿ" },
190 static int cmp_entity (const void *key, const void *elem)
192 const struct xml_entity_s *ent = elem;
193 const char *name = key;
195 return strncmp (name, ent->psz_entity, strlen (ent->psz_entity));
198 void vlc_xml_decode( char *psz_value )
200 char *p_pos = psz_value;
202 while ( *psz_value )
204 if( *psz_value == '&' )
206 if( psz_value[1] == '#' )
207 { /* &#DDD; or &#xHHHH; Unicode code point */
208 char *psz_end;
209 unsigned long cp;
211 if( psz_value[2] == 'x' ) /* The x must be lower-case. */
212 cp = strtoul( psz_value + 3, &psz_end, 16 );
213 else
214 cp = strtoul( psz_value + 2, &psz_end, 10 );
216 if( *psz_end == ';' )
218 psz_value = psz_end + 1;
219 if( cp == 0 )
220 (void)0; /* skip nulls */
221 else
222 if( cp <= 0x7F )
224 *p_pos = cp;
226 else
227 /* Unicode code point outside ASCII.
228 * &#xxx; representation is longer than UTF-8 :) */
229 if( cp <= 0x7FF )
231 *p_pos++ = 0xC0 | (cp >> 6);
232 *p_pos = 0x80 | (cp & 0x3F);
234 else
235 if( cp <= 0xFFFF )
237 *p_pos++ = 0xE0 | (cp >> 12);
238 *p_pos++ = 0x80 | ((cp >> 6) & 0x3F);
239 *p_pos = 0x80 | (cp & 0x3F);
241 else
242 if( cp <= 0x1FFFFF ) /* Outside the BMP */
243 { /* Unicode stops at 10FFFF, but who cares? */
244 *p_pos++ = 0xF0 | (cp >> 18);
245 *p_pos++ = 0x80 | ((cp >> 12) & 0x3F);
246 *p_pos++ = 0x80 | ((cp >> 6) & 0x3F);
247 *p_pos = 0x80 | (cp & 0x3F);
250 else
252 /* Invalid entity number */
253 *p_pos = *psz_value;
254 psz_value++;
257 else
258 { /* Well-known XML entity */
259 const struct xml_entity_s *ent;
261 ent = bsearch (psz_value + 1, xml_entities,
262 ARRAY_SIZE (xml_entities),
263 sizeof (*ent), cmp_entity);
264 if (ent != NULL)
266 size_t olen = strlen (ent->psz_char);
267 memcpy (p_pos, ent->psz_char, olen);
268 p_pos += olen - 1;
269 psz_value += strlen (ent->psz_entity) + 1;
271 else
272 { /* No match */
273 *p_pos = *psz_value;
274 psz_value++;
278 else
280 *p_pos = *psz_value;
281 psz_value++;
284 p_pos++;
287 *p_pos = '\0';
290 char *vlc_xml_encode (const char *str)
292 struct vlc_memstream stream;
293 size_t n;
294 uint32_t cp;
296 assert(str != NULL);
297 vlc_memstream_open(&stream);
299 while ((n = vlc_towc (str, &cp)) != 0)
301 if (unlikely(n == (size_t)-1))
303 if (vlc_memstream_close(&stream) == 0)
304 free(stream.ptr);
305 errno = EILSEQ;
306 return NULL;
309 switch (cp)
311 case '\"':
312 vlc_memstream_puts(&stream, "&quot;");
313 break;
314 case '&':
315 vlc_memstream_puts(&stream, "&amp;");
316 break;
317 case '\'':
318 vlc_memstream_puts(&stream, "&#39;");
319 break;
320 case '<':
321 vlc_memstream_puts(&stream, "&lt;");
322 break;
323 case '>':
324 vlc_memstream_puts(&stream, "&gt;");
325 break;
326 default:
327 if (cp < 32) /* C0 code not allowed (except 9, 10 and 13) */
328 break;
329 if (cp >= 128 && cp < 160) /* C1 code encoded (except 133) */
331 vlc_memstream_printf(&stream, "&#%"PRIu32";", cp);
332 break;
334 /* fall through */
335 case 9:
336 case 10:
337 case 13:
338 case 133:
339 vlc_memstream_write(&stream, str, n);
340 break;
342 str += n;
345 if (vlc_memstream_close(&stream))
346 return NULL;
347 return stream.ptr;
350 /* Base64 encoding */
351 char *vlc_b64_encode_binary( const uint8_t *src, size_t i_src )
353 static const char b64[] =
354 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
356 char *ret = malloc( ( i_src + 4 ) * 4 / 3 );
357 char *dst = ret;
359 if( dst == NULL )
360 return NULL;
362 while( i_src > 0 )
364 /* pops (up to) 3 bytes of input, push 4 bytes */
365 uint32_t v;
367 /* 1/3 -> 1/4 */
368 v = ((unsigned)*src++) << 24;
369 *dst++ = b64[v >> 26];
370 v = v << 6;
372 /* 2/3 -> 2/4 */
373 if( i_src >= 2 )
374 v |= *src++ << 22;
375 *dst++ = b64[v >> 26];
376 v = v << 6;
378 /* 3/3 -> 3/4 */
379 if( i_src >= 3 )
380 v |= *src++ << 20; // 3/3
381 *dst++ = ( i_src >= 2 ) ? b64[v >> 26] : '='; // 3/4
382 v = v << 6;
384 /* -> 4/4 */
385 *dst++ = ( i_src >= 3 ) ? b64[v >> 26] : '='; // 4/4
387 if( i_src <= 3 )
388 break;
389 i_src -= 3;
392 *dst = '\0';
394 return ret;
397 char *vlc_b64_encode( const char *src )
399 if( src )
400 return vlc_b64_encode_binary( (const uint8_t*)src, strlen(src) );
401 else
402 return vlc_b64_encode_binary( (const uint8_t*)"", 0 );
405 /* Base64 decoding */
406 size_t vlc_b64_decode_binary_to_buffer( uint8_t *p_dst, size_t i_dst, const char *p_src )
408 static const int b64[256] = {
409 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
410 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
411 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
412 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
413 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
414 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
415 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
416 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
417 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
418 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
419 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
420 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
421 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
422 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
423 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
424 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
426 uint8_t *p_start = p_dst;
427 uint8_t *p = (uint8_t *)p_src;
429 int i_level;
430 int i_last;
432 for( i_level = 0, i_last = 0; (size_t)( p_dst - p_start ) < i_dst && *p != '\0'; p++ )
434 const int c = b64[(unsigned int)*p];
435 if( c == -1 )
436 break;
438 switch( i_level )
440 case 0:
441 i_level++;
442 break;
443 case 1:
444 *p_dst++ = ( i_last << 2 ) | ( ( c >> 4)&0x03 );
445 i_level++;
446 break;
447 case 2:
448 *p_dst++ = ( ( i_last << 4 )&0xf0 ) | ( ( c >> 2 )&0x0f );
449 i_level++;
450 break;
451 case 3:
452 *p_dst++ = ( ( i_last &0x03 ) << 6 ) | c;
453 i_level = 0;
455 i_last = c;
458 return p_dst - p_start;
460 size_t vlc_b64_decode_binary( uint8_t **pp_dst, const char *psz_src )
462 const int i_src = strlen( psz_src );
463 uint8_t *p_dst;
465 *pp_dst = p_dst = malloc( i_src );
466 if( !p_dst )
467 return 0;
468 return vlc_b64_decode_binary_to_buffer( p_dst, i_src, psz_src );
470 char *vlc_b64_decode( const char *psz_src )
472 const int i_src = strlen( psz_src );
473 char *p_dst = malloc( i_src + 1 );
474 size_t i_dst;
475 if( !p_dst )
476 return NULL;
478 i_dst = vlc_b64_decode_binary_to_buffer( (uint8_t*)p_dst, i_src, psz_src );
479 p_dst[i_dst] = '\0';
481 return p_dst;
484 char *vlc_strftime( const char *tformat )
486 time_t curtime;
487 struct tm loctime;
489 if (strcmp (tformat, "") == 0)
490 return strdup (""); /* corner case w.r.t. strftime() return value */
492 /* Get the current time. */
493 time( &curtime );
495 /* Convert it to local time representation. */
496 localtime_r( &curtime, &loctime );
497 for (size_t buflen = strlen (tformat) + 32;; buflen += 32)
499 char *str = malloc (buflen);
500 if (str == NULL)
501 return NULL;
503 size_t len = strftime (str, buflen, tformat, &loctime);
504 if (len > 0)
506 char *ret = realloc (str, len + 1);
507 return ret ? ret : str; /* <- this cannot fail */
509 free (str);
511 vlc_assert_unreachable ();
514 static void write_duration(struct vlc_memstream *stream, int64_t duration)
516 lldiv_t d;
517 long long sec;
519 duration /= CLOCK_FREQ;
520 d = lldiv(duration, 60);
521 sec = d.rem;
522 d = lldiv(d.quot, 60);
523 vlc_memstream_printf(stream, "%02lld:%02lld:%02lld", d.quot, d.rem, sec);
526 static int write_meta(struct vlc_memstream *stream, input_item_t *item,
527 vlc_meta_type_t type)
529 if (item == NULL)
530 return EOF;
532 char *value = input_item_GetMeta(item, type);
533 if (value == NULL)
534 return EOF;
536 vlc_memstream_puts(stream, value);
537 free(value);
538 return 0;
541 char *vlc_strfinput(input_thread_t *input, const char *s)
543 struct vlc_memstream stream[1];
545 input_item_t *item = (input != NULL) ? input_GetItem(input) : NULL;
547 char c;
548 bool b_is_format = false;
549 bool b_empty_if_na = false;
551 assert(s != NULL);
553 vlc_memstream_open(stream);
555 while ((c = *s) != '\0')
557 s++;
559 if (!b_is_format)
561 if (c == '$')
563 b_is_format = true;
564 b_empty_if_na = false;
565 continue;
568 vlc_memstream_putc(stream, c);
569 continue;
572 b_is_format = false;
574 switch (c)
576 case 'a':
577 write_meta(stream, item, vlc_meta_Artist);
578 break;
579 case 'b':
580 write_meta(stream, item, vlc_meta_Album);
581 break;
582 case 'c':
583 write_meta(stream, item, vlc_meta_Copyright);
584 break;
585 case 'd':
586 write_meta(stream, item, vlc_meta_Description);
587 break;
588 case 'e':
589 write_meta(stream, item, vlc_meta_EncodedBy);
590 break;
591 case 'f':
592 if (item != NULL && item->p_stats != NULL)
594 vlc_mutex_lock(&item->p_stats->lock);
595 vlc_memstream_printf(stream, "%"PRIi64,
596 item->p_stats->i_displayed_pictures);
597 vlc_mutex_unlock(&item->p_stats->lock);
599 else if (!b_empty_if_na)
600 vlc_memstream_putc(stream, '-');
601 break;
602 case 'g':
603 write_meta(stream, item, vlc_meta_Genre);
604 break;
605 case 'l':
606 write_meta(stream, item, vlc_meta_Language);
607 break;
608 case 'n':
609 write_meta(stream, item, vlc_meta_TrackNumber);
610 break;
611 case 'o':
612 write_meta(stream, item, vlc_meta_TrackTotal);
613 break;
614 case 'p':
615 if (item == NULL)
616 break;
618 char *value = input_item_GetNowPlayingFb(item);
619 if (value == NULL)
620 break;
622 vlc_memstream_puts(stream, value);
623 free(value);
625 break;
626 case 'r':
627 write_meta(stream, item, vlc_meta_Rating);
628 break;
629 case 's':
631 char *lang = NULL;
633 if (input != NULL)
634 lang = var_GetNonEmptyString(input, "sub-language");
635 if (lang != NULL)
637 vlc_memstream_puts(stream, lang);
638 free(lang);
640 else if (!b_empty_if_na)
641 vlc_memstream_putc(stream, '-');
642 break;
644 case 't':
645 write_meta(stream, item, vlc_meta_Title);
646 break;
647 case 'u':
648 write_meta(stream, item, vlc_meta_URL);
649 break;
650 case 'A':
651 write_meta(stream, item, vlc_meta_Date);
652 break;
653 case 'B':
654 if (input != NULL)
655 vlc_memstream_printf(stream, "%"PRId64,
656 var_GetInteger(input, "bit-rate") / 1000);
657 else if (!b_empty_if_na)
658 vlc_memstream_putc(stream, '-');
659 break;
660 case 'C':
661 if (input != NULL)
662 vlc_memstream_printf(stream, "%"PRId64,
663 var_GetInteger(input, "chapter"));
664 else if (!b_empty_if_na)
665 vlc_memstream_putc(stream, '-');
666 break;
667 case 'D':
668 if (item != NULL)
669 write_duration(stream, input_item_GetDuration(item));
670 else if (!b_empty_if_na)
671 vlc_memstream_puts(stream, "--:--:--");
672 break;
673 case 'F':
674 if (item != NULL)
676 char *uri = input_item_GetURI(item);
677 if (uri != NULL)
679 vlc_memstream_puts(stream, uri);
680 free(uri);
683 break;
684 case 'I':
685 if (input != NULL)
686 vlc_memstream_printf(stream, "%"PRId64,
687 var_GetInteger(input, "title"));
688 else if (!b_empty_if_na)
689 vlc_memstream_putc(stream, '-');
690 break;
691 case 'L':
692 if (item != NULL)
694 assert(input != NULL);
695 write_duration(stream, input_item_GetDuration(item)
696 - var_GetInteger(input, "time"));
698 else if (!b_empty_if_na)
699 vlc_memstream_puts(stream, "--:--:--");
700 break;
701 case 'N':
702 if (item != NULL)
704 char *name = input_item_GetName(item);
705 if (name != NULL)
707 vlc_memstream_puts(stream, name);
708 free(name);
711 break;
712 case 'O':
714 char *lang = NULL;
716 if (input != NULL)
717 lang = var_GetNonEmptyString(input, "audio-language");
718 if (lang != NULL)
720 vlc_memstream_puts(stream, lang);
721 free(lang);
723 else if (!b_empty_if_na)
724 vlc_memstream_putc(stream, '-');
725 break;
727 case 'P':
728 if (input != NULL)
729 vlc_memstream_printf(stream, "%2.1f",
730 var_GetFloat(input, "position") * 100.f);
731 else if (!b_empty_if_na)
732 vlc_memstream_puts(stream, "--.-%");
733 break;
734 case 'R':
735 if (input != NULL)
736 vlc_memstream_printf(stream, "%.3f",
737 var_GetFloat(input, "rate"));
738 else if (!b_empty_if_na)
739 vlc_memstream_putc(stream, '-');
740 break;
741 case 'S':
742 if (input != NULL)
744 int rate = var_GetInteger(input, "sample-rate");
745 div_t dr = div((rate + 50) / 100, 10);
747 vlc_memstream_printf(stream, "%d.%01d", dr.quot, dr.rem);
749 else if (!b_empty_if_na)
750 vlc_memstream_putc(stream, '-');
751 break;
752 case 'T':
753 if (input != NULL)
754 write_duration(stream, var_GetInteger(input, "time"));
755 else if (!b_empty_if_na)
756 vlc_memstream_puts(stream, "--:--:--");
757 break;
758 case 'U':
759 write_meta(stream, item, vlc_meta_Publisher);
760 break;
761 case 'V':
763 float vol = 0.f;
765 if (input != NULL)
767 audio_output_t *aout = input_GetAout(input);
768 if (aout != NULL)
770 vol = aout_VolumeGet(aout);
771 vlc_object_release(aout);
774 if (vol >= 0.f)
775 vlc_memstream_printf(stream, "%ld", lroundf(vol * 256.f));
776 else if (!b_empty_if_na)
777 vlc_memstream_puts(stream, "---");
778 break;
780 case '_':
781 vlc_memstream_putc(stream, '\n');
782 break;
783 case 'Z':
784 if (item == NULL)
785 break;
787 char *value = input_item_GetNowPlayingFb(item);
788 if (value != NULL)
790 vlc_memstream_puts(stream, value);
791 free(value);
793 else
795 char *title = input_item_GetTitleFbName(item);
797 if (write_meta(stream, item, vlc_meta_Artist) >= 0
798 && title != NULL)
799 vlc_memstream_puts(stream, " - ");
801 if (title != NULL)
803 vlc_memstream_puts(stream, title);
804 free(title);
808 break;
809 case ' ':
810 b_empty_if_na = true;
811 b_is_format = true;
812 break;
813 default:
814 vlc_memstream_putc(stream, c);
815 break;
819 if (vlc_memstream_close(stream))
820 return NULL;
821 return stream->ptr;
824 int vlc_filenamecmp(const char *a, const char *b)
826 size_t i;
827 char ca, cb;
829 /* Attempt to guess if the sorting algorithm should be alphabetic
830 * (i.e. collation) or numeric:
831 * - If the first mismatching characters are not both digits,
832 * then collation is the only option.
833 * - If one of the first mismatching characters is 0 and the other is also
834 * a digit, the comparands are probably left-padded numerical values.
835 * It does not matter which algorithm is used: the zero will be smaller
836 * than non-zero either way.
837 * - Otherwise, the comparands are numerical values, and might not be
838 * aligned (i.e. not same order of magnitude). If so, collation would
839 * fail. So numerical comparison is performed. */
840 for (i = 0; (ca = a[i]) == (cb = b[i]); i++)
841 if (ca == '\0')
842 return 0; /* strings are exactly identical */
844 if ((unsigned)(ca - '0') > 9 || (unsigned)(cb - '0') > 9)
845 return strcoll(a, b);
847 unsigned long long ua = strtoull(a + i, NULL, 10);
848 unsigned long long ub = strtoull(b + i, NULL, 10);
850 /* The number may be identical in two cases:
851 * - leading zero (e.g. "012" and "12")
852 * - overflow on both sides (#ULLONG_MAX) */
853 if (ua == ub)
854 return strcoll(a, b);
856 return (ua > ub) ? +1 : -1;
860 * Sanitize a file name.
862 * Remove forbidden, potentially forbidden and otherwise evil characters from
863 * file names. That includes slashes, and popular characters like colon
864 * (on Unix anyway).
866 * \warning This function should only be used for automatically generated
867 * file names. Do not use this on full paths, only single file names without
868 * any directory separator!
870 void filename_sanitize( char *str )
872 unsigned char c;
874 /* Special file names, not allowed */
875 if( !strcmp( str, "." ) || !strcmp( str, ".." ) )
877 while( *str )
878 *(str++) = '_';
879 return;
882 /* On platforms not using UTF-8, VLC cannot access non-Unicode paths.
883 * Also, some file systems require Unicode file names.
884 * NOTE: This may inserts '?' thus is done replacing '?' with '_'. */
885 EnsureUTF8( str );
887 /* Avoid leading spaces to please Windows. */
888 while( (c = *str) != '\0' )
890 if( c != ' ' )
891 break;
892 *(str++) = '_';
895 char *start = str;
897 while( (c = *str) != '\0' )
899 /* Non-printable characters are not a good idea */
900 if( c < 32 )
901 *str = '_';
902 /* This is the list of characters not allowed by Microsoft.
903 * We also black-list them on Unix as they may be confusing, and are
904 * not supported by some file system types (notably CIFS). */
905 else if( strchr( "/:\\*\"?|<>", c ) != NULL )
906 *str = '_';
907 str++;
910 /* Avoid trailing spaces also to please Windows. */
911 while( str > start )
913 if( *(--str) != ' ' )
914 break;
915 *str = '_';