direct3d9: don't pass structures as values
[vlc.git] / src / text / strings.c
blob5ebf46c8b451923db4a3da9bf1be245261b4bdfe
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>
42 /* Needed by vlc_strfinput */
43 #include <vlc_input.h>
44 #include <vlc_meta.h>
45 #include <vlc_aout.h>
47 #include <vlc_strings.h>
48 #include <vlc_charset.h>
49 #include <libvlc.h>
50 #include <errno.h>
52 static const struct xml_entity_s
54 char psz_entity[8];
55 char psz_char[4];
56 } xml_entities[] = {
57 /* Important: this list has to be in alphabetical order (psz_entity-wise) */
58 { "AElig;", "Æ" },
59 { "Aacute;", "Á" },
60 { "Acirc;", "Â" },
61 { "Agrave;", "À" },
62 { "Aring;", "Å" },
63 { "Atilde;", "Ã" },
64 { "Auml;", "Ä" },
65 { "Ccedil;", "Ç" },
66 { "Dagger;", "‡" },
67 { "ETH;", "Ð" },
68 { "Eacute;", "É" },
69 { "Ecirc;", "Ê" },
70 { "Egrave;", "È" },
71 { "Euml;", "Ë" },
72 { "Iacute;", "Í" },
73 { "Icirc;", "Î" },
74 { "Igrave;", "Ì" },
75 { "Iuml;", "Ï" },
76 { "Ntilde;", "Ñ" },
77 { "OElig;", "Œ" },
78 { "Oacute;", "Ó" },
79 { "Ocirc;", "Ô" },
80 { "Ograve;", "Ò" },
81 { "Oslash;", "Ø" },
82 { "Otilde;", "Õ" },
83 { "Ouml;", "Ö" },
84 { "Scaron;", "Š" },
85 { "THORN;", "Þ" },
86 { "Uacute;", "Ú" },
87 { "Ucirc;", "Û" },
88 { "Ugrave;", "Ù" },
89 { "Uuml;", "Ü" },
90 { "Yacute;", "Ý" },
91 { "Yuml;", "Ÿ" },
92 { "aacute;", "á" },
93 { "acirc;", "â" },
94 { "acute;", "´" },
95 { "aelig;", "æ" },
96 { "agrave;", "à" },
97 { "amp;", "&" },
98 { "apos;", "'" },
99 { "aring;", "å" },
100 { "atilde;", "ã" },
101 { "auml;", "ä" },
102 { "bdquo;", "„" },
103 { "brvbar;", "¦" },
104 { "ccedil;", "ç" },
105 { "cedil;", "¸" },
106 { "cent;", "¢" },
107 { "circ;", "ˆ" },
108 { "copy;", "©" },
109 { "curren;", "¤" },
110 { "dagger;", "†" },
111 { "deg;", "°" },
112 { "divide;", "÷" },
113 { "eacute;", "é" },
114 { "ecirc;", "ê" },
115 { "egrave;", "è" },
116 { "eth;", "ð" },
117 { "euml;", "ë" },
118 { "euro;", "€" },
119 { "frac12;", "½" },
120 { "frac14;", "¼" },
121 { "frac34;", "¾" },
122 { "gt;", ">" },
123 { "hellip;", "…" },
124 { "iacute;", "í" },
125 { "icirc;", "î" },
126 { "iexcl;", "¡" },
127 { "igrave;", "ì" },
128 { "iquest;", "¿" },
129 { "iuml;", "ï" },
130 { "laquo;", "«" },
131 { "ldquo;", "“" },
132 { "lsaquo;", "‹" },
133 { "lsquo;", "‘" },
134 { "lt;", "<" },
135 { "macr;", "¯" },
136 { "mdash;", "—" },
137 { "micro;", "µ" },
138 { "middot;", "·" },
139 { "nbsp;", "\xc2\xa0" },
140 { "ndash;", "–" },
141 { "not;", "¬" },
142 { "ntilde;", "ñ" },
143 { "oacute;", "ó" },
144 { "ocirc;", "ô" },
145 { "oelig;", "œ" },
146 { "ograve;", "ò" },
147 { "ordf;", "ª" },
148 { "ordm;", "º" },
149 { "oslash;", "ø" },
150 { "otilde;", "õ" },
151 { "ouml;", "ö" },
152 { "para;", "¶" },
153 { "permil;", "‰" },
154 { "plusmn;", "±" },
155 { "pound;", "£" },
156 { "quot;", "\"" },
157 { "raquo;", "»" },
158 { "rdquo;", "”" },
159 { "reg;", "®" },
160 { "rsaquo;", "›" },
161 { "rsquo;", "’" },
162 { "sbquo;", "‚" },
163 { "scaron;", "š" },
164 { "sect;", "§" },
165 { "shy;", "­" },
166 { "sup1;", "¹" },
167 { "sup2;", "²" },
168 { "sup3;", "³" },
169 { "szlig;", "ß" },
170 { "thorn;", "þ" },
171 { "tilde;", "˜" },
172 { "times;", "×" },
173 { "trade;", "™" },
174 { "uacute;", "ú" },
175 { "ucirc;", "û" },
176 { "ugrave;", "ù" },
177 { "uml;", "¨" },
178 { "uuml;", "ü" },
179 { "yacute;", "ý" },
180 { "yen;", "¥" },
181 { "yuml;", "ÿ" },
184 static int cmp_entity (const void *key, const void *elem)
186 const struct xml_entity_s *ent = elem;
187 const char *name = key;
189 return strncmp (name, ent->psz_entity, strlen (ent->psz_entity));
192 void vlc_xml_decode( char *psz_value )
194 char *p_pos = psz_value;
196 while ( *psz_value )
198 if( *psz_value == '&' )
200 if( psz_value[1] == '#' )
201 { /* &#DDD; or &#xHHHH; Unicode code point */
202 char *psz_end;
203 unsigned long cp;
205 if( psz_value[2] == 'x' ) /* The x must be lower-case. */
206 cp = strtoul( psz_value + 3, &psz_end, 16 );
207 else
208 cp = strtoul( psz_value + 2, &psz_end, 10 );
210 if( *psz_end == ';' )
212 psz_value = psz_end + 1;
213 if( cp == 0 )
214 (void)0; /* skip nulls */
215 else
216 if( cp <= 0x7F )
218 *p_pos = cp;
220 else
221 /* Unicode code point outside ASCII.
222 * &#xxx; representation is longer than UTF-8 :) */
223 if( cp <= 0x7FF )
225 *p_pos++ = 0xC0 | (cp >> 6);
226 *p_pos = 0x80 | (cp & 0x3F);
228 else
229 if( cp <= 0xFFFF )
231 *p_pos++ = 0xE0 | (cp >> 12);
232 *p_pos++ = 0x80 | ((cp >> 6) & 0x3F);
233 *p_pos = 0x80 | (cp & 0x3F);
235 else
236 if( cp <= 0x1FFFFF ) /* Outside the BMP */
237 { /* Unicode stops at 10FFFF, but who cares? */
238 *p_pos++ = 0xF0 | (cp >> 18);
239 *p_pos++ = 0x80 | ((cp >> 12) & 0x3F);
240 *p_pos++ = 0x80 | ((cp >> 6) & 0x3F);
241 *p_pos = 0x80 | (cp & 0x3F);
244 else
246 /* Invalid entity number */
247 *p_pos = *psz_value;
248 psz_value++;
251 else
252 { /* Well-known XML entity */
253 const struct xml_entity_s *ent;
255 ent = bsearch (psz_value + 1, xml_entities,
256 sizeof (xml_entities) / sizeof (*ent),
257 sizeof (*ent), cmp_entity);
258 if (ent != NULL)
260 size_t olen = strlen (ent->psz_char);
261 memcpy (p_pos, ent->psz_char, olen);
262 p_pos += olen - 1;
263 psz_value += strlen (ent->psz_entity) + 1;
265 else
266 { /* No match */
267 *p_pos = *psz_value;
268 psz_value++;
272 else
274 *p_pos = *psz_value;
275 psz_value++;
278 p_pos++;
281 *p_pos = '\0';
284 char *vlc_xml_encode (const char *str)
286 assert (str != NULL);
288 const size_t len = strlen (str);
289 char *const buf = malloc (6 * len + 1), *ptr = buf;
290 if (unlikely(buf == NULL))
291 return NULL;
293 size_t n;
294 uint32_t cp;
296 while ((n = vlc_towc (str, &cp)) != 0)
298 if (unlikely(n == (size_t)-1))
300 free (buf);
301 errno = EILSEQ;
302 return NULL;
305 switch (cp)
307 case '\"': strcpy (ptr, "&quot;"); ptr += 6; break;
308 case '&': strcpy (ptr, "&amp;"); ptr += 5; break;
309 case '\'': strcpy (ptr, "&#39;"); ptr += 5; break;
310 case '<': strcpy (ptr, "&lt;"); ptr += 4; break;
311 case '>': strcpy (ptr, "&gt;"); ptr += 4; break;
312 default:
313 if (cp < 32) /* C0 code not allowed (except 9, 10 and 13) */
314 break;
315 if (cp >= 128 && cp < 160) /* C1 code encoded (except 133) */
317 ptr += sprintf (ptr, "&#%"PRIu32";", cp);
318 break;
320 /* fall through */
321 case 9:
322 case 10:
323 case 13:
324 case 133:
325 memcpy (ptr, str, n);
326 ptr += n;
327 break;
329 str += n;
331 *(ptr++) = '\0';
333 ptr = realloc (buf, ptr - buf);
334 return likely(ptr != NULL) ? ptr : buf; /* cannot fail */
337 /* Base64 encoding */
338 char *vlc_b64_encode_binary( const uint8_t *src, size_t i_src )
340 static const char b64[] =
341 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
343 char *ret = malloc( ( i_src + 4 ) * 4 / 3 );
344 char *dst = ret;
346 if( dst == NULL )
347 return NULL;
349 while( i_src > 0 )
351 /* pops (up to) 3 bytes of input, push 4 bytes */
352 uint32_t v;
354 /* 1/3 -> 1/4 */
355 v = *src++ << 24;
356 *dst++ = b64[v >> 26];
357 v = v << 6;
359 /* 2/3 -> 2/4 */
360 if( i_src >= 2 )
361 v |= *src++ << 22;
362 *dst++ = b64[v >> 26];
363 v = v << 6;
365 /* 3/3 -> 3/4 */
366 if( i_src >= 3 )
367 v |= *src++ << 20; // 3/3
368 *dst++ = ( i_src >= 2 ) ? b64[v >> 26] : '='; // 3/4
369 v = v << 6;
371 /* -> 4/4 */
372 *dst++ = ( i_src >= 3 ) ? b64[v >> 26] : '='; // 4/4
374 if( i_src <= 3 )
375 break;
376 i_src -= 3;
379 *dst = '\0';
381 return ret;
384 char *vlc_b64_encode( const char *src )
386 if( src )
387 return vlc_b64_encode_binary( (const uint8_t*)src, strlen(src) );
388 else
389 return vlc_b64_encode_binary( (const uint8_t*)"", 0 );
392 /* Base64 decoding */
393 size_t vlc_b64_decode_binary_to_buffer( uint8_t *p_dst, size_t i_dst, const char *p_src )
395 static const int b64[256] = {
396 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
397 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
398 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
399 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
400 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
401 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
402 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
403 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
404 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
405 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
406 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
407 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
408 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
409 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
410 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
411 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
413 uint8_t *p_start = p_dst;
414 uint8_t *p = (uint8_t *)p_src;
416 int i_level;
417 int i_last;
419 for( i_level = 0, i_last = 0; (size_t)( p_dst - p_start ) < i_dst && *p != '\0'; p++ )
421 const int c = b64[(unsigned int)*p];
422 if( c == -1 )
423 break;
425 switch( i_level )
427 case 0:
428 i_level++;
429 break;
430 case 1:
431 *p_dst++ = ( i_last << 2 ) | ( ( c >> 4)&0x03 );
432 i_level++;
433 break;
434 case 2:
435 *p_dst++ = ( ( i_last << 4 )&0xf0 ) | ( ( c >> 2 )&0x0f );
436 i_level++;
437 break;
438 case 3:
439 *p_dst++ = ( ( i_last &0x03 ) << 6 ) | c;
440 i_level = 0;
442 i_last = c;
445 return p_dst - p_start;
447 size_t vlc_b64_decode_binary( uint8_t **pp_dst, const char *psz_src )
449 const int i_src = strlen( psz_src );
450 uint8_t *p_dst;
452 *pp_dst = p_dst = malloc( i_src );
453 if( !p_dst )
454 return 0;
455 return vlc_b64_decode_binary_to_buffer( p_dst, i_src, psz_src );
457 char *vlc_b64_decode( const char *psz_src )
459 const int i_src = strlen( psz_src );
460 char *p_dst = malloc( i_src + 1 );
461 size_t i_dst;
462 if( !p_dst )
463 return NULL;
465 i_dst = vlc_b64_decode_binary_to_buffer( (uint8_t*)p_dst, i_src, psz_src );
466 p_dst[i_dst] = '\0';
468 return p_dst;
471 char *vlc_strftime( const char *tformat )
473 time_t curtime;
474 struct tm loctime;
476 if (strcmp (tformat, "") == 0)
477 return strdup (""); /* corner case w.r.t. strftime() return value */
479 /* Get the current time. */
480 time( &curtime );
482 /* Convert it to local time representation. */
483 localtime_r( &curtime, &loctime );
484 for (size_t buflen = strlen (tformat) + 32;; buflen += 32)
486 char *str = malloc (buflen);
487 if (str == NULL)
488 return NULL;
490 size_t len = strftime (str, buflen, tformat, &loctime);
491 if (len > 0)
493 char *ret = realloc (str, len + 1);
494 return ret ? ret : str; /* <- this cannot fail */
496 free (str);
498 vlc_assert_unreachable ();
501 static void write_duration(FILE *stream, int64_t duration)
503 lldiv_t d;
504 long long sec;
506 duration /= CLOCK_FREQ;
507 d = lldiv(duration, 60);
508 sec = d.rem;
509 d = lldiv(d.quot, 60);
510 fprintf(stream, "%02lld:%02lld:%02lld", d.quot, d.rem, sec);
513 static int write_meta(FILE *stream, input_item_t *item, vlc_meta_type_t type)
515 if (item == NULL)
516 return EOF;
518 char *value = input_item_GetMeta(item, type);
519 if (value == NULL)
520 return EOF;
522 int ret = fputs(value, stream);
523 free(value);
524 return ret;
527 char *vlc_strfinput(input_thread_t *input, const char *s)
529 char *str = NULL;
530 size_t len;
531 #ifdef HAVE_OPEN_MEMSTREAM
532 FILE *stream = open_memstream(&str, &len);
533 #elif defined( _WIN32 )
534 FILE *stream = vlc_win32_tmpfile();
535 #else
536 FILE *stream = tmpfile();
537 #endif
538 if (stream == NULL)
539 return NULL;
541 input_item_t *item = (input != NULL) ? input_GetItem(input) : NULL;
543 char c;
544 bool b_is_format = false;
545 bool b_empty_if_na = false;
547 assert(s != NULL);
549 while ((c = *s) != '\0')
551 s++;
553 if (!b_is_format)
555 if (c == '$')
557 b_is_format = true;
558 b_empty_if_na = false;
559 continue;
562 fputc(c, stream);
563 continue;
566 b_is_format = false;
568 switch (c)
570 case 'a':
571 write_meta(stream, item, vlc_meta_Artist);
572 break;
573 case 'b':
574 write_meta(stream, item, vlc_meta_Album);
575 break;
576 case 'c':
577 write_meta(stream, item, vlc_meta_Copyright);
578 break;
579 case 'd':
580 write_meta(stream, item, vlc_meta_Description);
581 break;
582 case 'e':
583 write_meta(stream, item, vlc_meta_EncodedBy);
584 break;
585 case 'f':
586 if (item != NULL && item->p_stats != NULL)
588 vlc_mutex_lock(&item->p_stats->lock);
589 fprintf(stream, "%"PRIi64,
590 item->p_stats->i_displayed_pictures);
591 vlc_mutex_unlock(&item->p_stats->lock);
593 else if (!b_empty_if_na)
594 fputc('-', stream);
595 break;
596 case 'g':
597 write_meta(stream, item, vlc_meta_Genre);
598 break;
599 case 'l':
600 write_meta(stream, item, vlc_meta_Language);
601 break;
602 case 'n':
603 write_meta(stream, item, vlc_meta_TrackNumber);
604 break;
605 case 'o':
606 write_meta(stream, item, vlc_meta_TrackTotal);
607 break;
608 case 'p':
609 if (item == NULL)
610 break;
612 char *value = input_item_GetNowPlayingFb(item);
613 if (value == NULL)
614 break;
616 fputs(value, stream);
617 free(value);
619 break;
620 case 'r':
621 write_meta(stream, item, vlc_meta_Rating);
622 break;
623 case 's':
625 char *lang = NULL;
627 if (input != NULL)
628 lang = var_GetNonEmptyString(input, "sub-language");
629 if (lang != NULL)
631 fputs(lang, stream);
632 free(lang);
634 else if (!b_empty_if_na)
635 fputc('-', stream);
636 break;
638 case 't':
639 write_meta(stream, item, vlc_meta_Title);
640 break;
641 case 'u':
642 write_meta(stream, item, vlc_meta_URL);
643 break;
644 case 'A':
645 write_meta(stream, item, vlc_meta_Date);
646 break;
647 case 'B':
648 if (input != NULL)
649 fprintf(stream, "%"PRId64,
650 var_GetInteger(input, "bit-rate") / 1000);
651 else if (!b_empty_if_na)
652 fputc('-', stream);
653 break;
654 case 'C':
655 if (input != NULL)
656 fprintf(stream, "%"PRId64,
657 var_GetInteger(input, "chapter"));
658 else if (!b_empty_if_na)
659 fputc('-', stream);
660 break;
661 case 'D':
662 if (item != NULL)
663 write_duration(stream, input_item_GetDuration(item));
664 else if (!b_empty_if_na)
665 fputs("--:--:--", stream);
666 break;
667 case 'F':
668 if (item != NULL)
670 char *uri = input_item_GetURI(item);
671 if (uri != NULL)
673 fputs(uri, stream);
674 free(uri);
677 break;
678 case 'I':
679 if (input != NULL)
680 fprintf(stream, "%"PRId64, var_GetInteger(input, "title"));
681 else if (!b_empty_if_na)
682 fputc('-', stream);
683 break;
684 case 'L':
685 if (item != NULL)
687 assert(input != NULL);
688 write_duration(stream, input_item_GetDuration(item)
689 - var_GetInteger(input, "time"));
691 else if (!b_empty_if_na)
692 fputs("--:--:--", stream);
693 break;
694 case 'N':
695 if (item != NULL)
697 char *name = input_item_GetName(item);
698 if (name != NULL)
700 fputs(name, stream);
701 free(name);
704 break;
705 case 'O':
707 char *lang = NULL;
709 if (input != NULL)
710 lang = var_GetNonEmptyString(input, "audio-language");
711 if (lang != NULL)
713 fputs(lang, stream);
714 free(lang);
716 else if (!b_empty_if_na)
717 fputc('-', stream);
718 break;
720 case 'P':
721 if (input != NULL)
722 fprintf(stream, "%2.1f",
723 var_GetFloat(input, "position") * 100.f);
724 else if (!b_empty_if_na)
725 fputs("--.-%", stream);
726 break;
727 case 'R':
728 if (input != NULL)
729 fprintf(stream, "%.3f", var_GetFloat(input, "rate"));
730 else if (!b_empty_if_na)
731 fputc('-', stream);
732 break;
733 case 'S':
734 if (input != NULL)
736 int rate = var_GetInteger(input, "sample-rate");
737 div_t dr = div((rate + 50) / 100, 10);
739 fprintf(stream, "%d.%01d", dr.quot, dr.rem);
741 else if (!b_empty_if_na)
742 fputc('-', stream);
743 break;
744 case 'T':
745 if (input != NULL)
746 write_duration(stream, var_GetInteger(input, "time"));
747 else if (!b_empty_if_na)
748 fputs("--:--:--", stream);
749 break;
750 case 'U':
751 write_meta(stream, item, vlc_meta_Publisher);
752 break;
753 case 'V':
755 float vol = 0.f;
757 if (input != NULL)
759 audio_output_t *aout = input_GetAout(input);
760 if (aout != NULL)
762 vol = aout_VolumeGet(aout);
763 vlc_object_release(aout);
766 if (vol >= 0.f)
767 fprintf(stream, "%ld", lroundf(vol * 256.f));
768 else if (!b_empty_if_na)
769 fputs("---", stream);
770 break;
772 case '_':
773 fputc('\n', stream);
774 break;
775 case 'Z':
776 if (item == NULL)
777 break;
779 char *value = input_item_GetNowPlayingFb(item);
780 if (value != NULL)
782 fputs(value, stream);
783 free(value);
785 else
787 char *title = input_item_GetTitleFbName(item);
789 if (write_meta(stream, item, vlc_meta_Artist) >= 0
790 && title != NULL)
791 fputs(" - ", stream);
793 if (title != NULL)
795 fputs(title, stream);
796 free(title);
800 break;
801 case ' ':
802 b_empty_if_na = true;
803 b_is_format = true;
804 break;
805 default:
806 fputc(c, stream);
807 break;
811 #ifdef HAVE_OPEN_MEMSTREAM
812 return (fclose(stream) == 0) ? str : NULL;
813 #else
814 len = ftell(stream);
815 if (len != (size_t)-1 && len != SIZE_MAX)
817 rewind(stream);
818 str = malloc(len + 1);
819 if(str)
821 fread(str, len, 1, stream);
822 str[len] = '\0';
825 fclose(stream);
826 return str;
827 #endif
831 * Sanitize a file name.
833 * Remove forbidden, potentially forbidden and otherwise evil characters from
834 * file names. That includes slashes, and popular characters like colon
835 * (on Unix anyway).
837 * \warning This function should only be used for automatically generated
838 * file names. Do not use this on full paths, only single file names without
839 * any directory separator!
841 void filename_sanitize( char *str )
843 unsigned char c;
845 /* Special file names, not allowed */
846 if( !strcmp( str, "." ) || !strcmp( str, ".." ) )
848 while( *str )
849 *(str++) = '_';
850 return;
853 /* On platforms not using UTF-8, VLC cannot access non-Unicode paths.
854 * Also, some file systems require Unicode file names.
855 * NOTE: This may inserts '?' thus is done replacing '?' with '_'. */
856 EnsureUTF8( str );
858 /* Avoid leading spaces to please Windows. */
859 while( (c = *str) != '\0' )
861 if( c != ' ' )
862 break;
863 *(str++) = '_';
866 char *start = str;
868 while( (c = *str) != '\0' )
870 /* Non-printable characters are not a good idea */
871 if( c < 32 )
872 *str = '_';
873 /* This is the list of characters not allowed by Microsoft.
874 * We also black-list them on Unix as they may be confusing, and are
875 * not supported by some file system types (notably CIFS). */
876 else if( strchr( "/:\\*\"?|<>", c ) != NULL )
877 *str = '_';
878 str++;
881 /* Avoid trailing spaces also to please Windows. */
882 while( str > start )
884 if( *(--str) != ' ' )
885 break;
886 *str = '_';