2 * \file mlt_tokeniser.c
3 * \brief string tokeniser
6 * Copyright (C) 2002-2009 Ushodaya Enterprises Limited
7 * \author Charles Yates <charles.yates@pandora.be>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 /* System header files */
28 /* Application header files */
29 #include "mlt_tokeniser.h"
31 /** Initialise a tokeniser.
34 mlt_tokeniser mlt_tokeniser_init( )
36 return calloc( 1, sizeof( mlt_tokeniser_t ) );
39 /** Clear the tokeniser.
42 static void mlt_tokeniser_clear( mlt_tokeniser tokeniser )
45 for ( index = 0; index < tokeniser->count; index ++ )
46 free( tokeniser->tokens[ index ] );
48 free( tokeniser->input );
49 tokeniser->input = NULL;
52 /** Append a string to the tokeniser.
55 static int mlt_tokeniser_append( mlt_tokeniser tokeniser, char *token )
59 if ( tokeniser->count == tokeniser->size )
61 tokeniser->size += 20;
62 tokeniser->tokens = realloc( tokeniser->tokens, tokeniser->size * sizeof( char * ) );
65 if ( tokeniser->tokens != NULL )
67 tokeniser->tokens[ tokeniser->count ++ ] = strdup( token );
77 /** Parse a string by splitting on the delimiter provided.
80 int mlt_tokeniser_parse_new( mlt_tokeniser tokeniser, char *string, const char *delimiter )
83 int length = strlen( string );
84 int delimiter_size = strlen( delimiter );
86 char *token = strdup( string );
88 mlt_tokeniser_clear( tokeniser );
89 tokeniser->input = strdup( string );
92 for ( index = 0; index < length; )
94 char *start = string + index;
95 char *end = strstr( start, delimiter );
99 strcat( token, start );
100 mlt_tokeniser_append( tokeniser, token );
104 else if ( start != end )
106 strncat( token, start, end - start );
107 index += end - start;
108 if ( strchr( token, '\"' ) == NULL || token[ strlen( token ) - 1 ] == '\"' )
110 mlt_tokeniser_append( tokeniser, token );
114 else while ( strncmp( string + index, delimiter, delimiter_size ) == 0 )
116 strncat( token, delimiter, delimiter_size );
117 index += delimiter_size;
122 index += delimiter_size;
126 /* Special case - malformed string condition */
127 if ( !strcmp( token, "" ) )
129 count = 0 - ( count - 1 );
130 mlt_tokeniser_append( tokeniser, token );
137 /** Get the original input.
140 char *mlt_tokeniser_get_input( mlt_tokeniser tokeniser )
142 return tokeniser->input;
145 /** Get the number of tokens.
148 int mlt_tokeniser_count( mlt_tokeniser tokeniser )
150 return tokeniser->count;
153 /** Get a token as a string.
156 char *mlt_tokeniser_get_string( mlt_tokeniser tokeniser, int index )
158 if ( index < tokeniser->count )
159 return tokeniser->tokens[ index ];
164 /** Close the tokeniser.
167 void mlt_tokeniser_close( mlt_tokeniser tokeniser )
169 mlt_tokeniser_clear( tokeniser );
170 free( tokeniser->tokens );