]> git.sesse.net Git - vlc/blob - src/extras/libc.c
* src/extras/libc.c: Fixed compilation on systems without scandir().
[vlc] / src / extras / libc.c
1 /*****************************************************************************
2  * libc.c: Extra libc function for some systems.
3  *****************************************************************************
4  * Copyright (C) 2002 the VideoLAN team
5  * $Id$
6  *
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  *
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.
16  *
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.
21  *
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() */
27 #include <stdlib.h>
28 #include <ctype.h>
29
30 #include <vlc/vlc.h>
31
32 #undef iconv_t
33 #undef iconv_open
34 #undef iconv
35 #undef iconv_close
36
37 #if defined(HAVE_ICONV)
38 #   include <iconv.h>
39 #endif
40
41 #ifdef HAVE_DIRENT_H
42 #   include <dirent.h>
43 #endif
44
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 )
50 {
51     return NULL;
52 }
53 #endif
54
55 /*****************************************************************************
56  * strdup: returns a malloc'd copy of a string
57  *****************************************************************************/
58 #if !defined( HAVE_STRDUP )
59 char *vlc_strdup( const char *string )
60 {
61     return strndup( string, strlen( string ) );
62 }
63 #endif
64
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 )
71 {
72     char *psz;
73     size_t len = strlen( string );
74
75     len = __MIN( len, n );
76     psz = (char*)malloc( len + 1 );
77
78     if( psz != NULL )
79     {
80         memcpy( (void*)psz, (const void*)string, len );
81         psz[ len ] = 0;
82     }
83
84     return psz;
85 }
86 #endif
87
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 )
93 {
94     int c1, c2;
95     if( !s1 || !s2 ) return  -1;
96
97     while( *s1 && *s2 )
98     {
99         c1 = tolower(*s1);
100         c2 = tolower(*s2);
101
102         if( c1 != c2 ) return (c1 < c2 ? -1 : 1);
103         s1++; s2++;
104     }
105
106     if( !*s1 && !*s2 ) return 0;
107     else return (*s1 ? 1 : -1);
108 }
109 #endif
110
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 )
116 {
117     int c1, c2;
118     if( !s1 || !s2 ) return  -1;
119
120     while( n > 0 && *s1 && *s2 )
121     {
122         c1 = tolower(*s1);
123         c2 = tolower(*s2);
124
125         if( c1 != c2 ) return (c1 < c2 ? -1 : 1);
126         s1++; s2++; n--;
127     }
128
129     if( !n || (!*s1 && !*s2) ) return 0;
130     else return (*s1 ? 1 : -1);
131 }
132 #endif
133
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 )
140 {
141     char *p_pos = (char *)psz_big;
142
143     if( !psz_big || !psz_little || !*psz_little ) return p_pos;
144  
145     while( *p_pos ) 
146     {
147         if( toupper( *p_pos ) == toupper( *psz_little ) )
148         {
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 ) )
153             {
154                 psz_cur1++;
155                 psz_cur2++;
156             }
157             if( !*psz_cur2 ) return p_pos;
158         }
159         p_pos++;
160     }
161     return NULL;
162 }
163 #endif
164
165 /*****************************************************************************
166  * vasprintf:
167  *****************************************************************************/
168 #if !defined(HAVE_VASPRINTF) || defined(SYS_DARWIN) || defined(SYS_BEOS)
169 int vlc_vasprintf(char **strp, const char *fmt, va_list ap)
170 {
171     /* Guess we need no more than 100 bytes. */
172     int     i_size = 100;
173     char    *p = malloc( i_size );
174     int     n;
175
176     if( p == NULL )
177     {
178         *strp = NULL;
179         return -1;
180     }
181
182     for( ;; )
183     {
184         /* Try to print in the allocated space. */
185         n = vsnprintf( p, i_size, fmt, ap );
186
187         /* If that worked, return the string. */
188         if (n > -1 && n < i_size)
189         {
190             *strp = p;
191             return strlen( p );
192         }
193         /* Else try again with more space. */
194         if (n > -1)    /* glibc 2.1 */
195         {
196            i_size = n+1; /* precisely what is needed */
197         }
198         else           /* glibc 2.0 */
199         {
200            i_size *= 2;  /* twice the old size */
201         }
202         if( (p = realloc( p, i_size ) ) == NULL)
203         {
204             *strp = NULL;
205             return -1;
206         }
207     }
208 }
209 #endif
210
211 /*****************************************************************************
212  * asprintf:
213  *****************************************************************************/
214 #if !defined(HAVE_ASPRINTF) || defined(SYS_DARWIN) || defined(SYS_BEOS)
215 int vlc_asprintf( char **strp, const char *fmt, ... )
216 {
217     va_list args;
218     int i_ret;
219
220     va_start( args, fmt );
221     i_ret = vasprintf( strp, fmt, args );
222     va_end( args );
223
224     return i_ret;
225 }
226 #endif
227
228 /*****************************************************************************
229  * atof: convert a string to a double.
230  *****************************************************************************/
231 #if !defined( HAVE_ATOF )
232 double vlc_atof( const char *nptr )
233 {
234     double f_result;
235     wchar_t *psz_tmp;
236     int i_len = strlen( nptr ) + 1;
237
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 );
241     free( psz_tmp );
242
243     return f_result;
244 }
245 #endif
246
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 )
252 {
253     int64_t i_value = 0;
254     int sign = 1, newbase = base ? base : 10;
255
256     while( isspace(*nptr) ) nptr++;
257
258     if( *nptr == '-' )
259     {
260         sign = -1;
261         nptr++;
262     }
263
264     /* Try to detect base */
265     if( *nptr == '0' )
266     {
267         newbase = 8;
268         nptr++;
269
270         if( *nptr == 'x' )
271         {
272             newbase = 16;
273             nptr++;
274         }
275     }
276
277     if( base && newbase != base )
278     {
279         if( endptr ) *endptr = (char *)nptr;
280         return i_value;
281     }
282
283     switch( newbase )
284     {
285         case 10:
286             while( *nptr >= '0' && *nptr <= '9' )
287             {
288                 i_value *= 10;
289                 i_value += ( *nptr++ - '0' );
290             }
291             if( endptr ) *endptr = (char *)nptr;
292             break;
293
294         case 16:
295             while( (*nptr >= '0' && *nptr <= '9') ||
296                    (*nptr >= 'a' && *nptr <= 'f') ||
297                    (*nptr >= 'A' && *nptr <= 'F') )
298             {
299                 int i_valc = 0;
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;
303                 i_value *= 16;
304                 i_value += i_valc;
305                 nptr++;
306             }
307             if( endptr ) *endptr = (char *)nptr;
308             break;
309
310         default:
311             i_value = strtol( nptr, endptr, newbase );
312             break;
313     }
314
315     return i_value * sign;
316 }
317 #endif
318
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 )
324 {
325     return strtoll( nptr, (char **)NULL, 10 );
326 }
327 #endif
328
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 )
334 {
335     return strcoll( (*a)->d_name, (*b)->d_name );
336 }
337
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 ** ) )
342 {
343     DIR            * p_dir;
344     struct dirent  * p_content;
345     struct dirent ** pp_list;
346     int              ret, size;
347
348     if( !namelist || !( p_dir = opendir( name ) ) ) return -1;
349
350     ret     = 0;
351     pp_list = NULL;
352     while( ( p_content = readdir( p_dir ) ) )
353     {
354         if( filter && !filter( p_content ) )
355         {
356             continue;
357         }
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 );
362         ret++;
363     }
364
365     closedir( p_dir );
366
367     if( compar )
368     {
369         qsort( pp_list, ret, sizeof( struct dirent * ),
370                (int (*)(const void *, const void *)) compar );
371     }
372
373     *namelist = pp_list;
374     return ret;
375 }
376 #endif
377
378 /*****************************************************************************
379  * dgettext: gettext for plugins.
380  *****************************************************************************/
381 char *vlc_dgettext( const char *package, const char *msgid )
382 {
383 #if defined( ENABLE_NLS ) \
384      && ( defined(HAVE_GETTEXT) || defined(HAVE_INCLUDED_GETTEXT) )
385     return dgettext( package, msgid );
386 #else
387     return (char *)msgid;
388 #endif
389 }
390
391 /*****************************************************************************
392  * count_utf8_string: returns the number of characters in the string.
393  *****************************************************************************/
394 static int count_utf8_string( const char *psz_string )
395 {
396     int i = 0, i_count = 0;
397     while( psz_string[ i ] != 0 )
398     {
399         if( ((unsigned char *)psz_string)[ i ] <  0x80UL ) i_count++;
400         i++;
401     }
402     return i_count;
403 }
404
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 )
410 {
411     int i_len;
412     char *psz_line, *psz_new_text;
413
414     psz_line = psz_new_text = strdup( psz_text );
415
416     if( b_utf8 )
417         i_len = count_utf8_string( psz_text );
418     else
419         i_len = strlen( psz_text );
420
421     while( i_len > i_line )
422     {
423         /* Look if there is a newline somewhere. */
424         char *psz_parser = psz_line;
425         int i_count = 0;
426         while( i_count <= i_line && *psz_parser != '\n' )
427         {
428             if( b_utf8 )
429             {
430                 while( *((unsigned char *)psz_parser) >= 0x80UL ) psz_parser++;
431             }
432             psz_parser++;
433             i_count++;
434         }
435         if( *psz_parser == '\n' )
436         {
437             i_len -= (i_count + 1);
438             psz_line = psz_parser + 1;
439             continue;
440         }
441
442         /* Find the furthest space. */
443         while( psz_parser > psz_line && *psz_parser != ' ' )
444         {
445             if( b_utf8 )
446             {
447                 while( *((unsigned char *)psz_parser) >= 0x80UL ) psz_parser--;
448             }
449             psz_parser--;
450             i_count--;
451         }
452         if( *psz_parser == ' ' )
453         {
454             *psz_parser = '\n';
455             i_len -= (i_count + 1);
456             psz_line = psz_parser + 1;
457             continue;
458         }
459
460         /* Wrapping has failed. Find the first space or newline */
461         while( i_count < i_len && *psz_parser != ' ' && *psz_parser != '\n' )
462         {
463             if( b_utf8 )
464             {
465                 while( *((unsigned char *)psz_parser) >= 0x80UL ) psz_parser++;
466             }
467             psz_parser++;
468             i_count++;
469         }
470         if( i_count < i_len ) *psz_parser = '\n';
471         i_len -= (i_count + 1);
472         psz_line = psz_parser + 1;
473     }
474
475     return psz_new_text;
476 }
477
478 /*****************************************************************************
479  * iconv wrapper
480  *****************************************************************************/
481 vlc_iconv_t vlc_iconv_open( const char *tocode, const char *fromcode )
482 {
483 #if defined(HAVE_ICONV)
484     return iconv_open( tocode, fromcode );
485 #else
486     return NULL;
487 #endif
488 }
489
490 size_t vlc_iconv( vlc_iconv_t cd, char **inbuf, size_t *inbytesleft,
491                   char **outbuf, size_t *outbytesleft )
492 {
493 #if defined(HAVE_ICONV)
494     return iconv( cd, inbuf, inbytesleft, outbuf, outbytesleft );
495 #else
496     int i_bytes = __MIN(*inbytesleft, *outbytesleft);
497     if( !inbuf || !outbuf || !i_bytes ) return (size_t)(-1);
498     memcpy( *outbuf, *inbuf, i_bytes );
499     inbuf += i_bytes;
500     outbuf += i_bytes;
501     inbytesleft -= i_bytes;
502     outbytesleft -= i_bytes;
503     return i_bytes;
504 #endif
505 }
506
507 int vlc_iconv_close( vlc_iconv_t cd )
508 {
509 #if defined(HAVE_ICONV)
510     return iconv_close( cd );
511 #else
512     return 0;
513 #endif
514 }
515
516 /*****************************************************************************
517  * reduce a fraction
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 )
522 {
523     vlc_bool_t b_exact = 1;
524     uint64_t i_gcd;
525
526     if( i_den == 0 )
527     {
528         *pi_dst_nom = 0;
529         *pi_dst_den = 1;
530         return 1;
531     }
532
533     i_gcd = GCD( i_nom, i_den );
534     i_nom /= i_gcd;
535     i_den /= i_gcd;
536
537     if( i_max == 0 ) i_max = I64C(0xFFFFFFFF);
538
539     if( i_nom > i_max || i_den > i_max )
540     {
541         uint64_t i_a0_num = 0, i_a0_den = 1, i_a1_num = 1, i_a1_den = 0;
542         b_exact = 0;
543
544         for( ; ; )
545         {
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;
549
550             if( i_a2n > i_max || i_a2d > i_max ) break;
551
552             i_nom %= i_den;
553
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;
558         }
559         i_nom = i_a1_num;
560         i_den = i_a1_den;
561     }
562
563     *pi_dst_nom = i_nom;
564     *pi_dst_den = i_den;
565
566     return b_exact;
567 }
568
569 /*************************************************************************
570  * vlc_parse_cmdline: Command line parsing into elements.
571  *
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
574  * escape a quote.
575  *************************************************************************/
576 static void find_end_quote( char **s, char **ppsz_parser, int i_quote )
577 {
578     int i_bcount = 0;
579
580     while( **s )
581     {
582         if( **s == '\\' )
583         {
584             **ppsz_parser = **s;
585             (*ppsz_parser)++; (*s)++;
586             i_bcount++;
587         }
588         else if( **s == '"' || **s == '\'' )
589         {
590             /* Preceeded by a number of '\' which we erase. */
591             *ppsz_parser -= i_bcount / 2;
592             if( i_bcount & 1 )
593             {
594                 /* '\\' followed by a '"' or '\'' */
595                 *ppsz_parser -= 1;
596                 **ppsz_parser = **s;
597                 (*ppsz_parser)++; (*s)++;
598                 i_bcount = 0;
599                 continue;
600             }
601
602             if( **s == i_quote )
603             {
604                 /* End */
605                 return;
606             }
607             else
608             {
609                 /* Different quoting */
610                 int i_quote = **s;
611                 **ppsz_parser = **s;
612                 (*ppsz_parser)++; (*s)++;
613                 find_end_quote( s, ppsz_parser, i_quote );
614                 **ppsz_parser = **s;
615                 (*ppsz_parser)++; (*s)++;
616             }
617
618             i_bcount = 0;
619         }
620         else
621         {
622             /* A regular character */
623             **ppsz_parser = **s;
624             (*ppsz_parser)++; (*s)++;
625             i_bcount = 0;
626         }
627     }
628 }
629
630 char **vlc_parse_cmdline( const char *psz_cmdline, int *i_args )
631 {
632     int argc = 0;
633     char **argv = 0;
634     char *s, *psz_parser, *psz_arg, *psz_orig;
635     int i_bcount = 0;
636
637     if( !psz_cmdline ) return 0;
638     psz_orig = strdup( psz_cmdline );
639     psz_arg = psz_parser = s = psz_orig;
640
641     while( *s )
642     {
643         if( *s == '\t' || *s == ' ' )
644         {
645             /* We have a complete argument */
646             *psz_parser = 0;
647             TAB_APPEND( argc, argv, strdup(psz_arg) );
648
649             /* Skip trailing spaces/tabs */
650             do{ s++; } while( *s == '\t' || *s == ' ' );
651
652             /* New argument */
653             psz_arg = psz_parser = s;
654             i_bcount = 0;
655         }
656         else if( *s == '\\' )
657         {
658             *psz_parser++ = *s++;
659             i_bcount++;
660         }
661         else if( *s == '"' || *s == '\'' )
662         {
663             if( ( i_bcount & 1 ) == 0 )
664             {
665                 /* Preceeded by an even number of '\', this is half that
666                  * number of '\', plus a quote which we erase. */
667                 int i_quote = *s;
668                 psz_parser -= i_bcount / 2;
669                 s++;
670                 find_end_quote( &s, &psz_parser, i_quote );
671                 s++;
672             }
673             else
674             {
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;
678                 *psz_parser++ = '"';
679                 s++;
680             }
681             i_bcount = 0;
682         }
683         else
684         {
685             /* A regular character */
686             *psz_parser++ = *s++;
687             i_bcount = 0;
688         }
689     }
690
691     /* Take care of the last arg */
692     if( *psz_arg )
693     {
694         *psz_parser = '\0';
695         TAB_APPEND( argc, argv, strdup(psz_arg) );
696     }
697
698     if( i_args ) *i_args = argc;
699     free( psz_orig );
700     return argv;
701 }