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)
41 /*****************************************************************************
42 * getenv: just in case, but it should never be called
43 *****************************************************************************/
44 #if !defined( HAVE_GETENV )
45 char *vlc_getenv( const char *name )
51 /*****************************************************************************
52 * strdup: returns a malloc'd copy of a string
53 *****************************************************************************/
54 #if !defined( HAVE_STRDUP )
55 char *vlc_strdup( const char *string )
57 return strndup( string, strlen( string ) );
61 /*****************************************************************************
62 * strndup: returns a malloc'd copy of at most n bytes of string
63 * Does anyone know whether or not it will be present in Jaguar?
64 *****************************************************************************/
65 #if !defined( HAVE_STRNDUP )
66 char *vlc_strndup( const char *string, size_t n )
69 size_t len = strlen( string );
71 len = __MIN( len, n );
72 psz = (char*)malloc( len + 1 );
76 memcpy( (void*)psz, (const void*)string, len );
84 /*****************************************************************************
85 * strcasecmp: compare two strings ignoring case
86 *****************************************************************************/
87 #if !defined( HAVE_STRCASECMP ) && !defined( HAVE_STRICMP )
88 int vlc_strcasecmp( const char *s1, const char *s2 )
91 if( !s1 || !s2 ) return -1;
98 if( c1 != c2 ) return (c1 < c2 ? -1 : 1);
102 if( !*s1 && !*s2 ) return 0;
103 else return (*s1 ? 1 : -1);
107 /*****************************************************************************
108 * strncasecmp: compare n chars from two strings ignoring case
109 *****************************************************************************/
110 #if !defined( HAVE_STRNCASECMP ) && !defined( HAVE_STRNICMP )
111 int vlc_strncasecmp( const char *s1, const char *s2, size_t n )
114 if( !s1 || !s2 ) return -1;
116 while( n > 0 && *s1 && *s2 )
121 if( c1 != c2 ) return (c1 < c2 ? -1 : 1);
125 if( !n || (!*s1 && !*s2) ) return 0;
126 else return (*s1 ? 1 : -1);
130 /******************************************************************************
131 * strcasestr: find a substring (little) in another substring (big)
132 * Case sensitive. Return NULL if not found, return big if little == null
133 *****************************************************************************/
134 #if !defined( HAVE_STRCASESTR ) && !defined( HAVE_STRISTR )
135 char * vlc_strcasestr( const char *psz_big, const char *psz_little )
137 char *p_pos = (char *)psz_big;
139 if( !psz_big || !psz_little || !*psz_little ) return p_pos;
143 if( toupper( *p_pos ) == toupper( *psz_little ) )
145 char * psz_cur1 = p_pos + 1;
146 char * psz_cur2 = (char *)psz_little + 1;
147 while( *psz_cur1 && *psz_cur2 &&
148 toupper( *psz_cur1 ) == toupper( *psz_cur2 ) )
153 if( !*psz_cur2 ) return p_pos;
161 /*****************************************************************************
163 *****************************************************************************/
164 #if !defined(HAVE_VASPRINTF) || defined(SYS_DARWIN) || defined(SYS_BEOS)
165 int vlc_vasprintf(char **strp, const char *fmt, va_list ap)
167 /* Guess we need no more than 100 bytes. */
169 char *p = malloc( i_size );
180 /* Try to print in the allocated space. */
181 n = vsnprintf( p, i_size, fmt, ap );
183 /* If that worked, return the string. */
184 if (n > -1 && n < i_size)
189 /* Else try again with more space. */
190 if (n > -1) /* glibc 2.1 */
192 i_size = n+1; /* precisely what is needed */
196 i_size *= 2; /* twice the old size */
198 if( (p = realloc( p, i_size ) ) == NULL)
207 /*****************************************************************************
209 *****************************************************************************/
210 #if !defined(HAVE_ASPRINTF) || defined(SYS_DARWIN) || defined(SYS_BEOS)
211 int vlc_asprintf( char **strp, const char *fmt, ... )
216 va_start( args, fmt );
217 i_ret = vasprintf( strp, fmt, args );
224 /*****************************************************************************
225 * atof: convert a string to a double.
226 *****************************************************************************/
227 #if !defined( HAVE_ATOF )
228 double vlc_atof( const char *nptr )
232 int i_len = strlen( nptr ) + 1;
234 psz_tmp = malloc( i_len * sizeof(wchar_t) );
235 MultiByteToWideChar( CP_ACP, 0, nptr, -1, psz_tmp, i_len );
236 f_result = wcstod( psz_tmp, NULL );
243 /*****************************************************************************
244 * strtoll: convert a string to a 64 bits int.
245 *****************************************************************************/
246 #if !defined( HAVE_STRTOLL )
247 int64_t vlc_strtoll( const char *nptr, char **endptr, int base )
250 int sign = 1, newbase = base ? base : 10;
252 while( isspace(*nptr) ) nptr++;
260 /* Try to detect base */
273 if( base && newbase != base )
275 if( endptr ) *endptr = (char *)nptr;
282 while( *nptr >= '0' && *nptr <= '9' )
285 i_value += ( *nptr++ - '0' );
287 if( endptr ) *endptr = (char *)nptr;
291 while( (*nptr >= '0' && *nptr <= '9') ||
292 (*nptr >= 'a' && *nptr <= 'f') ||
293 (*nptr >= 'A' && *nptr <= 'F') )
296 if(*nptr >= '0' && *nptr <= '9') i_valc = *nptr - '0';
297 else if(*nptr >= 'a' && *nptr <= 'f') i_valc = *nptr - 'a' +10;
298 else if(*nptr >= 'A' && *nptr <= 'F') i_valc = *nptr - 'A' +10;
303 if( endptr ) *endptr = (char *)nptr;
307 i_value = strtol( nptr, endptr, newbase );
311 return i_value * sign;
315 /*****************************************************************************
316 * atoll: convert a string to a 64 bits int.
317 *****************************************************************************/
318 #if !defined( HAVE_ATOLL )
319 int64_t vlc_atoll( const char *nptr )
321 return strtoll( nptr, (char **)NULL, 10 );
325 /*****************************************************************************
326 * scandir: scan a directory alpha-sorted
327 *****************************************************************************/
328 #if !defined( HAVE_SCANDIR )
329 int vlc_alphasort( const struct dirent **a, const struct dirent **b )
331 return strcoll( (*a)->d_name, (*b)->d_name );
334 int vlc_scandir( const char *name, struct dirent ***namelist,
335 int (*filter) ( const struct dirent * ),
336 int (*compar) ( const struct dirent **,
337 const struct dirent ** ) )
340 struct dirent * p_content;
341 struct dirent ** pp_list;
344 if( !namelist || !( p_dir = opendir( name ) ) ) return -1;
348 while( ( p_content = readdir( p_dir ) ) )
350 if( filter && !filter( p_content ) )
354 pp_list = realloc( pp_list, ( ret + 1 ) * sizeof( struct dirent * ) );
355 size = sizeof( struct dirent ) + strlen( p_content->d_name ) + 1;
356 pp_list[ret] = malloc( size );
357 memcpy( pp_list[ret], p_content, size );
365 qsort( pp_list, ret, sizeof( struct dirent * ),
366 (int (*)(const void *, const void *)) compar );
374 /*****************************************************************************
375 * dgettext: gettext for plugins.
376 *****************************************************************************/
377 char *vlc_dgettext( const char *package, const char *msgid )
379 #if defined( ENABLE_NLS ) \
380 && ( defined(HAVE_GETTEXT) || defined(HAVE_INCLUDED_GETTEXT) )
381 return dgettext( package, msgid );
383 return (char *)msgid;
387 /*****************************************************************************
388 * count_utf8_string: returns the number of characters in the string.
389 *****************************************************************************/
390 static int count_utf8_string( const char *psz_string )
392 int i = 0, i_count = 0;
393 while( psz_string[ i ] != 0 )
395 if( ((unsigned char *)psz_string)[ i ] < 0x80UL ) i_count++;
401 /*****************************************************************************
402 * wraptext: inserts \n at convenient places to wrap the text.
403 * Returns the modified string in a new buffer.
404 *****************************************************************************/
405 char *vlc_wraptext( const char *psz_text, int i_line, vlc_bool_t b_utf8 )
408 char *psz_line, *psz_new_text;
410 psz_line = psz_new_text = strdup( psz_text );
413 i_len = count_utf8_string( psz_text );
415 i_len = strlen( psz_text );
417 while( i_len > i_line )
419 /* Look if there is a newline somewhere. */
420 char *psz_parser = psz_line;
422 while( i_count <= i_line && *psz_parser != '\n' )
426 while( *((unsigned char *)psz_parser) >= 0x80UL ) psz_parser++;
431 if( *psz_parser == '\n' )
433 i_len -= (i_count + 1);
434 psz_line = psz_parser + 1;
438 /* Find the furthest space. */
439 while( psz_parser > psz_line && *psz_parser != ' ' )
443 while( *((unsigned char *)psz_parser) >= 0x80UL ) psz_parser--;
448 if( *psz_parser == ' ' )
451 i_len -= (i_count + 1);
452 psz_line = psz_parser + 1;
456 /* Wrapping has failed. Find the first space or newline */
457 while( i_count < i_len && *psz_parser != ' ' && *psz_parser != '\n' )
461 while( *((unsigned char *)psz_parser) >= 0x80UL ) psz_parser++;
466 if( i_count < i_len ) *psz_parser = '\n';
467 i_len -= (i_count + 1);
468 psz_line = psz_parser + 1;
474 /*****************************************************************************
476 *****************************************************************************/
477 vlc_iconv_t vlc_iconv_open( const char *tocode, const char *fromcode )
479 #if defined(HAVE_ICONV)
480 return iconv_open( tocode, fromcode );
486 size_t vlc_iconv( vlc_iconv_t cd, char **inbuf, size_t *inbytesleft,
487 char **outbuf, size_t *outbytesleft )
489 #if defined(HAVE_ICONV)
490 return iconv( cd, inbuf, inbytesleft, outbuf, outbytesleft );
492 int i_bytes = __MIN(*inbytesleft, *outbytesleft);
493 if( !inbuf || !outbuf || !i_bytes ) return (size_t)(-1);
494 memcpy( *outbuf, *inbuf, i_bytes );
497 inbytesleft -= i_bytes;
498 outbytesleft -= i_bytes;
503 int vlc_iconv_close( vlc_iconv_t cd )
505 #if defined(HAVE_ICONV)
506 return iconv_close( cd );
512 /*****************************************************************************
514 * (adapted from libavcodec, author Michael Niedermayer <michaelni@gmx.at>)
515 *****************************************************************************/
516 vlc_bool_t vlc_ureduce( unsigned *pi_dst_nom, unsigned *pi_dst_den,
517 uint64_t i_nom, uint64_t i_den, uint64_t i_max )
519 vlc_bool_t b_exact = 1;
529 i_gcd = GCD( i_nom, i_den );
533 if( i_max == 0 ) i_max = I64C(0xFFFFFFFF);
535 if( i_nom > i_max || i_den > i_max )
537 uint64_t i_a0_num = 0, i_a0_den = 1, i_a1_num = 1, i_a1_den = 0;
542 uint64_t i_x = i_nom / i_den;
543 uint64_t i_a2n = i_x * i_a1_num + i_a0_num;
544 uint64_t i_a2d = i_x * i_a1_den + i_a0_den;
546 if( i_a2n > i_max || i_a2d > i_max ) break;
550 i_a0_num = i_a1_num; i_a0_den = i_a1_den;
551 i_a1_num = i_a2n; i_a1_den = i_a2d;
552 if( i_nom == 0 ) break;
553 i_x = i_nom; i_nom = i_den; i_den = i_x;
565 /*************************************************************************
566 * vlc_parse_cmdline: Command line parsing into elements.
568 * The command line is composed of space/tab separated arguments.
569 * Quotes can be used as argument delimiters and a backslash can be used to
571 *************************************************************************/
572 static void find_end_quote( char **s, char **ppsz_parser, int i_quote )
581 (*ppsz_parser)++; (*s)++;
584 else if( **s == '"' || **s == '\'' )
586 /* Preceeded by a number of '\' which we erase. */
587 *ppsz_parser -= i_bcount / 2;
590 /* '\\' followed by a '"' or '\'' */
593 (*ppsz_parser)++; (*s)++;
605 /* Different quoting */
608 (*ppsz_parser)++; (*s)++;
609 find_end_quote( s, ppsz_parser, i_quote );
611 (*ppsz_parser)++; (*s)++;
618 /* A regular character */
620 (*ppsz_parser)++; (*s)++;
626 char **vlc_parse_cmdline( const char *psz_cmdline, int *i_args )
630 char *s, *psz_parser, *psz_arg, *psz_orig;
633 if( !psz_cmdline ) return 0;
634 psz_orig = strdup( psz_cmdline );
635 psz_arg = psz_parser = s = psz_orig;
639 if( *s == '\t' || *s == ' ' )
641 /* We have a complete argument */
643 TAB_APPEND( argc, argv, strdup(psz_arg) );
645 /* Skip trailing spaces/tabs */
646 do{ s++; } while( *s == '\t' || *s == ' ' );
649 psz_arg = psz_parser = s;
652 else if( *s == '\\' )
654 *psz_parser++ = *s++;
657 else if( *s == '"' || *s == '\'' )
659 if( ( i_bcount & 1 ) == 0 )
661 /* Preceeded by an even number of '\', this is half that
662 * number of '\', plus a quote which we erase. */
664 psz_parser -= i_bcount / 2;
666 find_end_quote( &s, &psz_parser, i_quote );
671 /* Preceeded by an odd number of '\', this is half that
672 * number of '\' followed by a '"' */
673 psz_parser = psz_parser - i_bcount/2 - 1;
681 /* A regular character */
682 *psz_parser++ = *s++;
687 /* Take care of the last arg */
691 TAB_APPEND( argc, argv, strdup(psz_arg) );
694 if( i_args ) *i_args = argc;