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>
11 * Christophe Massiot <massiot@via.ecp.fr>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
26 *****************************************************************************/
27 #include <string.h> /* strdup() */
38 #if defined(HAVE_ICONV)
46 /*****************************************************************************
47 * getenv: just in case, but it should never be called
48 *****************************************************************************/
49 #if !defined( HAVE_GETENV )
50 char *vlc_getenv( const char *name )
56 /*****************************************************************************
57 * strdup: returns a malloc'd copy of a string
58 *****************************************************************************/
59 #if !defined( HAVE_STRDUP )
60 char *vlc_strdup( const char *string )
62 return strndup( string, strlen( string ) );
66 /*****************************************************************************
67 * strndup: returns a malloc'd copy of at most n bytes of string
68 * Does anyone know whether or not it will be present in Jaguar?
69 *****************************************************************************/
70 #if !defined( HAVE_STRNDUP )
71 char *vlc_strndup( const char *string, size_t n )
74 size_t len = strlen( string );
76 len = __MIN( len, n );
77 psz = (char*)malloc( len + 1 );
81 memcpy( (void*)psz, (const void*)string, len );
89 /*****************************************************************************
90 * strcasecmp: compare two strings ignoring case
91 *****************************************************************************/
92 #if !defined( HAVE_STRCASECMP ) && !defined( HAVE_STRICMP )
93 int vlc_strcasecmp( const char *s1, const char *s2 )
96 if( !s1 || !s2 ) return -1;
103 if( c1 != c2 ) return (c1 < c2 ? -1 : 1);
107 if( !*s1 && !*s2 ) return 0;
108 else return (*s1 ? 1 : -1);
112 /*****************************************************************************
113 * strncasecmp: compare n chars from two strings ignoring case
114 *****************************************************************************/
115 #if !defined( HAVE_STRNCASECMP ) && !defined( HAVE_STRNICMP )
116 int vlc_strncasecmp( const char *s1, const char *s2, size_t n )
119 if( !s1 || !s2 ) return -1;
121 while( n > 0 && *s1 && *s2 )
126 if( c1 != c2 ) return (c1 < c2 ? -1 : 1);
130 if( !n || (!*s1 && !*s2) ) return 0;
131 else return (*s1 ? 1 : -1);
135 /******************************************************************************
136 * strcasestr: find a substring (little) in another substring (big)
137 * Case sensitive. Return NULL if not found, return big if little == null
138 *****************************************************************************/
139 #if !defined( HAVE_STRCASESTR ) && !defined( HAVE_STRISTR )
140 char * vlc_strcasestr( const char *psz_big, const char *psz_little )
142 char *p_pos = (char *)psz_big;
144 if( !psz_big || !psz_little || !*psz_little ) return p_pos;
148 if( toupper( *p_pos ) == toupper( *psz_little ) )
150 char * psz_cur1 = p_pos + 1;
151 char * psz_cur2 = (char *)psz_little + 1;
152 while( *psz_cur1 && *psz_cur2 &&
153 toupper( *psz_cur1 ) == toupper( *psz_cur2 ) )
158 if( !*psz_cur2 ) return p_pos;
166 /*****************************************************************************
168 *****************************************************************************/
169 #if !defined(HAVE_VASPRINTF) || defined(SYS_DARWIN) || defined(SYS_BEOS)
170 int vlc_vasprintf(char **strp, const char *fmt, va_list ap)
172 /* Guess we need no more than 100 bytes. */
174 char *p = malloc( i_size );
185 /* Try to print in the allocated space. */
186 n = vsnprintf( p, i_size, fmt, ap );
188 /* If that worked, return the string. */
189 if (n > -1 && n < i_size)
194 /* Else try again with more space. */
195 if (n > -1) /* glibc 2.1 */
197 i_size = n+1; /* precisely what is needed */
201 i_size *= 2; /* twice the old size */
203 if( (p = realloc( p, i_size ) ) == NULL)
212 /*****************************************************************************
214 *****************************************************************************/
215 #if !defined(HAVE_ASPRINTF) || defined(SYS_DARWIN) || defined(SYS_BEOS)
216 int vlc_asprintf( char **strp, const char *fmt, ... )
221 va_start( args, fmt );
222 i_ret = vasprintf( strp, fmt, args );
229 /*****************************************************************************
230 * atof: convert a string to a double.
231 *****************************************************************************/
232 #if !defined( HAVE_ATOF )
233 double vlc_atof( const char *nptr )
237 int i_len = strlen( nptr ) + 1;
239 psz_tmp = malloc( i_len * sizeof(wchar_t) );
240 MultiByteToWideChar( CP_ACP, 0, nptr, -1, psz_tmp, i_len );
241 f_result = wcstod( psz_tmp, NULL );
248 /*****************************************************************************
249 * strtoll: convert a string to a 64 bits int.
250 *****************************************************************************/
251 #if !defined( HAVE_STRTOLL )
252 int64_t vlc_strtoll( const char *nptr, char **endptr, int base )
255 int sign = 1, newbase = base ? base : 10;
257 while( isspace(*nptr) ) nptr++;
265 /* Try to detect base */
278 if( base && newbase != base )
280 if( endptr ) *endptr = (char *)nptr;
287 while( *nptr >= '0' && *nptr <= '9' )
290 i_value += ( *nptr++ - '0' );
292 if( endptr ) *endptr = (char *)nptr;
296 while( (*nptr >= '0' && *nptr <= '9') ||
297 (*nptr >= 'a' && *nptr <= 'f') ||
298 (*nptr >= 'A' && *nptr <= 'F') )
301 if(*nptr >= '0' && *nptr <= '9') i_valc = *nptr - '0';
302 else if(*nptr >= 'a' && *nptr <= 'f') i_valc = *nptr - 'a' +10;
303 else if(*nptr >= 'A' && *nptr <= 'F') i_valc = *nptr - 'A' +10;
308 if( endptr ) *endptr = (char *)nptr;
312 i_value = strtol( nptr, endptr, newbase );
316 return i_value * sign;
320 /*****************************************************************************
321 * atoll: convert a string to a 64 bits int.
322 *****************************************************************************/
323 #if !defined( HAVE_ATOLL )
324 int64_t vlc_atoll( const char *nptr )
326 return strtoll( nptr, (char **)NULL, 10 );
330 /*****************************************************************************
331 * vlc_*dir_wrapper: wrapper under Windows to return the list of drive letters
332 * when called with an empty argument or just '\'
333 *****************************************************************************/
334 #if defined(WIN32) || defined(UNDER_CE)
336 #define WIN32_LEAN_AND_MEAN
337 #include <windows.h> /* for GetLogicalDrives */
339 typedef struct vlc_DIR
343 struct dirent dd_dir;
344 vlc_bool_t b_insert_back;
347 void *vlc_opendir_wrapper( const char *psz_path )
352 if ( psz_path == NULL || psz_path[0] == '\0'
353 || (psz_path[0] == '\\' && psz_path[1] == '\0') )
355 /* Special mode to list drive letters */
356 p_dir = malloc( sizeof(vlc_DIR) );
357 p_dir->p_real_dir = NULL;
358 p_dir->i_drives = GetLogicalDrives();
359 return (void *)p_dir;
362 p_real_dir = opendir( psz_path );
363 if ( p_real_dir == NULL )
366 p_dir = malloc( sizeof(vlc_DIR) );
367 p_dir->p_real_dir = p_real_dir;
368 p_dir->b_insert_back = ( psz_path[1] == ':' && psz_path[2] == '\\'
369 && psz_path[3] =='\0' );
370 return (void *)p_dir;
373 struct dirent *vlc_readdir_wrapper( void *_p_dir )
375 vlc_DIR *p_dir = (vlc_DIR *)_p_dir;
379 if ( p_dir->p_real_dir != NULL )
381 if ( p_dir->b_insert_back )
383 p_dir->dd_dir.d_ino = 0;
384 p_dir->dd_dir.d_reclen = 0;
385 p_dir->dd_dir.d_namlen = 2;
386 strcpy( p_dir->dd_dir.d_name, ".." );
387 p_dir->b_insert_back = VLC_FALSE;
388 return &p_dir->dd_dir;
391 return readdir( p_dir->p_real_dir );
394 /* Drive letters mode */
395 i_drives = p_dir->i_drives;
397 return NULL; /* end */
399 for ( i = 0; i < sizeof(DWORD)*8; i++, i_drives >>= 1 )
400 if ( i_drives & 1 ) break;
403 return NULL; /* this should not happen */
405 sprintf( p_dir->dd_dir.d_name, "%c:\\", 'A' + i );
406 p_dir->dd_dir.d_namlen = strlen(p_dir->dd_dir.d_name);
407 p_dir->i_drives &= ~(1UL << i);
408 return &p_dir->dd_dir;
411 int vlc_closedir_wrapper( void *_p_dir )
413 vlc_DIR *p_dir = (vlc_DIR *)_p_dir;
415 if ( p_dir->p_real_dir != NULL )
417 int i_ret = closedir( p_dir->p_real_dir );
426 void *vlc_opendir_wrapper( const char *psz_path )
428 return (void *)opendir( psz_path );
430 struct dirent *vlc_readdir_wrapper( void *_p_dir )
432 return readdir( (DIR *)_p_dir );
434 int vlc_closedir_wrapper( void *_p_dir )
436 return closedir( (DIR *)_p_dir );
440 /*****************************************************************************
441 * scandir: scan a directory alpha-sorted
442 *****************************************************************************/
443 #if !defined( HAVE_SCANDIR )
444 int vlc_alphasort( const struct dirent **a, const struct dirent **b )
446 return strcoll( (*a)->d_name, (*b)->d_name );
449 int vlc_scandir( const char *name, struct dirent ***namelist,
450 int (*filter) ( const struct dirent * ),
451 int (*compar) ( const struct dirent **,
452 const struct dirent ** ) )
455 struct dirent * p_content;
456 struct dirent ** pp_list;
459 if( !namelist || !( p_dir = vlc_opendir_wrapper( name ) ) ) return -1;
463 while( ( p_content = vlc_readdir_wrapper( p_dir ) ) )
465 if( filter && !filter( p_content ) )
469 pp_list = realloc( pp_list, ( ret + 1 ) * sizeof( struct dirent * ) );
470 size = sizeof( struct dirent ) + strlen( p_content->d_name ) + 1;
471 pp_list[ret] = malloc( size );
472 memcpy( pp_list[ret], p_content, size );
476 vlc_closedir_wrapper( p_dir );
480 qsort( pp_list, ret, sizeof( struct dirent * ),
481 (int (*)(const void *, const void *)) compar );
489 /*****************************************************************************
490 * dgettext: gettext for plugins.
491 *****************************************************************************/
492 char *vlc_dgettext( const char *package, const char *msgid )
494 #if defined( ENABLE_NLS ) \
495 && ( defined(HAVE_GETTEXT) || defined(HAVE_INCLUDED_GETTEXT) )
496 return dgettext( package, msgid );
498 return (char *)msgid;
502 /*****************************************************************************
503 * count_utf8_string: returns the number of characters in the string.
504 *****************************************************************************/
505 static int count_utf8_string( const char *psz_string )
507 int i = 0, i_count = 0;
508 while( psz_string[ i ] != 0 )
510 if( ((unsigned char *)psz_string)[ i ] < 0x80UL ) i_count++;
516 /*****************************************************************************
517 * wraptext: inserts \n at convenient places to wrap the text.
518 * Returns the modified string in a new buffer.
519 *****************************************************************************/
520 char *vlc_wraptext( const char *psz_text, int i_line, vlc_bool_t b_utf8 )
523 char *psz_line, *psz_new_text;
525 psz_line = psz_new_text = strdup( psz_text );
528 i_len = count_utf8_string( psz_text );
530 i_len = strlen( psz_text );
532 while( i_len > i_line )
534 /* Look if there is a newline somewhere. */
535 char *psz_parser = psz_line;
537 while( i_count <= i_line && *psz_parser != '\n' )
541 while( *((unsigned char *)psz_parser) >= 0x80UL ) psz_parser++;
546 if( *psz_parser == '\n' )
548 i_len -= (i_count + 1);
549 psz_line = psz_parser + 1;
553 /* Find the furthest space. */
554 while( psz_parser > psz_line && *psz_parser != ' ' )
558 while( *((unsigned char *)psz_parser) >= 0x80UL ) psz_parser--;
563 if( *psz_parser == ' ' )
566 i_len -= (i_count + 1);
567 psz_line = psz_parser + 1;
571 /* Wrapping has failed. Find the first space or newline */
572 while( i_count < i_len && *psz_parser != ' ' && *psz_parser != '\n' )
576 while( *((unsigned char *)psz_parser) >= 0x80UL ) psz_parser++;
581 if( i_count < i_len ) *psz_parser = '\n';
582 i_len -= (i_count + 1);
583 psz_line = psz_parser + 1;
589 /*****************************************************************************
591 *****************************************************************************/
592 vlc_iconv_t vlc_iconv_open( const char *tocode, const char *fromcode )
594 #if defined(HAVE_ICONV)
595 return iconv_open( tocode, fromcode );
601 size_t vlc_iconv( vlc_iconv_t cd, char **inbuf, size_t *inbytesleft,
602 char **outbuf, size_t *outbytesleft )
604 #if defined(HAVE_ICONV)
605 return iconv( cd, inbuf, inbytesleft, outbuf, outbytesleft );
607 int i_bytes = __MIN(*inbytesleft, *outbytesleft);
608 if( !inbuf || !outbuf || !i_bytes ) return (size_t)(-1);
609 memcpy( *outbuf, *inbuf, i_bytes );
612 inbytesleft -= i_bytes;
613 outbytesleft -= i_bytes;
618 int vlc_iconv_close( vlc_iconv_t cd )
620 #if defined(HAVE_ICONV)
621 return iconv_close( cd );
627 /*****************************************************************************
629 * (adapted from libavcodec, author Michael Niedermayer <michaelni@gmx.at>)
630 *****************************************************************************/
631 vlc_bool_t vlc_ureduce( unsigned *pi_dst_nom, unsigned *pi_dst_den,
632 uint64_t i_nom, uint64_t i_den, uint64_t i_max )
634 vlc_bool_t b_exact = 1;
644 i_gcd = GCD( i_nom, i_den );
648 if( i_max == 0 ) i_max = I64C(0xFFFFFFFF);
650 if( i_nom > i_max || i_den > i_max )
652 uint64_t i_a0_num = 0, i_a0_den = 1, i_a1_num = 1, i_a1_den = 0;
657 uint64_t i_x = i_nom / i_den;
658 uint64_t i_a2n = i_x * i_a1_num + i_a0_num;
659 uint64_t i_a2d = i_x * i_a1_den + i_a0_den;
661 if( i_a2n > i_max || i_a2d > i_max ) break;
665 i_a0_num = i_a1_num; i_a0_den = i_a1_den;
666 i_a1_num = i_a2n; i_a1_den = i_a2d;
667 if( i_nom == 0 ) break;
668 i_x = i_nom; i_nom = i_den; i_den = i_x;
680 /*************************************************************************
681 * vlc_parse_cmdline: Command line parsing into elements.
683 * The command line is composed of space/tab separated arguments.
684 * Quotes can be used as argument delimiters and a backslash can be used to
686 *************************************************************************/
687 static void find_end_quote( char **s, char **ppsz_parser, int i_quote )
696 (*ppsz_parser)++; (*s)++;
699 else if( **s == '"' || **s == '\'' )
701 /* Preceeded by a number of '\' which we erase. */
702 *ppsz_parser -= i_bcount / 2;
705 /* '\\' followed by a '"' or '\'' */
708 (*ppsz_parser)++; (*s)++;
720 /* Different quoting */
723 (*ppsz_parser)++; (*s)++;
724 find_end_quote( s, ppsz_parser, i_quote );
726 (*ppsz_parser)++; (*s)++;
733 /* A regular character */
735 (*ppsz_parser)++; (*s)++;
741 char **vlc_parse_cmdline( const char *psz_cmdline, int *i_args )
745 char *s, *psz_parser, *psz_arg, *psz_orig;
748 if( !psz_cmdline ) return 0;
749 psz_orig = strdup( psz_cmdline );
750 psz_arg = psz_parser = s = psz_orig;
754 if( *s == '\t' || *s == ' ' )
756 /* We have a complete argument */
758 TAB_APPEND( argc, argv, strdup(psz_arg) );
760 /* Skip trailing spaces/tabs */
761 do{ s++; } while( *s == '\t' || *s == ' ' );
764 psz_arg = psz_parser = s;
767 else if( *s == '\\' )
769 *psz_parser++ = *s++;
772 else if( *s == '"' || *s == '\'' )
774 if( ( i_bcount & 1 ) == 0 )
776 /* Preceeded by an even number of '\', this is half that
777 * number of '\', plus a quote which we erase. */
779 psz_parser -= i_bcount / 2;
781 find_end_quote( &s, &psz_parser, i_quote );
786 /* Preceeded by an odd number of '\', this is half that
787 * number of '\' followed by a '"' */
788 psz_parser = psz_parser - i_bcount/2 - 1;
796 /* A regular character */
797 *psz_parser++ = *s++;
802 /* Take care of the last arg */
806 TAB_APPEND( argc, argv, strdup(psz_arg) );
809 if( i_args ) *i_args = argc;