Qt: ExtensionCopy: fix memleak
[vlc.git] / compat / strtoll.c
blob5ad79c43688219774c5e48a4c0015653938e9367
1 /*****************************************************************************
2 * strtoll.c: C strtoll() replacement
3 *****************************************************************************
4 * Copyright © 1998-2010 VLC authors and VideoLAN
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
25 #include <stdlib.h>
26 #include <string.h>
28 long long int strtoll( const char *nptr, char **endptr, int base )
30 long long i_value = 0;
31 int sign = 1, newbase = base ? base : 10;
33 nptr += strspn( nptr, "\t " );
34 if( *nptr == '-' )
36 sign = -1;
37 nptr++;
40 /* Try to detect base */
41 if( *nptr == '0' )
43 newbase = 8;
44 nptr++;
46 if( *nptr == 'x' )
48 newbase = 16;
49 nptr++;
53 if( base && newbase != base )
55 if( endptr ) *endptr = (char *)nptr;
56 return i_value;
59 switch( newbase )
61 case 10:
62 while( *nptr >= '0' && *nptr <= '9' )
64 i_value *= 10;
65 i_value += ( *nptr++ - '0' );
67 if( endptr ) *endptr = (char *)nptr;
68 break;
70 case 16:
71 while( (*nptr >= '0' && *nptr <= '9') ||
72 (*nptr >= 'a' && *nptr <= 'f') ||
73 (*nptr >= 'A' && *nptr <= 'F') )
75 int i_valc = 0;
76 if(*nptr >= '0' && *nptr <= '9') i_valc = *nptr - '0';
77 else if(*nptr >= 'a' && *nptr <= 'f') i_valc = *nptr - 'a' +10;
78 else if(*nptr >= 'A' && *nptr <= 'F') i_valc = *nptr - 'A' +10;
79 i_value *= 16;
80 i_value += i_valc;
81 nptr++;
83 if( endptr ) *endptr = (char *)nptr;
84 break;
86 default:
87 i_value = strtol( nptr, endptr, newbase );
88 break;
91 return i_value * sign;