1 /*****************************************************************************
2 * libc.c: Extra libc function for some systems.
3 *****************************************************************************
4 * Copyright (C) 2002 the VideoLAN team
7 * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
8 * Samuel Hocevar <sam@zoy.org>
9 * Gildas Bazin <gbazin@videolan.org>
10 * Derk-Jan Hartman <hartman at videolan dot org>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 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 General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
25 *****************************************************************************/
26 #include <string.h> /* strdup() */
37 #if defined(HAVE_ICONV)
45 /*****************************************************************************
46 * getenv: just in case, but it should never be called
47 *****************************************************************************/
48 #if !defined( HAVE_GETENV )
49 char *vlc_getenv( const char *name )
55 /*****************************************************************************
56 * strdup: returns a malloc'd copy of a string
57 *****************************************************************************/
58 #if !defined( HAVE_STRDUP )
59 char *vlc_strdup( const char *string )
61 return strndup( string, strlen( string ) );
65 /*****************************************************************************
66 * strndup: returns a malloc'd copy of at most n bytes of string
67 * Does anyone know whether or not it will be present in Jaguar?
68 *****************************************************************************/
69 #if !defined( HAVE_STRNDUP )
70 char *vlc_strndup( const char *string, size_t n )
73 size_t len = strlen( string );
75 len = __MIN( len, n );
76 psz = (char*)malloc( len + 1 );
80 memcpy( (void*)psz, (const void*)string, len );
88 /*****************************************************************************
89 * strcasecmp: compare two strings ignoring case
90 *****************************************************************************/
91 #if !defined( HAVE_STRCASECMP ) && !defined( HAVE_STRICMP )
92 int vlc_strcasecmp( const char *s1, const char *s2 )
95 if( !s1 || !s2 ) return -1;
102 if( c1 != c2 ) return (c1 < c2 ? -1 : 1);
106 if( !*s1 && !*s2 ) return 0;
107 else return (*s1 ? 1 : -1);
111 /*****************************************************************************
112 * strncasecmp: compare n chars from two strings ignoring case
113 *****************************************************************************/
114 #if !defined( HAVE_STRNCASECMP ) && !defined( HAVE_STRNICMP )
115 int vlc_strncasecmp( const char *s1, const char *s2, size_t n )
118 if( !s1 || !s2 ) return -1;
120 while( n > 0 && *s1 && *s2 )
125 if( c1 != c2 ) return (c1 < c2 ? -1 : 1);
129 if( !n || (!*s1 && !*s2) ) return 0;
130 else return (*s1 ? 1 : -1);
134 /******************************************************************************
135 * strcasestr: find a substring (little) in another substring (big)
136 * Case sensitive. Return NULL if not found, return big if little == null
137 *****************************************************************************/
138 #if !defined( HAVE_STRCASESTR ) && !defined( HAVE_STRISTR )
139 char * vlc_strcasestr( const char *psz_big, const char *psz_little )
141 char *p_pos = (char *)psz_big;
143 if( !psz_big || !psz_little || !*psz_little ) return p_pos;
147 if( toupper( *p_pos ) == toupper( *psz_little ) )
149 char * psz_cur1 = p_pos + 1;
150 char * psz_cur2 = (char *)psz_little + 1;
151 while( *psz_cur1 && *psz_cur2 &&
152 toupper( *psz_cur1 ) == toupper( *psz_cur2 ) )
157 if( !*psz_cur2 ) return p_pos;
165 /*****************************************************************************
167 *****************************************************************************/
168 #if !defined(HAVE_VASPRINTF) || defined(SYS_DARWIN) || defined(SYS_BEOS)
169 int vlc_vasprintf(char **strp, const char *fmt, va_list ap)
171 /* Guess we need no more than 100 bytes. */
173 char *p = malloc( i_size );
184 /* Try to print in the allocated space. */
185 n = vsnprintf( p, i_size, fmt, ap );
187 /* If that worked, return the string. */
188 if (n > -1 && n < i_size)
193 /* Else try again with more space. */
194 if (n > -1) /* glibc 2.1 */
196 i_size = n+1; /* precisely what is needed */
200 i_size *= 2; /* twice the old size */
202 if( (p = realloc( p, i_size ) ) == NULL)
211 /*****************************************************************************
213 *****************************************************************************/
214 #if !defined(HAVE_ASPRINTF) || defined(SYS_DARWIN) || defined(SYS_BEOS)
215 int vlc_asprintf( char **strp, const char *fmt, ... )
220 va_start( args, fmt );
221 i_ret = vasprintf( strp, fmt, args );
228 /*****************************************************************************
229 * atof: convert a string to a double.
230 *****************************************************************************/
231 #if !defined( HAVE_ATOF )
232 double vlc_atof( const char *nptr )
236 int i_len = strlen( nptr ) + 1;
238 psz_tmp = malloc( i_len * sizeof(wchar_t) );
239 MultiByteToWideChar( CP_ACP, 0, nptr, -1, psz_tmp, i_len );
240 f_result = wcstod( psz_tmp, NULL );
247 /*****************************************************************************
248 * strtoll: convert a string to a 64 bits int.
249 *****************************************************************************/
250 #if !defined( HAVE_STRTOLL )
251 int64_t vlc_strtoll( const char *nptr, char **endptr, int base )
254 int sign = 1, newbase = base ? base : 10;
256 while( isspace(*nptr) ) nptr++;
264 /* Try to detect base */
277 if( base && newbase != base )
279 if( endptr ) *endptr = (char *)nptr;
286 while( *nptr >= '0' && *nptr <= '9' )
289 i_value += ( *nptr++ - '0' );
291 if( endptr ) *endptr = (char *)nptr;
295 while( (*nptr >= '0' && *nptr <= '9') ||
296 (*nptr >= 'a' && *nptr <= 'f') ||
297 (*nptr >= 'A' && *nptr <= 'F') )
300 if(*nptr >= '0' && *nptr <= '9') i_valc = *nptr - '0';
301 else if(*nptr >= 'a' && *nptr <= 'f') i_valc = *nptr - 'a' +10;
302 else if(*nptr >= 'A' && *nptr <= 'F') i_valc = *nptr - 'A' +10;
307 if( endptr ) *endptr = (char *)nptr;
311 i_value = strtol( nptr, endptr, newbase );
315 return i_value * sign;
319 /*****************************************************************************
320 * atoll: convert a string to a 64 bits int.
321 *****************************************************************************/
322 #if !defined( HAVE_ATOLL )
323 int64_t vlc_atoll( const char *nptr )
325 return strtoll( nptr, (char **)NULL, 10 );
329 /*****************************************************************************
330 * scandir: scan a directory alpha-sorted
331 *****************************************************************************/
332 #if !defined( HAVE_SCANDIR )
333 int vlc_alphasort( const struct dirent **a, const struct dirent **b )
335 return strcoll( (*a)->d_name, (*b)->d_name );
338 int vlc_scandir( const char *name, struct dirent ***namelist,
339 int (*filter) ( const struct dirent * ),
340 int (*compar) ( const struct dirent **,
341 const struct dirent ** ) )
344 struct dirent * p_content;
345 struct dirent ** pp_list;
348 if( !namelist || !( p_dir = opendir( name ) ) ) return -1;
352 while( ( p_content = readdir( p_dir ) ) )
354 if( filter && !filter( p_content ) )
358 pp_list = realloc( pp_list, ( ret + 1 ) * sizeof( struct dirent * ) );
359 size = sizeof( struct dirent ) + strlen( p_content->d_name ) + 1;
360 pp_list[ret] = malloc( size );
361 memcpy( pp_list[ret], p_content, size );
369 qsort( pp_list, ret, sizeof( struct dirent * ),
370 (int (*)(const void *, const void *)) compar );
378 /*****************************************************************************
379 * dgettext: gettext for plugins.
380 *****************************************************************************/
381 char *vlc_dgettext( const char *package, const char *msgid )
383 #if defined( ENABLE_NLS ) \
384 && ( defined(HAVE_GETTEXT) || defined(HAVE_INCLUDED_GETTEXT) )
385 return dgettext( package, msgid );
387 return (char *)msgid;
391 /*****************************************************************************
392 * count_utf8_string: returns the number of characters in the string.
393 *****************************************************************************/
394 static int count_utf8_string( const char *psz_string )
396 int i = 0, i_count = 0;
397 while( psz_string[ i ] != 0 )
399 if( ((unsigned char *)psz_string)[ i ] < 0x80UL ) i_count++;
405 /*****************************************************************************
406 * wraptext: inserts \n at convenient places to wrap the text.
407 * Returns the modified string in a new buffer.
408 *****************************************************************************/
409 char *vlc_wraptext( const char *psz_text, int i_line, vlc_bool_t b_utf8 )
412 char *psz_line, *psz_new_text;
414 psz_line = psz_new_text = strdup( psz_text );
417 i_len = count_utf8_string( psz_text );
419 i_len = strlen( psz_text );
421 while( i_len > i_line )
423 /* Look if there is a newline somewhere. */
424 char *psz_parser = psz_line;
426 while( i_count <= i_line && *psz_parser != '\n' )
430 while( *((unsigned char *)psz_parser) >= 0x80UL ) psz_parser++;
435 if( *psz_parser == '\n' )
437 i_len -= (i_count + 1);
438 psz_line = psz_parser + 1;
442 /* Find the furthest space. */
443 while( psz_parser > psz_line && *psz_parser != ' ' )
447 while( *((unsigned char *)psz_parser) >= 0x80UL ) psz_parser--;
452 if( *psz_parser == ' ' )
455 i_len -= (i_count + 1);
456 psz_line = psz_parser + 1;
460 /* Wrapping has failed. Find the first space or newline */
461 while( i_count < i_len && *psz_parser != ' ' && *psz_parser != '\n' )
465 while( *((unsigned char *)psz_parser) >= 0x80UL ) psz_parser++;
470 if( i_count < i_len ) *psz_parser = '\n';
471 i_len -= (i_count + 1);
472 psz_line = psz_parser + 1;
478 /*****************************************************************************
480 *****************************************************************************/
481 vlc_iconv_t vlc_iconv_open( const char *tocode, const char *fromcode )
483 #if defined(HAVE_ICONV)
484 return iconv_open( tocode, fromcode );
490 size_t vlc_iconv( vlc_iconv_t cd, char **inbuf, size_t *inbytesleft,
491 char **outbuf, size_t *outbytesleft )
493 #if defined(HAVE_ICONV)
494 return iconv( cd, inbuf, inbytesleft, outbuf, outbytesleft );
496 int i_bytes = __MIN(*inbytesleft, *outbytesleft);
497 if( !inbuf || !outbuf || !i_bytes ) return (size_t)(-1);
498 memcpy( *outbuf, *inbuf, i_bytes );
501 inbytesleft -= i_bytes;
502 outbytesleft -= i_bytes;
507 int vlc_iconv_close( vlc_iconv_t cd )
509 #if defined(HAVE_ICONV)
510 return iconv_close( cd );
516 /*****************************************************************************
518 * (adapted from libavcodec, author Michael Niedermayer <michaelni@gmx.at>)
519 *****************************************************************************/
520 vlc_bool_t vlc_ureduce( unsigned *pi_dst_nom, unsigned *pi_dst_den,
521 uint64_t i_nom, uint64_t i_den, uint64_t i_max )
523 vlc_bool_t b_exact = 1;
533 i_gcd = GCD( i_nom, i_den );
537 if( i_max == 0 ) i_max = I64C(0xFFFFFFFF);
539 if( i_nom > i_max || i_den > i_max )
541 uint64_t i_a0_num = 0, i_a0_den = 1, i_a1_num = 1, i_a1_den = 0;
546 uint64_t i_x = i_nom / i_den;
547 uint64_t i_a2n = i_x * i_a1_num + i_a0_num;
548 uint64_t i_a2d = i_x * i_a1_den + i_a0_den;
550 if( i_a2n > i_max || i_a2d > i_max ) break;
554 i_a0_num = i_a1_num; i_a0_den = i_a1_den;
555 i_a1_num = i_a2n; i_a1_den = i_a2d;
556 if( i_nom == 0 ) break;
557 i_x = i_nom; i_nom = i_den; i_den = i_x;
569 /*************************************************************************
570 * vlc_parse_cmdline: Command line parsing into elements.
572 * The command line is composed of space/tab separated arguments.
573 * Quotes can be used as argument delimiters and a backslash can be used to
575 *************************************************************************/
576 static void find_end_quote( char **s, char **ppsz_parser, int i_quote )
585 (*ppsz_parser)++; (*s)++;
588 else if( **s == '"' || **s == '\'' )
590 /* Preceeded by a number of '\' which we erase. */
591 *ppsz_parser -= i_bcount / 2;
594 /* '\\' followed by a '"' or '\'' */
597 (*ppsz_parser)++; (*s)++;
609 /* Different quoting */
612 (*ppsz_parser)++; (*s)++;
613 find_end_quote( s, ppsz_parser, i_quote );
615 (*ppsz_parser)++; (*s)++;
622 /* A regular character */
624 (*ppsz_parser)++; (*s)++;
630 char **vlc_parse_cmdline( const char *psz_cmdline, int *i_args )
634 char *s, *psz_parser, *psz_arg, *psz_orig;
637 if( !psz_cmdline ) return 0;
638 psz_orig = strdup( psz_cmdline );
639 psz_arg = psz_parser = s = psz_orig;
643 if( *s == '\t' || *s == ' ' )
645 /* We have a complete argument */
647 TAB_APPEND( argc, argv, strdup(psz_arg) );
649 /* Skip trailing spaces/tabs */
650 do{ s++; } while( *s == '\t' || *s == ' ' );
653 psz_arg = psz_parser = s;
656 else if( *s == '\\' )
658 *psz_parser++ = *s++;
661 else if( *s == '"' || *s == '\'' )
663 if( ( i_bcount & 1 ) == 0 )
665 /* Preceeded by an even number of '\', this is half that
666 * number of '\', plus a quote which we erase. */
668 psz_parser -= i_bcount / 2;
670 find_end_quote( &s, &psz_parser, i_quote );
675 /* Preceeded by an odd number of '\', this is half that
676 * number of '\' followed by a '"' */
677 psz_parser = psz_parser - i_bcount/2 - 1;
685 /* A regular character */
686 *psz_parser++ = *s++;
691 /* Take care of the last arg */
695 TAB_APPEND( argc, argv, strdup(psz_arg) );
698 if( i_args ) *i_args = argc;