]> git.sesse.net Git - vlc/blob - src/extras/libc.c
* src/extras/libc.c: strtoll() replacement when not available.
[vlc] / src / extras / libc.c
1 /*****************************************************************************
2  * libc.c: Extra libc function for some systems.
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id$
6  *
7  * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
8  *          Samuel Hocevar <sam@zoy.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24 #include <string.h>                                              /* strdup() */
25 #include <stdlib.h>
26 #include <ctype.h>
27
28 #include <vlc/vlc.h>
29
30 #undef iconv_t
31 #undef iconv_open
32 #undef iconv
33 #undef iconv_close
34
35 #if defined(HAVE_ICONV)
36 #   include <iconv.h>
37 #endif
38
39 /*****************************************************************************
40  * getenv: just in case, but it should never be called
41  *****************************************************************************/
42 #if !defined( HAVE_GETENV )
43 char *vlc_getenv( const char *name )
44 {
45     return NULL;
46 }
47 #endif
48
49 /*****************************************************************************
50  * strdup: returns a malloc'd copy of a string
51  *****************************************************************************/
52 #if !defined( HAVE_STRDUP )
53 char *vlc_strdup( const char *string )
54 {
55     return strndup( string, strlen( string ) );
56 }
57 #endif
58
59 /*****************************************************************************
60  * strndup: returns a malloc'd copy of at most n bytes of string
61  * Does anyone know whether or not it will be present in Jaguar?
62  *****************************************************************************/
63 #if !defined( HAVE_STRNDUP )
64 char *vlc_strndup( const char *string, size_t n )
65 {
66     char *psz;
67     size_t len = strlen( string );
68
69     len = __MIN( len, n );
70     psz = (char*)malloc( len + 1 );
71
72     if( psz != NULL )
73     {
74         memcpy( (void*)psz, (const void*)string, len );
75         psz[ len ] = 0;
76     }
77
78     return psz;
79 }
80 #endif
81
82 /*****************************************************************************
83  * strcasecmp: compare two strings ignoring case
84  *****************************************************************************/
85 #if !defined( HAVE_STRCASECMP ) && !defined( HAVE_STRICMP )
86 int vlc_strcasecmp( const char *s1, const char *s2 )
87 {
88     int i_delta = 0;
89
90     while( !i_delta && *s1 && *s2 )
91     {
92         i_delta = *s1 - *s2;
93
94         if( *s1 >= 'A' && *s1 <= 'Z' )
95         {
96             i_delta -= 'A' - 'a';
97         }
98
99         if( *s2 >= 'A' && *s2 <= 'Z' )
100         {
101             i_delta += 'A' - 'a';
102         }
103
104         s1++; s2++;
105     }
106
107     return i_delta;
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 i_delta = 0;
118
119     while( n-- && !i_delta && *s1 )
120     {
121         i_delta = *s1 - *s2;
122
123         if( *s1 >= 'A' && *s1 <= 'Z' )
124         {
125             i_delta -= 'A' - 'a';
126         }
127
128         if( *s2 >= 'A' && *s2 <= 'Z' )
129         {
130             i_delta += 'A' - 'a';
131         }
132
133         s1++; s2++;
134     }
135
136     return i_delta;
137 }
138 #endif
139
140 /******************************************************************************
141  * strcasestr: find a substring (little) in another substring (big)
142  * Case sensitive. Return NULL if not found, return big if little == null
143  *****************************************************************************/
144 #if !defined( HAVE_STRCASESTR ) && !defined( HAVE_STRISTR )
145 char * vlc_strcasestr( const char *psz_big, const char *psz_little )
146 {
147     char *p_pos = psz_big;
148
149     if( !psz_big || !psz_little || !*psz_little ) return psz_big;
150  
151     while( *p_pos ) 
152     {
153         if( toupper( *p_pos ) == toupper( *psz_little ) )
154         {
155             char * psz_cur1 = p_pos + 1;
156             char * psz_cur2 = psz_little + 1;
157             while( *psz_cur1 && *psz_cur2 &&
158                    toupper( *psz_cur1 ) == toupper( *psz_cur2 ) )
159             {
160                 psz_cur1++;
161                 psz_cur2++;
162             }
163             if( !*psz_cur2 ) return p_pos;
164         }
165         p_pos++;
166     }
167     return NULL;
168 }
169 #endif
170
171 /*****************************************************************************
172  * vasprintf:
173  *****************************************************************************/
174 #if !defined(HAVE_VASPRINTF) || defined(SYS_DARWIN) || defined(SYS_BEOS)
175 int vlc_vasprintf(char **strp, const char *fmt, va_list ap)
176 {
177     /* Guess we need no more than 100 bytes. */
178     int     i_size = 100;
179     char    *p = malloc( i_size );
180     int     n;
181
182     if( p == NULL )
183     {
184         *strp = NULL;
185         return -1;
186     }
187
188     for( ;; )
189     {
190         /* Try to print in the allocated space. */
191         n = vsnprintf( p, i_size, fmt, ap );
192
193         /* If that worked, return the string. */
194         if (n > -1 && n < i_size)
195         {
196             *strp = p;
197             return strlen( p );
198         }
199         /* Else try again with more space. */
200         if (n > -1)    /* glibc 2.1 */
201         {
202            i_size = n+1; /* precisely what is needed */
203         }
204         else           /* glibc 2.0 */
205         {
206            i_size *= 2;  /* twice the old size */
207         }
208         if( (p = realloc( p, i_size ) ) == NULL)
209         {
210             *strp = NULL;
211             return -1;
212         }
213     }
214 }
215 #endif
216
217 /*****************************************************************************
218  * asprintf:
219  *****************************************************************************/
220 #if !defined(HAVE_ASPRINTF) || defined(SYS_DARWIN) || defined(SYS_BEOS)
221 int vlc_asprintf( char **strp, const char *fmt, ... )
222 {
223     va_list args;
224     int i_ret;
225
226     va_start( args, fmt );
227     i_ret = vasprintf( strp, fmt, args );
228     va_end( args );
229
230     return i_ret;
231 }
232 #endif
233
234 /*****************************************************************************
235  * atof: convert a string to a double.
236  *****************************************************************************/
237 #if !defined( HAVE_ATOF )
238 double vlc_atof( const char *nptr )
239 {
240     double f_result;
241     wchar_t *psz_tmp;
242     int i_len = strlen( nptr ) + 1;
243
244     psz_tmp = malloc( i_len * sizeof(wchar_t) );
245     MultiByteToWideChar( CP_ACP, 0, nptr, -1, psz_tmp, i_len );
246     f_result = wcstod( psz_tmp, NULL );
247     free( psz_tmp );
248
249     return f_result;
250 }
251 #endif
252
253 /*****************************************************************************
254  * strtoll: convert a string to a 64 bits int.
255  *****************************************************************************/
256 #if !defined( HAVE_STRTOLL )
257 int64_t vlc_strtoll( const char *nptr, char **endptr, int base )
258 {
259     int64_t i_value = 0;
260     int sign = 1, newbase = base ? base : 10;
261
262     while( isspace(*nptr) ) nptr++;
263
264     if( *nptr == '-' )
265     {
266         sign = -1;
267         nptr++;
268     }
269
270     /* Try to detect base */
271     if( *nptr == '0' )
272     {
273         newbase = 8;
274         nptr++;
275
276         if( *nptr == 'x' )
277         {
278             newbase = 16;
279             nptr++;
280         }
281     }
282
283     if( base && newbase != base )
284     {
285         if( endptr ) *endptr = (char *)nptr;
286         return i_value;
287     }
288
289     switch( newbase )
290     {
291         case 10:
292             while( *nptr >= '0' && *nptr <= '9' )
293             {
294                 i_value *= 10;
295                 i_value += ( *nptr++ - '0' );
296             }
297             if( endptr ) *endptr = (char *)nptr;
298             break;
299
300         case 16:
301             while( (*nptr >= '0' && *nptr <= '9') ||
302                    (*nptr >= 'a' && *nptr <= 'f') ||
303                    (*nptr >= 'A' && *nptr <= 'F') )
304             {
305                 int i_valc = 0;
306                 if(*nptr >= '0' && *nptr <= '9') i_valc = *nptr - '0';
307                 else if(*nptr >= 'a' && *nptr <= 'f') i_valc = *nptr - 'a' +10;
308                 else if(*nptr >= 'A' && *nptr <= 'F') i_valc = *nptr - 'A' +10;
309                 i_value *= 16;
310                 i_value += i_valc;
311                 nptr++;
312             }
313             if( endptr ) *endptr = (char *)nptr;
314             break;
315
316         default:
317             i_value = strtol( nptr, endptr, newbase );
318             break;
319     }
320
321     return i_value * sign;
322 }
323 #endif
324
325 /*****************************************************************************
326  * atoll: convert a string to a 64 bits int.
327  *****************************************************************************/
328 #if !defined( HAVE_ATOLL )
329 int64_t vlc_atoll( const char *nptr )
330 {
331     return strtoll( nptr, (char **)NULL, 10 );
332 }
333 #endif
334
335 /*****************************************************************************
336  * lseek: reposition read/write file offset.
337  *****************************************************************************
338  * FIXME: this cast sucks!
339  *****************************************************************************/
340 #if !defined( HAVE_LSEEK )
341 off_t vlc_lseek( int fildes, off_t offset, int whence )
342 {
343     return SetFilePointer( (HANDLE)fildes, (long)offset, NULL, whence );
344 }
345 #endif
346
347 /*****************************************************************************
348  * dgettext: gettext for plugins.
349  *****************************************************************************/
350 char *vlc_dgettext( const char *package, const char *msgid )
351 {
352 #if defined( ENABLE_NLS ) \
353      && ( defined(HAVE_GETTEXT) || defined(HAVE_INCLUDED_GETTEXT) )
354     return dgettext( package, msgid );
355 #else
356     return (char *)msgid;
357 #endif
358 }
359
360 /*****************************************************************************
361  * count_utf8_string: returns the number of characters in the string.
362  *****************************************************************************/
363 static int count_utf8_string( const char *psz_string )
364 {
365     int i = 0, i_count = 0;
366     while( psz_string[ i ] != 0 )
367     {
368         if( ((unsigned char *)psz_string)[ i ] <  0x80UL ) i_count++;
369         i++;
370     }
371     return i_count;
372 }
373
374 /*****************************************************************************
375  * wraptext: inserts \n at convenient places to wrap the text.
376  *           Returns the modified string in a new buffer.
377  *****************************************************************************/
378 char *vlc_wraptext( const char *psz_text, int i_line, vlc_bool_t b_utf8 )
379 {
380     int i_len;
381     char *psz_line, *psz_new_text;
382
383     psz_line = psz_new_text = strdup( psz_text );
384
385     if( b_utf8 )
386         i_len = count_utf8_string( psz_text );
387     else
388         i_len = strlen( psz_text );
389
390     while( i_len > i_line )
391     {
392         /* Look if there is a newline somewhere. */
393         char *psz_parser = psz_line;
394         int i_count = 0;
395         while( i_count <= i_line && *psz_parser != '\n' )
396         {
397             if( b_utf8 )
398             {
399                 while( *((unsigned char *)psz_parser) >= 0x80UL ) psz_parser++;
400             }
401             psz_parser++;
402             i_count++;
403         }
404         if( *psz_parser == '\n' )
405         {
406             i_len -= (i_count + 1);
407             psz_line = psz_parser + 1;
408             continue;
409         }
410
411         /* Find the furthest space. */
412         while( psz_parser > psz_line && *psz_parser != ' ' )
413         {
414             if( b_utf8 )
415             {
416                 while( *((unsigned char *)psz_parser) >= 0x80UL ) psz_parser--;
417             }
418             psz_parser--;
419             i_count--;
420         }
421         if( *psz_parser == ' ' )
422         {
423             *psz_parser = '\n';
424             i_len -= (i_count + 1);
425             psz_line = psz_parser + 1;
426             continue;
427         }
428
429         /* Wrapping has failed. Find the first space or newline */
430         while( i_count < i_len && *psz_parser != ' ' && *psz_parser != '\n' )
431         {
432             if( b_utf8 )
433             {
434                 while( *((unsigned char *)psz_parser) >= 0x80UL ) psz_parser++;
435             }
436             psz_parser++;
437             i_count++;
438         }
439         if( i_count < i_len ) *psz_parser = '\n';
440         i_len -= (i_count + 1);
441         psz_line = psz_parser + 1;
442     }
443
444     return psz_new_text;
445 }
446
447 /*****************************************************************************
448  * iconv wrapper
449  *****************************************************************************/
450 vlc_iconv_t vlc_iconv_open( const char *tocode, const char *fromcode )
451 {
452 #if defined(HAVE_ICONV)
453     return iconv_open( tocode, fromcode );
454 #else
455     return NULL;
456 #endif
457 }
458
459 size_t vlc_iconv( vlc_iconv_t cd, char **inbuf, size_t *inbytesleft,
460                   char **outbuf, size_t *outbytesleft )
461 {
462 #if defined(HAVE_ICONV)
463     return iconv( cd, inbuf, inbytesleft, outbuf, outbytesleft );
464 #else
465     int i_bytes = __MIN(inbytesleft, outbytesleft);
466     if( !inbuf || !outbuf || !i_bytes ) return (size_t)(-1);
467     memcpy( *outbuf, *inbuf, i_bytes );
468     inbuf += i_bytes;
469     outbuf += i_bytes;
470     inbytesleft -= i_bytes;
471     outbytesleft -= i_bytes;
472     return i_bytes;
473 #endif
474 }
475
476 int vlc_iconv_close( vlc_iconv_t cd )
477 {
478 #if defined(HAVE_ICONV)
479     return iconv_close( cd );
480 #else
481     return 0;
482 #endif
483 }
484
485 /*****************************************************************************
486  * reduce a fraction
487  *   (adapted from libavcodec, author Michael Niedermayer <michaelni@gmx.at>)
488  *****************************************************************************/
489 vlc_bool_t vlc_reduce( int *pi_dst_nom, int *pi_dst_den,
490                        int64_t i_nom, int64_t i_den, int64_t i_max )
491 {
492     vlc_bool_t b_exact = 1, b_sign = 0;
493     int64_t i_gcd;
494
495     if( i_den == 0 )
496     {
497         *pi_dst_nom = 0;
498         *pi_dst_den = 1;
499         return 1;
500     }
501
502     if( i_den < 0 )
503     {
504         i_den = - i_den;
505         i_nom = - i_nom;
506     }
507
508     if( i_nom < 0 )
509     {
510         i_nom = - i_nom;
511         b_sign = 1;
512     }
513
514     i_gcd = GCD( i_nom, i_den );
515     i_nom /= i_gcd;
516     i_den /= i_gcd;
517
518     if( i_max == 0 ) i_max = I64C(0xFFFFFFFF);
519
520     if( i_nom > i_max || i_den > i_max )
521     {
522         int i_a0_num = 0, i_a0_den = 1, i_a1_num = 1, i_a1_den = 0;
523         b_exact = 0;
524
525         for( ; ; )
526         {
527             int64_t i_x = i_nom / i_den;
528             int64_t i_a2n = i_x * i_a1_num + i_a0_num;
529             int64_t i_a2d = i_x * i_a1_den + i_a0_den;
530
531             if( i_a2n > i_max || i_a2d > i_max ) break;
532
533             i_nom %= i_den;
534
535             i_a0_num = i_a1_num; i_a0_den = i_a1_den;
536             i_a1_num = i_a2n; i_a1_den = i_a2d;
537             if( i_nom == 0 ) break;
538             i_x = i_nom; i_nom = i_den; i_den = i_x;
539         }
540         i_nom = i_a1_num;
541         i_den = i_a1_den;
542     }
543
544     if( b_sign ) i_nom = - i_nom;
545
546     *pi_dst_nom = i_nom;
547     *pi_dst_den = i_den;
548
549     return b_exact;
550 }