2 * \file mlt_tokeniser.c
3 * \brief string tokeniser
5 * Copyright (C) 2002-2008 Ushodaya Enterprises Limited
6 * \author Charles Yates <charles.yates@pandora.be>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 /* System header files */
27 /* Application header files */
28 #include "mlt_tokeniser.h"
30 /** Initialise a tokeniser.
33 mlt_tokeniser
mlt_tokeniser_init( )
35 return calloc( 1, sizeof( mlt_tokeniser_t
) );
38 /** Clear the tokeniser.
41 static void mlt_tokeniser_clear( mlt_tokeniser tokeniser
)
44 for ( index
= 0; index
< tokeniser
->count
; index
++ )
45 free( tokeniser
->tokens
[ index
] );
47 free( tokeniser
->input
);
48 tokeniser
->input
= NULL
;
51 /** Append a string to the tokeniser.
54 static int mlt_tokeniser_append( mlt_tokeniser tokeniser
, char *token
)
58 if ( tokeniser
->count
== tokeniser
->size
)
60 tokeniser
->size
+= 20;
61 tokeniser
->tokens
= realloc( tokeniser
->tokens
, tokeniser
->size
* sizeof( char * ) );
64 if ( tokeniser
->tokens
!= NULL
)
66 tokeniser
->tokens
[ tokeniser
->count
++ ] = strdup( token
);
76 /** Parse a string by splitting on the delimiter provided.
79 int mlt_tokeniser_parse_new( mlt_tokeniser tokeniser
, char *string
, char *delimiter
)
82 int length
= strlen( string
);
83 int delimiter_size
= strlen( delimiter
);
85 char *token
= strdup( string
);
87 mlt_tokeniser_clear( tokeniser
);
88 tokeniser
->input
= strdup( string
);
91 for ( index
= 0; index
< length
; )
93 char *start
= string
+ index
;
94 char *end
= strstr( start
, delimiter
);
98 strcat( token
, start
);
99 mlt_tokeniser_append( tokeniser
, token
);
103 else if ( start
!= end
)
105 strncat( token
, start
, end
- start
);
106 index
+= end
- start
;
107 if ( strchr( token
, '\"' ) == NULL
|| token
[ strlen( token
) - 1 ] == '\"' )
109 mlt_tokeniser_append( tokeniser
, token
);
113 else while ( strncmp( string
+ index
, delimiter
, delimiter_size
) == 0 )
115 strncat( token
, delimiter
, delimiter_size
);
116 index
+= delimiter_size
;
121 index
+= strlen( delimiter
);
125 /* Special case - malformed string condition */
126 if ( !strcmp( token
, "" ) )
128 count
= 0 - ( count
- 1 );
129 mlt_tokeniser_append( tokeniser
, token
);
136 /** Get the original input.
139 char *mlt_tokeniser_get_input( mlt_tokeniser tokeniser
)
141 return tokeniser
->input
;
144 /** Get the number of tokens.
147 int mlt_tokeniser_count( mlt_tokeniser tokeniser
)
149 return tokeniser
->count
;
152 /** Get a token as a string.
155 char *mlt_tokeniser_get_string( mlt_tokeniser tokeniser
, int index
)
157 if ( index
< tokeniser
->count
)
158 return tokeniser
->tokens
[ index
];
163 /** Close the tokeniser.
166 void mlt_tokeniser_close( mlt_tokeniser tokeniser
)
168 mlt_tokeniser_clear( tokeniser
);
169 free( tokeniser
->tokens
);