2 * mlt_tokeniser.c -- String tokeniser
3 * Copyright (C) 2002-2003 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 /* System header files */
25 /* Application header files */
26 #include "mlt_tokeniser.h"
28 /** Initialise a tokeniser.
31 mlt_tokeniser mlt_tokeniser_init( )
33 return calloc( 1, sizeof( mlt_tokeniser_t ) );
36 /** Clear the tokeniser.
39 static void mlt_tokeniser_clear( mlt_tokeniser tokeniser )
42 for ( index = 0; index < tokeniser->count; index ++ )
43 free( tokeniser->tokens[ index ] );
45 free( tokeniser->input );
46 tokeniser->input = NULL;
49 /** Append a string to the tokeniser.
52 static int mlt_tokeniser_append( mlt_tokeniser tokeniser, char *token )
56 if ( tokeniser->count == tokeniser->size )
58 tokeniser->size += 20;
59 tokeniser->tokens = realloc( tokeniser->tokens, tokeniser->size * sizeof( char * ) );
62 if ( tokeniser->tokens != NULL )
64 tokeniser->tokens[ tokeniser->count ++ ] = strdup( token );
74 /** Parse a string by splitting on the delimiter provided.
77 int mlt_tokeniser_parse_new( mlt_tokeniser tokeniser, char *string, char *delimiter )
80 int length = strlen( string );
81 int delimiter_size = strlen( delimiter );
83 char *token = strdup( string );
85 mlt_tokeniser_clear( tokeniser );
86 tokeniser->input = strdup( string );
89 for ( index = 0; index < length; )
91 char *start = string + index;
92 char *end = strstr( start, delimiter );
96 strcat( token, start );
97 mlt_tokeniser_append( tokeniser, token );
101 else if ( start != end )
103 strncat( token, start, end - start );
104 index += end - start;
105 if ( token[ 0 ] != '\"' || ( token[ 0 ] == '\"' && token[ strlen( token ) - 1 ] == '\"' ) )
107 mlt_tokeniser_append( tokeniser, token );
111 else while ( strncmp( string + index, delimiter, delimiter_size ) == 0 )
113 strncat( token, delimiter, delimiter_size );
114 index += delimiter_size;
119 index += strlen( delimiter );
123 /* Special case - malformed string condition */
124 if ( !strcmp( token, "" ) )
126 count = 0 - ( count - 1 );
127 mlt_tokeniser_append( tokeniser, token );
134 /** Get the original input.
137 char *mlt_tokeniser_get_input( mlt_tokeniser tokeniser )
139 return tokeniser->input;
142 /** Get the number of tokens.
145 int mlt_tokeniser_count( mlt_tokeniser tokeniser )
147 return tokeniser->count;
150 /** Get a token as a string.
153 char *mlt_tokeniser_get_string( mlt_tokeniser tokeniser, int index )
155 if ( index < tokeniser->count )
156 return tokeniser->tokens[ index ];
161 /** Close the tokeniser.
164 void mlt_tokeniser_close( mlt_tokeniser tokeniser )
166 mlt_tokeniser_clear( tokeniser );
167 free( tokeniser->tokens );