]> git.sesse.net Git - vlc/blob - src/extras/libc.c
* src/extras/libc.c: forgot a small modification.
[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
27 #include <vlc/vlc.h>
28
29 #undef iconv_t
30 #undef iconv_open
31 #undef iconv
32 #undef iconv_close
33
34 #if defined(HAVE_ICONV)
35 #   include <iconv.h>
36 #endif
37
38 /*****************************************************************************
39  * getenv: just in case, but it should never be called
40  *****************************************************************************/
41 #if !defined( HAVE_GETENV )
42 char *vlc_getenv( const char *name )
43 {
44     return NULL;
45 }
46 #endif
47
48 /*****************************************************************************
49  * strdup: returns a malloc'd copy of a string
50  *****************************************************************************/
51 #if !defined( HAVE_STRDUP )
52 char *vlc_strdup( const char *string )
53 {
54     return strndup( string, strlen( string ) );
55 }
56 #endif
57
58 /*****************************************************************************
59  * strndup: returns a malloc'd copy of at most n bytes of string
60  * Does anyone know whether or not it will be present in Jaguar?
61  *****************************************************************************/
62 #if !defined( HAVE_STRNDUP )
63 char *vlc_strndup( const char *string, size_t n )
64 {
65     char *psz;
66     size_t len = strlen( string );
67
68     len = __MIN( len, n );
69     psz = (char*)malloc( len + 1 );
70
71     if( psz != NULL )
72     {
73         memcpy( (void*)psz, (const void*)string, len );
74         psz[ len ] = 0;
75     }
76
77     return psz;
78 }
79 #endif
80
81 /*****************************************************************************
82  * strcasecmp: compare two strings ignoring case
83  *****************************************************************************/
84 #if !defined( HAVE_STRCASECMP ) && !defined( HAVE_STRICMP )
85 int vlc_strcasecmp( const char *s1, const char *s2 )
86 {
87     int i_delta = 0;
88
89     while( !i_delta && *s1 && *s2 )
90     {
91         i_delta = *s1 - *s2;
92
93         if( *s1 >= 'A' && *s1 <= 'Z' )
94         {
95             i_delta -= 'A' - 'a';
96         }
97
98         if( *s2 >= 'A' && *s2 <= 'Z' )
99         {
100             i_delta += 'A' - 'a';
101         }
102
103         s1++; s2++;
104     }
105
106     return i_delta;
107 }
108 #endif
109
110 /*****************************************************************************
111  * strncasecmp: compare n chars from two strings ignoring case
112  *****************************************************************************/
113 #if !defined( HAVE_STRNCASECMP ) && !defined( HAVE_STRNICMP )
114 int vlc_strncasecmp( const char *s1, const char *s2, size_t n )
115 {
116     int i_delta = 0;
117
118     while( n-- && !i_delta && *s1 )
119     {
120         i_delta = *s1 - *s2;
121
122         if( *s1 >= 'A' && *s1 <= 'Z' )
123         {
124             i_delta -= 'A' - 'a';
125         }
126
127         if( *s2 >= 'A' && *s2 <= 'Z' )
128         {
129             i_delta += 'A' - 'a';
130         }
131
132         s1++; s2++;
133     }
134
135     return i_delta;
136 }
137 #endif
138
139 /******************************************************************************
140  * strcasestr: find a substring (little) in another substring (big)
141  * Case sensitive. Return NULL if not found, return big if little == null
142  *****************************************************************************/
143 #if !defined( HAVE_STRCASESTR ) && !defined( HAVE_STRISTR )
144 char * vlc_strcasestr( const char *psz_big, const char *psz_little )
145 {
146     char *p_pos = psz_big;
147
148     if( !psz_big || !psz_little || !*psz_little ) return psz_big;
149  
150     while( *p_pos ) 
151     {
152         if( toupper( *p_pos ) == toupper( *psz_little ) )
153         {
154             char * psz_cur1 = p_pos + 1;
155             char * psz_cur2 = psz_little + 1;
156             while( *psz_cur1 && *psz_cur2 &&
157                    toupper( *psz_cur1 ) == toupper( *psz_cur2 ) )
158             {
159                 psz_cur1++;
160                 psz_cur2++;
161             }
162             if( !*psz_cur2 ) return p_pos;
163         }
164         p_pos++;
165     }
166     return NULL;
167 }
168 #endif
169
170 /*****************************************************************************
171  * vasprintf:
172  *****************************************************************************/
173 #if !defined(HAVE_VASPRINTF) || defined(SYS_DARWIN) || defined(SYS_BEOS)
174 int vlc_vasprintf(char **strp, const char *fmt, va_list ap)
175 {
176     /* Guess we need no more than 100 bytes. */
177     int     i_size = 100;
178     char    *p = malloc( i_size );
179     int     n;
180
181     if( p == NULL )
182     {
183         *strp = NULL;
184         return -1;
185     }
186
187     for( ;; )
188     {
189         /* Try to print in the allocated space. */
190         n = vsnprintf( p, i_size, fmt, ap );
191
192         /* If that worked, return the string. */
193         if (n > -1 && n < i_size)
194         {
195             *strp = p;
196             return strlen( p );
197         }
198         /* Else try again with more space. */
199         if (n > -1)    /* glibc 2.1 */
200         {
201            i_size = n+1; /* precisely what is needed */
202         }
203         else           /* glibc 2.0 */
204         {
205            i_size *= 2;  /* twice the old size */
206         }
207         if( (p = realloc( p, i_size ) ) == NULL)
208         {
209             *strp = NULL;
210             return -1;
211         }
212     }
213 }
214 #endif
215
216 /*****************************************************************************
217  * asprintf:
218  *****************************************************************************/
219 #if !defined(HAVE_ASPRINTF) || defined(SYS_DARWIN) || defined(SYS_BEOS)
220 int vlc_asprintf( char **strp, const char *fmt, ... )
221 {
222     va_list args;
223     int i_ret;
224
225     va_start( args, fmt );
226     i_ret = vasprintf( strp, fmt, args );
227     va_end( args );
228
229     return i_ret;
230 }
231 #endif
232
233 /*****************************************************************************
234  * atof: convert a string to a double.
235  *****************************************************************************/
236 #if !defined( HAVE_ATOF )
237 double vlc_atof( const char *nptr )
238 {
239     double f_result;
240     wchar_t *psz_tmp;
241     int i_len = strlen( nptr ) + 1;
242
243     psz_tmp = malloc( i_len * sizeof(wchar_t) );
244     MultiByteToWideChar( CP_ACP, 0, nptr, -1, psz_tmp, i_len );
245     f_result = wcstod( psz_tmp, NULL );
246     free( psz_tmp );
247
248     return f_result;
249 }
250 #endif
251
252 /*****************************************************************************
253  * atoll: convert a string to a 64 bits int.
254  *****************************************************************************/
255 #if !defined( HAVE_ATOLL )
256 int64_t vlc_atoll( const char *str )
257 {
258     int64_t i_value = 0;
259     int sign = 1;
260
261     if( *str == '-' )
262     {
263         sign = -1;
264     }
265
266     while( *str >= '0' && *str <= '9' )
267     {
268         i_value = i_value * 10 + ( *str++ - '0' );
269     }
270
271     return i_value * sign;
272 }
273 #endif
274
275 /*****************************************************************************
276  * lseek: reposition read/write file offset.
277  *****************************************************************************
278  * FIXME: this cast sucks!
279  *****************************************************************************/
280 #if !defined( HAVE_LSEEK )
281 off_t vlc_lseek( int fildes, off_t offset, int whence )
282 {
283     return SetFilePointer( (HANDLE)fildes, (long)offset, NULL, whence );
284 }
285 #endif
286
287 /*****************************************************************************
288  * dgettext: gettext for plugins.
289  *****************************************************************************/
290 char *vlc_dgettext( const char *package, const char *msgid )
291 {
292 #if defined( ENABLE_NLS ) \
293      && ( defined(HAVE_GETTEXT) || defined(HAVE_INCLUDED_GETTEXT) )
294     return dgettext( package, msgid );
295 #else
296     return (char *)msgid;
297 #endif
298 }
299
300 /*****************************************************************************
301  * count_utf8_string: returns the number of characters in the string.
302  *****************************************************************************/
303 static int count_utf8_string( const char *psz_string )
304 {
305     int i = 0, i_count = 0;
306     while( psz_string[ i ] != 0 )
307     {
308         if( ((unsigned char *)psz_string)[ i ] <  0x80UL ) i_count++;
309         i++;
310     }
311     return i_count;
312 }
313
314 /*****************************************************************************
315  * wraptext: inserts \n at convenient places to wrap the text.
316  *           Returns the modified string in a new buffer.
317  *****************************************************************************/
318 char *vlc_wraptext( const char *psz_text, int i_line, vlc_bool_t b_utf8 )
319 {
320     int i_len;
321     char *psz_line, *psz_new_text;
322
323     psz_line = psz_new_text = strdup( psz_text );
324
325     if( b_utf8 )
326         i_len = count_utf8_string( psz_text );
327     else
328         i_len = strlen( psz_text );
329
330     while( i_len > i_line )
331     {
332         /* Look if there is a newline somewhere. */
333         char *psz_parser = psz_line;
334         int i_count = 0;
335         while( i_count <= i_line && *psz_parser != '\n' )
336         {
337             if( b_utf8 )
338             {
339                 while( *((unsigned char *)psz_parser) >= 0x80UL ) psz_parser++;
340             }
341             psz_parser++;
342             i_count++;
343         }
344         if( *psz_parser == '\n' )
345         {
346             i_len -= (i_count + 1);
347             psz_line = psz_parser + 1;
348             continue;
349         }
350
351         /* Find the furthest space. */
352         while( psz_parser > psz_line && *psz_parser != ' ' )
353         {
354             if( b_utf8 )
355             {
356                 while( *((unsigned char *)psz_parser) >= 0x80UL ) psz_parser--;
357             }
358             psz_parser--;
359             i_count--;
360         }
361         if( *psz_parser == ' ' )
362         {
363             *psz_parser = '\n';
364             i_len -= (i_count + 1);
365             psz_line = psz_parser + 1;
366             continue;
367         }
368
369         /* Wrapping has failed. Find the first space or newline */
370         while( i_count < i_len && *psz_parser != ' ' && *psz_parser != '\n' )
371         {
372             if( b_utf8 )
373             {
374                 while( *((unsigned char *)psz_parser) >= 0x80UL ) psz_parser++;
375             }
376             psz_parser++;
377             i_count++;
378         }
379         if( i_count < i_len ) *psz_parser = '\n';
380         i_len -= (i_count + 1);
381         psz_line = psz_parser + 1;
382     }
383
384     return psz_new_text;
385 }
386
387 /*****************************************************************************
388  * iconv wrapper
389  *****************************************************************************/
390 vlc_iconv_t vlc_iconv_open( const char *tocode, const char *fromcode )
391 {
392 #if defined(HAVE_ICONV)
393     return iconv_open( tocode, fromcode );
394 #else
395     return NULL;
396 #endif
397 }
398
399 size_t vlc_iconv( vlc_iconv_t cd, char **inbuf, size_t *inbytesleft,
400                   char **outbuf, size_t *outbytesleft )
401 {
402 #if defined(HAVE_ICONV)
403     return iconv( cd, inbuf, inbytesleft, outbuf, outbytesleft );
404 #else
405     int i_bytes = __MIN(inbytesleft, outbytesleft);
406     if( !inbuf || !outbuf || !i_bytes ) return (size_t)(-1);
407     memcpy( *outbuf, *inbuf, i_bytes );
408     inbuf += i_bytes;
409     outbuf += i_bytes;
410     inbytesleft -= i_bytes;
411     outbytesleft -= i_bytes;
412     return i_bytes;
413 #endif
414 }
415
416 int vlc_iconv_close( vlc_iconv_t cd )
417 {
418 #if defined(HAVE_ICONV)
419     return iconv_close( cd );
420 #else
421     return 0;
422 #endif
423 }
424
425 /*****************************************************************************
426  * reduce a fraction
427  *   (adapted from libavcodec, author Michael Niedermayer <michaelni@gmx.at>)
428  *****************************************************************************/
429 vlc_bool_t vlc_reduce( int *pi_dst_nom, int *pi_dst_den,
430                        int64_t i_nom, int64_t i_den, int64_t i_max )
431 {
432     vlc_bool_t b_exact = 1, b_sign = 0;
433     int64_t i_gcd;
434
435     if( i_den == 0 )
436     {
437         *pi_dst_nom = 0;
438         *pi_dst_den = 1;
439         return 1;
440     }
441
442     if( i_den < 0 )
443     {
444         i_den = - i_den;
445         i_nom = - i_nom;
446     }
447
448     if( i_nom < 0 )
449     {
450         i_nom = - i_nom;
451         b_sign = 1;
452     }
453
454     i_gcd = GCD( i_nom, i_den );
455     i_nom /= i_gcd;
456     i_den /= i_gcd;
457
458     if( i_max == 0 ) i_max = I64C(0xFFFFFFFF);
459
460     if( i_nom > i_max || i_den > i_max )
461     {
462         int i_a0_num = 0, i_a0_den = 1, i_a1_num = 1, i_a1_den = 0;
463         b_exact = 0;
464
465         for( ; ; )
466         {
467             int64_t i_x = i_nom / i_den;
468             int64_t i_a2n = i_x * i_a1_num + i_a0_num;
469             int64_t i_a2d = i_x * i_a1_den + i_a0_den;
470
471             if( i_a2n > i_max || i_a2d > i_max ) break;
472
473             i_nom %= i_den;
474
475             i_a0_num = i_a1_num; i_a0_den = i_a1_den;
476             i_a1_num = i_a2n; i_a1_den = i_a2d;
477             if( i_nom == 0 ) break;
478             i_x = i_nom; i_nom = i_den; i_den = i_x;
479         }
480         i_nom = i_a1_num;
481         i_den = i_a1_den;
482     }
483
484     if( b_sign ) i_nom = - i_nom;
485
486     *pi_dst_nom = i_nom;
487     *pi_dst_den = i_den;
488
489     return b_exact;
490 }